def process(idnum, t, chromosome, bpp, items): # Process solutions (decode, check constraints, calculate # fitness values, make solution object) x, y = bp.ed(idnum, chromosome, bpp, items) concheck(idnum, x, bpp) fit = mop.calcfits(x, y, items) a = MultiSol(idnum, chromosome, x, y, t, fit, 0, 0.0) return a
def subfit(self, k, chrom, bpp, method): # This function calculates the fitness value for subprob. k # Options: 'ed', 'll', 'dp', 'combo' if method == 'ed': x, y = bp.ed(0, chrom, bpp, self.items) else: x, y = bp.ll(chrom, bpp.getwbin(), bpp.getub(), bpp.getlb(), self.items) subfit = np.dot(self.lambdas[k], self.calcfits(x, y)) return x, y, subfit
def makeq(self): if self.t == 0: new = range(self.pop) else: new, self.q = sols.oldnew(self.archive, self.q, self.newgenes) # Make new solutions in q for m in new: x, y = bp.ed(self.idnum, self.newgenes[m], self.bpp) self.addnewsol(x, y, self.newgenes[m])
def rungen(self): if self.t == 0: newgenes = self.initialize() for m in range(self.pop): x, y = bp.ed(m, newgenes[m], self.bpp, self.items) self.swarm.append(Particle(self.n, x, y, newgenes[m])) self.t += 1 print('t = ', self.t) self.fitevalarch() self.psooperator() self.mutation() self.heuristic() print(self.funkeval, 'function evaluations have been performed.\n')
def perturb(self, xk): # This function performs the pertubation in Step 3.1 of MOMAD. alpha = random.random() if alpha == 0: alpha = random.random() blocklen = int(alpha * self.n) splitj = self.n - blocklen cut1 = xk.getgenes()[:splitj] cut2 = xk.getgenes()[splitj:] cut3 = random.sample(cut2, len(cut2)) newgene = cut1 + cut3 x, y = bp.ed(self.idnum, newgene, self.bpp, self.items) fit = self.moop.calcfits(x, y) xksquiggly = sol.MultiSol(self.idnum, newgene, x, y, self.t, fit, 0, 0.0) self.updateid() self.updatefe() return xksquiggly
def neighborhood(self, m, hoodsize): # This function takes solution m and finds all of its neighbors. # input: m # output: the set of m's neighbors # Limiting neighborhood to 100 neighbors to reduce computationload neighbors = [] while len(neighbors) < hoodsize: newgene = m.getgenes() a = random.randrange(0, len(newgene)) b = random.randrange(0, len(newgene)) newgene[a], newgene[b] = newgene[b], newgene[a] x, y = bp.ed(self.idnum, newgene, self.bpp, self.items) fit = self.moop.calcfits(x, y) a = sol.MultiSol(self.idnum, newgene, x, y, self.t, fit, 0, 0.0) neighbors.append(a) self.updateid() self.updatefe() return neighbors
def makeqbybins(self): # This function is almost the same as makeq() except modified for # the bin packing operators. # Sort out what solutions were modified by the crossover operation new, self.q = sols.oldnew(self.archive, self.q, self.newgenes) # First go through what's left in q for m in range(len(self.q)): qx = self.q[m].getx() qy = self.q[m].gety() x, y = self.binmutation(qx, qy) if np.all(np.equal(x, qx)) is False: # Indicates new solution del self.q[m] newchrom = self.updatechrom(x) self.addnewsol(x, y, newchrom) # Then operate on new solutions for m in new: newx, newy = bp.ed(self.idnum, self.newgenes[m], self.bpp) x, y = self.binmutation(newx, newy) if np.all(np.equal(x, newx)) is False: self.newgenes[m] = self.updatechrom(x) self.addnewsol(x, y, self.newgenes[m])