-
Notifications
You must be signed in to change notification settings - Fork 1
/
parameter.cpp
223 lines (180 loc) · 4.83 KB
/
parameter.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
218
219
220
221
222
223
/***************************************************************************
parameter.cpp - description
-------------------
begin : ven ott 4 2002
copyright : (C) 2002 by Davide Patti
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "parameter.h"
#include "common.h"
#include <cstdlib>
#include <math.h> //for round
using namespace std;
Parameter::Parameter(){
set_label("NO_LABEL");
current = NOT_VALID;
}
Parameter::Parameter(const string& l,vector<int> possible_values,int default_val)
{
set_label(l);
set_values(possible_values,default_val);
}
//DEPRECATED constructor ! ( only for compatibility...)
Parameter::Parameter(int * possible_values,int default_val) {
int index=0;
while (possible_values[index]!=NOT_VALID)
{
values.push_back(possible_values[index]);
index++;
}
default_value = default_val;
set_val(default_value);
}
void Parameter::set_label(const string& l)
{
label = l;
}
string Parameter::get_label() const
{
return label;
}
Parameter::~Parameter(){
}
void Parameter::set_to_default()
{
set_val(default_value);
}
int Parameter::get_val() const
{
if (current!=NOT_VALID)
return values[current];
else
return NOT_VALID;
}
int Parameter::get_default() const
{
return default_value;
}
int Parameter::get_size() const
{
return values.size();
}
void Parameter::set_val(int new_value)
{
current = NOT_VALID;
for (int i =0;i<values.size();i++)
if (values[i]==new_value) current = i;
if (current==NOT_VALID)
{
string logfile = string(getenv(BASE_DIR))+string(EE_LOG_PATH);
int myid = get_mpi_rank();
write_to_log(myid,logfile," ERROR: not valid value for parameter '"+label+" (value=" + to_string(new_value) + "). Check subspace file.");
exit(EXIT_FAILURE);
}
}
void Parameter::set_random()
{
float r = (float)rand()/(RAND_MAX);
int random_index = (int)(r*values.size());
set_val(values[random_index]);
}
// added by [email protected]
void Parameter::set_random(int pos_a, int pos_b)
{
float r = (float)rand()*(pos_b - pos_a) /(RAND_MAX);
int offset = round(r);
vector<int> values = this->get_values();
int value = values[pos_a + offset];
set_val( value );
}
bool Parameter::increase()
{
if (++current==values.size())
{
current--;
return false;
}
return true;
}
bool Parameter::decrease()
{
if (current>0) return --current;
return false;
}
void Parameter::set_to_first()
{
current = 0;
}
void Parameter::set_to_last()
{
current = values.size()-1;
}
vector<int> Parameter::get_values() // mau
{
return values;
}
void Parameter::set_values(vector<int> values,int default_val)
{
this->values = values;
default_value = default_val;
#ifdef DEBUG
cout << "\n Parameter::set_values() ";
for(int i=0;i<values.size();i++) cout << "\n values " << this->values[i];
#endif
set_val(default_value);
}
void Parameter::set_values(Parameter parameter)
{
set_values(parameter.get_values(),parameter.get_default());
}
int Parameter::get_first()
{
#ifdef SEVERE_DEBUG
if ( values.size()<= 0 )
{
cout << "Parameter " << get_label();
printf("\nparameter.cpp %d: FATAL ERROR: values.size()=%u\n",__LINE__ ,values.size() );
exit(EXIT_FAILURE);
}
#endif
return values[0];
}
int Parameter::get_last()
{
#ifdef SEVERE_DEBUG
if ( values.size()<= 0 )
{
cout << "Parameter " << get_label();
printf("\nparameter.cpp %d: FATAL ERROR: values.size()=%u\n",__LINE__ ,values.size() );
exit(EXIT_FAILURE);
}
#endif
int i = values.size()-1;
int last_value = values[i];
return last_value;
}
int Parameter::get_pos(int value)
{
for(int i=0; i<values.size(); ++i)
if (values[i] == value) return (i+1);
return NOT_VALID;
}
// PARAMSPACE
// regardless a,b being allowed parameter values, this function returns values falling into interval
// notes: assuming a<=b , not the best implementation
//
vector<int> Parameter::get_interval(int a, int b)
{
vector<int> interval;
for(int i=0; i<values.size(); ++i)
if (values[i]>=a && values[i]<=b) interval.push_back(values[i]);
return interval;
}