Skip to content

Commit

Permalink
[codac2] new developments
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRohou committed Feb 24, 2024
1 parent 7c308e7 commit 2a4fadd
Show file tree
Hide file tree
Showing 61 changed files with 3,277 additions and 500 deletions.
6 changes: 3 additions & 3 deletions python/src/core/2/cn/codac2_py_Contractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <pybind11/functional.h>
#include "codac_type_caster.h"

#include "codac2_Contractor.h"
#include "codac2_Ctc.h"

using namespace std;
using namespace codac;
Expand All @@ -26,7 +26,7 @@ using namespace pybind11::literals;

void export_Contractor_codac2(py::module& m)
{
py::class_<codac2::ContractorNodeBase,
/*py::class_<codac2::ContractorNodeBase,
std::shared_ptr<ContractorNodeBase> // this is needed to handle shared_ptr with pybind11
> n(m, "ContractorNodeBase", "todo");
Expand All @@ -49,5 +49,5 @@ void export_Contractor_codac2(py::module& m)
.def("__call__", [](codac2::Contractor1& ctc,IntervalVector_<3>& x) { return ctc(x); },
"todo",
py::return_value_policy::reference_internal)
;
;*/
}
4 changes: 2 additions & 2 deletions python/src/core/2/cn/codac2_py_ContractorNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using namespace pybind11::literals;

void export_ContractorNetwork_codac2(py::module& m)
{
py::class_<codac2::ContractorNetwork> cn(m, "ContractorNetwork2", "todo");
/*py::class_<codac2::ContractorNetwork> cn(m, "ContractorNetwork2", "todo");
cn
// Definition
Expand All @@ -41,5 +41,5 @@ void export_ContractorNetwork_codac2(py::module& m)
.def("contract", &ContractorNetwork::contract,
"todo",
"verbose"_a=true)
;
;*/
}
32 changes: 0 additions & 32 deletions src/core/2/cn/codac2_Contractor.cpp

This file was deleted.

63 changes: 0 additions & 63 deletions src/core/2/cn/codac2_Contractor.h

This file was deleted.

175 changes: 119 additions & 56 deletions src/core/2/cn/codac2_ContractorNetwork.cpp
Original file line number Diff line number Diff line change
@@ -1,77 +1,140 @@
#include <cassert>
#include <time.h>
/**
* ContractorNetwork class
* ----------------------------------------------------------------------------
* \date 2023
* \author Simon Rohou, Luc Jaulin
* \copyright Copyright 2021 Codac Team
* \license This program is distributed under the terms of
* the GNU Lesser General Public License (LGPL).
*/

#include "codac2_ContractorNetwork.h"

using namespace std;
using namespace codac2;

ContractorNetwork::ContractorNetwork()
{ }

namespace codac2
ContractorNetwork::ContractorNetwork(std::initializer_list<std::shared_ptr<ContractorNodeBase>> l)
{
ContractorNetwork::ContractorNetwork()
{
for(const auto& li : l)
add(li);
}

}
void ContractorNetwork::add(const std::shared_ptr<ContractorNodeBase>& ctc)
{
ctc->associate_domains(_v_domains);
_v_ctc.push_back(ctc);
_stack.push_back(ctc); // to be contracted at least once
}

void ContractorNetwork::add(const shared_ptr<ContractorNodeBase>& ctc)
{
ctc->associate_domains(_v_domains);
_v_ctc.push_back(ctc);
add_ctc_to_stack(ctc); // to be contracted at least once
}
void ContractorNetwork::add(const std::vector<std::shared_ptr<ContractorNodeBase>>& v_ctc)
{
for(const auto& ci : v_ctc)
add(ci);
}

void ContractorNetwork::add_ctc_to_stack(const shared_ptr<ContractorNodeBase>& ctc)
{
if(find(_stack.begin(),_stack.end(),ctc) == _stack.end())
_stack.push_back(ctc);
}
void ContractorNetwork::merge_into_stack(std::shared_ptr<ContractorNodeBase> ctc)
{
std::list<std::shared_ptr<ContractorNodeBase>> merged_items;
for(auto& c : _stack)
if(c->same_raw_ctcdoms(ctc) && c->is_impacted(ctc->views()))
{
c->extend_views(ctc->views());
// todo: break?
merged_items.push_back(c);
}

if(merged_items.empty())
_stack.push_back(ctc);

void ContractorNetwork::disable_auto_fixpoint(bool disable)
else
for(const auto& merged_i : merged_items)
_stack.splice(_stack.end(), _stack, std::find(_stack.begin(), _stack.end(), merged_i));
}

template<typename W>
char plural(const W& w)
{
return w.size() > 1 ? 's' : '\0';
}

double ContractorNetwork::contract(bool verbose, bool verbose_ctc_calls)
{
if(verbose)
{
_auto_fixpoint = !disable;
std::cout << "Contractor network ("
<< _v_ctc.size() << " contractor" << plural(_v_ctc) << ", "
<< _v_domains.size() << " domain" << plural(_v_domains) << ")" << std::endl;
std::cout << "Computing, " << _stack.size() << " contractor" << plural(_stack) << " currently in stack..." << std::endl;
}

double ContractorNetwork::contract(bool verbose)
clock_t t_start = clock();

while(!_stack.empty())
{
if(verbose)
std::shared_ptr<ContractorNodeBase> ci = _stack.front();
_stack.pop_front();

if(verbose_ctc_calls)
std::cout << "Calling " << ci->to_string() << std::endl;

auto ci_contracted_views = ci->contract_and_propag();
ci_contracted_views.remove(nullptr);
ci_contracted_views.remove_if([](const std::shared_ptr<PropagationSpanBase>& v){ return v->is_empty(); });

if(verbose_ctc_calls)
{
std::cout << "Contractor network (" << _v_ctc.size()
<< " contractors, " << _v_domains.size() << " domains)" << std::endl;
std::cout << "Computing, " << _stack.size() << " contractors currently in stack" << std::endl;
std::cout << " Propagating views =";
if(ci_contracted_views.empty())
std::cout << " none.";
else
for(const auto& vi : ci_contracted_views)
std::cout << " " << vi->to_string();
std::cout << std::endl;
}

clock_t t_start = clock();

while(!_stack.empty())
for(const auto& cj : _v_ctc) // could be restricted to ctc of domains of this ctc
{
shared_ptr<ContractorNodeBase> current_ctc = _stack.front();
_stack.pop_front();

auto contracted_doms = current_ctc->call_contract(false);

for(auto& d : contracted_doms)
for(auto& ci : d->contractors())
{
auto p_c = ci.lock();
if(!_auto_fixpoint && current_ctc.get() == p_c.get()) continue;
add_ctc_to_stack(p_c);
}
// All the contractors related to contracted views are added to the stack
if((_force_fixed_point || !ci->same_raw_ctcdoms(cj)) && cj->is_impacted(ci_contracted_views))
{
auto new_cj = cj->create_viewed_ctc(ci_contracted_views);
if(verbose_ctc_calls)
std::cout << " Creating new contractor node: " << new_cj->to_string() << std::endl;
merge_into_stack(new_cj);
}
}

double elapsed_time = (double)(clock()-t_start)/CLOCKS_PER_SEC;
if(verbose)
std::cout << " Constraint propagation time: " << elapsed_time << "s" << std::endl;
return elapsed_time;
}

void ContractorNetwork::reset_all_vars()
{
for(auto& d : _v_domains)
if(d->is_var())
d->reset();
}

void ContractorNetwork::trigger_all_contractors()
{
for(auto& c : _v_ctc)
add_ctc_to_stack(c);
}
double elapsed_time = (double)(clock()-t_start)/CLOCKS_PER_SEC;
if(verbose)
std::cout << "> Constraint propagation time: " << elapsed_time << "s" << std::endl;
return elapsed_time;
}

void ContractorNetwork::reset_variables()
{
for(auto& x : _v_domains)
x->reset_if_var();
}

void ContractorNetwork::force_fixed_point(bool force)
{
_force_fixed_point = force;
}

void ContractorNetwork::get_views_from_cn(std::list<std::shared_ptr<PropagationSpanBase>>& l)
{
for(auto& ctc : _v_ctc)
for(auto& ctc_vi : ctc->views())
if(!ctc_vi->is_view_from_var() && !ctc_vi->is_view_from_const())
l.push_back(ctc_vi);
}

void ContractorNetwork::trigger_all_contractors()
{
for(auto& ctc : _v_ctc)
_stack.push_back(ctc);
}
Loading

0 comments on commit 2a4fadd

Please sign in to comment.