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

new tree implementation

parent ab49b87f
......@@ -16,6 +16,7 @@ set(SRCS
src/options.cpp
src/mcts.cpp
src/heuristic.cpp
src/SparseSet.cpp
)
......@@ -30,6 +31,7 @@ set(HEADERS
src/options.h
src/mcts.h
src/heuristic.h
src/SparseSet.hpp
${TCLAP}
)
......
......@@ -28,119 +28,119 @@
namespace fs = std::experimental::filesystem;
using namespace std;
void random_tree()
{
std::cout << " =============== " << std::endl;
Node::count = 0;
Node root(0, -1, 0, nullptr);
// vector<Node*> nodes_vector;
// nodes_vector.push_back(&root);
for(auto i = 0; i < 10; ++i){
root.add_child(0, 0, 0);
// nodes_vector.push_back(root.children.back());
}
/*
void random_tree()
{
std::list<Node*> nexts(root.children.begin(), root.children.end());
const int MAX_NODE = 10000000;
int nb_node = 1;
const double proba = 0.9;
while(!nexts.empty() && nb_node < MAX_NODE)
{
Node* current = nexts.front();
nexts.pop_front();
if(rand()/RAND_MAX < proba)
{
int nb_children = rand() % 8 + 3;
for(int i = 0; i < nb_children; ++i)
{
current->add_child(0, 0, 0);
nexts.push_back(current->children.back());
// nodes_vector.push_back(current->children.back());
nb_node++;
}
}
}
}
// std::cout << "Nb nodes : " << nodes_vector.size() << std::endl;
// nodes_vector.clear();
std::cout << "Root :";
for(auto child : root.children){
std::cout << child->id << ", ";
}
std::cout << std::endl;
sleep(3);
for(int i = 0; i < 5; ++i)
{
std::cout << " --- " << std::endl;
std::cout << "Delete node " << root.children[0]->id << std::endl;
delete root.children[0];
std::cout << "Root :";
std::cout << " =============== " << std::endl;
Node::count = 0;
Node root(0, -1, 0, nullptr);
// vector<Node*> nodes_vector;
// nodes_vector.push_back(&root);
for(auto i = 0; i < 10; ++i){
root.add_child(0, 0, 0);
// nodes_vector.push_back(root.children.back());
}
{
std::list<Node*> nexts(root.children.begin(), root.children.end());
const int MAX_NODE = 10000000;
int nb_node = 1;
const double proba = 0.9;
while(!nexts.empty() && nb_node < MAX_NODE)
{
Node* current = nexts.front();
nexts.pop_front();
if(rand()/RAND_MAX < proba)
{
int nb_children = rand() % 8 + 3;
for(int i = 0; i < nb_children; ++i)
{
current->add_child(0, 0, 0);
nexts.push_back(current->children.back());
// nodes_vector.push_back(current->children.back());
nb_node++;
}
}
}
}
// std::cout << "Nb nodes : " << nodes_vector.size() << std::endl;
// nodes_vector.clear();
std::cout << "Root :";
for(auto child : root.children){
std::cout << child->id << ", ";
}
std::cout << std::endl << " --- " << std::endl;
sleep(3);
}
std::cout << "Creating new nodes..." << std::endl;
{
const int MAX_NODE = 4491163 + 247541 + 434232 + 786651 + 396212;
// int nb_node = 1;
for(int i = 0; i < MAX_NODE; ++i)
root.add_child(0, 0, 0);
}
sleep(5);
std::cout << std::endl;
sleep(3);
std::cout << "Adding a child to a new node" << std::endl;
root.children.back()->add_child(0, 0, 0);
for(int i = 0; i < 5; ++i)
{
std::cout << " --- " << std::endl;
std::cout << "Delete node " << root.children[0]->id << std::endl;
delete root.children[0];
std::cout << "Root :";
for(auto child : root.children){
std::cout << child->id << ", ";
}
std::cout << std::endl << " --- " << std::endl;
sleep(3);
}
std::cout << "Creating new nodes..." << std::endl;
{
std::cout << std::endl;
std::cout << "Root has " << root.children.size() << " children" << std::endl;
delete root.children.back()->children[0];
std::cout << "Root has " << root.children.size() << " children" << std::endl;
}
const int MAX_NODE = 4491163 + 247541 + 434232 + 786651 + 396212;
// int nb_node = 1;
for(int i = 0; i < MAX_NODE; ++i)
root.add_child(0, 0, 0);
}
sleep(5);
void greedy_sample_stat(double temperature, Instance& data, int nb_iter)
{
std::cout << "Greedy sample t=" << temperature << std::endl;
set_default_temperature(temperature);
long lmax_sum = 0;
long depth_sum = 0;
int best_obj = std::numeric_limits<std::int32_t>::max();
int best_depth = std::numeric_limits<std::int32_t>::min();;
for(int i = 0; i < nb_iter; ++i){
Solution sol(&data);
build(sol);
lmax_sum += sol.lmax();
if(sol.lmax() < best_obj)
{
best_obj = sol.lmax();
}
int depth = 0;
if(sol.lmax() > 0){
for(int j = 0; j < sol.size(); ++j){
if(sol.tardiness(j) > 0){
depth = j+1;
break;
}
std::cout << "Adding a child to a new node" << std::endl;
root.children.back()->add_child(0, 0, 0);
std::cout << std::endl;
std::cout << "Root has " << root.children.size() << " children" << std::endl;
delete root.children.back()->children[0];
std::cout << "Root has " << root.children.size() << " children" << std::endl;
}
void greedy_sample_stat(double temperature, Instance& data, int nb_iter)
{
std::cout << "Greedy sample t=" << temperature << std::endl;
set_default_temperature(temperature);
long lmax_sum = 0;
long depth_sum = 0;
int best_obj = std::numeric_limits<std::int32_t>::max();
int best_depth = std::numeric_limits<std::int32_t>::min();;
for(int i = 0; i < nb_iter; ++i){
Solution sol(&data);
build(sol);
lmax_sum += sol.lmax();
if(sol.lmax() < best_obj)
{
best_obj = sol.lmax();
}
}
depth_sum += depth;
if(depth > best_depth)
{
best_depth = depth;
}
}
std::cout << nb_iter << std::endl << "- lmax = " << (double)lmax_sum/nb_iter << std::endl;
std::cout << "- depth = " << (double)depth_sum/nb_iter << std::endl;
std::cout << "- Best = " << best_obj << std::endl;
std::cout << "- Best depth = " << best_depth << std::endl;
}
int depth = 0;
if(sol.lmax() > 0){
for(int j = 0; j < sol.size(); ++j){
if(sol.tardiness(j) > 0){
depth = j+1;
break;
}
}
}
depth_sum += depth;
if(depth > best_depth)
{
best_depth = depth;
}
}
std::cout << nb_iter << std::endl << "- lmax = " << (double)lmax_sum/nb_iter << std::endl;
std::cout << "- depth = " << (double)depth_sum/nb_iter << std::endl;
std::cout << "- Best = " << best_obj << std::endl;
std::cout << "- Best depth = " << best_depth << std::endl;
}
*/
// int Node::count = 0;
int main(int argc, char **argv){
......
This diff is collapsed.
......@@ -6,6 +6,7 @@
#include "instance.h"
#include "solution.h"
#include "SparseSet.hpp"
#define BACKUP_MEAN 0
#define BACKUP_BEST 1
......@@ -32,6 +33,15 @@ int samples(Node& current_node, const Solution& sol, const State& s, Solution& b
void sample(Solution& sol, State& s);
/**
* Return the idx of the added node
*/
void add_node(std::vector<Node>& node_vector, SparseSet& node_idx_set, double proba,
int action, int end_date, int parent_idx);
void delete_node(std::vector<Node>& node_vector, SparseSet& node_set, int current_node_idx);
class Node
{
public:
......@@ -50,28 +60,27 @@ class Node
int action; // Action taken by the parent
int end_date; //end date of the task
std::vector<Node*> children;
Node *parent;
std::vector<int> children_idxs;
int parent_idx;
int idx_in_children;
int my_idx;
int id; // Identificator
int depth;
int max_depth; // maximum depth under this node
int depth; //depth of the node
int max_depth_subtree; // maximum depth under this node
int nb_nodes_subtree; // Size of the subtree
double Q;
// Node();
Node(double proba, int action, int end_date, Node* parent);
Node(Node&& n) noexcept;
Node(const Node&);
~Node();
/**
* @param Reward : Immediate reward on the edge from the parent
* @param Prior_proba : Prior probability
* @return : Pointer to the created node
**/
void add_child(double prior_proba, int action, int end_date);
void add_child(Node* child);
Node(double proba, int action, int end_date, const Node& parent, int idx);
Node(double proba, int action, int end_date, int idx);
void reset(double proba, int action, int end_date, const Node& parent, int idx);
void reset(double proba, int action, int end_date, int idx);
void clear();
void add_child(int child_idx);
void update(double v, const Solution& sol, int sub_tree_depth, int added_node);
......@@ -79,15 +88,8 @@ class Node
bool is_root();
double get_Q();
void delete_node();
friend std::ostream& operator<<(std::ostream& os, const Node& dt);
private :
// boolean for destructor should be false, exept when the node is going to be deleted
bool is_collected;
};
......
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