-
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.
- Loading branch information
1 parent
7c308e7
commit 2a4fadd
Showing
61 changed files
with
3,277 additions
and
500 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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); | ||
} |
Oops, something went wrong.