Feat: remove random mutation and introduce mutation point as argument

This commit is contained in:
Richard Wong 2024-02-23 11:36:40 +09:00
parent 8ca09dbf9e
commit e35462ef2d
Signed by: richard
GPG Key ID: 5BD36BA2E9EE33D0
5 changed files with 48 additions and 43 deletions

View File

@ -158,7 +158,7 @@ public:
int find_mps(string input_file);
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);
vector<int> generate_mutated_post_order(string input_file, vector<int> post_order, int mutate_point);
vector<int> generate_guided_post_order(string input_file, vector<int> post_order);
node* get_new_node(node_type t);
void read_from_gml(string input_file);
@ -166,7 +166,7 @@ public:
vector<int> return_post_order();
void postOrderTraversal();
void guidedPostOrderTraversal(vector<int> post_order);
void mutatedPostOrderTraversal(vector<int> post_order);
void mutatedPostOrderTraversal(vector<int> post_order, int mutate_point);
// void set_post_order(vector<int> post_order);
void print_post_order();
void sort_adj_list();

View File

@ -19,32 +19,41 @@ using namespace std;
int compute_removed_edge_size(string input_file, vector<int> post_order);
// these functions are defined in mps_test.cpp
// but their signatures are not in mps.h, hence they are declared here
vector<int> generate_post_order(string input_file);
vector<int> generate_mutated_post_order(string input_file, vector<int> post_order);
vector<int> generate_mutated_post_order(string input_file, vector<int> post_order, int mutate_point);
vector<int> generate_guided_post_order(string input_file, vector<int> post_order);
void vector_printer(vector<int> state) {
for (int i = 0; i < state.size(); ++i) {
std::cout << state[i] << ",";
}
std::cout << std::endl;
}
vector<int> repeated_mutation(string input_file, int k_max) {
vector<int> repeated_mutation(string input_file, int k_max, int mutate_point) {
// generate first post order
std::cout << "generate first post order" << std::endl;
vector<int> state_old = generate_post_order(input_file);
vector<int> state_new;
int num_removed_edges;
for (int k = 0; k < k_max; ++k) {
// rotate it first
std::cout << "cycle:" << k << std::endl;
std::cout << "rotate the dfs tree" << std::endl;
state_new = generate_guided_post_order(input_file, state_old);
// then the next traversal will rotate it back
std::cout << "mutate the dfs tree" << std::endl;
state_new = generate_mutated_post_order(input_file, state_new);
// num_removed_edges = compute_removed_edge_size(input_file, state_new);
// first time will rotate the tree
std::cout << "rotate the dfs tree" << std::endl;
state_new = generate_guided_post_order(input_file, state_new);
// second time will rotate back the rotated tree
std::cout << "print the mutated tree again" << std::endl;
vector_printer(state_old);
// mutation produces rotated view
state_new = generate_mutated_post_order(input_file, state_old, mutate_point);
vector_printer(state_new);
// another round of guided post order gives canonical representation
state_new = generate_guided_post_order(input_file, state_new);
vector_printer(state_new);
std::cout << std::endl;
}
return state_new;
@ -76,9 +85,10 @@ 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]);
int mutate_point = std::stoi(argv[3]);
// generate order here
vector<int> post_order = repeated_mutation(input_file, k_max);
vector<int> post_order = repeated_mutation(input_file, k_max, mutate_point);
// test_correctness(input_file);
// // print final order and number of edges

View File

@ -85,7 +85,7 @@ maximal_planar_subgraph_finder::guidedPostOrderTraversal(vector<int> post_order)
// take in a post-order argument then traces the graph in the same order
// return is by reference via _post_order_list
void
maximal_planar_subgraph_finder::mutatedPostOrderTraversal(vector<int> post_order) {
maximal_planar_subgraph_finder::mutatedPostOrderTraversal(vector<int> post_order, int mutate_point) {
node::init_mark();
vector<int> rev_post_order;
@ -95,27 +95,13 @@ maximal_planar_subgraph_finder::mutatedPostOrderTraversal(vector<int> post_order
int postOrderID = 0;
int traversal_index = 0;
// introduce random selection
std::random_device rd;
std::mt19937 rng(rd());
// Define the range [0, n]
// Define the range [0, n]
int n = _node_list.size() - 1; // Change 'n' to your desired upper bound
// Create a uniform distribution for the range [0, n]
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;
// set loop variables
int start = rev_post_order[0];
int i = start;
// if mutate_point = 0
if (mutate_point == 0) {
// generate another point
start = distribution(rng);
}
int end_condition = _node_list.size();
// this loop assumes start is not from 0

View File

@ -5,7 +5,7 @@
#include "mps.h"
#include <ogdf/fileformats/GraphIO.h>
#define DEBUG
// #define DEBUG
//-----------------------------------------------------------------------------------
// Finding MPS
@ -28,9 +28,9 @@ vector<int> generate_post_order(string input_file) {
return m.generate_post_order(input_file);
}
vector<int> generate_mutated_post_order(string input_file, vector<int> post_order) {
vector<int> generate_mutated_post_order(string input_file, vector<int> post_order, int mutate_point) {
maximal_planar_subgraph_finder m;
return m.generate_mutated_post_order(input_file, post_order);
return m.generate_mutated_post_order(input_file, post_order, mutate_point);
}
vector<int> generate_guided_post_order(string input_file, vector<int> post_order) {
@ -68,9 +68,9 @@ vector<int> maximal_planar_subgraph_finder::generate_post_order(string input_fil
}
// result of this will be used as input to "compute_removed_edge_size"
vector<int> maximal_planar_subgraph_finder::generate_mutated_post_order(string input_file, vector<int> post_order) {
vector<int> maximal_planar_subgraph_finder::generate_mutated_post_order(string input_file, vector<int> post_order, int mutate_point) {
read_from_gml(input_file);
mutatedPostOrderTraversal(post_order);
mutatedPostOrderTraversal(post_order, mutate_point);
#ifdef DEBUG
std::cout << "mutated post order traversal" << std::endl;
@ -85,10 +85,10 @@ vector<int> maximal_planar_subgraph_finder::generate_guided_post_order(string in
read_from_gml(input_file);
guidedPostOrderTraversal(post_order);
#ifdef DEBUG
std::cout << "guided post order traversal" << std::endl;
print_post_order();
#endif
// #ifdef DEBUG
// std::cout << "guided post order traversal" << std::endl;
// print_post_order();
// #endif
return return_post_order();
}

View File

@ -5,6 +5,7 @@
#include "mps.h"
// #define DEBUG
#define DEBUG_MUTATION
//-----------------------------------------------------------------------------------
// CONSTRUCTOR
@ -165,6 +166,14 @@ void node::mutated_DFS_visit(vector<node*> &dfsList,
std::mt19937 rng(rd());
// Use std::shuffle to shuffle the elements in the vector
std::shuffle(neighbor_list.begin(), neighbor_list.end(), rng);
#ifdef DEBUG_MUTATION
std::cout << "current node:" << this->node_id() << std::endl;
for (int i = 0; i < neighbor_list.size(); ++i) {
std::cout << neighbor_list[i]->node_id() << "(" << neighbor_list[i]->is_marked() << ")" << ",";
}
std::cout << std::endl;
#endif
}
// increment traversal index after checking