Esempio n. 1
0
    def testNsgaii(self):
        pop = [ChromosomeTestImplementation() for i in range(10)]
        self.assertEqual(3, len(pop[0].genes))
        pop = NSGAII.nsgaii(pop, 100)
        
        expected = [5.198961969831384, 0.08305567759063237, 5.9249528286171405, -2.292726823088209, 5.9249528286171405, 2.8836408759525156, -2.292726823088209, 5.9249528286171405, 5.198961969831384, 13.01326687754792]
        actual = [p.x() for p in pop]
        self.assertEqual(expected, actual)

        expected = [-14.893629327366531, -14.893629327366531, -4.073355846562075, -14.893629327366531, -10.698973150220933, -14.893629327366531, -14.893629327366531, -4.073355846562075, -14.893629327366531, 1.956945448043296]
        actual = [p.y() for p in pop]
        self.assertEqual(expected, actual)
Esempio n. 2
0
    def testFastNondominatedSort(self):
        def testRankList(expected, actual):
            self.assertEqual(3, actual[1])
            self.assertEqual(5, actual[8])
            self.assertEqual(11, actual[58])
            self.assertEqual(expected, actual)

        def testDominationFronts(expected, actual):
            self.assertEqual(expected, actual)
            self.assertEqual(expected[3], actual[3])
            self.assertEqual(expected[13], actual[13])
            self.assertEqual(len(expected), len(actual))

        expected = [{1: 3, 2: 11, 3: 7, 4: 10, 5: 12, 6: 2, 7: 4, 8: 5, 9: 12, 10: 5, 11: 7, 12: 7, 13: 4, 14: 8, 15: 8, 16: 7, 17: 2, 18: 14, 19: 5, 20: 8, 21: 9, 22: 5, 23: 3, 24: 8, 25: 6, 26: 5, 27: 2, 28: 6, 29: 8, 30: 9, 31: 1, 32: 10, 33: 6, 34: 6, 35: 5, 36: 4, 37: 7, 38: 3, 39: 6, 40: 7, 41: 1, 42: 8, 43: 1, 44: 8, 45: 9, 46: 5, 47: 7, 48: 1, 49: 2, 50: 1, 51: 10, 52: 4, 53: 6, 54: 10, 55: 1, 56: 4, 57: 7, 58: 11, 59: 2, 60: 9, 61: 4, 62: 3, 63: 3, 64: 12, 65: 3, 66: 7, 67: 7, 68: 3, 69: 2, 70: 11, 71: 4, 72: 9, 73: 9, 74: 7, 75: 3, 76: 4, 77: 1, 78: 4, 79: 13, 80: 2, 81: 5, 82: 11, 83: 3, 84: 9, 85: 10, 86: 10, 87: 12, 88: 7, 89: 8, 90: 5, 91: 7, 92: 1, 93: 6, 94: 9, 95: 7, 96: 3, 97: 1, 98: 5, 99: 9}, [[31, 41, 43, 48, 50, 55, 77, 92, 97], [59, 49, 6, 17, 27, 69, 80], [1, 23, 38, 62, 68, 75, 83, 63, 65, 96], [13, 71, 52, 56, 7, 36, 78, 61, 76], [8, 19, 46, 26, 35, 10, 81, 98, 22, 90], [53, 93, 28, 34, 33, 25, 39], [40, 37, 47, 57, 74, 91, 95, 66, 67, 12, 3, 11, 16, 88], [15, 89, 44, 29, 14, 42, 20, 24], [21, 45, 99, 30, 84, 60, 72, 73, 94], [32, 54, 51, 86, 4, 85], [70, 82, 2, 58], [87, 5, 9, 64], [79], [18]]]
        actual = NSGAII.fast_nondominated_sort(self.experiment_data)
        testRankList(expected[0], actual[0])
        testDominationFronts(expected[1], actual[1])
Esempio n. 3
0
import NSGAII as ga

g = ga.random_genome(10)
print(g)
a = ga.mutation(g)
print(a)
print(g-a)

Esempio n. 4
0
 def testCrowdingDistance(self):
     expected = {1: -0.005277881727443084, 2: -0.0389856133101291, 3: -0.11912221891050032, 4: -0.08710911662952933, 5: float("-inf"), 6: -0.0057697374019387605, 7: -0.00373786919007382, 8: -0.09215729954971238, 9: -0.7180540452514209, 10: -0.03126349023458373, 11: -0.11208814197186898, 12: -0.0064446680190158945, 13: -0.0419491286873437, 14: -0.024365739034623524, 15: -0.05838208499164267, 16: -0.015279797924385337, 17: -0.004535461063470064, 18: -0.06673137456927547, 19: -0.020323957466655242, 20: -0.021762618932308648, 21: -0.10781900390880647, 22: -0.09575401534870055, 23: -0.012742759629667278, 24: -0.021789612233681192, 25: -0.0840484815096793, 26: -0.01517860652704613, 27: -0.01018970997668605, 28: -0.01088364503008504, 29: -0.06142357996854804, 30: -0.06327375130634261, 31: -0.01577333249587775, 32: float("-inf"), 33: -0.009131566774265909, 34: -0.0291784634956522, 35: -0.016789767636670033, 36: -0.012710781551096683, 37: -0.01609771271332911, 38: -0.006787221807156954, 39: -0.03122136912850171, 40: -0.026455424368965474, 41: -0.00928121913123563, 42: -0.02711472351451418, 43: -0.009477253237482903, 44: -0.015217587074488611, 45: -0.07211367679963573, 46: -0.023556277936233383, 47: -0.03066217424610381, 48: float("-inf"), 49: -0.010066390046907504, 50: -0.00747812083927807, 51: -0.028933986398220087, 52: -0.00815847116981297, 53: -0.015735115304988338, 54: -0.17667782734839851, 55: float("-inf"), 56: -0.024298363414294602, 57: -0.04004794175604141, 58: -0.05352262541236784, 59: -0.00983764885039189, 60: -0.011575278773299533, 61: -0.01326997000875187, 62: -0.00575190129977881, 63: -0.02427620361859884, 64: -0.11231804101684652, 65: -0.03578980171485262, 66: -0.01846052412834165, 67: -0.024713138308141244, 68: -0.007318961198646914, 69: -0.03725066019629929, 70: -0.2855447103528305, 71: -0.011665672469770953, 72: -0.017801315497686376, 73: -0.028385022591023552, 74: -0.015766178288676946, 75: -0.005328516667542428, 76: -0.01621156654139609, 77: -0.007597673833036753, 78: -0.01967007823498536, 79: -0.05283504157859012, 80: -0.016706950867734376, 81: -0.01795597954749839, 82: -0.06083226039757647, 83: -0.005773139579648826, 84: -0.025099198487531782, 85: -0.04296860987285665, 86: -0.040388068636175355, 87: -0.7173463169955279, 88: -0.011580975333273858, 89: -0.27878256248551997, 90: -0.014638433556744011, 91: -0.07738453481020385, 92: float("-inf"), 93: -0.00937482384614046, 94: -0.03329380270680644, 95: -0.012539055900694544, 96: -0.016100872380293937, 97: -0.008006092887506535, 98: -0.003707822325512265, 99: -0.07267051591403287}
     actual = NSGAII.crowding_distance(self.experiment_data)
     self.assertEqual(expected, actual)
Esempio n. 5
0

if algorithmType == 1:
    # init population
    population = Utils.oldInitPopulation(pop_size, input_params)
    obj_constr = []
    for i in range(0, pop_size):
        obj_constr.append(Objectives.objectives_constraints(population[i], input_params))

    # convert to populationInfo
    populationInfo = []
    for i in range(0, pop_size):
        populationInfo.append((population[i], obj_constr[i]))

    # run algorithm
    P = NSGAII.algorithm(population, nsga_param, input_params,1)

    Utils.printPop(populationInfo, n)

elif algorithmType == 2:
    population = Utils.myInitPopulation(pop_size, input_params)
    obj_constr = []
    for i in range(0, pop_size):
        obj_constr.append(Objectives.objectives_constraints(population[i], input_params))

    # convert to populationInfo
    populationInfo = []
    for i in range(0, pop_size):
        populationInfo.append((population[i], obj_constr[i]))

    # run algorithm
Esempio n. 6
0
def demonstrate(seed = 42):
    random.seed(seed)
    logger = NSGAII.log_file('demonsga.log')
    pop = [ChromosomeTestImplementation() for i in range(10)]
    return NSGAII.nsgaii(pop, 100, logger.log)
Esempio n. 7
0
def evolveMnist(seed = 42):
    logger = NSGAII.log_file('mnistgenerations.log')
    pop = chromosome.initialMnistPopulation(40)
    sys.stdout.flush()
    result = NSGAII.nsgaii(pop, 100, logger.log)
    print result