-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchromosome.h
executable file
·143 lines (100 loc) · 3.02 KB
/
chromosome.h
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
/***************************************************************************
* Copyright (C) 2011 by TEIL *
* *
***************************************************************************/
#ifndef _CHROMOSOME_H
#define _CHROMOSOME_H
#include <unordered_map>
#include "global.h"
#include "nk-wa.h"
using namespace std;
class Chromosome {
public:
static enum Function {
ONEMAX=0,
MKTRAP=1,
FTRAP=2,
CYCTRAP=3,
NK=4,
SPINGLASS=5,
SAT=6
} function;
Chromosome ();
Chromosome (int n_ell);
~Chromosome ();
bool hasSeen() const;
bool GHC();
void steepestDescent();
void init (int _ell);
void init0 (int _ell);
void initR (int _ell);
bool tryFlipping (int index);
int getVal (int index) const {
assert (index >= 0 && index < length);
int q = quotientLong(index);
int r = remainderLong(index);
if ( (gene[q] & (1lu << r)) == 0 )
return 0;
else
return 1;
}
void setVal (int index, int val) {
assert (index >= 0 && index < length);
if (getVal(index) == val) return;
setValF(index, val);
key ^= zKey[index];
}
unsigned long getKey () const {
return key;
}
void setValF (int index, int val) {
assert (index >= 0 && index < length);
//outputErrMsg ("Index overrange in Chromosome::operator[]");
int q = quotientLong(index);
int r = remainderLong(index);
if (val == 1)
gene[q] |= (1lu<<r);
else
gene[q] &= ~(1lu<<r);
evaluated = false;
}
void flip (int index) {
assert (index >= 0 && index < length);
int q = quotientLong(index);
int r = remainderLong(index);
gene[q] ^= (1lu<<r);
key ^= zKey[index];
evaluated = false;
}
/** real evaluator */
double evaluate ();
bool isEvaluated () const;
bool operator== (const Chromosome & c) const;
Chromosome & operator= (const Chromosome & c);
double getFitness ();
double trap (int u, double high, double low, int trapK) const;
double oneMax () const;
double mkTrap (double high, double low) const;
double cycTrap(double fHigh, double fLow) const;
double fTrap () const;
double spinGlass () const;
double nkFitness() const;
double satFitness() const;
int getLength () const;
void setLength ();
double getMaxFitness () const;
public:
static int nfe;
static int lsnfe;
static int hitnfe;
static bool hit;
static unordered_map<unsigned long, double> cache;
protected:
unsigned long *gene;
int length;
int lengthLong;
double fitness;
bool evaluated;
unsigned long key;
};
#endif