import numpy as np from cpso_swarm import COMB_Swarm from cpso_particle import COMB_Particle s = COMB_Swarm(3, 2.1, 2.1, 2.1, 5, 0.8, 0.2, (-6.0, 6.0), (-4.0, 0.25), (0.4, 0.9), (0, 10), 'data/data.csv', 'data/target.csv') s.swarm.append(COMB_Particle(2.1, 2.1, 2.1, 5, (-6.0, 6.0), (-4.0, 0.25), (0.4, 0.9))) s.swarm.append(COMB_Particle(2.1, 2.1, 2.1, 5, (-6.0, 6.0), (-4.0, 0.25), (0.4, 0.9))) s.swarm.append(COMB_Particle(2.1, 2.1, 2.1, 5, (-6.0, 6.0), (-4.0, 0.25), (0.4, 0.9))) s.swarm[0].x = np.array([1, 32, 8, 4, 13]) s.swarm[1].x = np.array([24, 3, 28, 3, 21]) s.swarm[2].x = np.array([12, 5, 24, 15, 31]) print('{:35} : {}'.format('Calculated Spread', s.calc_spread())) print('{:35} : {}'.format('Correct Spread', 59.863405937339586))
args.x_bounds = tuple(args.x_bounds) args.v_bounds = tuple(args.v_bounds) args.w_bounds = tuple(args.w_bounds) args.t_bounds = (0, args.t) # Downstream file saving code expects directories to end with a '/' if args.output_path[-1] != '/': args.output_path.append('/') if args.copyscript: import shutil shutil.copy(__file__, args.output_path + 'cpso_script.py') # Initialize swarm and execute algorithm s = COMB_Swarm(args.npart, args.c[0], args.c[1], args.c[2], args.ndim, args.alpha, args.test_size, args.x_bounds, args.v_bounds, args.w_bounds, args.t_bounds, args.data_path, args.target_path) s.initialize_particles() s.execute_search() s.final_eval() # Collect and process data from inside the swarm data = [] columns = [] for k, v in s.var_by_time.items(): columns.append(k) data.append(v) var_by_time = pd.DataFrame(data=np.transpose(data), columns=columns) var_by_time.to_csv(args.output_path + 'var_by_time.csv') np.savetxt(args.output_path + 'abinary.csv', s.abinary, delimiter=',') np.savetxt(args.output_path + 'X_train.csv', s.X_train, delimiter=',') np.savetxt(args.output_path + 'y_train.csv', s.y_train, delimiter=',')
# Creating the fitness function terms dict terms = {} for k, v in zip(args.terms, args.weights): terms[k] = v # Downstream file saving code expects directories to end with a '/' if args.output_path[-1] != '/': args.output_path += '/' if args.copyscript: import shutil shutil.copy(__file__, args.output_path) # Initialize swarm and execute algorithm s = COMB_Swarm(args.npart, args.c[0], args.c[1], args.c[2], args.ndim, terms, args.stagn_limit, args.x_bounds, args.v_bounds, args.w_bounds, args.t_bounds, args.data_path, args.target_path, args.init_particles) if not args.init_particles: s.initialize_particles() s.execute_search() # Collect and process data from inside the swarm data = [] columns = [] for k, v in s.var_by_time.items(): if k == 'g_score': columns.append('g_accuracy') data.append([i[0] for i in v]) columns.append('g_sensitivity') data.append([i[1] for i in v]) columns.append('g_specificity') data.append([i[2] for i in v])