def test_calc_cost_function_had12(self):
     size, m_flow, m_distance = data_loader.load('had12.dat', '..')
     assignment_had12 = Phenotype(size=size,
                                  m_flow=m_flow,
                                  m_dist=m_distance)
     factory = np.array([3, 10, 11, 2, 12, 5, 6, 7, 8, 1, 4, 9])
     assignment_had12.factories = list(map(lambda x: x - 1, factory))
     assignment_had12.calc_fitness_and_cost_fun()
     print(assignment_had12.m_dist[:, assignment_had12.factories][
         assignment_had12.factories])
     self.assertEqual(1652, assignment_had12.val_cost_fun)
 def test_calc_cost_function_chr25a(self):
     size, m_flow, m_distance = data_loader.load('chr25a.dat', '..')
     assignment_chr25a = Phenotype(size=size,
                                   m_flow=m_flow,
                                   m_dist=m_distance)
     factory = np.array([
         25, 12, 5, 3, 18, 4, 16, 8, 20, 10, 14, 6, 15, 23, 24, 19, 13, 1,
         21, 11, 17, 2, 22, 7, 9
     ])
     assignment_chr25a.factories = list(map(lambda x: x - 1, factory))
     assignment_chr25a.calc_fitness_and_cost_fun()
     self.assertEqual(3796, assignment_chr25a.val_cost_fun)
Exemple #3
0
 def __init__(self,
              filename,
              pop_size=POPULATIONS,
              gen_size=GENERATIONS,
              prob_cross=PROBABILITY_BECOME_PARENT,
              prob_mutate=PROBABILITY_MUTATE,
              save_best=False,
              selection_type=SelectionType.ROULETTE_TYPE,
              tournament_size=TOURNAMENT_SIZE):
     self.n, self.m_flow, self.m_dist = data_loader.load(filename)
     self.filename = filename
     self.pop_size = pop_size
     self.gen_size = gen_size
     self.prob_cross = prob_cross
     self.prob_mutate = prob_mutate
     self.save_best = save_best
     self.list_populations = []
     self.list_results = np.empty(shape=(gen_size, 3))
     self.selection_type = selection_type
     self.tournament_size = tournament_size
import random
import unittest
import os
from unittest import makeSuite

import numpy as np

# from ai.genetic_algorithm import phenotype
from genetic_algorithm import data_loader
from genetic_algorithm.solvers.phenotype import Phenotype

n, matrix_flow, matrix_distance = data_loader.load('had20.dat', '..')
assignment_had20 = Phenotype(size=n,
                             m_flow=matrix_flow,
                             m_dist=matrix_distance)


class TestSuite(unittest.TestSuite):
    def __init__(self):
        super(TestSuite, self).__init__()
        self.addTest(makeSuite(TestDataLoader))


class TestDataLoader(unittest.TestCase):
    def test_matrix_flow_size(self):
        self.assertEqual(n, matrix_flow.shape[0])

    def test_matrix_distance_size(self):
        self.assertEqual(n, matrix_flow.shape[1])