Sfoglia il codice sorgente

Add of logger to optimization algorithms

Jérôme BUISINE 4 anni fa
parent
commit
f4d9c3d690
5 ha cambiato i file con 33 aggiunte e 19 eliminazioni
  1. 1 1
      README.md
  2. 6 2
      algorithms/Algorithm.py
  3. 10 8
      algorithms/IteratedLocalSearch.py
  4. 5 3
      algorithms/LocalSearch.py
  5. 11 5
      mainExample.py

+ 1 - 1
README.md

@@ -15,7 +15,7 @@ Optimisation generic framework built for optimization problem during thesis
 
 ## How to use ?
 
-You can see an example of use in the `mainExample.py` python file.
+You can see an example of use in the `mainExample.py` python file. You need to clone this repository with `optimization` folder name to get it works.
 
 ## Add as dependency
 

+ 6 - 2
algorithms/Algorithm.py

@@ -1,3 +1,5 @@
+import logging
+
 # Generic algorithm class
 class Algorithm():
 
@@ -91,13 +93,15 @@ class Algorithm():
         """
         self.maxEvalutations = _evaluations
 
+        logging.info("Run %s with %s evaluations" % (self.__str__(), _evaluations))
+
 
     def progress(self):
-        return "Evaluation n°%s/%s, %s%%" % (self.numberOfEvaluations, self.maxEvalutations, "{0:.2f}".format((self.numberOfEvaluations) / self.maxEvalutations * 100.))
+        logging.info("-- Evaluation n°%s/%s, %s%%" % (self.numberOfEvaluations, self.maxEvalutations, "{0:.2f}".format((self.numberOfEvaluations) / self.maxEvalutations * 100.)))
 
 
     def information(self):
-        return "%s found with score of %s" % (self.bestSolution, self.bestSolution.fitness())
+        logging.info("-- Found %s with score of %s" % (self.bestSolution, self.bestSolution.fitness()))
 
 
     def __str__(self):

+ 10 - 8
algorithms/IteratedLocalSearch.py

@@ -1,10 +1,13 @@
+# main imports 
+import logging
+
 # module imports
 from .Algorithm import Algorithm
 from.LocalSearch import LocalSearch
 
 class IteratedLocalSearch(Algorithm):
 
-    def run(self, _evaluations):
+    def run(self, _evaluations, _lc_evaluations=100):
 
         # by default use of mother method to initialize variables
         super().run(_evaluations)
@@ -13,21 +16,20 @@ class IteratedLocalSearch(Algorithm):
 
         # local search algorithm implementation
         while self.numberOfEvaluations < self.maxEvalutations:
-
+            
             # create and search solution from local search
-            newSolution = ls.run(100)
+            newSolution = ls.run(_lc_evaluations)
 
             # if better solution than currently, replace it
             if self.isBetter(newSolution):
                 self.bestSolution = newSolution
 
             # increase number of evaluations
-            self.numberOfEvaluations += 100
+            self.numberOfEvaluations += _lc_evaluations
 
-            print(self.progress())
-        print(self.information())
-            
+            self.progress()
+            self.information()            
 
-        print("End of local search algorithm..")
+        logging.info("End of %s, best solution found %s" % (type(self).__name__, self.bestSolution))
 
         return self.bestSolution

+ 5 - 3
algorithms/LocalSearch.py

@@ -1,3 +1,5 @@
+# main imports
+
 # module imports
 from .Algorithm import Algorithm
 
@@ -25,14 +27,14 @@ class LocalSearch(Algorithm):
                 # increase number of evaluations
                 self.numberOfEvaluations += 1
 
-                print(self.progress())
+                self.progress()
 
                 # stop algorithm if necessary
                 if self.numberOfEvaluations >= self.maxEvalutations:
                     break
             
-            print(self.information())
+            self.information()
 
-        print("End of local search algorithm..")
+        logging.info("End of %s, best solution found %s" % (type(self).__name__, self.bestSolution))
 
         return self.bestSolution

+ 11 - 5
mainExample.py

@@ -1,14 +1,20 @@
+# main imports
+import logging
+
 # module imports
 
 # Note: you need to import from folder dependency name
 # examples: `from optimization.solutions.BinarySolution import BinarySolution`
 
-from algorithms.IteratedLocalSearch import IteratedLocalSearch as ILS
-from solutions.BinarySolution import BinarySolution
-from evaluators.EvaluatorExample import evaluatorExample
+from optimization.algorithms.IteratedLocalSearch import IteratedLocalSearch as ILS
+from optimization.solutions.BinarySolution import BinarySolution
+from optimization.evaluators.EvaluatorExample import evaluatorExample
+
+from optimization.updators.mutators.SimpleMutation import SimpleMutation, SimpleBinaryMutation
+from optimization.updators.policies.RandomPolicy import RandomPolicy
 
-from updators.mutators.SimpleMutation import SimpleMutation, SimpleBinaryMutation
-from updators.policies.RandomPolicy import RandomPolicy
+# logging configuration
+logging.basicConfig(format='%(asctime)s %(message)s', filename='example.log', level=logging.DEBUG)
 
 # default validator
 def validator(solution):