forked from jal278/novelty-search-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoveltyset.cpp
61 lines (56 loc) · 1.57 KB
/
noveltyset.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "noveltyset.h"
#include "population.h"
#include "organism.h"
//for sorting by novelty
bool cmp(const noveltyitem *a, const noveltyitem* b)
{
return a->novelty < b->novelty;
}
//for sorting by fitness
bool cmp_fit(const noveltyitem *a, const noveltyitem *b)
{
return a->fitness < b->fitness;
}
noveltyitem::noveltyitem(const noveltyitem& item)
{
added=item.added;
genotype=new Genome(*(item.genotype));
phenotype=new Network(*(item.phenotype));
age=item.age;
fitness=item.fitness;
novelty=item.novelty;
generation=item.generation;
indiv_number=item.indiv_number;
for(int i=0;i<(int)item.data.size();i++)
{
vector<float> temp;
for(int j=0;j<(int)item.data[i].size();j++)
temp.push_back(item.data[i][j]);
data.push_back(temp);
}
}
//evaluate the novelty of the whole population
void noveltyarchive::evaluate_population(Population* pop,bool fitness)
{
Population *p = (Population*)pop;
vector<Organism*>::iterator it;
for(it=p->organisms.begin();it<p->organisms.end();it++)
evaluate_individual((*it),pop,fitness);
}
//evaluate the novelty of a single individual
void noveltyarchive::evaluate_individual(Organism* ind,Population* pop,bool fitness)
{
float result;
if(fitness) //assign fitness according to average novelty
{
result = novelty_avg_nn(ind->noveltypoint,-1,false,pop);
ind->fitness = result;
}
else //consider adding a point to archive based on dist to nearest neighbor
{
result = novelty_avg_nn(ind->noveltypoint,1,false);
ind->noveltypoint->novelty=result;
if(add_to_novelty_archive(result))
add_novel_item(ind->noveltypoint);
}
}