Skip to content

Commit

Permalink
First implementation of CN in Codac2 version
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRohou committed Dec 1, 2023
1 parent 0a8fa74 commit e77c0ac
Show file tree
Hide file tree
Showing 20 changed files with 648 additions and 3 deletions.
2 changes: 2 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@
codac_py_codac2.cpp
src/core/2/actions/codac2_py_Action.cpp
src/core/2/contractors/codac2_py_CtcAction.cpp
src/core/2/cn/codac2_py_ContractorNetwork.cpp
src/core/2/cn/codac2_py_Contractor.cpp
)

target_include_directories(codac2
Expand Down
4 changes: 4 additions & 0 deletions python/codac_py_codac2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace py = pybind11;

void export_Action(py::module& m);
void export_CtcAction(py::module& m, py::class_<Ctc, pyCtc>& ctc);
void export_ContractorNetwork_codac2(py::module& m);
void export_Contractor_codac2(py::module& m);

PYBIND11_MODULE(codac2, m)
{
Expand All @@ -29,4 +31,6 @@ PYBIND11_MODULE(codac2, m)

export_Action(m);
export_CtcAction(m, ctc);
export_ContractorNetwork_codac2(m);
export_Contractor_codac2(m);
}
53 changes: 53 additions & 0 deletions python/src/core/2/cn/codac2_py_Contractor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* \file
* Python binding
* ----------------------------------------------------------------------------
* \date 2020
* \author Simon Rohou, Benoît Desrochers
* \copyright Copyright 2021 Codac Team
* \license This program is distributed under the terms of
* the GNU Lesser General Public License (LGPL).
*/

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/operators.h>
#include <pybind11/functional.h>
#include "codac_type_caster.h"

#include "codac2_Contractor.h"

using namespace std;
using namespace codac;
using namespace codac2;
namespace py = pybind11;
using namespace pybind11::literals;


void export_Contractor_codac2(py::module& m)
{
py::class_<codac2::ContractorNodeBase,
std::shared_ptr<ContractorNodeBase> // this is needed to handle shared_ptr with pybind11
> n(m, "ContractorNodeBase", "todo");

py::class_<codac2::IntervalVector_<3>> x(m, "IntervalVector_3", "todo");
x
.def(py::init<>(),
"todo")
.def("print", [](codac2::IntervalVector_<3>& x) { cout << x << endl; },
"todo")
;

py::class_<codac2::Contractor1> ctc(m, "Contractor1", "todo");
ctc

// Definition

.def(py::init<>(),
"todo")

.def("__call__", [](codac2::Contractor1& ctc,IntervalVector_<3>& x) { return ctc(x); },
"todo",
py::return_value_policy::reference_internal)
;
}
44 changes: 44 additions & 0 deletions python/src/core/2/cn/codac2_py_ContractorNetwork.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* \file
* Python binding
* ----------------------------------------------------------------------------
* \date 2020
* \author Simon Rohou, Benoît Desrochers
* \copyright Copyright 2021 Codac Team
* \license This program is distributed under the terms of
* the GNU Lesser General Public License (LGPL).
*/

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/operators.h>
#include <pybind11/functional.h>
#include "codac_type_caster.h"

#include "codac2_ContractorNetwork.h"

using namespace std;
using namespace codac;
using namespace codac2;
namespace py = pybind11;
using namespace pybind11::literals;


void export_ContractorNetwork_codac2(py::module& m)
{
py::class_<codac2::ContractorNetwork> cn(m, "ContractorNetwork2", "todo");
cn

// Definition

.def(py::init<>(),
"todo")

.def("add", &ContractorNetwork::add,
"todo",
"n"_a)

.def("contract", &ContractorNetwork::contract,
"todo")
;
}
32 changes: 32 additions & 0 deletions src/core/2/cn/codac2_Contractor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "codac2_Contractor.h"

using namespace std;

namespace codac2
{
Contractor1::Contractor1()
{

}

void Contractor1::contract(IntervalVector_<3>& x)
{
cout << "Calling C1" << endl;
x[0] = Interval(0,1);
x[1] = Interval(0,1);
x[2] = Interval(42,43);
}


Contractor2::Contractor2()
{

}

void Contractor2::contract(IntervalVector_<2>& x, IntervalVector_<3>& y)
{
cout << "Calling C2" << endl;
x[0] &= y[2];
y[1] &= Interval(-5,6);
}
}
48 changes: 48 additions & 0 deletions src/core/2/cn/codac2_Contractor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef __CODAC2_CONTRACTOR__
#define __CODAC2_CONTRACTOR__

#include <memory>
#include "codac2_IntervalVector.h"
#include "codac2_ContractorNode.h"

namespace codac2
{

#define make_available_in_cn() \
template<typename... T> \
std::shared_ptr<ContractorNodeBase> operator()(T&... a) \
{ \
return std::make_shared<ContractorNode<std::remove_reference<decltype(*this)>::type,T...>>(*this, a...); \
} \

class Contractor
{
public:

virtual ~Contractor() = default;
};

class Contractor1 : public Contractor
{
public:

Contractor1();
void contract(IntervalVector_<3>& x);

make_available_in_cn()
};


class Contractor2 : public Contractor
{
public:

Contractor2();
void contract(IntervalVector_<2>& x, IntervalVector_<3>& y);

make_available_in_cn()
};

}

#endif
52 changes: 52 additions & 0 deletions src/core/2/cn/codac2_ContractorNetwork.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <cassert>
#include "codac2_ContractorNetwork.h"

using namespace std;

namespace codac2
{
ContractorNetwork::ContractorNetwork()
{

}

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_ctc_to_stack(const shared_ptr<ContractorNodeBase>& ctc)
{
assert(!ctc->to_be_called());
_stack.push_back(ctc);
ctc->set_call_flag(true);
}

void ContractorNetwork::disable_auto_fixpoint(bool disable)
{
_auto_fixpoint = !disable;
}

void ContractorNetwork::contract()
{
do
{
shared_ptr<ContractorNodeBase> current_ctc = _stack.front();
_stack.pop_front();

auto contracted_doms = current_ctc->call_contract();

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;
if(!p_c->to_be_called())
add_ctc_to_stack(p_c);
}

} while(!_stack.empty());
}
}
34 changes: 34 additions & 0 deletions src/core/2/cn/codac2_ContractorNetwork.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef __CODAC2_CONTRACTORNETWORK__
#define __CODAC2_CONTRACTORNETWORK__

#include <list>
#include <vector>
#include <memory>
#include "codac2_DomainNode.h"
#include "codac2_ContractorNode.h"

namespace codac2
{

class ContractorNetwork
{
public:

ContractorNetwork();
void add(const std::shared_ptr<ContractorNodeBase>& ctc);
void add_ctc_to_stack(const std::shared_ptr<ContractorNodeBase>& ctc);
void disable_auto_fixpoint(bool disable = true);
void contract();

//protected:

std::vector<std::shared_ptr<ContractorNodeBase>> _v_ctc;
std::vector<std::shared_ptr<DomainNodeBase>> _v_domains;
std::list<std::shared_ptr<ContractorNodeBase>> _stack;

bool _auto_fixpoint = true;
};

}

#endif
23 changes: 23 additions & 0 deletions src/core/2/cn/codac2_ContractorNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
#include "codac2_ContractorNode.h"

using namespace std;

namespace codac2
{
void ContractorNodeBase::set_call_flag(bool flag)
{
_to_be_called = flag;
}

bool ContractorNodeBase::to_be_called() const
{
return _to_be_called;
}

ostream& operator<<(ostream& os, const ContractorNodeBase& d)
{
os << "Contractor: " << d.contractor_class_name() << ", dom=" << d.nb_args() << endl;
return os;
}
}
Loading

0 comments on commit e77c0ac

Please sign in to comment.