Skip to content

Commit

Permalink
Added the possibility of basing Acceptance Params updates on time lim…
Browse files Browse the repository at this point in the history
…it, rather than iterations
  • Loading branch information
alberto-santini committed Apr 27, 2018
1 parent cfd0b3f commit ac0a1af
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 53 deletions.
5 changes: 5 additions & 0 deletions Params.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
],
"acceptance-criterion": "Simulated annealing",

"acceptance-params-base-possible-values": [
"time", "iterations"
],
"acceptance-params-base": "time",

"scores": {
"accepted": 2.0,
"improved": 4.0,
Expand Down
32 changes: 22 additions & 10 deletions src/AcceptanceCriteria/ConservativeWorseAccept.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ namespace mlpalns {
/*! This method updates the acceptance criterion's parameters, based on running info
*
* @param iter_number is the current iteration number
* @param elapsed_time is the current elapsed time
* @param best_obj is the value of the current best solution
*/
void update_parameters(std::uint32_t iter_number, double best_obj) override;
void update_parameters(std::uint32_t iter_number, double elapsed_time, double best_obj) override;

/*! This method returns true iff the solution should be accepted according to the acceptance criterion
*
Expand Down Expand Up @@ -78,16 +79,27 @@ namespace mlpalns {
}

template<typename Solution>
void ConservativeWorseAccept<Solution>::update_parameters(std::uint32_t iter_number, double) {
if(prob_decrease_is_linear) {
current_prob -= prob_decrease;
void ConservativeWorseAccept<Solution>::update_parameters(std::uint32_t iter_number, double elapsed_time, double) {
const double S = this->params.cwa_params.start_prob;
const double E = this->params.cwa_params.end_prob;

if(this->timebase) {
if(prob_decrease_is_linear) {
current_prob = S + (elapsed_time / this->params.max_seconds) * (E - S);
} else {
const double T = this->params.max_seconds;
const double lambda = std::log(S / T) / T;
current_prob = S * std::exp(-lambda * T);
}
} else {
double S = this->params.cwa_params.start_prob;
double E = this->params.cwa_params.end_prob;
auto N = this->params.max_iters - this->params.prerun_iters;
double lambda = log(S / E) / N;
auto n = iter_number - this->params.prerun_iters;
current_prob = S * exp(-lambda * n);
if (prob_decrease_is_linear) {
current_prob -= prob_decrease;
} else {
const auto N = this->params.max_iters - this->params.prerun_iters;
const double lambda = std::log(S / E) / N;
const auto n = iter_number - this->params.prerun_iters;
current_prob = S * std::exp(-lambda * n);
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/AcceptanceCriteria/DiscreetWorseAccept.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ namespace mlpalns {
/*! This method updates the acceptance criterion's parameters, based on running info
*
* @param iter_number is the current iteration number
* @param elapsed_time is the current elapsed time
* @param best_obj is the value of the current best solution
*/
void update_parameters(std::uint32_t iter_number, double best_obj) override;
void update_parameters(std::uint32_t iter_number, double elapsed_time, double best_obj) override;

/*! This method returns true iff the solution should be accepted according to the acceptance criterion
*
Expand Down Expand Up @@ -65,7 +66,7 @@ namespace mlpalns {
}

template<typename Solution>
void DiscreetWorseAccept<Solution>::update_parameters(std::uint32_t, double) {
void DiscreetWorseAccept<Solution>::update_parameters(std::uint32_t, double, double) {
auto N = this->params.dwa_params.consecutive_rejects_for_100p;

if(this->params.dwa_params.prob_increase_is_linear) {
Expand Down
5 changes: 3 additions & 2 deletions src/AcceptanceCriteria/GreatDeluge.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ namespace mlpalns {
/*! This method updates the acceptance criterion's parameters, based on running info
*
* @param iter_number is the current iteration number
* @param elapsed_time is the current elapsed time
* @param best_obj is the value of the current best solution
*/
void update_parameters(std::uint32_t iter_number, double best_obj) override;
void update_parameters(std::uint32_t iter_number, double elapsed_time, double best_obj) override;

/*! This method returns true iff the solution should be accepted according to the acceptance criterion
*
Expand Down Expand Up @@ -71,7 +72,7 @@ namespace mlpalns {
}

template<typename Solution>
void GreatDeluge<Solution>::update_parameters(std::uint32_t, double) {
void GreatDeluge<Solution>::update_parameters(std::uint32_t, double, double) {
water_level -= this->params.gd_params.water_level_decrease_percentage * (water_level - last_obj_value);
}

Expand Down
5 changes: 3 additions & 2 deletions src/AcceptanceCriteria/HillClimbing.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ namespace mlpalns {
/*! This method updates the acceptance criterion's parameters, based on running info
*
* @param iter_number is the current iteration number
* @param elapsed_time is the current elapsed time
* @param best_obj is the value of the current best solution
*/
void update_parameters(std::uint32_t iter_number, double best_obj) override;
void update_parameters(std::uint32_t iter_number, double elapsed_time, double best_obj) override;

/*! This method returns true iff the solution should be accepted according to the acceptance criterion
*
Expand All @@ -51,7 +52,7 @@ namespace mlpalns {
}

template<typename Solution>
void HillClimbing<Solution>::update_parameters(std::uint32_t, double) {}
void HillClimbing<Solution>::update_parameters(std::uint32_t, double, double) {}

template<typename Solution>
bool HillClimbing<Solution>::should_accept(double, double current_obj, double new_obj, double eps, Solution&, std::mt19937&) {
Expand Down
5 changes: 3 additions & 2 deletions src/AcceptanceCriteria/LateAcceptanceHillClimbing.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ namespace mlpalns {
/*! This method updates the acceptance criterion's parameters, based on running info
*
* @param iter_number is the current iteration number
* @param elapsed_time is the current elapsed time
* @param best_obj is the value of the current best solution
*/
void update_parameters(std::uint32_t iter_number, double best_obj) override;
void update_parameters(std::uint32_t iter_number, double elapsed_time, double best_obj) override;

/*! This method returns true iff the solution should be accepted according to the acceptance criterion
*
Expand Down Expand Up @@ -76,7 +77,7 @@ namespace mlpalns {
}

template<typename Solution>
void LateAcceptanceHillClimbing<Solution>::update_parameters(std::uint32_t, double) {
void LateAcceptanceHillClimbing<Solution>::update_parameters(std::uint32_t, double, double) {
sol_index++;

if(sol_index == sol_list.size()) {
Expand Down
5 changes: 3 additions & 2 deletions src/AcceptanceCriteria/NLGreatDeluge.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ namespace mlpalns {
/*! This method updates the acceptance criterion's parameters, based on running info
*
* @param iter_number is the current iteration number
* @param elapsed_time is the current elapsed time
* @param best_obj is the value of the current best solution
*/
void update_parameters(std::uint32_t iter_number, double best_obj) override;
void update_parameters(std::uint32_t iter_number, double elapsed_time, double best_obj) override;

/*! This method returns true iff the solution should be accepted according to the acceptance criterion
*
Expand Down Expand Up @@ -77,7 +78,7 @@ namespace mlpalns {
}

template<typename Solution>
void NLGreatDeluge<Solution>::update_parameters(std::uint32_t, double best_obj) {
void NLGreatDeluge<Solution>::update_parameters(std::uint32_t, double, double best_obj) {
double gap = (water_level - current_obj) / water_level;

if(gap < this->params.nlgd_params.nl_gap_to_increase_water_level) {
Expand Down
5 changes: 3 additions & 2 deletions src/AcceptanceCriteria/RandomWalk.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ namespace mlpalns {
/*! This method updates the acceptance criterion's parameters, based on running info
*
* @param iter_number is the current iteration number
* @param elapsed_time is the current elapsed time
* @param best_obj is the value of the current best solution
*/
void update_parameters(std::uint32_t iter_number, double best_obj) override;
void update_parameters(std::uint32_t iter_number, double elapsed_time, double best_obj) override;

/*! This method returns true iff the solution should be accepted according to the acceptance criterion
*
Expand All @@ -51,7 +52,7 @@ namespace mlpalns {
}

template<typename Solution>
void RandomWalk<Solution>::update_parameters(std::uint32_t, double) {}
void RandomWalk<Solution>::update_parameters(std::uint32_t, double, double) {}

template<typename Solution>
bool RandomWalk<Solution>::should_accept(double, double, double, double, Solution&, std::mt19937&) {
Expand Down
30 changes: 22 additions & 8 deletions src/AcceptanceCriteria/RecordToRecordTravel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace mlpalns {
/*! Deviation */
double deviation;

/*! Deviation step, when decrease is linear */
/*! Deviation step per iteration, when decrease is linear */
double deviation_step;

/*! Deviation multiplicative coefficient, when decrease is exponential */
/*! Deviation multiplicative coefficient per iteration, when decrease is exponential */
double deviation_exp_coeff;

/*! Basic constructor */
Expand All @@ -32,9 +32,10 @@ namespace mlpalns {
/*! This method updates the acceptance criterion's parameters, based on running info
*
* @param iter_number is the current iteration number
* @param elapsed_time is the current elapsed time
* @param best_obj is the value of the current best solution
*/
void update_parameters(std::uint32_t iter_number, double best_obj) override;
void update_parameters(std::uint32_t iter_number, double elapsed_time, double best_obj) override;

/*! This method returns true iff the solution should be accepted according to the acceptance criterion
*
Expand All @@ -61,7 +62,7 @@ namespace mlpalns {
deviation_step =
(this->params.rrt_params.start_deviation - this->params.rrt_params.end_deviation) / (this->params.max_iters - this->params.prerun_iters);
deviation_exp_coeff =
pow(this->params.rrt_params.start_deviation / this->params.rrt_params.end_deviation, 1.0 / (this->params.max_iters - this->params.prerun_iters));
std::pow(this->params.rrt_params.start_deviation / this->params.rrt_params.end_deviation, 1.0 / (this->params.max_iters - this->params.prerun_iters));
}

template<typename Solution>
Expand All @@ -70,11 +71,24 @@ namespace mlpalns {
}

template<typename Solution>
void RecordToRecordTravel<Solution>::update_parameters(std::uint32_t, double) {
if(this->params.rrt_params.deviation_decrease_is_linear) {
deviation -= deviation_step;
void RecordToRecordTravel<Solution>::update_parameters(std::uint32_t, double elapsed_time, double) {
if(this->timebase) {
const double S = this->params.rrt_params.start_deviation;
const double E = this->params.rrt_params.end_deviation;
const double T = this->params.max_seconds;

if(this->params.rrt_params.deviation_decrease_is_linear) {
deviation = S + (elapsed_time / T) * (E - S);
} else {
const double lambda = std::log(S / T) / T;
deviation = S * std::exp(-lambda * T);
}
} else {
deviation *= deviation_exp_coeff;
if(this->params.rrt_params.deviation_decrease_is_linear) {
deviation -= deviation_step;
} else {
deviation *= deviation_exp_coeff;
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/AcceptanceCriteria/SimulatedAnnealing.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ namespace mlpalns {
/*! This method updates the acceptance criterion's parameters, based on running info
*
* @param iter_number is the current iteration number
* @param elapsed_time is the current elapsed time
* @param best_obj is the value of the current best solution
*/
void update_parameters(std::uint32_t iter_number, double best_obj) override;
void update_parameters(std::uint32_t iter_number, double elapsed_time, double best_obj) override;

/*! This method returns true iff the solution should be accepted according to the acceptance criterion
*
Expand Down Expand Up @@ -130,9 +131,11 @@ namespace mlpalns {
}

template<typename Solution>
void SimulatedAnnealing<Solution>::update_parameters(std::uint32_t iter_number, double best_obj) {
void SimulatedAnnealing<Solution>::update_parameters(std::uint32_t iter_number, double, double best_obj) {
bool reheating = false;

assert(params.acceptance_params_base == Parameters::AcceptanceParamsBase::Iterations);

// Check if we have to reheat
if(this->params.sa_params.reheating_is_enabled && (iter_number - this->params.prerun_iters) % reheating_iters == 0) {
temperature = last_improv_temperature * this->params.sa_params.reheating_coefficient;
Expand All @@ -155,7 +158,7 @@ namespace mlpalns {
std::cout << "Recalibrating parameters to go from " << temperature << " to " << end_temperature << " in " << reheatingIterations
<< " iterations" << std::endl;

alpha = std::min(pow(end_temperature / temperature, 1.0 / reheatingIterations), 1.0);
alpha = std::min(std::pow(end_temperature / temperature, 1.0 / reheatingIterations), 1.0);
beta = std::max((temperature - end_temperature) / reheatingIterations, 0.0);
}
} else {
Expand Down
24 changes: 19 additions & 5 deletions src/AcceptanceCriteria/ThresholdAcceptance.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ namespace mlpalns {
/*! This method updates the acceptance criterion's parameters, based on running info
*
* @param iter_number is the current iteration number
* @param elapsed_time is the current elapsed time
* @param best_obj is the value of the current best solution
*/
void update_parameters(std::uint32_t iter_number, double best_obj) override;
void update_parameters(std::uint32_t iter_number, double elapsed_time, double best_obj) override;

/*! This method returns true iff the solution should be accepted according to the acceptance criterion
*
Expand Down Expand Up @@ -70,11 +71,24 @@ namespace mlpalns {
}

template<typename Solution>
void ThresholdAcceptance<Solution>::update_parameters(std::uint32_t, double) {
if(this->params.ta_params.threshold_decrease_is_linear) {
current_threshold -= threshold_decrease;
void ThresholdAcceptance<Solution>::update_parameters(std::uint32_t, double elapsed_time, double) {
if(this->timebase) {
const double S = this->params.ta_params.start_threshold;
const double E = this->params.ta_params.end_threshold;
const double T = this->params.max_seconds;

if(this->params.ta_params.threshold_decrease_is_linear) {
current_threshold = S + (elapsed_time / T) * (E - S);
} else {
const double lambda = std::log(S / T) / T;
current_threshold = S * std::exp(-lambda * T);
}
} else {
current_threshold *= threshold_exp_coeff;
if (this->params.ta_params.threshold_decrease_is_linear) {
current_threshold -= threshold_decrease;
} else {
current_threshold *= threshold_exp_coeff;
}
}
}

Expand Down
33 changes: 23 additions & 10 deletions src/AcceptanceCriteria/WorseAccept.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ namespace mlpalns {
/*! This method updates the acceptance criterion's parameters, based on running info
*
* @param iter_number is the current iteration number
* @param elapsed_time is the current elapsed time
* @param best_obj is the value of the current best solution
*/
void update_parameters(std::uint32_t iter_number, double best_obj) override;
void update_parameters(std::uint32_t iter_number, double elapsed_time, double best_obj) override;

/*! This method returns true iff the solution should be accepted according to the acceptance criterion
*
Expand Down Expand Up @@ -68,16 +69,28 @@ namespace mlpalns {
}

template<typename Solution>
void WorseAccept<Solution>::update_parameters(std::uint32_t iter_number, double) {
if(prob_decrease_is_linear) {
current_prob -= prob_decrease;
void WorseAccept<Solution>::update_parameters(std::uint32_t iter_number, double elapsed_time, double) {
const double S = this->params.wa_params.start_prob;
const double E = this->params.wa_params.end_prob;

if(this->timebase) {
const double T = this->params.max_seconds;

if(prob_decrease_is_linear) {
current_prob = S + (elapsed_time / T) * (E - S);
} else {
const auto lambda = std::log(S / T) / T;
current_prob = S * std::exp(- lambda * T);
}
} else {
double S = this->params.wa_params.start_prob;
double E = this->params.wa_params.end_prob;
auto N = this->params.max_iters - this->params.prerun_iters;
double lambda = log(S / E) / N;
auto n = iter_number - this->params.prerun_iters;
current_prob = S * exp(-lambda * n);
if (prob_decrease_is_linear) {
current_prob -= prob_decrease;
} else {
const auto N = this->params.max_iters - this->params.prerun_iters;
const double lambda = std::log(S / E) / N;
const auto n = iter_number - this->params.prerun_iters;
current_prob = S * std::exp(-lambda * n);
}
}
}

Expand Down
Loading

0 comments on commit ac0a1af

Please sign in to comment.