def multiview(): return multi.run(request.args.get('locus'))
def run(function,min_p,max_p,constants,load_data=False): ######################################################### """ run takes the following arguments: function: the function the PSO is to minimise min_p: the minimium bounding parameters to search from. Passed as a Vector. One element per parameter max_p: the maximium bounding parameters to search to. Passed as a Vector. One element per parameter constants: These are the constants of the PSO. In order: No. particles No. iterations No. of groups (This is the number of groups the swarm will be split into for the calculation of the group local mins) local constant (These constants allow for a weighted average of the different parameters) global constant group constant particle mass procs: The number of processors that the PSO should use. PSO are almost 100% trivially parallel. The only serial calculations that are required is the calculation of the global min. """ ######################################################### N = constants[0] iterations = constants[1] grouping_N = constants[2] constants = constants[3:] swarm=[]#list of swarm particles #initialise the particles if load_data: swarm = pickle_read() else: for particle in range(N): swarm.append(Particle(min_p,max_p,constants)) #distribute and calculate the value of the function at all of these points min_position = min_p for iteration in range(iterations): #calculate the function at the particle positions cost_results = multi.run([particle.position.data for particle in swarm]) [swarm[i].set_cost(cost_results[i]) for i in range(len(swarm))] #update all the local extrema and decide the new global extrema min_position = sorted(swarm,key=lambda x: x.cost)[0].position logging.info("Iteration: %d, global min: %s"%(iteration," ".join([str(x) for x in min_position]))) sed_file("best_so_far.sed",min_position) groups=grouping(swarm,grouping_N) for group in groups: group_position=sorted(group,key=lambda x: x.cost)[0].position for particle in group: particle.update(min_position,group_position) logging.info("Particle positions and velocities update complete") #save swarm to disk for safe keeping pickle_write(swarm) p=sorted(swarm,key=lambda x: x.cost)[0].position sed_file("final.sed",p) print "Results:", return [math.sqrt(sum([i**2 for i in p])),p]
def multiview(): return multi.run(request.args.get("locus"))