import benchmark_functions as bf import matplotlib.pyplot as plt from ga import GA from de import DE # Select function for minimization f = bf.Sphere(dim=2) # View 3d plot of the function if f.dim == 2: bf.plot3d(function=f, show_contour=True) plt.show() # Create algorithm, Differential evolution or Genetic algorithm de = DE(obj_function=f, F=0.8, Cr=0.3, gen_max=10000) ga = GA(obj_function=f, k=70, pm=0.5, gen_max=10000, sigma=2) # Find solution de_solution, de_gen = de.search(show_progress=False, error_tolerance=f.epsilon) ga_solution, ga_gen = ga.search(show_progress=False, error_tolerance=f.epsilon, c=0.9, msc=50) # Print results print("============== Differential evolution ==============") print( "Solution {:f} found in {:d} generations. Optimal value is {:f}. Error is: {:f}." .format(f.value(de_solution), de_gen, f.optimum, f.value(de_solution) - f.optimum)) print("============== Genetic algorithm ==============") print( "Solution {:f} found in {:d} generations. Optimal value is {:f}. Error is: {:f}." .format(f.value(ga_solution), ga_gen, f.optimum,