Parcourir la source

fix multi backup issue

Jérôme BUISINE il y a 3 ans
Parent
commit
6967d01752

Fichier diff supprimé car celui-ci est trop grand
+ 1 - 1
README.md


+ 19 - 4
docs/source/documentations.rst

@@ -150,6 +150,21 @@ Some specific methods are available:
             """
             ...
 
+
+        @property
+        def size(self):
+            """
+            Returns solution size (by default `size` private attribute)
+            """
+            ...
+
+        @size.setter
+        def size(self, size):
+            """
+            Set solution size (by default `size` private attribute)
+            """
+            ...
+
         @staticmethod
         def random(size, validator=None):
             """
@@ -164,7 +179,7 @@ Some specific methods are available:
             ...
 
 .. caution::
-    An important thing here are the ``fitness`` and ``data`` functions brought as an editable attribute by the ``@property`` and ``@XXXXX.setter`` decorators. The idea is to allow the user to modify these functions in order to change the expected result of the algorithm regardless of the data to be returned/modified. 
+    An important thing here are the ``fitness``, ``size`` and ``data`` functions brought as an editable attribute by the ``@property`` and ``@XXXXX.setter`` decorators. The idea is to allow the user to modify these functions in order to change the expected result of the algorithm regardless of the data to be returned/modified. 
 
 From these basic methods, it is possible to manage a representation of a solution to our problem. 
 
@@ -513,7 +528,7 @@ The modification applied here is just a bit swapped. Let's define the ``SimpleBi
         def apply(self, solution):
             
             # obtain targeted cell using solution size
-            size = solution._size
+            size = solution.size
             cell = random.randint(0, size - 1)
 
             # copy of solution
@@ -576,7 +591,7 @@ The first half of solution 1 will be saved and added to the second half of solut
 
         def apply(self, solution1, solution2):
             
-            size = solution1._size
+            size = solution1.size
 
             # default split index used
             splitIndex = int(size / 2)
@@ -936,7 +951,7 @@ Let's implement an algorithm well known under the name of hill climber best impr
             # initialise current solution and best solution
             self.initRun()
 
-            solutionSize = self._currentSolution._size
+            solutionSize = self._currentSolution.size
 
             # local search algorithm implementation
             while not self.stop():

+ 1 - 1
docs/source/documentations/algorithms.rst

@@ -196,7 +196,7 @@ Let's implement an algorithm well known under the name of hill climber best impr
             # initialise current solution and best solution
             self.initRun()
 
-            solutionSize = self._currentSolution._size
+            solutionSize = self._currentSolution.size
 
             # local search algorithm implementation
             while not self.stop():

+ 2 - 2
docs/source/documentations/operators.rst

@@ -98,7 +98,7 @@ The modification applied here is just a bit swapped. Let's define the ``SimpleBi
         def apply(self, solution):
             
             # obtain targeted cell using solution size
-            size = solution._size
+            size = solution.size
             cell = random.randint(0, size - 1)
 
             # copy of solution
@@ -161,7 +161,7 @@ The first half of solution 1 will be saved and added to the second half of solut
 
         def apply(self, solution1, solution2):
             
-            size = solution1._size
+            size = solution1.size
 
             # default split index used
             splitIndex = int(size / 2)

+ 2 - 2
macop/algorithms/mono.py

@@ -84,7 +84,7 @@ class HillClimberFirstImprovment(Algorithm):
         # initialise current solution and best solution
         self.initRun()
 
-        solutionSize = self._currentSolution._size
+        solutionSize = self._currentSolution.size
 
         # local search algorithm implementation
         while not self.stop():
@@ -197,7 +197,7 @@ class HillClimberBestImprovment(Algorithm):
         # initialise current solution and best solution
         self.initRun()
 
-        solutionSize = self._currentSolution._size
+        solutionSize = self._currentSolution.size
 
         # local search algorithm implementation
         while not self.stop():

+ 2 - 0
macop/algorithms/multi.py

@@ -380,6 +380,7 @@ class MOEAD(Algorithm):
 
         return paFront
 
+    @property
     def result(self):
         """Get the expected result of the current algorithm
 
@@ -388,6 +389,7 @@ class MOEAD(Algorithm):
         """
         return self._pfPop
 
+    @result.setter
     def result(self, result):
         """Set current default result of the algorithm
 

+ 13 - 13
macop/callbacks/multi.py

@@ -37,21 +37,21 @@ class MultiCheckpoint(Callback):
             with open(self._filepath, 'w') as f:
 
                 for solution in population:
-                    solution.data = ""
+                    solution_data = ""
                     solutionSize = len(solution.data)
 
                     for index, val in enumerate(solution.data):
-                        solution.data += str(val)
+                        solution_data += str(val)
 
                         if index < solutionSize - 1:
-                            solution.data += ' '
+                            solution_data += ' '
 
                     line = str(currentEvaluation) + ';'
 
                     for i in range(len(self._algo.evaluator)):
                         line += str(solution.scores[i]) + ';'
 
-                    line += solution.data + ';\n'
+                    line += solution_data + ';\n'
 
                     f.write(line)
 
@@ -84,14 +84,14 @@ class MultiCheckpoint(Callback):
                     scores = [float(s) for s in data[1:nObjectives + 1]]
 
                     # get best solution data information
-                    solution.data = list(map(int, data[-1].split(' ')))
+                    current_data = list(map(int, data[-1].split(' ')))
 
                     # initialise and fill with data
                     self._algo.population[i] = self._algo.initialiser()
-                    self._algo.population[i].data = np.array(solution.data)
+                    self._algo.population[i].data = np.array(current_data)
                     self._algo.population[i].scores = scores
 
-                    self._algo._pfPop.append(self._algo.population[i])
+                    self._algo.result.append(self._algo.population[i])
 
             macop_line(self._algo)
             macop_text(
@@ -138,21 +138,21 @@ class ParetoCheckpoint(Callback):
             with open(self._filepath, 'w') as f:
 
                 for solution in pfPop:
-                    solution.data = ""
+                    solution_data = ""
                     solutionSize = len(solution.data)
 
                     for index, val in enumerate(solution.data):
-                        solution.data += str(val)
+                        solution_data += str(val)
 
                         if index < solutionSize - 1:
-                            solution.data += ' '
+                            solution_data += ' '
 
                     line = ''
 
                     for i in range(len(self._algo.evaluator)):
                         line += str(solution.scores[i]) + ';'
 
-                    line += solution.data + ';\n'
+                    line += solution_data + ';\n'
 
                     f.write(line)
 
@@ -174,9 +174,9 @@ class ParetoCheckpoint(Callback):
                     scores = [float(s) for s in data[0:nObjectives]]
 
                     # get best solution data information
-                    solution.data = list(map(int, data[-1].split(' ')))
+                    current_data = list(map(int, data[-1].split(' ')))
 
-                    self._algo.result[i].data = solution.data
+                    self._algo.result[i].data = np.array(current_data)
                     self._algo.result[i].scores = scores
 
             macop_text(

+ 2 - 2
macop/operators/discrete/crossovers.py

@@ -71,7 +71,7 @@ class SimpleCrossover(Crossover):
             {:class:`~macop.solutions.base.Solution`}: new generated solution
         """
 
-        size = solution1._size
+        size = solution1.size
 
         # copy data of solution
         firstData = solution1.data.copy()
@@ -151,7 +151,7 @@ class RandomSplitCrossover(Crossover):
         Returns:
             {:class:`~macop.solutions.base.Solution`}: new generated solution
         """
-        size = solution1._size
+        size = solution1.size
 
         # copy data of solution
         firstData = solution1.data.copy()

+ 2 - 2
macop/operators/discrete/mutators.py

@@ -37,7 +37,7 @@ class SimpleMutation(Mutation):
             {:class:`~macop.solutions.base.Solution`}: new generated solution
         """
 
-        size = solution._size
+        size = solution.size
 
         firstCell = 0
         secondCell = 0
@@ -87,7 +87,7 @@ class SimpleBinaryMutation(Mutation):
             {:class:`~macop.solutions.base.Solution`}: new generated solution
         """
 
-        size = solution._size
+        size = solution.size
         cell = random.randint(0, size - 1)
 
         # copy of solution

+ 17 - 0
macop/solutions/base.py

@@ -84,6 +84,23 @@ class Solution():
         """
         self._data = data
 
+    @property
+    def size(self):
+        """
+        Returns solution size (by default `size` private attribute)
+
+        Returns:
+            {object}: data size
+        """
+        return self._size
+
+    @size.setter
+    def size(self, size):
+        """
+        Set solution size (by default `size` private attribute)
+        """
+        self._size = size
+
     @staticmethod
     def random(size, validator=None):
         """