list_exec_time = [] list_functions = [(testFunctions.ackley_function, (0.0, 0.0)), (testFunctions.easom_function, (math.pi, math.pi)), (testFunctions.sphere_function, (0.0 , 0.0)), (testFunctions.bohachevsky_function, (0.0 , 0.0)), (testFunctions.sum_squares_function, (0.0 , 0.0))] for l in range(len(list_functions)): # gets the function function = list_functions[l][0] #print("Function: " + ", " + str(function)) #print("Run, Mean Error, Execution Time") avg_error = 0 avg_delta = 0 for i in range(num_runs): start = datetime.datetime.now() alh = SwarmPackagePy.fa(num_agents, function, -10, 10, 2, num_iterations) end = datetime.datetime.now() delta = end - start last_list_pos = alh.get_agents()[-1] # gets the global optima of the function global_optima = list_functions[l][1] error = 0 for j in range(num_agents): last_pos = last_list_pos[j] ind_error = ((last_pos[0] - global_optima[0]) ** 2 + (last_pos[1] - global_optima[1]) ** 2) ** 0.5 error += ind_error error /= num_agents list_error.append(error) list_exec_time.append(delta.microseconds) #print("average error for run " + str(i + 1) + ": " + str(error)) #print("exec time for run " + str(i + 1) + ": " + str(delta,microseconds))
import SwarmPackagePy as sw from SwarmPackagePy import testFunctions as tf from SwarmPackagePy import animation, animation3D import numpy as np sw._version_ swarm_strength = np.random.random_integers(25, 55) print("Swarm strength =", swarm_strength) firefly = sw.fa(n=swarm_strength, function=tf.sum_squares_function, lb=-10, ub=10, dimension=2, psi=2, iteration=50) animation(firefly.get_agents(), tf.sum_squares_function, -10, 10) animation3D(firefly.get_agents(), tf.sum_squares_function, -10, 10)