def test_fix_tfill(self): solution = sols.MultiSol(130, self.chrom, self.tfill, self.bpp) testviolations = self.moop.rackcapacity(solution.getx(), solution.gettfill()) self.moop.fixcoolingrack(testviolations, solution) solution = coordarrays(solution) fillviolations = self.moop.period_fill_limit(solution) if fillviolations: self.moop.fix_tfill(fillviolations, solution) violations_left = self.moop.period_fill_limit(solution) self.assertEqual(violations_left, [])
def test_openonebox(self): solution = sols.MultiSol(0, self.chrom0, self.tfill0, self.bpp) mooproblem.checkformismatch(solution) violations = self.moop.rackcapacity(solution.getx(), solution.gettfill()) for tv in range(len(violations)): t = violations[tv][0] self.moop.openonebox(t, solution) out = StringIO() mooproblem.checkformismatch(solution, out=out) output = out.getvalue().strip() self.assertEqual(output, '')
def setUp(self): storedchrom = np.load('tests/chrom0.npz') chrom = storedchrom[storedchrom.files[0]] self.n = len(chrom) storedtfill = np.load('tests/tfill0.npz') tfill = storedtfill[storedtfill.files[0]] cookies = coolcookies.makeobjects(self.n, 100, 'tests/Cookies1000.txt') bpp = bp.BPP(self.n, 24, 300, cookies) self.moop = mooproblem.MOCookieProblem(self.n, 24, 300, 8, cookies) self.gen = moma.Generation(self.n, 100, 200, cookies, bpp, self.moop) self.m = sols.MultiSol(0, chrom, tfill, bpp) self.m = self.gen.moop.calcfeasibility(self.m)
def test_reduce(self): # Create 10 solutions for m in range(10): tfill = self.gen.initialtfill() newsol = sols.MultiSol(m, sample(range(24), 24), tfill, self.bpp) newsol.updaterank(round(m / 2)) self.gen.archive[m] = newsol # Test for normal reduction self.gen.reduce(6) self.assertEqual(len(self.gen.archive), 6) # Recreate 10 solutions with different crowding distance values for m in range(6): self.gen.archive[m].updaterank(round(m / 5)) self.gen.archive[m].updatecd(m) for m in range(4): tfill = self.gen.initialtfill() newsol = sols.MultiSol(m, sample(range(24), 24), tfill, self.bpp) newsol.updaterank(round((m + 6) / 5)) newsol.updatecd(m + 6) self.gen.archive[m + 6] = newsol # Test for cd reduction self.gen.reduce(6) self.assertEqual(len(self.gen.archive), 6)
def test_approx(self): # Create 100 solutions self.gen.archive = {} ids = sample(range(1000), 100) for m in range(100): tfill = self.gen.initialtfill() newsol = sols.MultiSol(m, sample(range(24), 24), tfill, self.bpp) newsol = self.gen.moop.calcfeasibility(newsol) newsol = bp.coordarrays(newsol) fits = self.gen.moop.calcfits(newsol) newsol.updatefitvals(fits) self.gen.archive[newsol.index] = newsol ndset = self.gen.finalapproxset() self.assertNotEqual(len(ndset), 0)
def addnewsol(self, newgenes): # This function creates a new solution from an individual newgenes. # Step 1: Make new solution newsol = sols.MultiSol(self.idnum, newgenes[0], newgenes[1], self.bpp) # Step 2: Check & Fix feasibility of solution newsol = self.moop.calcfeasibility(newsol) checkformismatch(newsol) # Step 3: Calculate fitness values of solution fits = self.moop.calcfits(newsol) newsol.updatefitvals(fits) self.updateid() self.updatefe() self.q.append(newsol) self.archive[newsol.index] = newsol
def test_fixcoolingrack(self): solution = sols.MultiSol(129, self.chrom, self.tfill, self.bpp) testviolations = self.moop.rackcapacity(solution.getx(), solution.gettfill()) self.moop.fixcoolingrack(testviolations, solution) solution = coordarrays(solution) # Make sure fixcoolingrack() fixed all violations testviolations = self.moop.rackcapacity(solution.getx(), solution.gettfill()) self.assertEqual(testviolations, []) # Make sure it didn't create other constraint errors self.moop.noreplacement(129, solution.getx()) tviolations = self.moop.timeconstraint(solution) self.assertEqual(tviolations, []) self.moop.calcfeasibility(solution)
def test_fittruncation(self): # Create 20 solutions with cd-values self.gen.archive = {} for m in range(20): tfill = self.gen.initialtfill() newsol = sols.MultiSol(m, sample(range(24), 24), tfill, self.bpp) newsol.updatefitvals([10, 50, 900]) newsol.updaterank(choice([1, 2])) newsol.updatecd(choice(range(10)) / 10) self.gen.archive[m] = newsol approxset = [m for k, m in self.gen.archive.items() if m.getrank() == 1] keys = [k for k, m in self.gen.archive.items() if m.getrank() == 1] self.gen.fittruncation(keys, approxset, 5) newapproxset = [m for k, m in self.gen.archive.items() if m.getrank() == 1] self.assertEqual(len(newapproxset), 5)
def test_approx(self): # Create 100 solutions self.gen.archive = {} ids = sample(range(1000), 100) for m in range(100): newsol = sols.MultiSol(ids[m], sample(range(10), 10), np.zeros((10, 1), dtype=int), self.bpp) newsol = self.gen.moop.calcfeasibility(newsol) newsol.x, newsol.y, newsol.tfill = bp.coordarrays( newsol.x, newsol.y, newsol.tfill) fits = self.gen.moop.calcfits(newsol) newsol.updatefitvals(fits) self.gen.archive[newsol.index] = newsol ndset = self.gen.finalapproxset() self.assertNotEqual(len(ndset), 0)
def test_getvlrep(self): solution = sols.MultiSol(8, self.chrom, self.tfill, self.bpp) vlrep = solution.getvlrep() self.assertTrue(vlrep) vlrepi = solution.getvlrep(i=0) self.assertEqual(vlrep[0], vlrepi)
def test_init(self): solution = sols.MultiSol(8, self.chrom, self.tfill, self.bpp) open = solution.getopenbins() self.assertEqual(len(solution.vlrep), open) self.assertEqual(np.sum(solution.getx()[open, :]), 0)