Commit 45857af5 authored by Valentin Antuori's avatar Valentin Antuori
Browse files

debug

parent 7c82793b
......@@ -44,10 +44,10 @@ double DFSRollout::fitness_func(const int action) const
auto prev_tardiness{maximum_positive_tardiness[maximum_positive_tardiness.size() - 2]};
// double lambda1{static_cast<double>(prev_tardiness-tardiness[action]) / static_cast<double>(data.max_slack)};
double lambda1{fast_sigmoid(prev_tardiness - tardiness[action], sigmoid_slope)};
// double lambda1{fast_sigmoid(prev_tardiness - tardiness[action], sigmoid_slope)};
// For Lambda1, test was with :
// double lambda1{static_cast<double>(data.due_date(action) - ect) / static_cast<double>(data.max_slack)};
double lambda1{static_cast<double>(data.due_date(action) - ect) / static_cast<double>(data.max_slack)};
double lambda2{static_cast<double>(start[action] - tour_length) / static_cast<double>(data.max_dist)};
double lambda3{1 - (static_cast<double>(data.trolley_length(action)) / static_cast<double>(data.T_max))};
......@@ -193,6 +193,14 @@ void DFSRollout::get_operations() {
if(trainOk(p))
actions.push_back(p);
} else if(data.is_pickup(prev_operation[i])) {
if(num_operation[i] != 1 && num_operation[i] != 3){
std::cout << num_operation[i] <<"/" << period[i] << std::endl;
std::cout << "prev = " << prev_operation[i] << std::endl;
std::cout << "compo = " << i << std::endl;
for(auto elt : sequence)
std::cout << elt << std::endl;
}
assert(num_operation[i] == 1 || num_operation[i] == 3);
actions.push_back(data.get_delivery(prev_operation[i]));
} else {
if(num_operation[i] != 2){
......@@ -302,7 +310,7 @@ void DFSRollout::compute_distance(const int op) {
tardiness[op] = component_ect(op) - data.due_date(op);
// Test was with :
// tardiness[op] = start[op] + data.duration(op) - data.due_date(op);
tardiness[op] = start[op] + data.duration(op) - data.due_date(op);
// auto f{(1.0 - fast_sigmoid(tardiness[op] - get_tardiness()))};
......@@ -471,7 +479,6 @@ void DFSRollout::extend_rollout() {
restore_state();
if(get_tardiness() > 0)
{
timeout = first_tard_depth + 1000;
......@@ -521,6 +528,9 @@ void DFSRollout::do_op(const int op) {
maximum_tardiness.push_back(tardiness[op]);
maximum_positive_tardiness.push_back(std::max(0,tardiness[op]));
// maximum_tardiness.push_back(max(maximum_tardiness.back(), tardiness[op]));
// maximum_positive_tardiness.push_back(std::max(maximum_tardiness.back(),0 ));
// We shouldn't have tardiness
if(get_tardiness() == 0)
......@@ -566,13 +576,13 @@ void DFSRollout::commit(const int op) {
}
// #endif
int prev_op = prev_operation[c];
int prev_num = num_operation[c];
prev_operation[c] = op;
if(++num_operation[c] == 4) {
num_operation[c] = 0;
++period[c];
}
#ifdef DEBUG_DFS_ROLLOUT
verify("after commit");
......@@ -765,6 +775,19 @@ int DFSRollout::search(int ub, int ub_depth) {
get_operations();
compute_distances();
// if(debug_dfs)
// {
// std::cerr << "Sequence : " << std::endl;
// for(auto elt: sequence)
// std::cerr << elt << " ";
// std::cerr << std::endl;
// std::cerr << "Action :" << std::endl;
// for(auto a{actions.rbegin()}; a!=actions.rend(); ++a) {
// std::cerr << *a << " ";
// }
// std::cerr << std::endl;
// }
if(sequence.size() > first_tard_depth && get_tardiness() == 0){
first_tard_depth = sequence.size();
}
......@@ -864,7 +887,7 @@ int DFSRollout::search(int ub, int ub_depth) {
// backtrack
while(sequence.size() >= start_level) {
while(sequence.size() > start_level) {
// undo until the lower bound is lower than the upper bound
int i;
do {
......@@ -878,6 +901,18 @@ int DFSRollout::search(int ub, int ub_depth) {
// if there is an action that we haven't tried yet, try it, otherwise backtrack again
if(action[i] != domain[i].end()) {
// if(debug_dfs){
// std::cerr << "right branch : " << *action[i] << std::endl;
// for(auto elt : sequence){
// std::cerr << elt << " ";
// }
// std::cerr << std::endl;
// for(auto elt : domain[i])
// {
// std::cerr << elt << " ";
// }
// std::cerr << std::endl;
// }
compute_distance(*action[i]);
maximum_tardiness.push_back(std::max(maximum_tardiness.back(), tardiness[*action[i]]));
maximum_positive_tardiness.push_back(std::max(0, maximum_tardiness.back()));
......@@ -914,175 +949,5 @@ int DFSRollout::search(int ub, int ub_depth) {
return max_iter;
}
//*/
/*
int DFSRollout::search(int ub) { //depth ub for computing iter_max
double sum_domain_size = 0;
int nb_dom = 0;
const auto start_level{sequence.size()};
best_tardiness = numeric_limits<int>::max(); // best sol for this run may be worst than the UB
_tardi_depth = 0; // best depth for this search
tardi_depth = 0;
best_sequence.clear();
int iter{0};
bool first_fail = true;
int tardiness_at_fail;
max_iter = 1;
while(true) {
++iter;
// branch
while(sequence.size() < data.nb_tasks) {
get_operations();
compute_distances();
if(sequence.size() > tardi_depth && get_tardiness() == 0){
tardi_depth = sequence.size();
}
// fail because of the upper bound
if(get_tardiness() > 0) {
if(tardi_depth > _tardi_depth || tardi_depth == _tardi_depth && get_tardiness() < best_tardiness){
save_state();
}
maximum_tardiness.pop_back();
break;
}
// get the probability distribution on the actions, and create the domain from that
get_distribution(temperature);
auto i{sequence.size()};
domain[i].clear();
for(auto a{actions.rbegin()}; a!=actions.rend(); ++a) {
if(proba[*a] > 0)
domain[i].push_back(*a);
}
sum_domain_size += domain[i].size();
nb_dom++;
// if randomized, swap the first action with one chosen at random given the distribution of the heuristic
if(randomized) {
auto act{domain[i].begin()};
auto r{random_generator() % precision};
while(act != domain[i].end()) {
if(r < proba[*act])
break;
else {
r -= proba[*act];
++act;
}
}
std::swap(*act, *domain[i].begin());
}
// branch left
action[i] = domain[i].begin();
// if(debug_dfs){
// std::cout << "branch left on " << *action[i] << std::endl;
// std::cout << "Domain : " << std::endl;
// for(auto elt : domain[i]){
// std::cout << elt << " ";
// }
// std::cout << "\nIterator at position : " << (action[i] - domain[i].begin()) << std::endl;
// }
commit(*action[i]);
}
if(first_fail) // Fail limit
{
first_fail = false;
if(tardi_depth > ub)
{
max_iter = FAIL_LIMIT;
}else if(tardi_depth > ALPHA * ub)
{
double term = 1-(ub - tardi_depth)/(ub - ub*ALPHA);
max_iter = FAIL_LIMIT * term * term;
// std::cout << max_iter << " : "<< tardi_depth <<"/"<<ub << std::endl;
}else{
// std::cout << max_iter << " : "<< tardi_depth <<"<"<<ALPHA*ub << std::endl;
}
}
// the branch is full
if(sequence.size() == data.nb_tasks) {
save_state();
ub = best_tardiness;
if(verbose)
cout << "d lmax=" << best_tardiness << " iter=" << iter << endl;
if(verified)
verify("solution");
if(best_tardiness == 0) {
if(verbose)
cout << "o solution found\n";
break;
}
}
// if(debug_dfs){
// std::cout << "Backtrack" << std::endl;
// }
while(sequence.size() > start_level) {
int i;
do {
undo();
i = sequence.size();
++action[i];
} while(sequence.size() > start_level and get_tardiness() > 0);
if(sequence.empty() || get_tardiness() > 0)
break;
// if there is an action that we haven't tried yet, try it, otherwise backtrack again
if(action[i] != domain[i].end()) {
maximum_tardiness.push_back(std::max(get_tardiness(), compute_distance(*action[i])));
// if(debug_dfs){
// std::cout << "branch right on " << *action[i] << std::endl;
// std::cout << "Domain : " << std::endl;
// for(auto elt : domain[i]){
// std::cout << elt << " ";
// }
// std::cout << "\nIterator at position : " << (action[i] - domain[i].begin()) << std::endl;
// }
commit(*action[i]);
break;
}
}
// if no tardiness after backtrack, then update tardi_depth
if(get_tardiness() == 0)
{
tardi_depth = sequence.size();
}
// check if the algorithm should stop
if(sequence.empty() and action[0] == domain[0].end()) {
// if(verbose)
cout << "o search tree exhausted: " << best_tardiness << " at temperature " << temperature << " \n" ;
} else if(best_tardiness == 0) {
if(verbose)
cout << "o solution found\n";
} else if(iter >= max_iter) {
if(verbose)
cout << "o time out ( "<< max_iter << ")" <<"\n";
} else
continue;
break;
}
// std::cout << sum_domain_size/nb_dom << std::endl;
return max_iter;
}
// */
......@@ -62,7 +62,7 @@ public:
double temperature{0.01};
double sigmoid_slope{0.0005};
bool verbose{false};
bool verified{false};
bool verified{true};
bool randomized{true};
bool auto_limit{true};
......
......@@ -199,6 +199,13 @@ Solution solve(const Instance& data, double c, int timeout)
int iteration = 0;
while(upper_bound > 0 && std::chrono::steady_clock::now() < run_until)
{
// if(iteration == 190417){
// debug_dfs = true;
// rollout.verbose = true;
// }
// else
// debug_dfs = false;
rollout.clear();
iteration++;
iter_count_display++;
......@@ -243,7 +250,7 @@ Solution solve(const Instance& data, double c, int timeout)
///////// EXPAND /////////
////////////////////////////
if( DEBUG)
std::cerr << "-- Expansion --" << std::endl;
std::cerr << "-- Expansion --" << std::endl;
int added_node = expand(s, node_vector, node_set, current_node_idx);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment