Commit d9b39b32 authored by Valentin Antuori's avatar Valentin Antuori
Browse files

commit post lockdown

parent 3a919c08
......@@ -2,15 +2,15 @@ cmake_minimum_required(VERSION 3.0)
project(main)
find_package(Torch REQUIRED)
#find_package(Torch REQUIRED)
link_libraries(stdc++fs)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
set(SRCS
src/main.cpp
src/policy-gradient.cpp
#src/policy-gradient.cpp
src/instance.cpp
src/solution.cpp
src/options.cpp
......@@ -25,7 +25,7 @@ include_directories(src)
AUX_SOURCE_DIRECTORY(src/tclap TCLAP)
set(HEADERS
src/policy-gradient.h
#src/policy-gradient.h
src/instance.h
src/solution.h
src/options.h
......@@ -37,7 +37,7 @@ set(HEADERS
add_executable(main ${SRCS} ${HEADERS})
target_link_libraries(main "${TORCH_LIBRARIES}")
#target_link_libraries(main "${TORCH_LIBRARIES}")
......
#include <assert.h>
#include "SparseSet.hpp"
SparseSet::SparseSet(const size_t n) {
size_ = 0;
start_ = 0;
reserve(n);
}
void SparseSet::reserve(const size_t n) {
while (list_.size() < n) {
index_.push_back(list_.size());
list_.push_back(list_.size());
}
}
//
// void SparseSet::save(size_t &stamp1, size_t &stamp2) { stamp1 = size_; stamp2
// = start_; }
// void SparseSet::restore(const size_t stamp1, const size_t stamp2) { size_ =
// stamp1; start_ = stamp2; }
void SparseSet::save_start(size_t &stamp) { stamp = start_; }
void SparseSet::save_size(size_t &stamp) { stamp = size_; }
void SparseSet::restore_start(const size_t stamp) { start_ = stamp; }
void SparseSet::restore_size(const size_t stamp) { size_ = stamp; }
//@}
/*!@name Accessors*/
//@{
bool SparseSet::safe_contain(const int elt) const {
if (elt >= 0 && (size_t)elt < index_.size())
return contain(elt);
return false;
}
bool SparseSet::contain(const int elt) const { return index_[elt] < size_ and index_[elt] >= start_; }
size_t SparseSet::count() const { return size_ - start_; }
size_t SparseSet::size() const { return size_; }
size_t SparseSet::start() const { return start_; }
size_t SparseSet::capacity() const { return index_.size(); }
bool SparseSet::empty() const { return size_ == start_; }
int SparseSet::next(const int elt) const {
size_t idx = index_[elt] + 1;
return (idx < size_ ? list_[idx] : elt);
}
int SparseSet::prev(const int elt) const {
size_t idx = index_[elt];
return (idx > start_ ? list_[idx - 1] : elt);
}
int SparseSet::operator[](const size_t idx) const { return list_[idx+start_]; }
int &SparseSet::operator[](const size_t idx) { return list_[idx+start_]; }
//@}
/*!@name List Manipulation*/
//@{
std::vector<int>::iterator SparseSet::fbegin() { return list_.begin(); }
std::vector<int>::iterator SparseSet::begin() { return list_.begin() + start_; }
std::vector<int>::iterator SparseSet::bbegin() { return list_.begin() + size_; }
std::vector<int>::reverse_iterator SparseSet::frbegin() {
return list_.rend() - start_;
}
std::vector<int>::reverse_iterator SparseSet::rbegin() {
return list_.rend() - size_;
}
std::vector<int>::reverse_iterator SparseSet::brbegin() {
return list_.rbegin();
}
std::vector<int>::iterator SparseSet::fend() { return list_.begin() + start_; }
std::vector<int>::iterator SparseSet::end() { return list_.begin() + size_; }
std::vector<int>::iterator SparseSet::bend() { return list_.end(); }
std::vector<int>::reverse_iterator SparseSet::frend() { return list_.rend(); }
std::vector<int>::reverse_iterator SparseSet::rend() {
return list_.rend() - start_;
}
std::vector<int>::reverse_iterator SparseSet::brend() {
return list_.rend() - size_;
}
std::vector<int>::const_iterator SparseSet::fbegin() const {
return list_.begin();
}
std::vector<int>::const_iterator SparseSet::begin() const {
return list_.begin() + start_;
}
std::vector<int>::const_iterator SparseSet::bbegin() const {
return list_.begin() + size_;
}
std::vector<int>::const_reverse_iterator SparseSet::frbegin() const {
return list_.rend() - start_;
}
std::vector<int>::const_reverse_iterator SparseSet::rbegin() const {
return list_.rend() - size_;
}
std::vector<int>::const_reverse_iterator SparseSet::brbegin() const {
return list_.rbegin();
}
std::vector<int>::const_iterator SparseSet::fend() const {
return list_.begin() + start_;
}
std::vector<int>::const_iterator SparseSet::end() const {
return list_.begin() + size_;
}
std::vector<int>::const_iterator SparseSet::bend() const { return list_.end(); }
std::vector<int>::const_reverse_iterator SparseSet::frend() const {
return list_.rend();
}
std::vector<int>::const_reverse_iterator SparseSet::rend() const {
return list_.rend() - start_;
}
std::vector<int>::const_reverse_iterator SparseSet::brend() const {
return list_.rend() - size_;
}
std::vector<int>::const_iterator SparseSet::get_iterator(const size_t i) const {
return list_.begin() + i;
}
std::vector<int>::iterator SparseSet::get_iterator(const size_t i) {
return list_.begin() + i;
}
// std::vector<int>::iterator SparseSet::begin_after() { return end(); }
// std::vector<int>::reverse_iterator SparseSet::rbegin_after() {
// return list_.rend();
// }
//
// std::vector<int>::iterator SparseSet::end_after() { return list_.end(); }
// std::vector<int>::reverse_iterator SparseSet::rend_after() { return rbegin(); }
//
// std::vector<int>::const_iterator SparseSet::begin_after() const {
// return end();
// }
// std::vector<int>::const_reverse_iterator SparseSet::rbegin_after() const {
// return list_.rend();
// }
//
// std::vector<int>::const_iterator SparseSet::end_after() const {
// return list_.end();
// }
// std::vector<int>::const_reverse_iterator SparseSet::rend_after() const {
// return rend();
// }
void SparseSet::fill() { size_ = list_.size(); start_ = 0; }
void SparseSet::clear() { size_ = 0; start_ = 0; }
// void SparseSet::set_size(const int s) { size_ = s; }
// void SparseSet::safe_remove_back(const int elt) {
// if (elt >= 0) {
// if (static_cast<size_t>(elt) >= list_.size()) {
// reserve(elt + 1);
// }
// remove_back(elt);
// }
// }
void SparseSet::remove_back(const int elt) {
if (index_[elt] < size_ and index_[elt] >= start_)
pull_back(elt);
}
void SparseSet::pull_back(const int elt) {
auto last = list_[--size_];
index_[last] = index_[elt];
list_[index_[elt]] = last;
list_[size_] = elt;
index_[elt] = size_;
}
// void SparseSet::safe_remove_front(const int elt) {
// if (elt >= 0) {
// if (static_cast<size_t>(elt) >= list_.size()) {
// reserve(elt + 1);
// }
// remove_front(elt);
// }
// }
void SparseSet::remove_front(const int elt) {
if (index_[elt] < size_ and index_[elt] >= start_)
pull_front(elt);
}
void SparseSet::pull_front(const int elt) {
auto first = list_[start_];
index_[first] = index_[elt];
list_[index_[elt]] = first;
list_[start_] = elt;
index_[elt] = start_++;
}
// void SparseSet::move(const int elt, const int idx_to) {
// auto idx_from = index_[elt];
//
// // assert(idx_from )
//
// // assert(index_[elt] <= static_cast<size_t>(idx_to));
//
// auto last = list_[idx_to];
// index_[last] = idx_from;
// list_[idx_from] = last;
// list_[idx_to] = elt;
// index_[elt] = idx_to;
// }
void SparseSet::pop_back() { --size_; }
void SparseSet::pop_front() { ++start_; }
int SparseSet::front() const { return list_[start_]; }
int SparseSet::back() const { return list_[size_ - 1]; }
void SparseSet::safe_add(const int elt) {
if (elt >= 0) {
if (static_cast<size_t>(elt) >= list_.size()) {
reserve(elt + 1);
}
add(elt);
}
}
void SparseSet::add(const int elt) {
// std::cout << "add " << elt << " to " << *this << std::endl;
if (index_[elt] >= size_)
push_back(elt);
else if(index_[elt] < start_)
push_front(elt);
}
void SparseSet::push_back(const int elt) {
auto next = list_[size_];
index_[next] = index_[elt];
list_[index_[elt]] = next;
index_[elt] = size_;
list_[size_++] = elt;
}
void SparseSet::push_front(const int elt) {
auto next = list_[--start_];
index_[next] = index_[elt];
list_[index_[elt]] = next;
index_[elt] = start_;
list_[start_] = elt;
}
int SparseSet::index(const int elt) const { return index_[elt]; }
//@}
std::ostream &SparseSet::display(std::ostream &os) const {
for (auto i = 0; i < capacity(); ++i) {
if (start_ == i)
os << " |";
if (size_ == i)
os << " |";
os << " " << list_[i];
}
if (size_ == capacity())
os << " |";
// os << std::endl;
// os << "(";
// for (auto it = begin(); it < end(); ++it) {
// os << " " << *it;
// }
// os << " )";
return os;
}
std::ostream &operator<<(std::ostream &os, const SparseSet &x) {
return x.display(os);
}
std::ostream &operator<<(std::ostream &os, const SparseSet *x) {
return (x ? x->display(os) : os);
}
#include <iostream>
#include <vector>
#ifndef _MINISCHEDULER_SPARSESET_HPP
#define _MINISCHEDULER_SPARSESET_HPP
/**********************************************
* SparseSet
**********************************************/
/// Sparse set representation
class SparseSet {
private:
/*!@name Parameters*/
//@{
/// list of values
std::vector<int> list_;
size_t size_;
// so that we can remove from the start
size_t start_;
/// values' indices
std::vector<size_t> index_;
//@}
public:
/*!@name Constructors*/
//@{
explicit SparseSet(const size_t n = 0);
explicit SparseSet(std::vector<size_t> &common);
void reserve(const size_t n);
/*!@name Accessors*/
//@{
bool safe_contain(const int elt) const;
bool contain(const int elt) const;
size_t capacity() const;
size_t count() const;
size_t start() const;
size_t size() const;
bool empty() const;
int next(const int elt) const;
int prev(const int elt) const;
int operator[](const size_t idx) const;
int &operator[](const size_t idx);
//@}
/*!@name List Manipulation*/
//@{
std::vector<int>::iterator begin();
std::vector<int>::reverse_iterator rbegin();
std::vector<int>::iterator end();
std::vector<int>::reverse_iterator rend();
std::vector<int>::const_iterator begin() const;
std::vector<int>::const_reverse_iterator rbegin() const;
std::vector<int>::const_iterator end() const;
std::vector<int>::const_reverse_iterator rend() const;
std::vector<int>::iterator fbegin();
std::vector<int>::reverse_iterator frbegin();
std::vector<int>::iterator fend();
std::vector<int>::reverse_iterator frend();
std::vector<int>::const_iterator fbegin() const;
std::vector<int>::const_reverse_iterator frbegin() const;
std::vector<int>::const_iterator fend() const;
std::vector<int>::const_reverse_iterator frend() const;
std::vector<int>::iterator bbegin();
std::vector<int>::reverse_iterator brbegin();
std::vector<int>::iterator bend();
std::vector<int>::reverse_iterator brend();
std::vector<int>::const_iterator bbegin() const;
std::vector<int>::const_reverse_iterator brbegin() const;
std::vector<int>::const_iterator bend() const;
std::vector<int>::const_reverse_iterator brend() const;
std::vector<int>::iterator get_iterator(const size_t i);
std::vector<int>::const_iterator get_iterator(const size_t i) const;
// std::vector<int>::iterator begin_not_in();
// std::vector<int>::reverse_iterator rbegin_not_in();
//
// std::vector<int>::iterator end_not_in();
// std::vector<int>::reverse_iterator rend_not_in();
//
// std::vector<int>::const_iterator begin_not_in() const;
// std::vector<int>::const_reverse_iterator rbegin_not_in() const;
//
// std::vector<int>::const_iterator end_not_in() const;
// std::vector<int>::const_reverse_iterator rend_not_in() const;
void fill();
void clear();
void resize(const size_t n);
// void move_up(const int elt, const int idx);
void pop_back();
void pop_front();
int front() const;
int back() const;
template <typename R> int any(const size_t limit, R &random_generator) const {
auto m{std::min(limit, count())};
return list_[start_ + (random_generator() % m)];
}
void push_front(const int elt);
void push_back(const int elt);
void add(const int elt);
void safe_add(const int elt);
void pull_back(const int elt);
void remove_back(const int elt);
// void safe_remove(const int elt);
void pull_front(const int elt);
void remove_front(const int elt);
int index(const int elt) const;
void save_start(size_t &);
void save_size(size_t &);
void restore_start(const size_t);
void restore_size(const size_t);
//@}
/*!@name Miscellaneous*/
//@{
std::ostream &display(std::ostream &os) const;
};
std::ostream &operator<<(std::ostream &os, const SparseSet &x);
#endif // _MINISCHEDULER_SPARSESET_HPP
......@@ -9,8 +9,8 @@
#include <typeinfo>
#include <list>
#include <torch/torch.h>
#include "policy-gradient.h"
// #include <torch/torch.h>
// #include "policy-gradient.h"
#include <stdlib.h>
#include <limits>
......@@ -21,7 +21,7 @@
#include "mcts.h"
#include "heuristic.h"
#include "test.h"
// #include "test.h"
#include <unistd.h>
......@@ -181,7 +181,7 @@ int main(int argc, char **argv){
}
srand (opt.seed);
/*
if(opt.learn)
{
torch::manual_seed(opt.seed);
......@@ -206,20 +206,20 @@ int main(int argc, char **argv){
if(opt.adam){
auto learning_rate = 0.01;
auto optimizer = torch::optim::Adam(policy->parameters(), /*lr=*/learning_rate);
auto optimizer = torch::optim::Adam(policy->parameters(), learning_rate);
std::cout << "optimizer : " << typeid(optimizer).name() << std::endl;
set_temperature(opt.temperature);
improve_policy(policy, data, device, optimizer, opt.epoch, opt.episode);
}else{
auto learning_rate = pow(2, -12) / (nb_tasks_sum / data.size());
auto optimizer = torch::optim::SGD(policy->parameters(), /*lr=*/learning_rate);
auto optimizer = torch::optim::SGD(policy->parameters(), learning_rate);
std::cout << "optimizer : " << typeid(optimizer).name() << std::endl;
set_temperature(opt.temperature);
improve_policy(policy, data, device, optimizer, opt.epoch, opt.episode);
}
}
else
else */
if(opt.mcts)
{
Solution sol = solve(data[0], opt.c,/*print_stat=*/true, opt.time, opt.mcts_backup);
......@@ -234,7 +234,7 @@ int main(int argc, char **argv){
set_default_temperature(0.01);
// test_sample(data[0], opt.stat);
// solve_by_sample(data[0], opt.stat);
range_children(data[0], opt.stat);
// range_children(data[0], opt.stat);
}
// std::cout << "Fin" << std::endl;
......
......@@ -19,11 +19,12 @@
#define ITER_OUT true // if true, iteration info on cout, cerr else
#define DEBUG_MODE true
#define EXPAND_TEMPERATURE 0.1 // temperature used for prior prior probability
#define PRINT_STAT_ROOT true
#define PRINT_STAT_ROOT false
#define NB_SAMPLE 1
#define MAX_ITER 1000000
#define MAX_NODE 25000000
bool DEBUG = DEBUG_MODE;
......@@ -88,8 +89,8 @@ Solution solve(const Instance& data, double c, bool print_stat, int timeout, int
std::vector<Node> node_vector;
SparseSet node_set(25000000);
node_vector.reserve(25000000); // Maybe better to set the maximum size of the tree here ?
SparseSet node_set(MAX_NODE);
node_vector.reserve(MAX_NODE); // Maybe better to set the maximum size of the tree here ?
add_node(node_vector, node_set, 0, -1, 0, -1);
Node& root(node_vector.front());
......@@ -97,7 +98,7 @@ Solution solve(const Instance& data, double c, bool print_stat, int timeout, int
int iter_count = 0;
while(best_sol.lmax() > 0 && std::chrono::steady_clock::now() < run_until && iter_count < MAX_ITER)
while(best_sol.lmax() > 0 && std::chrono::steady_clock::now() < run_until)
{
iter_count++;
iter_count_display++;
......@@ -106,7 +107,7 @@ Solution solve(const Instance& data, double c, bool print_stat, int timeout, int
(iter_count < 10 || (iter_count < 100 && iter_count%10 == 0) || (iter_count < 1000 && iter_count%100 == 0) ||
(iter_count < 10000 && iter_count%1000 == 0) || iter_count%10000 == 0);
display_data = iter_count > 10000;
// display_data = iter_count > 10000;
DEBUG = DEBUG_MODE && display_data;
......@@ -148,8 +149,10 @@ Solution solve(const Instance& data, double c, bool print_stat, int timeout, int
double norm_Q;
if(child.N < 1)
norm_Q = 0.0;
else
else{
norm_Q = -(child.get_Q() / current_node.get_Q() - 1); // divided by the max_Q over the children?
norm_Q = child.get_Q() / current_node.get_Q() - 1;
}
double ucb = norm_Q + c * child.P * sqrt_n / (1 + child.N);
if(DEBUG){
......@@ -184,6 +187,7 @@ Solution solve(const Instance& data, double c, bool print_stat, int timeout, int
if(DEBUG)
std::cerr << "-- Expansion --" << std::endl;
int added_node = 0;
// Need available actions and probability distribution in order to expand
std::vector<int> actions = s.get_actions();
std::vector<double> action_distribution = get_distribution(data, s, actions, EXPAND_TEMPERATURE);
......@@ -192,8 +196,21 @@ Solution solve(const Instance& data, double c, bool print_stat, int timeout, int
Node& current_node(node_vector[current_node_idx]);
for(size_t i = 0; i < actions.size(); ++i){
int end_date = std::max(current_node.end_date + data.distance(current_node.action, actions[i]), data.release_date(actions[i])) + data.duration(actions[i]);
if(end_date > data.due_date(actions[i])){ // Don't expand if a child has tardiness
int count_root = root.nb_nodes_subtree; // to know how many node will be deleted in case of tardiness
int start_date = std::max(current_node.end_date + data.distance(current_node.action, actions[i]), data.release_date(actions[i]));
bool no_add = false;
for(size_t j = 0; j < actions.size(); ++j){