def execute(self, output_path: str = ''): self.algorithm.run() if output_path: file_name = os.path.join(output_path, 'FUN.{}.tsv'.format(self.run_tag)) print_function_values_to_file(self.algorithm.get_result(), filename=file_name) file_name = os.path.join(output_path, 'VAR.{}.tsv'.format(self.run_tag)) print_variables_to_file(self.algorithm.get_result(), filename=file_name) file_name = os.path.join(output_path, 'TIME.{}'.format(self.run_tag)) with open(file_name, 'w+') as of: of.write(str(self.algorithm.total_computing_time))
def update(self, *args, **kwargs): problem = kwargs['PROBLEM'] solutions = kwargs['SOLUTIONS'] if solutions: if isinstance(problem, DynamicProblem): termination_criterion_is_met = kwargs.get('TERMINATION_CRITERIA_IS_MET', None) if termination_criterion_is_met: print_function_values_to_file(solutions, '{}/FUN.{}'.format(self.directory, self.counter)) self.counter += 1 else: print_function_values_to_file(solutions, '{}/FUN.{}'.format(self.directory, self.counter)) self.counter += 1
mutation=PolynomialMutation(probability=1.0 / problem.number_of_variables, distribution_index=20), leaders=CrowdingDistanceArchive(100), termination_criterion=StoppingByEvaluations(max=max_evaluations) ) algorithm.observable.register(observer=ProgressBarObserver(max=max_evaluations)) algorithm.run() front = algorithm.get_result() label = algorithm.get_name() + "." + problem.get_name() # Plot front plot_front = Plot(plot_title='Pareto front approximation', reference_front=problem.reference_front, axis_labels=problem.obj_labels) plot_front.plot(front, label=algorithm.label, filename=algorithm.get_name()) # Plot interactive front plot_front = InteractivePlot(plot_title='Pareto front approximation', reference_front=problem.reference_front, axis_labels=problem.obj_labels) plot_front.plot(front, label=algorithm.label, filename=algorithm.get_name()) # Save results to file print_function_values_to_file(front, 'FUN.' + algorithm.label) print_variables_to_file(front, 'VAR.' + algorithm.label) print('Algorithm (continuous problem): ' + algorithm.get_name()) print('Problem: ' + problem.get_name()) print('Computing time: ' + str(algorithm.total_computing_time))
if __name__ == '__main__': problem = Rastrigin(10) max_evaluations = 50000 algorithm = NSGAII( problem=problem, population_size=100, offspring_population_size=100, mutation=PolynomialMutation(probability=1.0 / problem.number_of_variables, distribution_index=20.0), crossover=SBXCrossover(probability=0.9, distribution_index=20.0), termination_criterion=StoppingByEvaluations(max=max_evaluations), dominance_comparator=DominanceComparator()) algorithm.observable.register(observer=PrintObjectivesObserver(1000)) algorithm.run() front = algorithm.get_result() # Save results to file print_function_values_to_file( front, 'FUN.' + algorithm.get_name() + "-" + problem.get_name()) print_variables_to_file( front, 'VAR.' + algorithm.get_name() + "-" + problem.get_name()) print('Algorithm (continuous problem): ' + algorithm.get_name()) print('Problem: ' + problem.get_name()) print('Computing time: ' + str(algorithm.total_computing_time))
from jmetal.util.observer import PrintObjectivesObserver from jmetal.util.solutions import print_function_values_to_file, print_variables_to_file from jmetal.util.termination_criterion import StoppingByEvaluations if __name__ == '__main__': problem = OneMax(number_of_bits=1024) max_evaluations = 10000 algorithm = LocalSearch( problem=problem, mutation=BitFlipMutation(probability=1.0 / problem.number_of_bits), termination_criterion=StoppingByEvaluations(max=max_evaluations)) objectives_observer = PrintObjectivesObserver(frequency=100) algorithm.observable.register(observer=objectives_observer) algorithm.run() result = algorithm.get_result() # Save results to file print_function_values_to_file( result, 'FUN.' + algorithm.get_name() + "." + problem.get_name()) print_variables_to_file( result, 'VAR.' + algorithm.get_name() + "." + problem.get_name()) print('Algorithm: ' + algorithm.get_name()) print('Problem: ' + problem.get_name()) print('Solution: ' + result.get_binary_string()) print('Fitness: ' + str(result.objectives[0])) print('Computing time: ' + str(algorithm.total_computing_time))
def train(self): problem = Ejemplo(X=self.Xtrain, Y=self.Ytrain, kernel=self.kernel, gamma=self.gamma, degree=self.degree, C=self.C, coef0=self.coef0) #problem.reference_front = read_solutions(filename='resources/reference_front/ZDT1.pf') max_evaluations = self.maxEvaluations algorithm = NSGAII( problem=problem, population_size=self.popsize, offspring_population_size=self.popsize, mutation=BitFlipMutation(probability=1.0 / np.shape(self.Xtrain)[0]), crossover=SPXCrossover(probability=1.0), termination_criterion=StoppingByEvaluations(max=max_evaluations)) algorithm.observable.register(observer=ProgressBarObserver( max=max_evaluations)) #algorithm.observable.register(observer=VisualizerObserver(reference_front=problem.reference_front)) algorithm.run() front = algorithm.get_result() # Plot front plot_front = Plot(plot_title='Pareto front approximation', reference_front=None, axis_labels=problem.obj_labels) plot_front.plot(front, label=algorithm.label, filename=algorithm.get_name()) # Plot interactive front plot_front = InteractivePlot(plot_title='Pareto front approximation', axis_labels=problem.obj_labels) plot_front.plot(front, label=algorithm.label, filename=algorithm.get_name()) # Save results to file print_function_values_to_file(front, 'FUN.' + algorithm.label) print_variables_to_file(front, 'VAR.' + algorithm.label) print('Algorithm (continuous problem): ' + algorithm.get_name()) # Get normalized matrix of results normed_matrix = normalize( list(map(lambda result: result.objectives, front))) # Get the sum of each objective results and select the best (min) scores = list(map(lambda item: sum(item), normed_matrix)) solution = front[scores.index(min(scores))] self.instances = solution.variables[0] self.attributes = solution.variables[1] # Generate masks # Crop by characteristics and instances X = self.Xtrain[self.instances, :] X = X[:, self.attributes] Y = self.Ytrain[self.instances] self.model = SVC(gamma=self.gamma, C=self.C, degree=self.degree, kernel=self.kernel) self.model.fit(X=X, y=Y) # write your code here return self.model
from jmetal.algorithm.multiobjective.nsgaii import NSGAII from jmetal.operator import SBXCrossover, PolynomialMutation from jmetal.util.solutions import print_function_values_to_file, print_variables_to_file from jmetal.util.termination_criterion import StoppingByEvaluations if __name__ == '__main__': problem = ZDT1Modified() max_evaluations = 100 algorithm = NSGAII( problem=problem, population_size=10, offspring_population_size=10, mutation=PolynomialMutation(probability=1.0 / problem.number_of_variables, distribution_index=20), crossover=SBXCrossover(probability=1.0, distribution_index=20), termination_criterion=StoppingByEvaluations(max=max_evaluations)) algorithm.run() front = algorithm.get_result() # Save results to file print_function_values_to_file(front, 'FUN.NSGAII.ZDT1') print_variables_to_file(front, 'VAR.NSGAII.ZDT1') print('Algorithm (continuous problem): ' + algorithm.get_name()) print('Problem: ' + problem.get_name()) print('Computing time: ' + str(algorithm.total_computing_time))
def train(self): problem = SVM_Problem(X=self.Xtrain, Y=self.Ytrain) #problem.reference_front = read_solutions(filename='resources/reference_front/ZDT1.pf') max_evaluations = self.maxEvaluations algorithm = NSGAII( problem=problem, population_size=self.popsize, offspring_population_size=self.popsize, mutation=PolynomialMutation(probability=1.0 / problem.number_of_variables, distribution_index=20), crossover=SBXCrossover(probability=1.0, distribution_index=20), termination_criterion=StoppingByEvaluations(max=max_evaluations)) algorithm.observable.register(observer=ProgressBarObserver( max=max_evaluations)) #algorithm.observable.register(observer=VisualizerObserver(reference_front=problem.reference_front)) algorithm.run() front = algorithm.get_result() # Plot front plot_front = Plot(plot_title='Pareto front approximation', reference_front=None, axis_labels=problem.obj_labels) plot_front.plot(front, label=algorithm.label, filename=algorithm.get_name()) # Plot interactive front plot_front = InteractivePlot(plot_title='Pareto front approximation', axis_labels=problem.obj_labels) plot_front.plot(front, label=algorithm.label, filename=algorithm.get_name()) # Save results to file print_function_values_to_file(front, 'FUN.' + algorithm.label) print_variables_to_file(front, 'VAR.' + algorithm.label) print('Algorithm (continuous problem): ' + algorithm.get_name()) print( "-----------------------------------------------------------------------------" ) print('Problem: ' + problem.get_name()) print('Computing time: ' + str(algorithm.total_computing_time)) # Get normalized matrix of results normed_matrix = normalize( list(map(lambda result: result.objectives, front))) # Get the sum of each objective results and select the best (min) scores = list(map(lambda item: sum(item), normed_matrix)) solution = front[scores.index(min(scores))] # Get our variables self.gamma = solution.variables[0] self.C = solution.variables[1] self.coef0 = solution.variables[2] self.degree = solution.variables[3] self.kernel = solution.variables[4] self.instances = solution.masks[0] self.attributes = solution.masks[1] # Select pick a random array with length of the variable X = self.Xtrain[self.instances, :] X = X[:, self.attributes] Y = self.Ytrain[self.instances] print(*front, sep=", ") # Contruct model self.model = SVM(Xtrain=X, Ytrain=Y, kernel=self.kernel, C=self.C, degree=self.degree, coef0=self.coef0, gamma=self.gamma, seed=self.seed).train() print('Objectives: ', *solution.objectives, sep=", ") # write your code here return self.model