Commit 3c068e56 authored by Valentin Antuori's avatar Valentin Antuori
Browse files

restore without saved data

parent 2be48955
......@@ -81,27 +81,15 @@ DFSRollout::DFSRollout(const Instance& data) : data(data) {
period.resize(data.nb_components, 0);
start_period.resize(data.nb_components, 0);
saved_period.resize(data.nb_components, 0);
prev_operation.resize(data.nb_components, -1);
start_prev_operation.resize(data.nb_components, -1);
saved_prev_operation.resize(data.nb_components, -1);
start_prev_operation.resize(data.nb_components, -1);
num_operation.resize(data.nb_components, 0);
start_num_operation.resize(data.nb_components, 0);
saved_num_operation.resize(data.nb_components, 0);
start.resize(data.nb_tasks, 0);
saved_start.resize(data.nb_tasks, 0);
maximum_positive_tardiness.push_back(0);
saved_maximum_positive_tardiness.push_back(0);
maximum_tardiness.push_back(numeric_limits<int>::min());
saved_maximum_tardiness.push_back(numeric_limits<int>::min());
maximum_tardiness.push_back(numeric_limits<int>::min());
distance.resize(data.nb_tasks, 0);
tardiness.resize(data.nb_tasks, 0);
......@@ -111,7 +99,7 @@ DFSRollout::DFSRollout(const Instance& data) : data(data) {
random_generator.seed(12345);
// #ifdef STATS
#ifdef STATS
// travel_time.resize(data.nb_components);
for(auto i{0}; i<4; ++i) {
avg_travel_time[i].resize(data.nb_components, 0.0);
......@@ -120,7 +108,7 @@ DFSRollout::DFSRollout(const Instance& data) : data(data) {
for(auto i{0}; i<2; ++i) {
avg_train_time[i].resize(data.nb_components, 0.0);
}
// #endif
#endif
}
......@@ -356,7 +344,7 @@ void DFSRollout::print_step() const {
}
void DFSRollout::print_sol() const {
// #ifdef STATS
#ifdef STATS
for(int c{0}; c<data.nb_components; ++c) {
if(c)
cout
......@@ -366,7 +354,7 @@ void DFSRollout::print_sol() const {
}
}
cout << endl;
// #endif
#endif
}
void DFSRollout::greedy_first() {
......@@ -478,6 +466,7 @@ void DFSRollout::extend_rollout() {
bool can_stop = false;
restore_state();
std::cout << "Restored" << std::endl;
if(get_tardiness() > 0)
{
......@@ -562,7 +551,7 @@ void DFSRollout::commit(const int op) {
auto c{data.component(op)};
// #ifdef STATS
#ifdef STATS
// travel_time[c].push_back(distance[op]);
auto t{(op%4)};
avg_travel_time[t][c] += (static_cast<double>(distance[op]) - avg_travel_time[t][c])/(++num_att[t][c]);
......@@ -574,7 +563,7 @@ void DFSRollout::commit(const int op) {
avg_train_time[t/2][c] += (static_cast<double>(train_time) - avg_train_time[t/2][c])/num_att[t][c];
}
// #endif
#endif
int prev_op = prev_operation[c];
int prev_num = num_operation[c];
......@@ -640,49 +629,24 @@ void DFSRollout::save_state()
verify("Saving state", offset);
#endif
saved_maximum_tardiness = maximum_tardiness;
saved_maximum_positive_tardiness = maximum_positive_tardiness;
// remove the last tardiness computation
// Extends-rollout re-adds it, then prevent bug
if(sequence.size() != data.nb_tasks){
saved_maximum_tardiness.pop_back();
saved_maximum_positive_tardiness.pop_back();
}
saved_sequence = sequence;
saved_period = period;
saved_num_operation = num_operation;
saved_prev_operation = prev_operation;
saved_tour_length = tour_length;
saved_train_length = train_length;
saved_first_tard_depth = first_tard_depth;
saved_start = start;
}
void DFSRollout::restore_state()
{
maximum_tardiness = std::move(saved_maximum_tardiness);
maximum_positive_tardiness = std::move(saved_maximum_positive_tardiness);
sequence = std::move(saved_sequence);
period = std::move(saved_period);
num_operation = std::move(saved_num_operation);
prev_operation = std::move(saved_prev_operation);
tour_length = saved_tour_length;
train_length = saved_train_length;
first_tard_depth = saved_first_tard_depth;
start = std::move(saved_start);
restore_start(start_level);
std::cout << "Restored to start" << std::endl;
while(sequence.size() < saved_sequence.size())
{
do_op(saved_sequence[sequence.size()]);
}
for(int i = 0; i < sequence.size(); ++i){
assert(saved_sequence[i] == sequence[i]);
}
}
void DFSRollout::restart(const int lvl, int& restart_size, int& restart_limit)
void DFSRollout::restore_start(const int lvl)
{
restart_size *= restart_factor;
restart_limit += restart_size;
if(verbose)
cout << "o restart\n";
sequence.resize(lvl);
for(auto i{0}; i<data.nb_components; ++i) {
period[i] = start_period[i];
num_operation[i] = start_num_operation[i];
......@@ -697,12 +661,19 @@ void DFSRollout::restart(const int lvl, int& restart_size, int& restart_limit)
{
first_tard_depth = sequence.size();
}
}
void DFSRollout::restart(const int lvl, int& restart_size, int& restart_limit)
{
restart_size *= restart_factor;
restart_limit += restart_size;
if(verbose)
cout << "o restart\n";
if(warming){
temperature += warming_step;
// std::cout << "T = " << temperature << std::endl;
}
restore_start(lvl);
}
void DFSRollout::clear() {
......@@ -712,39 +683,30 @@ void DFSRollout::clear() {
std::fill(period.begin(), period.end(), 0);
std::fill(start_period.begin(), start_period.end(), 0);
std::fill(saved_period.begin(), saved_period.end(), 0);
std::fill(num_operation.begin(), num_operation.end(), 0);
std::fill(start_num_operation.begin(), start_num_operation.end(), 0);
std::fill(saved_num_operation.begin(), saved_num_operation.end(), 0);
std::fill(prev_operation.begin(), prev_operation.end(), -1);
std::fill(start_prev_operation.begin(), start_prev_operation.end(), -1);
std::fill(saved_prev_operation.begin(), saved_prev_operation.end(), -1);
maximum_tardiness.clear();
saved_maximum_tardiness.clear();
maximum_tardiness.push_back(0);
saved_maximum_tardiness.push_back(0);
maximum_positive_tardiness.clear();
saved_maximum_positive_tardiness.clear();
maximum_positive_tardiness.push_back(0);
saved_maximum_positive_tardiness.push_back(0);
tour_length = 0;
start_tour_length = 0;
saved_tour_length = 0;
train_length = 0;
start_train_length = 0;
saved_train_length = 0;
best_tardiness = numeric_limits<int>::max();
first_tard_depth = 0;
saved_first_tard_depth = 0;
start_level = 0;
}
......@@ -756,7 +718,7 @@ int DFSRollout::search(int ub, int ub_depth) {
double sum_domain_size = 0;
int nb_dom = 0;
const auto start_level{sequence.size()};
start_level = sequence.size();
start_train_length = train_length;
start_tour_length = tour_length;
......
......@@ -107,41 +107,33 @@ private:
const Instance& data;
vector<int> saved_sequence;
int saved_first_tard_depth{0};
vector<int> saved_sequence;
int start_level = 0;
int start_tour_length{0};
int saved_tour_length{0};
int start_train_length{0};
int saved_train_length{0};
//
vector<int> maximum_tardiness;
vector<int> saved_maximum_tardiness;
vector<int> maximum_positive_tardiness;
vector<int> saved_maximum_positive_tardiness;
// the current period, for every component
vector<int> period;
vector<int> start_period;
vector<int> saved_period;
// the latest operation, for every component
vector<int> prev_operation;
vector<int> start_prev_operation;
vector<int> saved_prev_operation;
// the number of operations in the period, for every component
vector<int> num_operation;
vector<int> start_num_operation;
vector<int> saved_num_operation;
// for every operation, its start time if scheduled, or a lower bound on it if it is a possible actions choice
vector<int> start;
vector<int> saved_start;
// used to store the tardiness of the actions
vector<int> tardiness;
......@@ -176,7 +168,7 @@ private:
// return maximum_positive_tardiness.back();
// }
void undo();
void restart(const int lvl, int& restart_size, int& restart_limit);
int back() const {
return sequence.back();
......@@ -203,9 +195,16 @@ private:
// void verify(const char* msg, const int offset=0);
//Keep the current state
void save_state();
// Restore to the saved state
void restore_state();
// Go back to the start sequence
void restore_start(int lvl);
// Call restore_start, and update the fail limit
void restart(const int lvl, int& restart_size, int& restart_limit);
double fast_sigmoid(const int t, const double slope=.01) const {
// return .5 * (slope * t / (1 + abs(slope * t))) + .5;
......
......@@ -294,6 +294,7 @@ Solution solve(const Instance& data, double c, int timeout)
mean_fail_iter = mean_fail_iter + (fail - mean_fail_iter)/count_fail;
}
// finished by a greedy, using the best solution found
std::cout << "Extend" << std::endl;
rollout.extend_rollout();
rollout_depth = rollout.first_tard_depth;
rollout_tardiness = rollout.get_tardiness();
......
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