-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First implementation of CN in Codac2 version
- Loading branch information
1 parent
0a8fa74
commit e77c0ac
Showing
20 changed files
with
648 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.