From 4bc88dc181976e6d3e0f0112ec6cb6edc19058de Mon Sep 17 00:00:00 2001 From: Kevin Huck Date: Thu, 26 Sep 2024 17:59:17 -0400 Subject: [PATCH] Debugging simulated annealing on frontier --- src/apex/simulated_annealing.cpp | 2 +- src/apex/simulated_annealing.hpp | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/apex/simulated_annealing.cpp b/src/apex/simulated_annealing.cpp index aa7f9095..bcf07cf7 100644 --- a/src/apex/simulated_annealing.cpp +++ b/src/apex/simulated_annealing.cpp @@ -51,7 +51,7 @@ double SimulatedAnnealing::acceptance_probability(double new_cost) { void SimulatedAnnealing::evaluate(double new_cost) { /* T <- temperature( (k+1)/kmax ) */ - temp = (double)(k)/(double)(kmax); + temp = std::min((double)(k)/(double)(kmax), 1.0); /* If P(E(s), E(snew), T) ≥ random(0, 1): */ /* s <- snew */ if (new_cost < cost) { diff --git a/src/apex/simulated_annealing.hpp b/src/apex/simulated_annealing.hpp index a8eae93b..4f3f5210 100644 --- a/src/apex/simulated_annealing.hpp +++ b/src/apex/simulated_annealing.hpp @@ -66,34 +66,44 @@ class Variable { if (delta < 0 && (current_index < (size_t)(abs(delta)))) { // do nothing //neighbor_index = 0; - } else if (delta > 0 && ((current_index + delta) > maxlen)) { + } else if (delta > 0 && ((current_index + delta) >= maxlen)) { // do nothing //neighbor_index = maxlen; } else { neighbor_index = current_index + delta; } + /* + std::cout << "scope: " << scope + << " quarter: " << quarter + << " delta: " << delta + << " current_index: " << delta + << " neighbor_index: " << delta + << std::endl; + */ if (vtype == VariableType::doubletype) { *((double*)(value)) = dvalues[neighbor_index]; + //std::cout << "next value : " << *((double*)(value)) << std::endl; } else if (vtype == VariableType::longtype) { *((long*)(value)) = lvalues[neighbor_index]; + //std::cout << "next value : " << *((long*)(value)) << std::endl; } else { *((const char**)(value)) = svalues[neighbor_index].c_str(); + //std::cout << "next value : " << *((const char**)(value)) << std::endl; } -/* std::cout << "scope: " << scope - << " quarter: " << quarter - << " delta: " << delta - << " current_index: " << delta - << " neighbor_index: " << delta - << std::endl; */ } void choose_neighbor() { current_index = neighbor_index; } void save_best() { best_index = current_index; } void restore_best() { current_index = best_index; } /* For initializing in the center of the space */ + void set_init(void) { + half = maxlen/2; + quarter = (double)half/2; + } void set_init(double init_value) { maxlen = dvalues.size(); + set_init(); auto it = std::find(dvalues.begin(), dvalues.end(), init_value); if (it == dvalues.end()) { current_index = 0; @@ -103,6 +113,7 @@ class Variable { } void set_init(long init_value) { maxlen = lvalues.size(); + set_init(); auto it = std::find(lvalues.begin(), lvalues.end(), init_value); if (it == lvalues.end()) { current_index = 0; @@ -112,6 +123,7 @@ class Variable { } void set_init(std::string init_value) { maxlen = svalues.size(); + set_init(); auto it = std::find(svalues.begin(), svalues.end(), init_value); if (it == svalues.end()) { current_index = 0;