-
Minimum element
Hi guys
I have this code
Code:
while (timer.elapsed_time(Timer::VIRTUAL) < tmax) {
.
.
.
.
cout << " value " << aSol->value() <<"\ttime " << timer.elapsed_time(Timer::VIRTUAL) << endl;
.
.
}
where while the time is smaller than tmax( a value that i put) prints some values.What i want is to print the smallest value of them. I thought of using min_element but i don't know how and i don't know if it is the right way.Just to help the value is defined like this
Code:
class Solution {
public:
Solution();
Solution(SC_instance* my_instance);
~Solution();
SC_instance* instance;
vector<int> selected_columns;
map<int,bool> is_covered;
map<int,bool> is_added;
int obj_value;
int n_covered;
int value();
void add_column(int i);
void rmv_column(int i);
bool is_complete();
};
If you want any other information please ask me.
Thanks a lot
-
What does int value() return? What does it do?
-
Returns the value of a solution(it is an instance). the code above represents an algorithm(if u want i can post it)
Ok i better have to post it. Here it is
Code:
while (timer.elapsed_time(Timer::VIRTUAL) < tmax) {
while (!aSol -> is_complete()) {
for (int i = 0; i < instance -> n_cols ; i++) {
if(aSol -> is_added[i]){
k[i] = -1;}
else{
not_covered = 0;
for (int j = 0; j < (instance->covers)[i].size(); j++) {
int aRow = (instance->covers)[i][j];
if (!aSol -> is_covered[aRow]) {
// cout<<"not covered "<<endl;
not_covered++;
}
}
k[i] = (double) (not_covered)*1000.0/(double) (instance -> cost[i]);
}
}
aSol -> add_column(GetMax(k,instance->n_cols));
}
cout << " value " << aSol->value() <<"\ttime " << timer.elapsed_time(Timer::VIRTUAL) << endl;
vector<int> sc = aSol->selected_columns;
random_shuffle (sc.begin(),sc.end() );
int count = 0;
for (int i = 0; i < sc.size()/2; i++){
count++;
aSol-> rmv_column(sc[i]);
}
}
with this ouput it gives all the values of every solution it finds within the time limits. I want to return me the better( which is the smallest). Hope u understand sth of this
-
You really need to go to your C++ IDE options and check some box that tells you to turn all TAB spaces into regular spaces. It makes your code hard to read because of the messed up indentation when copy-pasting to places like these forums.
I'm sorry Joan. I thought this would be a simpler problem to solve. I don't have the time right now to look into your code. Hopefully later today.
-
>>this ouput it gives all the values of every solution it finds within the time limits. I want to return me the better( which is the smallest).
Seems to me you will need to define what you mean by the smallest. Smallest could mean shortest time to run solution, least number of moves, whatever. Then you will need to be able to measure the parameter and store appropriate values to compare with other solutions.
For example, to time a solution you can start timer each time you look for a different solution, store the start time in a variable called start; stop the timer when solution found, and store that time in a variable called stop; determine interval between start and stop and store the time interval to use to comparison with next solution, if any.
Timing can be tricky because current computer cpu speeds allow execution of so many steps in so short of time that the time intervals become "meaningless". If you are trying to measure very short time intervals the ability to measure short intervals becomes the critical event, not the code.
-
No, elad..it is not that complicated.What i mean by smallest is simply the smallest value, it is an integer.But anyway thanx..I think i fixed it.
I defined a vector where are being stored the values of the solution ( after each step ) and then with *min_element() i get the smallest one. But i have another problem.For each step ( until it gets a solution) i have a timer that counts the time needed to get that solution ( you can see it above in the cout).Now, apart from the best solution ( this one with the smallest value) i need somehow to get also the corresponding time(for this solution). Any idea about this?? if you don't understand sth or u need some extra code, please be free to ask me. to help a little, the output is sth like this
Code:
value .... time .....
value .... time .....
value .... time .....
value .... time .....
value .... time .....
.
.
.
the number of iterations depends on the time limit i put ( tmax in the above code)
Thanx
-
>>while (timer.elapsed_time(Timer::VIRTUAL) < tmax)
This implies you are able to determine the difference between a reference start time and current time to get elapsed time. Whatever mechanism you use to do that (I described one possible algorhythm in my first post) can be used to calculate the time it took to develop each solution. If you can't have two timers going at the same time, one to keep track of elapsed time of the program and one to keep track of elapsed time of a given solution, then maybe you can keep track of the total time used for all previous solutions and elapsed time of the current solution and compare the total time to tmax to keep track of the run time of the program.