Commit 6362e5eb authored by Valentin Antuori's avatar Valentin Antuori
Browse files

major update : rapid random, stop greedy when late, open nodes when new best solution...

parent d9b39b32
......@@ -6,6 +6,7 @@
#include "heuristic.h"
#define DEBUG_H_MACCRO false
#define SHIFT_TIME 43500
using namespace std;
......@@ -16,8 +17,23 @@ void set_default_temperature(double temperature)
default_temperature = temperature;
}
static unsigned long x=123456789, y=362436069, z=521288629;
unsigned long xorshf96(void) { //period 2^96-1
unsigned long t;
x ^= x << 16;
x ^= x >> 5;
x ^= x << 1;
t = x;
x = y;
y = z;
z = t ^ x ^ y;
return z;
}
unsigned int choice(vector<double> proba)
{
int precision = 10000000;
// double r = (double)(xorshf96() % precision) / precision;
double r = (double)rand() / RAND_MAX;
unsigned int i;
......@@ -29,8 +45,9 @@ unsigned int choice(vector<double> proba)
r -= prob;
}
// proba should sum to one, but....
return rand() % proba.size();
// proba should sum to one, but instability can happened (sure ?)
// return rand() % proba.size();
return proba.size()-1;
// std::cerr << "Error Proba : " << std::endl;
// for(double p : proba){
......@@ -73,19 +90,24 @@ std::unique_ptr<Solution> random_sample(const Instance& data)
return sol;
}
void build(Solution& sol)
void build(Solution& sol, bool stop)
{
State s(sol.data);
build(sol, s);
build(sol, s, stop);
}
void build(Solution& sol, State& s)
{
void build(Solution& sol, State& s, bool stop)
{
int date_out = sol.end() + SHIFT_TIME;
for(int i = sol.size(); i < sol.data->nb_tasks; ++i)
{
if(DEBUG_H)
std::cerr << "step "<< i << std::endl;
append_action(sol, s);
if(stop && sol.lmax() > 0 && sol.end() >= date_out){
break;
}
}
if(DEBUG_H)
......@@ -145,6 +167,7 @@ vector<double> get_distribution(const Instance& data, const State& s, const std:
std::cerr << s.last_action << std::endl;
}
// Need Max fitness for soft max regularization
for(size_t i = 0; i < actions.size(); ++i)
{
//compute fitness for action i
......
......@@ -17,12 +17,14 @@ extern bool DEBUG_H;
void set_default_temperature(double temperature);
/**
* Build solution from scratch
* if stop, the algorithm will stop at first tardiness
*/
void build(Solution& sol);
void build(Solution& sol, bool stop=false);
/**
* Build solution from partial solution, and state
* if stop, the algorithm will stop at first tardiness
*/
void build(Solution& sol, State& state);
void build(Solution& sol, State& state, bool stop=false);
/**
* One iteration
......
......@@ -21,7 +21,7 @@
#include "mcts.h"
#include "heuristic.h"
// #include "test.h"
#include "test.h"
#include <unistd.h>
......@@ -151,6 +151,7 @@ int main(int argc, char **argv){
if (opt.print_par)
opt.display(cout);
vector<Instance> data;
......@@ -163,12 +164,16 @@ int main(int argc, char **argv){
if( s.st_mode & S_IFDIR )
{
for(const auto& entry : fs::directory_iterator(opt.instance_file)){
string name = entry.path().string().substr(entry.path().string().find_last_of("/")+1);
data.emplace_back(entry.path().string());
std::cout << "Instance:\t" << name << std::endl << "nb tasks:\t" << data.back().nb_tasks << std::endl;
}
}
else if( s.st_mode & S_IFREG )
{
string name = opt.instance_file.substr(opt.instance_file.find_last_of("/")+1);
data.emplace_back(opt.instance_file);
std::cout << "Instance:\t" << name << std::endl << "nb tasks:\t" << data.back().nb_tasks << std::endl;
}
else
{
......@@ -222,6 +227,9 @@ int main(int argc, char **argv){
else */
if(opt.mcts)
{
set_open_nodes(opt.open_nodes);
normalized(opt.normalized_without_biais);
set_epsilon(opt.epsilon);
Solution sol = solve(data[0], opt.c,/*print_stat=*/true, opt.time, opt.mcts_backup);
}
else if(opt.heuristic)
......@@ -233,7 +241,7 @@ int main(int argc, char **argv){
else{
set_default_temperature(0.01);
// test_sample(data[0], opt.stat);
// solve_by_sample(data[0], opt.stat);
solve_by_sample(data[0], opt.stat);
// range_children(data[0], opt.stat);
}
......
This diff is collapsed.
......@@ -13,6 +13,14 @@
extern bool DEBUG;
extern std::vector<int> node_per_depth;
extern double epsilon;
extern bool normalized_without_biais;
extern bool open_nodes_at_best;
void set_epsilon(double ep);
void set_open_nodes(bool active);
void normalized(bool without_biais);
class Node;
/**
Main function, run the algorithm on the instance in data
......@@ -27,6 +35,10 @@ Solution solve(const Instance& data, double c, bool print_stat, int timeout, int
*/
int samples(Node& current_node, const Solution& sol, const State& s, Solution& best_sol);
/**
* Expansion phase, return the number of node added to the tree, -1 if the node need to be deleted
*/
int expand(const State& s, std::vector<Node>& node_vector, SparseSet& node_set, int current_node_idx);
/**
* Unique sample from partial solution and state
*/
......@@ -41,6 +53,9 @@ void add_node(std::vector<Node>& node_vector, SparseSet& node_idx_set, double pr
void delete_node(std::vector<Node>& node_vector, SparseSet& node_set, int current_node_idx);
void open_nodes_in_solution(const Solution& sol, double backup_value, std::vector<Node>& node_vector, SparseSet& node_set, int last_node_idx,
State& last_state, int& added_node, int& depth_reached);
class Node
{
......@@ -55,9 +70,12 @@ class Node
double mean_lmax;
double mean_depth;
int best_obj;
double best_backup;
int action; // Action taken by the parent
int action; // Action taken from the parent
int end_date; //end date of the task
std::vector<int> children_idxs;
......
......@@ -163,9 +163,17 @@ ChariotOptions parse(int argc, char *argv[]) {
cmd.add<SwitchArg>(opt.heuristic, "", "greedy", "Greedy run",
false);
cmd.add<SwitchArg>(opt.normalized_without_biais, "", "norm", "normalized without biais",
false);
cmd.add<SwitchArg>(opt.open_nodes, "", "open", "open nodes when a best solution is found", false);
cmd.add<ValueArg<double>>(opt.c, "", "c", "Exploitation/exploration parameter",/*required=*/false, /*default=*/1,
"double");
cmd.add<ValueArg<double>>(opt.epsilon, "", "epsilon", "epsilon",/*required=*/false, /*default=*/0.000001,
"double");
cmd.add<ValueArg<int>>(
opt.stat, "", "stat",
......@@ -205,8 +213,13 @@ ostream &ChariotOptions::display(ostream &os) {
<< endl;
os << setw(20) << left << "p c coefficient" << setw(30) << right << c
<< endl;
os << setw(20) << left << "p backup" << setw(30) << right
<< (mcts_backup == 0 ? "mean" : "best")
os << setw(20) << left << "p epsilon" << setw(30) << right
<< epsilon
<< endl;
os << setw(20) << left << "p norm" << setw(30) << right << normalized_without_biais
<< endl;
os << setw(20) << left << "p open" << setw(30) << right << open_nodes
<< endl;
}
......
......@@ -37,7 +37,11 @@ public:
bool mcts;
bool heuristic;
double c;
double epsilon;
int mcts_backup;
bool normalized_without_biais;
bool open_nodes;
int stat;
......
......@@ -33,6 +33,13 @@ int Solution::end(const int pos)const
{
return m_starts[pos] + data->duration(sequence[pos]);
}
int Solution::end()const
{
if(size() == 0)
return 0;
else
return end(size()-1);
}
int Solution::start_of_task(const int task)const
{
return start(position(task));
......
......@@ -30,6 +30,8 @@ class Solution
int start(const int pos) const;
int end(const int pos) const;
/** end time of the solution **/
int end() const;
int start_of_task(const int task) const;
int end_of_task(const int task) const;
......
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