-
Notifications
You must be signed in to change notification settings - Fork 3
/
genotype.cpp
109 lines (88 loc) · 2.12 KB
/
genotype.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
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <random>
#include <fstream>
#include <assert.h>
#include "include/genotype.hpp"
using namespace std;
Genotype::Genotype(unsigned long sz) {
this->fitness = 0;
this->alloc(sz);
}
Genotype::~Genotype(void) {
this->dealloc();
}
bool Genotype::alloc(unsigned long sz) {
this->buff = (char *)malloc(sz+1);
if (this->buff == NULL) {
cerr << "No memory for a new Genotype sz:" << (sz+1) << endl;
this->sz = 0;
return false;
}
this->sz = sz;
return true;
}
void Genotype::dealloc(void) {
if (this->buff != NULL)
free(this->buff);
this->sz = 0;
}
unsigned long Genotype::size() {
return this->sz;
}
char *Genotype::read(void) {
return this->buff;
}
void Genotype::write(char *buff) {
memcpy(this->buff, buff, this->sz);
}
Genotype *Genotype::clone(void) {
Genotype *geno = new Genotype(this->sz);
geno->write(this->buff);
geno->set_fitness(this->fitness);
return geno;
}
void Genotype::random() {
for (unsigned int i=0; i<this->sz; i++) {
this->buff[i] = rand()%255;
}
}
void Genotype::show(void) {
cout << "< ";
for (unsigned int i=0; i<this->sz; i++)
printf("%hhx ", this->buff[i]);
cout << ">" << endl;
}
void Genotype::save(const char *filename) {
ofstream fd;
fd.open(filename, ios::out | ios::binary);
fd.write(this->buff, this->sz);
fd.close();
cout << "saved " << this->sz << " bytes" << endl;
}
void Genotype::load(const char *filename) {
this->dealloc();
ifstream fd;
fd.open(filename, ios::in | ios::binary | ios::ate);
if (this->alloc(fd.tellg())) {
fd.seekg(0);
fd.read(this->buff, this->sz);
cout << "loaded " << this->sz << " bytes" << endl;
}
fd.close();
}
void Genotype::set_fitness(float fitness) {
this->fitness = fitness;
}
float Genotype::get_fitness(void) {
return this->fitness;
}
void Genotype::put(int pos, char c) {
assert(pos<this->sz);
this->buff[pos] = c;
}
char Genotype::get(int pos) {
assert(pos<this->sz);
return this->buff[pos];
}