Ejemplo n.º 1
0
def put_sample(json_data, population):
    sample = {
        'sample': json_data['sample'],
        'sample_id': json_data['sample_id']
    }
    evospace = Population(population)
    evospace.put_sample(sample)
Ejemplo n.º 2
0
def putback_sample(json_data, population):
    sample = None
    evospace = Population(population)
    if not isinstance(json_data, dict):
        sample = json.loads(json_data)
    else:
        sample = {
            'sample': json_data['sample'],
            'sample_id': json_data['sample_id']
        }

    evospace.putback_sample(sample)
Ejemplo n.º 3
0
def init_pop( populationSize, popName= "pop", rangemin = 0 ,rangemax = 11, listSize = 66):

    server = Population(popName)
    server.initialize()
    for individual in range(populationSize):
        chrome = [random.randint(rangemin,rangemax) for _ in range(listSize)]
        individual = {"id":None,"fitness":{"DefaultContext":0.0 },"chromosome":chrome,"views":0}
        server.put_individual(**individual)
Ejemplo n.º 4
0
class TestIndividual(TestCase):
    def setUp(self):
        self.population1 = Population("test_population1")
        self.population2 = Population("test_population2")

        self.population1.initialize()
        self.population2.initialize()


    def test_put(self):
        key = self.population1.individual_next_key()
        individual = Individual(id=key)
        individual.another_property = "test"
        self.assertEqual(individual.put(self.population1.name),True)
        self.assertEqual(individual.id,key)
        self.assertEqual(individual.another_property,"test")


    def test_get(self):
        key = self.population1.individual_next_key()
        individual = Individual(id=key)

        #No se puede hacer get sin guardar antes
        try:
            self.assertRaises(LookupError, individual.get())
        except:
            pass
        individual.put(self.population1.name)
        self.assertIsInstance(individual.get(),Individual)
        self.assertIsInstance(individual.get(as_dict=True),dict)


    def test_as_dict(self):
        key = self.population1.individual_next_key()
        individual = Individual(id=key)
        self.assertIsInstance(individual.as_dict(),dict)


    def test_two_populations(self):
        key = self.population1.individual_next_key()
        individual = Individual(id=key)
        individual.put(self.population1.name)
        individual.put(self.population2.name)
        self.assertIsInstance(individual.get(),Individual)
        self.assertIsInstance(individual.get(as_dict=True),dict)
Ejemplo n.º 5
0
class TestIndividual(TestCase):
    def setUp(self):
        self.population1 = Population("test_population1")
        self.population2 = Population("test_population2")

        self.population1.initialize()
        self.population2.initialize()

    def test_put(self):
        key = self.population1.individual_next_key()
        individual = Individual(id=key)
        individual.another_property = "test"
        self.assertEqual(individual.put(self.population1.name), True)
        self.assertEqual(individual.id, key)
        self.assertEqual(individual.another_property, "test")

    def test_get(self):
        key = self.population1.individual_next_key()
        individual = Individual(id=key)

        #No se puede hacer get sin guardar antes
        try:
            self.assertRaises(LookupError, individual.get())
        except:
            pass
        individual.put(self.population1.name)
        self.assertIsInstance(individual.get(), Individual)
        self.assertIsInstance(individual.get(as_dict=True), dict)

    def test_as_dict(self):
        key = self.population1.individual_next_key()
        individual = Individual(id=key)
        self.assertIsInstance(individual.as_dict(), dict)

    def test_two_populations(self):
        key = self.population1.individual_next_key()
        individual = Individual(id=key)
        individual.put(self.population1.name)
        individual.put(self.population2.name)
        self.assertIsInstance(individual.get(), Individual)
        self.assertIsInstance(individual.get(as_dict=True), dict)
Ejemplo n.º 6
0
def init_pop_crh(populationSize, popName= "pop"):

    server = Population(popName)
    server.initialize()
    for individual in range(populationSize):
        chrome = [random.randint(1,19),random.randint(10,30),random.randint(0,1),random.randint(0,1),random.randint(0,1),random.randint(0,1),random.randint(0,5),random.randint(0,1),random.randint(0,1),random.randint(0,1),random.randint(0,4),random.randint(0,1),random.randint(0,1),random.randint(0,2),random.randint(1,3)]
        individual = {"id":None,"fitness":{"DefaultContext":0.0 },"chromosome":chrome,"views":0}
        print individual
        server.put_individual(**individual)

    r = redis.StrictRedis(host='localhost', port=6379, db=0)
    redis_data = [eval(r.get(ind)) for ind in r.keys("pop:individual:*")]
    for ind in redis_data:
        #print len(redis_data)
        if ind["id"] != "":
            individual_node = Nodo()
            individual_node.create_nodo(element_type="individual",
                                        id=ind["id"],
                                        chromosome=ind["chromosome"],
                                        views=ind["views"])
Ejemplo n.º 7
0
def initialize_population(population_name='pop', size=10, template=None):
    evospace = Population(population_name)
    #Delete  population
    evospace.initialize()

    # Add 100 Individuals
    # Chromosome  Size determined by template

    # GENE      TYPE
    #   0       number of styles
    #   1       title
    #   2       image
    #   3       option 0
    #   N       option N

    # TODO: Generate individual chromosomes from template

    for i in range(size):
        individual = {
            'chromosome': [
                randint(0, 2),  # number of styles
                randint(0, 2),  # titles
                randint(0, 3),  # images
                randint(0, 2),  # salutation
                randint(0, 5),  # name
                randint(0, 4),  # action
            ],
            "fitness": {
                "DefaultContext": 0.0
            },
            "score":
            0,
            "id":
            None
        }

        evospace.put_individual(**individual)
Ejemplo n.º 8
0
def take_sample(population, size):
    evospace = Population(population)
    r = evospace.take_sample(size)
    return r
Ejemplo n.º 9
0
def get_sample(population, size):
    evospace = Population(population)
    r = evospace.get_sample(size)
    return r
Ejemplo n.º 10
0
def get_all_info_list(population):
    evospace = Population(population)
    all = evospace.get_all()
    return get_info_list(all)
Ejemplo n.º 11
0
    def setUp(self):
        self.population1 = Population("test_population1")
        self.population2 = Population("test_population2")

        self.population1.initialize()
        self.population2.initialize()
Ejemplo n.º 12
0
    def setUp(self):
        self.population1 = Population("test_population1")
        self.population2 = Population("test_population2")

        self.population1.initialize()
        self.population2.initialize()
Ejemplo n.º 13
0
def put_sample(sample_id, sample, popName="pop"):
    result =  {'sample_id':sample_id , 'sample':   sample}
    server = Population(popName)
    #server.put_sample(json.dumps(result))
    server.put_sample(result)
Ejemplo n.º 14
0
def get_sample(sample_size, popName= "pop" ):
    server =  Population(popName)
    sample =  server.get_sample(sample_size)
    return sample