Parcourir la source

Update log of local search

Jérôme BUISINE il y a 4 ans
Parent
commit
12004625da

+ 2 - 2
algorithms/Algorithm.py

@@ -99,11 +99,11 @@ class Algorithm():
 
 
     def progress(self):
-        logging.info("-- %s evaluation n°%s of %s, %s%% - %s" % (type(self).__name__, self.numberOfEvaluations, self.maxEvalutations, "{0:.2f}".format((self.numberOfEvaluations) / self.maxEvalutations * 100.), self.bestSolution.fitness()))
+        logging.info("-- %s evaluation n°%s of %s (%s%%) - BEST SCORE %s" % (type(self).__name__, self.numberOfEvaluations, self.maxEvalutations, "{0:.2f}".format((self.numberOfEvaluations) / self.maxEvalutations * 100.), self.bestSolution.fitness()))
 
 
     def information(self):
-        logging.info("-- Best solution %s with score of %s" % (self.bestSolution, self.bestSolution.fitness()))
+        logging.info("-- Best solution %s - SCORE %s" % (self.bestSolution, self.bestSolution.fitness()))
 
 
     def __str__(self):

+ 1 - 1
algorithms/LocalSearch.py

@@ -30,7 +30,7 @@ class LocalSearch(Algorithm):
                 self.numberOfEvaluations += 1
 
                 self.progress()
-                logging.info("-- Found %s with score of %s" % (newSolution, newSolution.fitness()))
+                logging.info("---- Current %s - SCORE %s" % (newSolution, newSolution.fitness()))
 
                 # stop algorithm if necessary
                 if self.numberOfEvaluations >= self.maxEvalutations:

+ 1 - 1
mainExample.py

@@ -34,7 +34,7 @@ def main():
 
     algo = ILS(init, evaluatorExample, operators, policy, validator, True)
 
-    bestSol = algo.run(100000)
+    bestSol = algo.run(1000)
 
     print("Found ", bestSol)
 

+ 6 - 5
operators/policies/Policy.py

@@ -26,14 +26,15 @@ class Policy():
         
         operator = self.select()
 
-        logging.info("-- Applying %s on %s" % (type(operator).__name__, solution))
+        logging.info("---- Applying %s on %s" % (type(operator).__name__, solution))
 
         # check kind of operator
         if operator.kind == Operator.CROSSOVER:
-            return operator.apply(solution, secondSolution)
+            newSolution = operator.apply(solution, secondSolution)
         
         if operator.kind == Operator.MUTATOR:
-            return operator.apply(solution)
+            newSolution = operator.apply(solution)
 
-        # by default
-        return operator.apply(solution)
+        logging.info("---- Obtaining %s" % (solution))
+
+        return newSolution

+ 3 - 1
solutions/BinarySolution.py

@@ -27,6 +27,8 @@ class BinarySolution(Solution):
         Use of validator to generate valid random solution
         """
 
+        self.data = np.random.randint(2, size=self.size)
+
         while not self.isValid(_validator):
             self.data = np.random.randint(2, size=self.size)
 
@@ -34,5 +36,5 @@ class BinarySolution(Solution):
 
 
     def __str__(self):
-        return "Binary solution %s of size %s" % (self.data, self.size)
+        return "Binary solution %s" % (self.data)
         

+ 3 - 1
solutions/CombinatoryIntegerSolution.py

@@ -27,6 +27,8 @@ class CombinatoryIntegerSolution(Solution):
         Use of validator to generate valid random solution
         """
 
+        self.data = np.random.shuffle(np.arange(self.size))
+
         while not self.isValid(_validator):
             self.data = np.random.shuffle(np.arange(self.size))
 
@@ -34,5 +36,5 @@ class CombinatoryIntegerSolution(Solution):
 
 
     def __str__(self):
-        return "Combinatory integer solution %s of size %s" % (self.data, self.size)
+        return "Combinatory integer solution %s" % (self.data)
         

+ 3 - 1
solutions/IntegerSolution.py

@@ -27,6 +27,8 @@ class IntegerSolution(Solution):
         Use of validator to generate valid random solution
         """
 
+        self.data = np.random.randint(self.size, size=self.size)
+
         while not self.isValid(_validator):
             self.data = np.random.randint(self.size, size=self.size)
 
@@ -34,5 +36,5 @@ class IntegerSolution(Solution):
 
 
     def __str__(self):
-        return "Integer solution %s of size %s" % (self.data, self.size)
+        return "Integer solution %s" % (self.data)