import genetic
import unittest
import numpy as np
import statistics

class TestSolveEquation(unittest.TestCase):

    def testSolve(self):
        size = int(10) #jen suda cisla

        def fitness_def(population):
            x = population
            solution = x+10 * np.sin(5 * x)+7 * np.cos(4 * x)
            return (x, solution)

        def evolve(population):
            C = genetic._aritmetics_crossover(population, size)
            X = genetic._mutation(C, size)
            population = np.concatenate((genetic._selection_elitism(population, size, fitness_def), X), axis=0)
            population = population
            vysledek = np.asarray(fitness_def(population))
            return vysledek, population

        def genetic_steps():
            populationSet = np.random.uniform(low=-size, high=size, size=(size, size))
            v = ""
            values = []
            for i in range(100):
                vysledek, population = evolve(populationSet)
                populationSet = population
                values.append(vysledek.max())
                v = vysledek.max()

            mean = statistics.mean(values)
            print(" {} {:3.2f} {:3.2f}".format(v, mean, statistics.stdev(values, mean)))

        def run():
            genetic.Benchmark.run(genetic_steps)

        run()


if __name__ == '__main__':
    unittest.main()


