-
Notifications
You must be signed in to change notification settings - Fork 0
/
bisection.cpp
218 lines (180 loc) · 5.17 KB
/
bisection.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// -*- c++ -*-
//
// File: bisection.cpp
//
// Description: Contains the main function.
//
// Author: Fernando Lobo
//
// Date: June/1999
//
// Modified to be compliant with gcc 3.4 by Kumara Sastry
// Date: March/2006
#include "utility.hpp" // utility functions and procedures
#include "random.hpp" // random number generator
#include "parameter.hpp" // parameters for the ECGA
#include "ecga.hpp" // ECGA code
#include "objfunc.hpp"
#include <assert.h>
#include <iostream>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
double parameter::seed;
int parameter::lchrom;
int parameter::popsize;
double parameter::pcross;
int parameter::tournament_size;
int parameter::stop_criteria;
double parameter::stop_criteria_arg;
bool parameter::learn_MPM;
bool parameter::report_pop;
bool parameter::report_string;
bool parameter::report_fitness;
bool parameter::report_MPM;
OBJFUNC parameter::objfunc = TRAP;
bool parameter::found_optima = false;
int parameter::generation = 0;
model *parameter::schemata;
randomG RANDOM;
// return the identifier of the stop criteria. return -1 if not found.
int find_stop_criteria( char *s )
{
if( strcmp( s, "allele_convergence" ) == 0 ) return ALLELE_CONVERGENCE;
if( strcmp( s, "max_generations" ) == 0 ) return MAX_GENERATIONS;
if( strcmp( s, "found_optima") == 0) return FOUND_OPTIMA;
return -1;
}
//
// read parameters from input file.
//
void read_parameters( std::ifstream &in )
{
const int unset = -1;
int op;
char s[100] = "";
char s2[100] = "";
while( strcmp( s, "BEGIN" ) != 0 )
{
in.getline( s, 100 );
std::cout << s << std::endl;
}
in >> s >> parameter::lchrom;
std::cout << s << " " << parameter::lchrom << std::endl;
errorcheck("Check lchrom.", parameter::lchrom > 0 );
in >> s >> parameter::seed;
std::cout << s << " " << parameter::seed << std::endl;
errorcheck("seed must be in 0..1", parameter::seed > 0 && parameter::seed < 1 );
in >> s >> parameter::popsize;
std::cout << s << " " << parameter::popsize << std::endl;
errorcheck("population size must be even.", parameter::popsize % 2 == 0 );
errorcheck("population size must be positive.", parameter::popsize > 0 );
in >> s >> parameter::pcross;
std::cout << s << " " << parameter::pcross << std::endl;
errorcheck("pcross must be in 0..1", parameter::pcross >= 0 && parameter::pcross <= 1 );
in >> s >> parameter::tournament_size;
std::cout << s << " " << parameter::tournament_size << std::endl;
in >> s >> s2; parameter::learn_MPM = (strcmp(s2,"on") == 0);
std::cout << s << " " << s2 << std::endl;
in >> s >> s2; op = find_stop_criteria( s2 );
std::cout << s << " " << s2 << std::endl;
parameter::stop_criteria = op;
errorcheck("stop criteria not available.", op != -1 );
in >> s >> parameter::stop_criteria_arg;
std::cout << s << " " << parameter::stop_criteria_arg << std::endl;
in.getline( s, 100 );
in.getline( s, 100 );
in.getline( s, 100 );
in.getline( s, 100 );
in >> s >> s2;
parameter::report_pop = (strcmp(s2,"on") == 0);
std::cout << s << " " << s2 << std::endl;
in >> s >> s2;
parameter::report_string = (strcmp(s2,"on") == 0);
std::cout << s << " " << s2 << std::endl;
in >> s >> s2;
parameter::report_fitness = (strcmp(s2,"on") == 0);
std::cout << s << " " << s2 << std::endl;
in >> s >> s2;
parameter::report_MPM = (strcmp(s2,"on") == 0);
std::cout << s << " " << s2 << std::endl;
std::cout << "--------------------------------------------------------------------" << std::endl;
} // end of read_parameters
int main( int argc, char *argv[] )
{
// process command line
if( argc != 3 )
{
std::cout << "Usage: " << argv[0] << " inputfile outputfile" << std::endl;
std::cout << " Please read the README file." << std::endl;
exit(1);
}
// read parameters from input file
std::ifstream infile( argv[1] );
read_parameters( infile );
if(parameter::objfunc == TRAP)
{
errorcheck("TRAP_K doesn't divide lchrom" , parameter::lchrom %TRAP_K == 0);
infile.close();
}
// open output file
std::ofstream outfile( argv[2] );
// initilalize random number generator
RANDOM.randomize( parameter::seed );
// run the ECGA algorithm
//ecga ECGA;
//ECGA.run( outfile );
int lower = 0;
int higher = 30000;
int mid = 200;
char *best = new char[parameter::lchrom];
for(int i = 0 ; i < parameter::lchrom ; i++)
{
best[i] == '1';
}
while(1)
{
int i;
parameter::popsize = mid;
printf("[%d]",parameter::popsize);
for( i = 0 ; i < 10 ; i++)
{
parameter::found_optima = false;
ecga ECGA;
ECGA.run(outfile);
if(ECGA.best == best)
{
printf("+");
continue;
}
else
{
printf("-");
break;
}
}
std::cout<<std::endl;
if(i==10)
{
higher = mid;
mid = (mid+lower)/2;
}
else
{
lower = mid ;
mid = (mid+higher)/2;
}
if(lower *1.05 >= higher)
break;
mid = mid / 2 * 2 ;
}
delete[] best;
FILE *Result = fopen("bisectionresult","w");
fprintf(Result , "%d\n" , mid);
fclose(Result);
std::cout << "bisection result : " << higher <<std::endl;
std::cout << "ECGA done" << std::endl;
outfile.close();
return 1;
}