'''
Created on Feb 14, 2017

@author: julien
'''
from minos.experiment.experiment import Blueprint, Experiment
from minos.model.design import create_random_blueprint, mutate_blueprint,\
    mix_blueprints
from minos.search.ga.ga_search import search, GaExperiment, GaIndividual
from copy import deepcopy


class GaModelExperiment(Experiment, GaExperiment):

    def __init__(self, experiment):
        vars(self).update(vars(experiment))
        self.individual_type = GaBlueprint

    def random_individual(self):
        return GaBlueprint(individual=create_random_blueprint(self))

    def mutate_individual(self, individual):
        return GaBlueprint(
            individual=mutate_blueprint(
                individual,
                self.parameters,
                mutate_in_place=False))

    def mix_individuals(self, individual1, individual2):
        return GaBlueprint(
            individual=mix_blueprints(
                individual1,
                individual2,
                self.parameters))


class GaBlueprint(Blueprint, GaIndividual):

    def __init__(self, **kwargs):
        individual = kwargs.get('individual', None)
        if individual:
            vars(self).update(vars(individual))
        else:
            vars(self).update(kwargs)

    @classmethod
    def copy(cls, other_individual):
        return GaBlueprint(deepcopy(other_individual.blueprint))

    def clone(self):
        return GaBlueprint.copy(self)


def run_ga_search_experiment(experiment):
    search(GaModelExperiment(experiment))

@@ -1483,24 +1483,104 @@
 t(experiment
+, population_size=50,%0A                             generations=100, resume=False
 ):%0A    searc
@@ -1581,16 +1581,25 @@
  search(
+%0A        
 GaModelE
@@ -1607,22 +1607,119 @@
 periment(experiment)
+,%0A        population_size=population_size,%0A        generations=generations,%0A        resume=resume
 )%0A

add experiment parameters