forked from jal278/novelty-search-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorganism.cpp
144 lines (121 loc) · 3.43 KB
/
organism.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "organism.h"
#include "noveltyset.h"
#include <cstring>
using namespace NEAT;
Organism::Organism(double fit, Genome *g,int gen, const char* md) {
noveltypoint=NULL;
fitness=fit;
orig_fitness=fitness;
gnome=g;
net=gnome->genesis(gnome->genome_id);
species=0; //Start it in no Species
expected_offspring=0;
generation=gen;
eliminate=false;
error=0;
winner=false;
champion=false;
super_champ_offspring=0;
// If md is null, then we don't have metadata, otherwise we do have metadata so copy it over
if(md == 0) {
strcpy(metadata, "");
} else {
strncpy(metadata, md, 128);
}
time_alive=0;
//DEBUG vars
pop_champ=false;
pop_champ_child=false;
high_fit=0;
mut_struct_baby=0;
mate_baby=0;
modified = true;
}
Organism::Organism(const Organism& org)
{
noveltypoint=NULL;
fitness = org.fitness;
orig_fitness = org.orig_fitness;
if(org.noveltypoint)
{
noveltypoint=new noveltyitem(*(org.noveltypoint));
}
gnome = new Genome(*(org.gnome)); // Associative relationship
//gnome = org.gnome->duplicate(org.gnome->genome_id);
net = new Network(*(org.net)); // Associative relationship
species = org.species; // Delegation relationship
expected_offspring = org.expected_offspring;
generation = org.generation;
eliminate = org.eliminate;
error = org.error;
winner = org.winner;
champion = org.champion;
super_champ_offspring = org.super_champ_offspring;
strcpy(metadata, org.metadata);
//printf("copying %s did it work? %s", org.metadata, metadata);
time_alive = org.time_alive;
pop_champ = org.pop_champ;
pop_champ_child = org.pop_champ_child;
high_fit = org.high_fit;
mut_struct_baby = org.mut_struct_baby;
mate_baby = org.mate_baby;
modified = false;
}
Organism::~Organism() {
delete net;
delete gnome;
if(noveltypoint)
{
if(!noveltypoint->added)
delete noveltypoint;
}
}
void Organism::update_phenotype() {
//First, delete the old phenotype (net)
delete net;
//Now, recreate the phenotype off the new genotype
net=gnome->genesis(gnome->genome_id);
modified = true;
}
bool Organism::print_to_file(char *filename) {
std::ofstream oFile(filename);
return write_to_file(oFile);
}
bool Organism::write_to_file(std::ostream &outFile) {
char tempbuf2[1024];
if(modified == true) {
sprintf(tempbuf2, "/* Organism #%d Fitness: %f Time: %d */\n", (gnome)->genome_id, orig_fitness, time_alive);
} else {
sprintf(tempbuf2, "/* %s */\n", metadata);
}
outFile << tempbuf2;
gnome->print_to_file(outFile);
return 1;
}
//// Print the Organism's genome to a file preceded by a comment
//// detailing the organism's species, number, and fitness
//bool Organism::print_to_file(char *filename) {
//
//ofstream oFile(filename,ios::out);
//
//cout<<"FILENAME: "<<filename<<endl;
//
////Make sure it worked
//if (!oFile) {
//cerr<<"Can't open "<<filename<<" for output"<<endl;
//return 0;
//}
//
////Put the fitness and other information for each organism in a comment
//oFile<<endl<<"/* Organism #"<<gnome->genome_id<<" Fitness: "<<fitness<<" *///"<<endl;
//
//gnome->print_to_file(oFile);
//
//return 1;
//}
bool NEAT::order_orgs(Organism *x, Organism *y) {
return (x)->fitness > (y)->fitness;
}
bool NEAT::order_orgs_by_adjusted_fit(Organism *x, Organism *y) {
return (x)->fitness / (x->species)->organisms.size() > (y)->fitness / (y->species)->organisms.size();
}