def classicalGlobalPSOV2(model, retries, changes, goal = 0.01, pat = 100, era = 100, np=30, phi_1=2.8, phi_2=1.3, inertia=0.8): emin = 0 #setting vmax to full search range for an particle (from lit) # val bounds = [model.getBounds(i) for i in range(model.numOfDecisions())][0] # val difference vmax = max([(x[1] - x[0]) for x in [model.getBounds(i) for i in range(model.numOfDecisions())]]) / 10.0 radius = vmax * 0.20; s = gens(model, np, radius) #initialize grapher g = grapher(model, np) for can in s: g.addVector(can.pos, can.uniq) # Energy here is set to zero since we're not actively using it for now. st = state(model.name, 'classicalGlobalPSOV2', s, 0, retries, changes, era) print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++' print 'Model Name: ', model.name, '\nOptimizer: Classical Global PSO V2, K: ', changes # Set an initial value for the global best # The downside to setting pbest equal to the current # particle position is that if there is a high phi_1 value # the particle will stay still until something happens globally # that pushes it away. st.sb = st.s[0].pbest #Initialize objective mins and maxs model.initializeObjectiveMaxMin(st.sb) #we whould do this just in case for c in st.s: model.updateObjectiveMaxMin(c.pos) tot_deaths = 0 while st.t: st.k = changes patience = pat while st.k: num_deaths = 0 for can in st.s: can.vel = [inertia * vel + (phi_1 * random.uniform(0,1) * (best - pos)) + (phi_2 * random.uniform(0,1) * (gbest - pos)) \ for vel, pos, best, gbest in zip(can.vel, can.pos, can.pbest, st.sb)] #checking if velocity is at max can.vel = [vmax if vel > vmax else vel for vel in can.vel] can.pos = [pos + vel for pos, vel in zip(can.pos, can.vel)] # Currently doing the same thing for particles that are # out of bounds and out of constraints, simply killing them # definitely some other options with this. If they get to # bounds can set vector to boundary, and vel ot zero, then # just let them be pulled back into the space. if not model.checkBounds(can.pos): can.pos = model.retry() can.vel = [0.0 for x in can.pos] # Should a killed candidate maintain it's phantom memory? # Uncomment this to wipe its memory. # can.pbest = list(can.pos) num_deaths += 1 if not model.checkConstraints(can.pos): can.pos = model.retry() can.vel = [0.0 for x in can.pos] # Should a killed candidate maintain it's phantom memory? # Uncomment this to wipe its memory. # can.pbest = list(can.pos) num_deaths += 1 #Update objective maxs and mins g.addVector(can.pos, can.uniq) model.updateObjectiveMaxMin(can.pos) #logic for dealing with collision and combining #particles for c in st.s: for can in st.s: if c == can: continue else: radius = distance(c.pos, can.pos) #if we are within the particles gravitational pull #combine into one particle if radius < max(c.radius, can.radius): #the particle with better energy #becomes the final particle #we delete the particle with worse energy print("particle " + str(c.uniq) + " combined") if model.energy(c.pos) < model.energy(can.pos): c.weight = c.weight + can.weight c.radius = c.radius + can.radius can.pos = model.retry() can.vel = [0.0 for x in can.pos] else: can.weight = can.weight + c.weight c.radius = c.radius + can.radius c.pos = model.retry() c.vel = [0.0 for x in c.pos] else: #here we repulse if we are outside #the radius of gravitation for the #particles repulsedVelocity = repulsion(c, can, radius) print(repulsedVelocity) can.vel = [repulsedVelocity * vel for vel in can.vel] can.pos = [pos + vel for pos, vel in zip(can.pos, can.vel)] #instead of resetting wiggle #each index until bounds and constraints #are met for i in xrange(len(can.pos)): can.pos = model.singleRetry(can.pos, i) #other particle will repulse the opposite direction c.vel = [-repulsedVelocity * vel for vel in c.vel] c.pos = [pos + vel for pos, vel in zip(c.pos, c.vel)] for i in xrange(len(c.pos)): c.pos = model.singleRetry(can.pos, i) tot_deaths += num_deaths #if you want to see step by step particle movement uncomment below #warning you will end up having to terminate this manually #g.graph() # print "=======================" # print "BEGIN DOM PROC K: ", st.k # print "=======================" best = st.s[0] best_list = [] low_diff = [] for c in st.s: best = c # print 'c is ', c.uniq # We first check the can's personal best # and update it if its current position # dominates. if model.cdom(c.pos, c.pbest, c): c.pbest = list(c.pos) for can in st.s: if c == can: continue elif c.pos == can.pos: # print "PARTICLES IN SAME POSITION" continue # If we're at this point then the particles # aren't exactly equal in position, and also # aren't the same particle. diff = sum([math.fabs(x-y) for x,y in zip(c.pos, can.pos)]) # if diff < 1: # # print 'diff: ', diff, ' particle ids: ', c.uniq, can.uniq # low_diff.append((c.uniq, can.uniq)) if model.cdom(can.pos, best.pos, can, best): #If we're here then we've been #bettered by the next can, so we #just set it to be our cursor best = can # Once we make it out here we should have the # global best candidate. if(not best.uniq in best_list): best_list.append(best.uniq) # print 'best id after run ', best.uniq st.sb = best.pos st.eb = model.energy(st.sb) # print 'low diff list ', low_diff # print 'best_list ', best_list st.k -= 1 # We need a clean slate here. # print '++++++++++++++++++++++++++++++++++++++++++++++++++++' # print 'Global best: ', st.sb, '\nGlobal best energy: ', st.eb # print 'Num deaths: ', tot_deaths # print 'Total number of particles ', changes*np # print "Attrition %0.2f percent" % (100.0 * (tot_deaths/(changes*np))) #st.s = gens(model, np) st.sb = st.s[0].pbest st.t -= 1 g.graph() g.graphEnergy() st.term()
def classicalGlobalPSO(model, retries, changes, graph=False, goal = 0.01, pat = 100, era = 100, np=30, phi_1=2.8, phi_2=1.3, inertia=0.8): emin = 0 #setting vmax to full search range for an particle (from lit) # val bounds = [model.getBounds(i) for i in range(model.numOfDecisions())][0] # val difference vmax = max([(x[1] - x[0]) for x in [model.getBounds(i) for i in range(model.numOfDecisions())]]) print(vmax) s = gens(model, np) #initialize grapher if graph: g = grapher(model, np) for can in s: g.addVector(can.pos, can.uniq) # Energy here is set to zero since we're not actively using it for now. st = state(model.name, 'classicalGlobalPSO', s, 0, retries, changes, era) # print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++' # print 'Model Name: ', model.name, '\nOptimizer: Classical Global PSO, K: ', changes # Set an initial value for the global best # The downside to setting pbest equal to the current # particle position is that if there is a high phi_1 value # the particle will stay still until something happens globally # that pushes it away. st.sb = st.s[0].pbest st.sblast = st.s[0].pos bestcan = st.s[0] #Initialize objective mins and maxs model.initializeObjectiveMaxMin(st.sb) #we whould do this just in case for c in st.s: model.updateObjectiveMaxMin(c.pos) tot_deaths = 0 frontier = list() while st.t: st.k = changes while st.k: if st.sb == st.sblast: pat -= 1 if pat == 0: st.bored() break num_deaths = 0 for can in st.s: can.vel = [inertia * vel + (phi_1 * random.uniform(0,1) * (best - pos)) + (phi_2 * random.uniform(0,1) * (gbest - pos)) \ for vel, pos, best, gbest in zip(can.vel, can.pos, can.pbest, st.sb)] #checking if velocity is at max can.vel = [vmax if vel > vmax else vel for vel in can.vel] can.pos = [pos + vel for pos, vel in zip(can.pos, can.vel)] # Currently doing the same thing for particles that are # out of bounds and out of constraints, simply killing them # definitely some other options with this. If they get to # bounds can set vector to boundary, and vel ot zero, then # just let them be pulled back into the space. if not model.checkBounds(can.pos) or not model.checkConstraints(can.pos): can.pos = model.retry() can.vel = [0.0 for x in can.pos] # Should a killed candidate maintain it's phantom memory? # Uncomment this to wipe its memory. # can.pbest = list(can.pos) num_deaths += 1 #Update objective maxs and mins if graph: g.addVector(can.pos, can.uniq) model.updateObjectiveMaxMin(can.pos) tot_deaths += num_deaths #if you want to see step by step particle movement uncomment below #warning you will end up having to terminate this manually #g.graph() best = st.s[0] best_list = [] low_diff = [] for i in xrange(len(st.s) - 1): pbret = model.cdom(st.s[i].pos, st.s[i].pbest, st.s[i], st.s[i]) if pbret == 0 or pbret == -1: st.s[i].pbest = list(st.s[i].pos) ret_val = model.cdom(st.sb, st.s[i+1].pos, bestcan, st.s[i+1]) if ret_val == 0: st.sb = st.s[i+1].pos st.sblast = st.s[i+1].pos bestcan = st.s[i+1] elif ret_val == -1: addToFront(model, frontier, st.s[i+1].pos) if not st.sb in frontier: addToFront(model, frontier, st.sb) st.k -= 1 # We need a clean slate here. # print '++++++++++++++++++++++++++++++++++++++++++++++++++++' # print 'Global best: ', st.sb, '\nGlobal best energy: ', st.eb # print 'Num deaths: ', tot_deaths # print 'Total number of particles ', changes*np # print "Attrition %0.2f percent" % (100.0 * (tot_deaths/(changes*np))) for can in st.s: addToFront(model, frontier, can.pbest) f = [model.cal_objs(pos) for pos in frontier] st.addFrontier(f) st.s = gens(model, np) st.sb = st.s[0].pbest st.sblast = st.s[0].pbest bestcan = st.s[0] st.t -= 1 if graph: g.graph() g.graphEnergy() st.termPSO()
def PSOProbs(model, retries, changes, graph=False, goal = 0.01, pat = 100, \ era = 100, np=60, phi_1=3.8, phi_2=2.2, personalListSize=5, out='out.txt'): g = grapher(model, int(retries), 1, changes, "60NP_PSOProbsK" + str(changes)) probTracker = [Probs(np) for i in xrange(np)] emin = 0 phi_tot = phi_1 + phi_2 k = (2.0/math.fabs(2.0 - (phi_tot) - math.sqrt(phi_tot**2.0 - 4.0*phi_tot))) # print "constriction factor ", k s = gens(model, np, personalListSize) st = state(model.name, '60NP_PSOProbs', s, 0, retries, changes, era, out=out) st.sb = st.s[0].pos bestcan = st.s[0] tot_deaths = 0 model.initializeObjectiveMaxMin(st.sb) for c in st.s: model.updateObjectiveMaxMin(c.pos) frontier = runDom(st, model, list()) # basic idea is this is a list of positions that # persist between retries. global_frontier = list() #retry loop while st.t: print "." # model.initializeObjectiveMaxMin(st.sb) # for c in st.s: # model.updateObjectiveMaxMin(c.pos) st.k = changes #iterative loop while st.k: if st.k % 100 == 0: print "="*29 print "k ", st.k print "tot_deaths ", tot_deaths num_deaths = 0 for can in st.s: previousEnergy = model.energy(can.pos) #we could also use domination instead against the pbest list currentParticle = probTracker[can.uniq].getParticle() pbest = [x for x in st.s if x.uniq == currentParticle][0].pos can.vel = [k * (vel + (phi_1 * random.uniform(0,1) * (best - pos)) + \ (phi_2 * random.uniform(0,1) * (gbest - pos))) \ for vel, pos, best, gbest in zip(can.vel, can.pos, pbest, frontier[0])] # print 'can.vel ', can.vel can.pos = [pos + vel for pos, vel in zip(can.pos, can.vel)] if not model.checkBounds(can.pos) or not model.checkConstraints(can.pos): can.pos = model.retry() can.vel = [0.1 for x in can.pos] num_deaths += 1 model.updateObjectiveMaxMin(can.pos) if(model.energy(can.pos) < previousEnergy): #could use dom against pbest list probTracker[can.uniq].increaseProb(currentParticle) else: probTracker[can.uniq].decreaseProb(currentParticle) #probTracker[0].printProbs() tot_deaths += num_deaths #g.trackParticle(st.s[0].pos, 0, st.k) # for v in st.s: # g.addVector(v.pos, v.uniq) runDom(st, model, frontier) st.k -= 1 st.s = gens(model, np, personalListSize) st.sb = st.s[0].pbest bestcan = st.s[0] for f in frontier: st.app_out(str(model.cal_objs_2(f)) + '\n') global_frontier.append(f) g.addVector(f, int(st.t)) frontier = runDom(st,model,list()) st.t -= 1 # for v in st.s: # g.addVector(v.pbest[0], v.uniq) for f in global_frontier: st.reg_front.append(model.cal_objs_2(f)) st.norm_front.append(model.cal_objs(f)) g.graph() g.graphEnergy() #g.graphTrackedParticle() st.termPSO()
def classicalGlobalPSO(model, retries, changes, graph=False, goal=0.01, pat=100, era=100, np=30, phi_1=2.8, phi_2=1.3, inertia=0.8): emin = 0 #setting vmax to full search range for an particle (from lit) # val bounds = [model.getBounds(i) for i in range(model.numOfDecisions())][0] # val difference vmax = max([ (x[1] - x[0]) for x in [model.getBounds(i) for i in range(model.numOfDecisions())] ]) print(vmax) s = gens(model, np) #initialize grapher if graph: g = grapher(model, np) for can in s: g.addVector(can.pos, can.uniq) # Energy here is set to zero since we're not actively using it for now. st = state(model.name, 'classicalGlobalPSO', s, 0, retries, changes, era) # print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++' # print 'Model Name: ', model.name, '\nOptimizer: Classical Global PSO, K: ', changes # Set an initial value for the global best # The downside to setting pbest equal to the current # particle position is that if there is a high phi_1 value # the particle will stay still until something happens globally # that pushes it away. st.sb = st.s[0].pbest st.sblast = st.s[0].pos bestcan = st.s[0] #Initialize objective mins and maxs model.initializeObjectiveMaxMin(st.sb) #we whould do this just in case for c in st.s: model.updateObjectiveMaxMin(c.pos) tot_deaths = 0 frontier = list() while st.t: st.k = changes while st.k: if st.sb == st.sblast: pat -= 1 if pat == 0: st.bored() break num_deaths = 0 for can in st.s: can.vel = [inertia * vel + (phi_1 * random.uniform(0,1) * (best - pos)) + (phi_2 * random.uniform(0,1) * (gbest - pos)) \ for vel, pos, best, gbest in zip(can.vel, can.pos, can.pbest, st.sb)] #checking if velocity is at max can.vel = [vmax if vel > vmax else vel for vel in can.vel] can.pos = [pos + vel for pos, vel in zip(can.pos, can.vel)] # Currently doing the same thing for particles that are # out of bounds and out of constraints, simply killing them # definitely some other options with this. If they get to # bounds can set vector to boundary, and vel ot zero, then # just let them be pulled back into the space. if not model.checkBounds( can.pos) or not model.checkConstraints(can.pos): can.pos = model.retry() can.vel = [0.0 for x in can.pos] # Should a killed candidate maintain it's phantom memory? # Uncomment this to wipe its memory. # can.pbest = list(can.pos) num_deaths += 1 #Update objective maxs and mins if graph: g.addVector(can.pos, can.uniq) model.updateObjectiveMaxMin(can.pos) tot_deaths += num_deaths #if you want to see step by step particle movement uncomment below #warning you will end up having to terminate this manually #g.graph() best = st.s[0] best_list = [] low_diff = [] for i in xrange(len(st.s) - 1): pbret = model.cdom(st.s[i].pos, st.s[i].pbest, st.s[i], st.s[i]) if pbret == 0 or pbret == -1: st.s[i].pbest = list(st.s[i].pos) ret_val = model.cdom(st.sb, st.s[i + 1].pos, bestcan, st.s[i + 1]) if ret_val == 0: st.sb = st.s[i + 1].pos st.sblast = st.s[i + 1].pos bestcan = st.s[i + 1] elif ret_val == -1: addToFront(model, frontier, st.s[i + 1].pos) if not st.sb in frontier: addToFront(model, frontier, st.sb) st.k -= 1 # We need a clean slate here. # print '++++++++++++++++++++++++++++++++++++++++++++++++++++' # print 'Global best: ', st.sb, '\nGlobal best energy: ', st.eb # print 'Num deaths: ', tot_deaths # print 'Total number of particles ', changes*np # print "Attrition %0.2f percent" % (100.0 * (tot_deaths/(changes*np))) for can in st.s: addToFront(model, frontier, can.pbest) f = [model.cal_objs(pos) for pos in frontier] st.addFrontier(f) st.s = gens(model, np) st.sb = st.s[0].pbest st.sblast = st.s[0].pbest bestcan = st.s[0] st.t -= 1 if graph: g.graph() g.graphEnergy() st.termPSO()
def PSO(model, retries, changes, graph=False, goal = 0.01, pat = 100, \ era = 100, np=30, phi_1=3.8, phi_2=2.2, personalListSize=5): g = grapher(model, int(retries), 1, changes, "PSO" + str(changes)) emin = 0 phi_tot = phi_1 + phi_2 k = (2.0 / math.fabs(2.0 - (phi_tot) - math.sqrt(phi_tot**2.0 - 4.0 * phi_tot))) s = gens(model, np, personalListSize) st = state(model.name, 'adaptiveGlobalPSO', s, 0, retries, changes, era) st.sb = st.s[0].pos bestcan = st.s[0] tot_deaths = 0 model.initializeObjectiveMaxMin(st.sb) for c in st.s: model.updateObjectiveMaxMin(c.pos) frontier = runDom(st, model, list()) # basic idea is this is a list of positions that # persist between retries. global_frontier = list() #retry loop while st.t: print "." st.k = changes #iterative loop while st.k: if st.k % 100 == 0: print "=" * 29 print "k ", st.k print "tot_deaths ", tot_deaths num_deaths = 0 for can in st.s: can.vel = [k * (vel + (phi_1 * random.uniform(0,1) * (best - pos)) + \ (phi_2 * random.uniform(0,1) * (gbest - pos))) \ for vel, pos, best, gbest in zip(can.vel, can.pos, can.pbest[0], frontier[0])] # print 'can.vel ', can.vel can.pos = [pos + vel for pos, vel in zip(can.pos, can.vel)] if not model.checkBounds( can.pos) or not model.checkConstraints(can.pos): can.pos = model.retry() can.vel = [0.1 for x in can.pos] num_deaths += 1 model.updateObjectiveMaxMin(can.pos) tot_deaths += num_deaths g.trackParticle(st.s[0].pos, 0, st.k) # for v in st.s: # g.addVector(v.pos, v.uniq) runDom(st, model, frontier) st.k -= 1 st.s = gens(model, np, personalListSize) st.sb = st.s[0].pbest bestcan = st.s[0] st.t -= 1 for f in frontier: global_frontier.append(f) g.addVector(f, int(st.t)) frontier = runDom(st, model, list()) # for v in st.s: # g.addVector(v.pbest[0], v.uniq) for f in global_frontier: st.reg_front.append(model.cal_objs_2(f)) st.norm_front.append(model.cal_objs(f)) g.graph() g.graphEnergy() g.graphTrackedParticle() st.termPSO()
def PSO(model, retries, changes, graph=False, goal = 0.01, pat = 100, \ era = 100, np=30, phi_1=3.8, phi_2=2.2, personalListSize=5): g = grapher(model, int(retries), 1, changes, "PSO" + str(changes)) emin = 0 phi_tot = phi_1 + phi_2 k = (2.0/math.fabs(2.0 - (phi_tot) - math.sqrt(phi_tot**2.0 - 4.0*phi_tot))) s = gens(model, np, personalListSize) st = state(model.name, 'adaptiveGlobalPSO', s, 0, retries, changes, era) st.sb = st.s[0].pos bestcan = st.s[0] tot_deaths = 0 model.initializeObjectiveMaxMin(st.sb) for c in st.s: model.updateObjectiveMaxMin(c.pos) frontier = runDom(st, model, list()) # basic idea is this is a list of positions that # persist between retries. global_frontier = list() #retry loop while st.t: print "." st.k = changes #iterative loop while st.k: if st.k % 100 == 0: print "="*29 print "k ", st.k print "tot_deaths ", tot_deaths num_deaths = 0 for can in st.s: can.vel = [k * (vel + (phi_1 * random.uniform(0,1) * (best - pos)) + \ (phi_2 * random.uniform(0,1) * (gbest - pos))) \ for vel, pos, best, gbest in zip(can.vel, can.pos, can.pbest[0], frontier[0])] # print 'can.vel ', can.vel can.pos = [pos + vel for pos, vel in zip(can.pos, can.vel)] if not model.checkBounds(can.pos) or not model.checkConstraints(can.pos): can.pos = model.retry() can.vel = [0.1 for x in can.pos] num_deaths += 1 model.updateObjectiveMaxMin(can.pos) tot_deaths += num_deaths g.trackParticle(st.s[0].pos, 0, st.k) # for v in st.s: # g.addVector(v.pos, v.uniq) runDom(st, model, frontier) st.k -= 1 st.s = gens(model, np, personalListSize) st.sb = st.s[0].pbest bestcan = st.s[0] st.t -= 1 for f in frontier: global_frontier.append(f) g.addVector(f, int(st.t)) frontier = runDom(st,model,list()) # for v in st.s: # g.addVector(v.pbest[0], v.uniq) for f in global_frontier: st.reg_front.append(model.cal_objs_2(f)) st.norm_front.append(model.cal_objs(f)) g.graph() g.graphEnergy() g.graphTrackedParticle() st.termPSO()