Parcourir la source

update of policy and operators use

Jérôme BUISINE il y a 4 ans
Parent
commit
a96e686c26
2 fichiers modifiés avec 32 ajouts et 14 suppressions
  1. 29 3
      operators/policies/Policy.py
  2. 3 11
      operators/policies/RandomPolicy.py

+ 29 - 3
operators/policies/Policy.py

@@ -1,3 +1,9 @@
+# main imports
+import logging
+
+# module imports
+from ..Operator import Operator
+
 # define policy to choose `operator` function at current iteration
 class Policy():
 
@@ -6,8 +12,28 @@ class Policy():
     def __init__(self, _operators):
         self.operators = _operators
 
-    def apply(self, solution):
+
+    def select(self):
+        """
+        Select specific operator to solution and returns solution
         """
-        Apply specific operator to solution and returns solution
+        raise NotImplementedError
+        
+    def apply(self, solution, secondSolution=None):
         """
-        raise NotImplementedError
+        Apply specific operator chosen to solution and returns solution
+        """
+        
+        operator = self.select()
+
+        logging.info("-- Applying %s on %s" % (type(operator).__name__, solution))
+
+        # check kind of operator
+        if operator.kind == Operator.CROSSOVER:
+            return operator.apply(solution, secondSolution)
+        
+        if operator.kind == Operator.MUTATOR:
+            return operator.apply(solution)
+
+        # by default
+        return operator.apply(solution)

+ 3 - 11
operators/policies/RandomPolicy.py

@@ -2,23 +2,15 @@
 import random
 
 # module imports
-from ..Operator import Operator
 from .Policy import Policy
 
 class RandomPolicy(Policy):
 
-    def apply(self, solution, secondSolution=None):  
+    def select(self):  
 
         # choose operator randomly
         index = random.randint(0, len(self.operators) - 1)
-        operator = self.operators[index]
+        return self.operators[index]
 
-        # check kind of operator
-        if operator.kind == Operator.CROSSOVER:
-            return operator.apply(solution, secondSolution)
-        
-        if operator.kind == Operator.MUTATOR:
-            return operator.apply(solution)
 
-        # by default
-        return operator.apply(solution)
+