Feat: introduced simulated annealing

This commit is contained in:
Richard Wong 2023-09-12 10:20:28 +09:00
parent f2ba57b57f
commit 28dceaa575
Signed by: richard
GPG Key ID: 5BD36BA2E9EE33D0
4 changed files with 62 additions and 30 deletions

View File

@ -21,6 +21,44 @@ int compute_removed_edge_size(string input_file, vector<int> post_order);
vector<int> generate_post_order(string input_file);
vector<int> generate_mutated_post_order(string input_file, vector<int> post_order);
double temp_decay(int k, int k_max) {
return 1.0 - ((k + 1.0) / (k_max));
}
vector<int> sa_solve(string input_file, int k_max) {
// create sampling function
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_real_distribution<> distribution(0.0, 1.0);
// generate first state
vector<int> state_old = generate_post_order(input_file);
vector<int> state_new;
int e_old = compute_removed_edge_size(input_file, state_old);
int e_new = 0;
int delta = 0;
// initialize terms
double temp = 100;
for (int k = 0; k < k_max; ++k) {
temp = temp_decay(k, k_max);
state_new = generate_mutated_post_order(input_file, state_old);
e_new = compute_removed_edge_size(input_file, state_new);
delta = e_new - e_old;
if (std::exp( -(delta) / temp) > distribution(rng)) {
state_old = state_new;
e_old = e_new;
}
// std::cout << "temp: " << temp << ", score: " << e_old << std::endl;
}
return state_old;
}
int get_graph_size(string input_file) {
ogdf::Graph G;
@ -39,23 +77,17 @@ int get_graph_size(string input_file) {
int main(int argc, char* argv[]) {
string input_file = argv[1];
int k_max = std::stoi(argv[2]);
// generate order here
vector<int> post_order = generate_post_order(input_file);
std::copy(post_order.begin(), post_order.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
// generate mutated order
vector<int> mutated_order = generate_mutated_post_order(input_file, post_order);
post_order = mutated_order;
std::copy(post_order.begin(), post_order.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
vector<int> post_order = sa_solve(input_file, k_max);
// std::copy(post_order.begin(), post_order.end(), std::ostream_iterator<int>(std::cout, " "));
// std::cout << std::endl;
// print order
int removed_edges = compute_removed_edge_size(input_file, post_order);
std::cout << "Number of removed edges: " << removed_edges << std::endl;
return 0;
}

View File

@ -91,7 +91,7 @@ maximal_planar_subgraph_finder::mutatedPostOrderTraversal(vector<int> post_order
std::uniform_int_distribution<int> distribution(0, n);
// Generate a random number between 0 and n (inclusive)
int mutate_point = distribution(rng);
std::cout << "the mutate point: " << mutate_point << std::endl;
// std::cout << "the mutate point: " << mutate_point << std::endl;
// set loop variables
int start = rev_post_order[0];

View File

@ -65,12 +65,12 @@ int maximal_planar_subgraph_finder::compute_removed_edge_size(string input_file,
_node_list[_post_order_list[i]->node_id()]->set_post_order_index(i);
}
std::cout << "check order of duplicated traversal" << std::endl;
for (int i = 0; i < _post_order_list.size(); ++i) {
std::cout << _post_order_list[i]->node_id() << " ";
}
// std::cout << "check order of duplicated traversal" << std::endl;
// for (int i = 0; i < _post_order_list.size(); ++i) {
// std::cout << _post_order_list[i]->node_id() << " ";
// }
// std::cout << std::endl;
std::cout << std::endl;
sort_adj_list();
determine_edges();
back_edge_traversal();

View File

@ -124,11 +124,11 @@ void node::mutated_DFS_visit(vector<node*> &dfsList, vector<node*> &node_list, i
}
// print the neighbors
std::cout << "current index: " << this->node_id() << std::endl;
for (int i = 0; i < neighbor_list.size(); ++i) {
std::cout << neighbor_list[i]->node_id() << " ";
}
std::cout << std::endl;
// std::cout << "current index: " << this->node_id() << std::endl;
// for (int i = 0; i < neighbor_list.size(); ++i) {
// std::cout << neighbor_list[i]->node_id() << " ";
// }
// std::cout << std::endl;
set_post_order_index(index);
dfsList.push_back(this);
@ -136,19 +136,19 @@ void node::mutated_DFS_visit(vector<node*> &dfsList, vector<node*> &node_list, i
if (index - 1 == mutate_point) {
// Create a random number generator and seed it
std::cout << "mutated at index: " << index - 1<< "and at mutate point: " << mutate_point << std::endl;
// std::cout << "mutated at index: " << index - 1<< "and at mutate point: " << mutate_point << std::endl;
std::random_device rd;
std::mt19937 rng(rd());
// Use std::shuffle to shuffle the elements in the vector
std::shuffle(neighbor_list.begin(), neighbor_list.end(), rng);
// print the neighbors
std::cout << "order after mutation: " << std::endl;
std::cout << "current index: " << this->node_id() << std::endl;
for (int i = 0; i < neighbor_list.size(); ++i)
{
std::cout << neighbor_list[i]->node_id() << " ";
}
std::cout << std::endl;
// // print the neighbors
// std::cout << "order after mutation: " << std::endl;
// std::cout << "current index: " << this->node_id() << std::endl;
// for (int i = 0; i < neighbor_list.size(); ++i)
// {
// std::cout << neighbor_list[i]->node_id() << " ";
// }
// std::cout << std::endl;
}
for (int i = 0; i < neighbor_list.size(); ++i)