Browse Source

update the way of computing best solution from population

Jérôme BUISINE 1 year ago
parent
commit
9d25d07812

+ 1 - 1
find_best_attributes_surrogate_openML_multi_specific.py

@@ -243,7 +243,7 @@ def main():
     policy = RandomPolicy(operators)
 
     # custom start surrogate variable based on problem size
-    p_start = int(problem_size / p_k_division * 3) # 3 \times number of features for each sub-model
+    p_start = int(problem_size / p_k_division * 2) # 2 \times number of features for each sub-model
 
     # fixed minimal number of real evaluations
     if p_start < 50:

+ 18 - 18
optimization/ILSMultiSpecificSurrogate.py

@@ -424,12 +424,9 @@ class ILSMultiSpecificSurrogate(Algorithm):
             local_search_list = [] 
 
             for i in range(self._k_division):
-                # create new local search instance
-                # passing global evaluation param from ILS
 
                 # use specific initializer for pop_initialiser
                 # specific surrogate evaluator for this local search
-                # TODO : check this part
                 ls = LocalSearchSurrogate(lambda index=i: self.pop_initializer(index),
                             lambda s: self._surrogates[i].surrogate.predict([s._data])[0],
                             self._operators,
@@ -444,7 +441,7 @@ class ILSMultiSpecificSurrogate(Algorithm):
 
                 local_search_list.append(ls)
 
-            # parallel run of local search
+            # parallel run of each local search
             num_cores = multiprocessing.cpu_count()
             ls_solutions = Parallel(n_jobs=num_cores)(delayed(ls.run)(ls_evaluations) for ls in local_search_list)
 
@@ -476,27 +473,30 @@ class ILSMultiSpecificSurrogate(Algorithm):
             # main best solution update
             if self._start_train_surrogates <= self.getGlobalEvaluation():
 
-                # need to create virtual solution
-                obtained_solution_data = np.array([ s._data for s in ls_solutions ]).flatten().tolist()
+                # need to create virtual solution from current population
+                obtained_solution_data = np.array([ s._data for s in self._population ]).flatten().tolist()
 
-                # init random solution 
-                current_solution = self._initializer()
-                current_solution.data = obtained_solution_data
+                if obtained_solution_data == self._bestSolution.data:
+                    print(f'-- No updates found from sub-model surrogates LS (best solution score: {self._bestSolution._score}')
+                else:
+                    print(f'-- Updates found from sub-model surrogates LS')
+                    # init random solution 
+                    current_solution = self._initializer()
+                    current_solution.data = obtained_solution_data
 
-                fitness_score = self._main_evaluator(current_solution)
+                    fitness_score = self._main_evaluator(current_solution)
 
-                # new computed solution score
-                current_solution._score = fitness_score
+                    # new computed solution score
+                    current_solution._score = fitness_score
 
-                # if solution is really better after real evaluation, then we replace
-                if self.isBetter(current_solution):
-                    self._bestSolution = current_solution
+                    # if solution is really better after real evaluation, then we replace
+                    if self.isBetter(current_solution):
+                        self._bestSolution = current_solution
 
-                print(f'-- Current solution obtained is {current_solution._score} vs. {self._bestSolution._score}')
-                self.progress()
+                    print(f'-- Current solution obtained is {current_solution._score} vs. {self._bestSolution._score}')
+                    self.progress()
     
 
-
             # check using specific dynamic criteria based on r^2
             r_squared_scores = self.surrogates_coefficient_of_determination()
             r_squared = sum(r_squared_scores) / len(r_squared_scores)

+ 6 - 5
run_openML_surrogate_multi_specific.py

@@ -5,10 +5,10 @@ open_ml_problems_folder = 'OpenML_datasets'
 surrogate_data_path = 'data/surrogate/data/'
 
 # fixed test params as first part
-k_params = [100] # 100, 150, 200
+k_params = [30, 50, 100] # 100, 150, 200
 k_random = [0] # 0, 1
 k_reinit = [0] # 0, 1
-every_ls = 50
+every_ls = 5
 
 n_times = 5
 
@@ -62,7 +62,7 @@ def main():
                             output_problem_name = f'{ml_problem_name}_everyLS_{every_ls}_k{k}_random{k_r}_reinit{k_init}_{str_index}'
 
                             # copy pre-computed real evaluation data for this instance
-                            current_output_real_eval_path = os.path.join(surrogate_data_path, output_problem_name)
+                            # current_output_real_eval_path = os.path.join(surrogate_data_path, output_problem_name)
                             # shutil.copy2(real_evaluation_data_file_path, current_output_real_eval_path)
 
                             ml_surrogate_multi_command = f"python find_best_attributes_surrogate_openML_multi_specific.py " \
@@ -82,7 +82,7 @@ def main():
                         output_problem_name = f'{ml_problem_name}_everyLS_{every_ls}_k{k}_random{k_r}_reinit{k_init}'
 
                         # copy pre-computed real evaluation data for this instance
-                        current_output_real_eval_path = os.path.join(surrogate_data_path, output_problem_name)
+                        # current_output_real_eval_path = os.path.join(surrogate_data_path, output_problem_name)
                         # shutil.copy2(real_evaluation_data_file_path, current_output_real_eval_path)
 
                         ml_surrogate_multi_command = f"python find_best_attributes_surrogate_openML_multi_specific.py " \
@@ -96,7 +96,8 @@ def main():
                                         f"--output {output_problem_name}"
                                         
                         print(f'Running extraction data for {ml_problem_name} with [ils: {p_ils}, ls: {p_ls}, k: {k}, k_r: {k_r}]')
-                        os.system(ml_surrogate_multi_command)
+                        #os.system(ml_surrogate_multi_command)
+                        print(ml_surrogate_multi_command)