21 #ifndef DOUBLEDESCRIPTION_Rn
22 #define DOUBLEDESCRIPTION_Rn
40 Segment_Rn(
const std::vector< unsigned int >& LC) {_isActive =
true;}
41 Segment_Rn(boost::shared_ptr< Generator_Rn_SD>& e1, boost::shared_ptr< Generator_Rn_SD>& e2):_firstEnd(e1),_secondEnd(e2) {_isActive =
true;}
42 Segment_Rn(boost::shared_ptr< Generator_Rn_SD>& e1, boost::shared_ptr< Generator_Rn_SD>& e2,
const std::vector< unsigned int >& LC):_firstEnd(e1),_secondEnd(e2) {_isActive =
true;}
50 void dump(std::ostream &ofs) {
51 ofs <<
"[[ "; (_isActive ==
true) ? ofs <<
"t, " : ofs <<
"f, ";
62 boost::shared_ptr< Generator_Rn_SD > _firstEnd;
64 boost::shared_ptr< Generator_Rn_SD > _secondEnd;
91 _RnDIM = poly->dimension();
92 std::vector< boost::shared_ptr<Generator_Rn_SD> > listOfGenSD;
96 poly->getListOfGeneratorsSD(listOfGenSD);
97 unsigned int nbProcessHyp =
init(poly, ite, truncationStep);
106 std::cout <<
"Entering poly->setListOfGeneratorsSD(listOfGenSD) ..." << std::endl;
108 poly->setListOfGeneratorsSD(listOfGenSD);
110 std::cout <<
"Leaving poly->setListOfGeneratorsSD(listOfGenSD) ..." << std::endl;
111 std::cout << std::endl <<
"// Display the final list of generators." << std::endl;
116 std::vector< unsigned int > HS2Remove;
118 std::set< unsigned int >::const_iterator it;
120 HS2Remove.push_back(*it);
123 std::cout <<
"Before sort() ..." << std::endl;
125 for (
unsigned i=0; i<poly->numberOfHalfSpaces(); ++i) {
126 poly->getHalfSpace(i)->dump(std::cout);
127 std::cout << std::endl;
130 std::cout << std::endl;
132 std::sort(HS2Remove.begin(), HS2Remove.end(), std::greater<unsigned int>());
134 std::vector< unsigned int >::const_iterator it2;
135 for (it2=HS2Remove.begin(); it2!=HS2Remove.end(); ++it2) {
139 poly->removeHalfSpace(*it2);
144 std::cout <<
"Display HS ..." << std::endl;
146 for (
unsigned i=0; i<poly->numberOfHalfSpaces(); ++i) {
147 poly->getHalfSpace(i)->dump(std::cout);
148 std::cout << std::endl;
151 std::cout << std::endl;
152 std::cout <<
"Before unhookRedundantGenerators() ..." << std::endl;
153 std::cout << std::endl <<
"// Display the final list of generators." << std::endl;
164 std::cout <<
"After unhookRedundantGenerators() ..." << std::endl;
173 _RnDIM = poly->dimension();
174 std::vector< boost::shared_ptr<Generator_Rn_SD> > listOfGenSD;
178 poly->getListOfGeneratorsSD(listOfGenSD);
179 unsigned int nbProcessHyp =
init(poly, ite, truncationStep);
188 unsigned int nbRes=0;
189 std::vector< boost::shared_ptr<Generator_Rn_SD> >::const_iterator iteGenSD;
191 for (iteGenSD=listOfGenSD.begin(); iteGenSD!=listOfGenSD.end(); ++iteGenSD) {
195 unsigned int nbOp1 = (*iteGenSD)->getGeneratorNumber();
217 poly->setListOfGeneratorsSD(listOfGenSD);
219 std::vector< unsigned int > HS2Remove;
221 std::set< unsigned int >::const_iterator it;
223 HS2Remove.push_back(*it);
225 std::sort(HS2Remove.begin(), HS2Remove.end(), std::greater<unsigned int>());
227 std::vector< unsigned int >::const_iterator it2;
228 for (it2=HS2Remove.begin(); it2!=HS2Remove.end(); ++it2) {
232 poly->removeHalfSpace(*it2);
247 unsigned int init(POLYHEDRON poly, ITERATOR ite,
int truncationStep) {
251 unsigned int nbHyp = 0, nbProcessHyp = 0;
253 for (ite.begin(); ite.end()!=
true; ite.next()) {
254 const boost::shared_ptr<HalfSpace_Rn>& currentHalfSpace = ite.current();
255 boost::shared_ptr<Hyperplane_Rn> currentHyp = boost::dynamic_pointer_cast<Hyperplane_Rn>(currentHalfSpace);
258 if (ite.currentIteratorNumber() < truncationStep)
261 double halfSpaceNorm = std::inner_product(currentHalfSpace->begin(), currentHalfSpace->end(), currentHalfSpace->begin(), 0.);
264 std::set< boost::shared_ptr< Generator_Rn_SD > > genSet;
269 boost::shared_ptr<NormedHalfSpace_Rn> currentNormedHalfSpace;
280 void computeStatesOfAllGenerators(std::vector< boost::shared_ptr<Generator_Rn_SD> >& arrayOfGenOUT,
const boost::shared_ptr<HalfSpace_Rn>& currentHalfSpace,
281 unsigned int halfspaceNumber,
unsigned int& nbGeneratorsIn,
unsigned int& nbGeneratorsOut,
unsigned int& nbGeneratorsOn) {
285 double halfSpaceNorm = std::inner_product(currentHalfSpace->begin(), currentHalfSpace->end(), currentHalfSpace->begin(), 0.);
286 halfSpaceNorm = sqrt(halfSpaceNorm);
292 std::vector< Segment_Rn >::iterator it_GN;
294 it_GN->getFirstGenerator()->resetState();
295 it_GN->getSecondGenerator()->resetState();
300 std::vector< boost::shared_ptr<Generator_Rn_SD> > allCurrentGenerators;
304 std::vector< Segment_Rn >::iterator it_GN;
306 if (it_GN->getActive() ==
true) {
309 allCurrentGenerators.push_back( it_GN->getFirstGenerator() );
311 allCurrentGenerators.push_back( it_GN->getSecondGenerator() );
315 arrayOfGenOUT.push_back(it_GN->getFirstGenerator());
319 arrayOfGenOUT.push_back(it_GN->getSecondGenerator());
326 std::sort( allCurrentGenerators.begin(), allCurrentGenerators.end(), &
inferior );
327 std::vector< boost::shared_ptr<Generator_Rn_SD> >::iterator it_GN0;
328 for (it_GN0 = allCurrentGenerators.begin(); it_GN0 != allCurrentGenerators.end(); ++it_GN0) {
329 boost::shared_ptr<Generator_Rn_SD> Gen = *it_GN0;
330 std::cout.precision(15);
332 std::cout <<
"# G" << Gen->getGeneratorNumber() <<
" = [";
333 Gen->save(std::cout);
334 std::cout <<
"]" << std::endl <<
"{ ";
336 for (
unsigned int ii=0; ii<Gen->numberOfFacets(); ii++) {
337 std::cout << Gen->getFacet(ii);
338 if (ii != Gen->numberOfFacets()-1)
342 double scalarProduct = Gen->getScalarProduct();
343 std::cout <<
"}" << std::endl;
344 std::cout <<
"dotP = " << scalarProduct;
345 std::cout <<
", dist = " << (scalarProduct+currentHalfSpace->getConstant()) / halfSpaceNorm;
347 double distanceToHyperplane = (scalarProduct+currentHalfSpace->getConstant()) / halfSpaceNorm;
349 if (distanceToHyperplane >
_TOL)
351 else if (distanceToHyperplane < -
_TOL)
362 static bool inferior(
const boost::shared_ptr<Generator_Rn_SD>& HS1,
const boost::shared_ptr<Generator_Rn_SD>& HS2) {
364 unsigned int n=HS1->dimension();
366 if (HS1->getCoordinate(i) < HS2->getCoordinate(i))
368 else if (HS1->getCoordinate(i) > HS2->getCoordinate(i))
378 double halfSpaceNorm,
unsigned int& nbGeneratorsIn,
unsigned int& nbGeneratorsOut,
unsigned int& nbGeneratorsOn) {
379 double scalarProduct = std::inner_product(Gen->begin(), Gen->end(), currentHalfSpace->begin(), 0.);
398 double distanceToHyperplane = (scalarProduct+currentHalfSpace->getConstant()) / halfSpaceNorm;
399 if (distanceToHyperplane >
_TOL) {
404 else if (distanceToHyperplane < -
_TOL) {
427 bool findBestLinearConstraint(std::vector< boost::shared_ptr<HalfSpace_Rn> >::iterator currentLC, std::vector< boost::shared_ptr<HalfSpace_Rn> >::iterator bestLC) {
428 double bestParameter = std::numeric_limits<double>::max();
447 double scalarProduct1 = std::inner_product(Gen1->begin(), Gen1->end(), currentHalfSpace->begin(), 0.);
449 double scalarProduct2 = std::inner_product(Gen2->begin(), Gen2->end(), currentHalfSpace->begin(), 0.);
453 double distanceToHyperplane1 = (scalarProduct1+currentHalfSpace->getConstant()) / halfSpaceNorm;
455 double distanceToHyperplane2 = (scalarProduct2+currentHalfSpace->getConstant()) / halfSpaceNorm;
457 if (distanceToHyperplane1 >
_TOL)
459 else if (distanceToHyperplane1 < -
_TOL)
461 if (distanceToHyperplane2 >
_TOL)
463 else if (distanceToHyperplane2 < -
_TOL)
478 double param = (currentHalfSpace->getConstant() + scalarProduct1) / (scalarProduct1 - scalarProduct2);
480 if (param < bestParameter) {
481 bestParameter = param;
490 double param = (currentHalfSpace->getConstant() + scalarProduct2) / (scalarProduct2 - scalarProduct1);
492 if (param < bestParameter) {
493 bestParameter = param;
506 bool compute(POLYHEDRON poly, ITERATOR iteHS,
int truncationStep, std::vector< boost::shared_ptr<Generator_Rn_SD> >& listOfGenSD) {
509 std::vector< boost::shared_ptr<Generator_Rn_SD> > GN_IN;
510 std::vector< boost::shared_ptr<Generator_Rn_SD> > GN_OUT;
511 std::vector< boost::shared_ptr<Generator_Rn_SD> > GN_ON;
512 std::vector< boost::shared_ptr<Generator_Rn_SD> > GN_new;
513 std::vector< boost::shared_ptr<Generator_Rn_SD> > listOfGeneratorsOUT2recycle;
514 int halfspaceNumber = truncationStep, generatorNumber = 0;
517 std::vector< unsigned int > listOfPairsToRemove;
520 std::cout << std::endl <<
"DoubleDescription::compute(" << truncationStep <<
")" << std::endl;
524 std::vector< boost::shared_ptr<Generator_Rn_SD> >::iterator iteGN_1;
525 for (iteGN_1 = listOfGenSD.begin(); iteGN_1 != listOfGenSD.end(); ++iteGN_1) {
528 for (
unsigned int i = 0; i < (*iteGN_1)->numberOfFacets(); ++i)
535 std::cout <<
" // Begin: list of generators per linear constraint //" << std::endl;
540 std::set< boost::shared_ptr< Generator_Rn_SD > >::iterator itGN1 = listOfGen1->begin();
541 for (; itGN1 != listOfGen1->end(); ++itGN1) {
542 (*itGN1)->dump(std::cout);
545 std::cout << std::endl;
548 std::cout <<
" // End: list of generators per linear constraint //" << std::endl;
552 unsigned int count_IN = 0;
553 std::set< std::pair< unsigned int, unsigned int > > allNgb;
554 std::vector< boost::shared_ptr<Generator_Rn_SD> >::const_iterator iteGN_1;
555 for (iteGN_1 = listOfGenSD.begin(); iteGN_1 != listOfGenSD.end(); ++iteGN_1, ++count_IN) {
556 unsigned int count_OUT = 0;
558 std::vector< boost::shared_ptr<Generator_Rn_SD> >::const_iterator iteGN_2;
559 for (iteGN_2 = listOfGenSD.begin(); iteGN_2 != listOfGenSD.end(); ++iteGN_2, ++count_OUT) {
560 if (iteGN_1 != iteGN_2) {
561 std::vector< unsigned int > commonPFacets;
570 std::set< boost::shared_ptr< Generator_Rn_SD > > setOfNGB;
571 boost::shared_ptr<Generator_Rn_SD> currentGeneratorIn;
572 for (newGeneratorsTool.
begin(); newGeneratorsTool.
end()!=
true; newGeneratorsTool.
next()) {
576 if ( allNgb.find( std::make_pair(firstNgbNb,secondNgbNb) ) == allNgb.end() &&
577 allNgb.find( std::make_pair(secondNgbNb,firstNgbNb) ) == allNgb.end() ) {
582 allNgb.insert( std::make_pair(firstNgbNb, secondNgbNb) );
583 allNgb.insert( std::make_pair(secondNgbNb, firstNgbNb) );
597 generatorNumber = poly->numberOfGenerators();
600 std::vector< boost::shared_ptr<HalfSpace_Rn> >::iterator currentLC;
602 std::vector< boost::shared_ptr<HalfSpace_Rn> >::iterator bestLC = currentLC;
606 std::cout <<
"All segments all inactive (1)." << std::endl;
614 if (currentLC != bestLC) {
616 std::iter_swap(currentLC, bestLC);
619 std::iter_swap(currentNLC, bestNLC);
629 const boost::shared_ptr<HalfSpace_Rn> currentHalfSpace = *currentLC;
630 boost::shared_ptr<Hyperplane_Rn> currentHyp = boost::dynamic_pointer_cast<Hyperplane_Rn>(currentHalfSpace);
636 double halfSpaceNorm = std::inner_product(currentHalfSpace->begin(), currentHalfSpace->end(), currentHalfSpace->begin(), 0.);
637 halfSpaceNorm = sqrt(halfSpaceNorm);
638 std::cout <<
"Facet number " << halfspaceNumber << std::endl;
639 std::cout <<
"H" << halfspaceNumber <<
" = ";
640 currentHalfSpace->dump(std::cout);
641 std::cout <<
", norm=" << halfSpaceNorm;
642 std::cout << std::endl;
660 std::vector< Segment_Rn >::iterator pairOfNeighbours3;
668 std::vector< boost::shared_ptr<Generator_Rn_SD> > arrayOfGenOUT;
669 unsigned int nbGeneratorsIn = 0, nbGeneratorsOut = 0, nbGeneratorsOn = 0;
674 std::cout <<
"Begin dump DS" << std::endl;
675 std::vector< Segment_Rn >::iterator pairOfNeighbours2print;
677 std::cout << pairOfNeighbours2print -
_allNeighbours.begin() <<
". " ;
678 (pairOfNeighbours2print->getActive() ==
true) ? std::cout <<
"t, " : std::cout <<
"f, ";
679 std::cout <<
"NGB1= ";
680 pairOfNeighbours2print->getFirstGenerator()->dump(std::cout);
681 std::cout <<
", NGB2= ";
682 pairOfNeighbours2print->getSecondGenerator()->dump(std::cout);
683 std::cout << std::endl;
685 std::cout <<
" End dump DS" << std::endl;
690 if (currentHalfSpace->testEmptyness(nbGeneratorsIn, nbGeneratorsOut, nbGeneratorsOn)) {
693 std::cout <<
"Truncation is empty or flat." << std::endl;
701 if (nbGeneratorsOut == 0) {
709 currentHalfSpace->noGeneratorsOUT(listOfGenSD, GN_ON);
711 std::cout <<
"VX_OUT= " << nbGeneratorsOut << std::endl;
712 std::cout <<
"VX_IN = " << nbGeneratorsIn << std::endl;
713 std::cout <<
"VX_ON = " << nbGeneratorsOn << std::endl;
715 std::cout <<
"============================================" << std::endl;
718 else if (nbGeneratorsIn != 0) {
720 std::set< std::pair< unsigned int, unsigned int > > listOfAlreadyCreatedSegments;
727 std::set< boost::shared_ptr< Generator_Rn_SD > > setOfGeneratorsOn;
728 std::vector< Segment_Rn >::iterator pairOfNeighbours0;
730 if (pairOfNeighbours0->getActive()) {
736 std::pair< std::set< boost::shared_ptr< Generator_Rn_SD > >::iterator,
bool > ret = setOfGeneratorsOn.insert(pairOfNeighbours0->getFirstGenerator());
737 if (ret.second ==
true) {
738 GN_ON.push_back(pairOfNeighbours0->getFirstGenerator());
739 pairOfNeighbours0->getFirstGenerator()->setFacet(halfspaceNumber);
754 std::pair< std::set< boost::shared_ptr< Generator_Rn_SD > >::iterator,
bool > ret = setOfGeneratorsOn.insert(pairOfNeighbours0->getSecondGenerator());
755 if (ret.second ==
true) {
756 GN_ON.push_back(pairOfNeighbours0->getSecondGenerator());
757 pairOfNeighbours0->getSecondGenerator()->setFacet(halfspaceNumber);
770 unsigned int genON1 = pairOfNeighbours0->getFirstGenerator()->getGeneratorNumber();
771 unsigned int genON2 = pairOfNeighbours0->getSecondGenerator()->getGeneratorNumber();
772 listOfAlreadyCreatedSegments.insert( std::make_pair(genON1, genON2) );
773 listOfAlreadyCreatedSegments.insert( std::make_pair(genON2, genON1) );
784 std::vector< unsigned int > commonFacets;
785 std::vector< Segment_Rn >::iterator pairOfNeighbours1;
787 if (pairOfNeighbours1->getActive()) {
797 boost::shared_ptr< Generator_Rn_SD > GenA, GenB;
800 GenA = pairOfNeighbours1->getFirstGenerator();
801 GenB = pairOfNeighbours1->getSecondGenerator();
805 GenB = pairOfNeighbours1->getFirstGenerator();
806 GenA = pairOfNeighbours1->getSecondGenerator();
809 double ay = GenB->getScalarProduct();
810 double az = GenA->getScalarProduct();
811 boost::shared_ptr< Generator_Rn_SD > newV;
813 if (!listOfGeneratorsOUT2recycle.empty()) {
814 newV = listOfGeneratorsOUT2recycle.back();
815 listOfGeneratorsOUT2recycle.pop_back();
823 poly->createTruncatedGenerator(GenB, GenA, newV, ay, az, currentHalfSpace->getConstant());
825 commonFacets.clear();
826 std::set_intersection(GenA->facetsBegin(), GenA->facetsEnd(), GenB->facetsBegin(), GenB->facetsEnd(), std::inserter(commonFacets, commonFacets.end()));
827 newV->setAllFacets(commonFacets);
829 std::set< unsigned int > setOfFacets;
830 bool alreadyCreated =
false;
832 boost::shared_ptr< Generator_Rn_SD > alreadyExistingGenerator;
834 std::vector< boost::shared_ptr< Generator_Rn_SD > >::const_iterator iteONGen;
835 for (iteONGen = GN_ON.begin(); iteONGen != GN_ON.end() && alreadyCreated ==
false; ++iteONGen) {
836 alreadyCreated = (*iteONGen)->isEqual2(newV,
_RnDIM, TOL2);
837 if (alreadyCreated ==
true) {
839 for (
unsigned int i = 0; i < newV->numberOfFacets(); ++i)
842 alreadyExistingGenerator = (*iteONGen);
843 newV->exportFacets(setOfFacets);
844 (*iteONGen)->exportFacets(setOfFacets);
845 (*iteONGen)->importFacets(setOfFacets);
846 (*iteONGen)->orderFacets();
849 pairOfNeighbours1->setSecondGenerator(*iteONGen);
851 pairOfNeighbours1->setFirstGenerator(*iteONGen);
857 std::vector< boost::shared_ptr< Generator_Rn_SD > >::const_iterator iteNewGen;
858 for (iteNewGen = GN_new.begin(); iteNewGen != GN_new.end() && alreadyCreated ==
false; ++iteNewGen) {
861 alreadyCreated = (*iteNewGen)->isEqual2(newV,
_RnDIM, TOL2);
862 if (alreadyCreated ==
true) {
864 for (
unsigned int i = 0; i < newV->numberOfFacets(); ++i)
866 newV->setFacet(halfspaceNumber);
867 newV->exportFacets(setOfFacets);
868 (*iteNewGen)->exportFacets(setOfFacets);
869 (*iteNewGen)->importFacets(setOfFacets);
870 (*iteNewGen)->orderFacets();
872 alreadyExistingGenerator = (*iteNewGen);
874 pairOfNeighbours1->setSecondGenerator(*iteNewGen);
876 pairOfNeighbours1->setFirstGenerator(*iteNewGen);
882 if (alreadyCreated ==
false) {
884 std::cout <<
"New Generator " << newV->getGeneratorNumber() <<
" = [";
885 newV->dump(std::cout);
886 std::cout <<
"] GeneratorIN ";
887 (firstState ==
HalfSpace_Rn::hs_IN) ? std::cout << pairOfNeighbours1->getFirstGenerator()->getGeneratorNumber() : std::cout << pairOfNeighbours1->getSecondGenerator()->getGeneratorNumber();
889 (firstState ==
HalfSpace_Rn::hs_IN) ? pairOfNeighbours1->getFirstGenerator()->dump(std::cout) : pairOfNeighbours1->getSecondGenerator()->dump(std::cout);
890 std::cout <<
"], VX_IN_sp=" << az <<
" GeneratorOUT ";
891 (firstState ==
HalfSpace_Rn::hs_IN) ? std::cout << pairOfNeighbours1->getSecondGenerator()->getGeneratorNumber() : std::cout << pairOfNeighbours1->getFirstGenerator()->getGeneratorNumber();
893 (firstState ==
HalfSpace_Rn::hs_IN) ? pairOfNeighbours1->getSecondGenerator()->dump(std::cout) : pairOfNeighbours1->getFirstGenerator()->dump(std::cout);
894 std::cout <<
"], VX_OUT_sp=" << ay;
895 std::cout <<
", Common half-spaces: { ";
896 std::copy( commonFacets.begin(), commonFacets.end(), std::ostream_iterator<unsigned int>(std::cout,
" ") );
897 std::cout <<
"}" << std::endl;
899 std::vector< unsigned int > potentialNewFacets1, newFacets1;
902 std::set_difference(pairOfNeighbours1->getFirstGenerator()->facetsBegin(), pairOfNeighbours1->getFirstGenerator()->facetsEnd(),
903 pairOfNeighbours1->getSecondGenerator()->facetsBegin(), pairOfNeighbours1->getSecondGenerator()->facetsEnd(), std::inserter(potentialNewFacets1, potentialNewFacets1.end()));
905 bool addFacets =
false;
906 unsigned int size1 = potentialNewFacets1.size();
907 for (
unsigned int i1 = 0; i1 < size1; ++i1) {
911 if (-
_TOL < distanceToHyperplane && distanceToHyperplane <
_TOL) {
912 newFacets1.push_back(potentialNewFacets1[i1]);
918 std::vector< unsigned int > potentialNewFacets2, newFacets2;
919 std::set_difference(pairOfNeighbours1->getSecondGenerator()->facetsBegin(), pairOfNeighbours1->getSecondGenerator()->facetsEnd(),
920 pairOfNeighbours1->getFirstGenerator()->facetsBegin(), pairOfNeighbours1->getFirstGenerator()->facetsEnd(), std::inserter(potentialNewFacets2, potentialNewFacets2.end()));
921 unsigned int size2 = potentialNewFacets2.size();
922 for (
unsigned int i2 = 0; i2 < size2; ++i2) {
926 if (-
_TOL < distanceToHyperplane && distanceToHyperplane <
_TOL) {
927 newFacets2.push_back(potentialNewFacets2[i2]);
943 if (!newFacets1.empty() && newFacets1.size() == potentialNewFacets1.size() && !newFacets2.empty() && newFacets2.size() == potentialNewFacets2.size()) {
946 std::cout <<
"--Generator " << newV->getGeneratorNumber() <<
" contains all facets of generator " << pairOfNeighbours1->getFirstGenerator()->getGeneratorNumber();
947 std::cout <<
" and generator " << pairOfNeighbours1->getSecondGenerator()->getGeneratorNumber() << std::endl;
951 pairOfNeighbours1->getFirstGenerator()->setCoordinates(newV->vect());
958 double scalarProduct = std::inner_product(pairOfNeighbours1->getFirstGenerator()->begin(), pairOfNeighbours1->getFirstGenerator()->end(), currentHalfSpace->begin(), 0.);
959 pairOfNeighbours1->getFirstGenerator()->setScalarProduct(scalarProduct);
961 pairOfNeighbours1->setInactive();
962 newV = pairOfNeighbours1->getFirstGenerator();
965 std::cout <<
"--Add to the generator " << newV->getGeneratorNumber() <<
" the following facets:";
967 for (
unsigned int i2 = 0; i2 < newFacets2.size(); ++i2) {
968 newV->setFacet( newFacets2[i2] );
971 std::cout <<
" " << newFacets2[i2];
975 std::cout << std::endl;
982 pairOfNeighbours1->getSecondGenerator()->setCoordinates(newV->vect());
989 double scalarProduct = std::inner_product(pairOfNeighbours1->getSecondGenerator()->begin(), pairOfNeighbours1->getSecondGenerator()->end(), currentHalfSpace->begin(), 0.);
990 pairOfNeighbours1->getSecondGenerator()->setScalarProduct(scalarProduct);
992 pairOfNeighbours1->setInactive();
993 newV = pairOfNeighbours1->getSecondGenerator();
996 std::cout <<
"--Add to the generator " << newV->getGeneratorNumber() <<
" the following facets:";
998 for (
unsigned int i1 = 0; i1 < newFacets1.size(); ++i1) {
999 newV->setFacet( newFacets1[i1] );
1002 std::cout <<
" " << newFacets1[i1];
1006 std::cout << std::endl;
1010 else if (!newFacets1.empty() && newFacets1.size() == potentialNewFacets1.size()) {
1013 std::cout <<
" .Change generator IN " << pairOfNeighbours1->getFirstGenerator()->getGeneratorNumber() <<
" coordinates with generator " << newV->getGeneratorNumber() << std::endl;
1016 pairOfNeighbours1->getFirstGenerator()->setCoordinates(newV->vect());
1023 double scalarProduct = std::inner_product(pairOfNeighbours1->getFirstGenerator()->begin(), pairOfNeighbours1->getFirstGenerator()->end(), currentHalfSpace->begin(), 0.);
1024 pairOfNeighbours1->getFirstGenerator()->setScalarProduct(scalarProduct);
1026 pairOfNeighbours1->setInactive();
1027 newV = pairOfNeighbours1->getFirstGenerator();
1033 std::cout <<
" .Change generator OUT " << pairOfNeighbours1->getFirstGenerator()->getGeneratorNumber() <<
" coordinates with generator " << newV->getGeneratorNumber() << std::endl;
1034 std::cout <<
" .Add to this generator " << newV->getGeneratorNumber() <<
" the following facets:";
1036 for (
unsigned int i1 = 0; i1 < newFacets1.size(); ++i1) {
1037 newV->setFacet( newFacets1[i1] );
1040 std::cout <<
" " << newFacets1[i1];
1044 std::cout << std::endl;
1048 else if (!newFacets2.empty() && newFacets2.size() == potentialNewFacets2.size()) {
1051 std::cout <<
"..Change generator IN " << pairOfNeighbours1->getSecondGenerator()->getGeneratorNumber() <<
" coordinates with generator " << newV->getGeneratorNumber() << std::endl;
1054 pairOfNeighbours1->getSecondGenerator()->setCoordinates(newV->vect());
1061 double scalarProduct = std::inner_product(pairOfNeighbours1->getSecondGenerator()->begin(), pairOfNeighbours1->getSecondGenerator()->end(), currentHalfSpace->begin(), 0.);
1062 pairOfNeighbours1->getSecondGenerator()->setScalarProduct(scalarProduct);
1064 pairOfNeighbours1->setInactive();
1065 newV = pairOfNeighbours1->getSecondGenerator();
1071 std::cout <<
"..Change generator OUT " << pairOfNeighbours1->getSecondGenerator()->getGeneratorNumber() <<
" coordinates with generator " << newV->getGeneratorNumber() << std::endl;
1072 std::cout <<
"..Add to this generator " << newV->getGeneratorNumber() <<
" the following facets:";
1074 for (
unsigned int i2 = 0; i2 < newFacets2.size(); ++i2) {
1075 newV->setFacet( newFacets2[i2] );
1078 std::cout <<
" " << newFacets2[i2];
1082 std::cout << std::endl;
1088 if (!newFacets1.empty() && newFacets1.size() < size1) {
1090 size1 = newFacets1.size();
1092 std::cout <<
" .Add to the generator " << newV->getGeneratorNumber() <<
" the following facets:";
1094 for (
unsigned int i1 = 0; i1 < size1; ++i1) {
1095 newV->setFacet( newFacets1[i1] );
1098 std::cout <<
" " << newFacets1[i1];
1102 std::cout << std::endl;
1105 if (!newFacets2.empty() && newFacets2.size() < size2) {
1106 size2 = newFacets2.size();
1108 std::cout <<
"..Add to the generator " << newV->getGeneratorNumber() <<
" the following facets:";
1110 for (
unsigned int i2 = 0; i2 < size2; ++i2) {
1111 newV->setFacet( newFacets2[i2] );
1114 std::cout <<
" " << newFacets2[i2];
1118 std::cout << std::endl;
1122 for (
unsigned int i = 0; i < newV->numberOfFacets(); ++i)
1125 if (addFacets ==
true)
1126 newV->orderFacets();
1128 newV->setFacet(halfspaceNumber);
1135 boost::shared_ptr< Generator_Rn_SD > ptr = newV;
1136 if (pairOfNeighbours1->getActive() ==
true) {
1139 pairOfNeighbours1->setSecondGenerator(newV);
1140 ptr = pairOfNeighbours1->getSecondGenerator();
1144 pairOfNeighbours1->setFirstGenerator(newV);
1145 ptr = pairOfNeighbours1->getFirstGenerator();
1152 GN_new.push_back(ptr);
1161 std::cout <<
"No new Generator " << newV->getGeneratorNumber() <<
" = [";
1162 newV->dump(std::cout);
1163 std::cout <<
"] GeneratorIN ";
1164 (firstState ==
HalfSpace_Rn::hs_IN) ? std::cout << pairOfNeighbours1->getFirstGenerator()->getGeneratorNumber() : std::cout << pairOfNeighbours1->getSecondGenerator()->getGeneratorNumber();
1166 (firstState ==
HalfSpace_Rn::hs_IN) ? pairOfNeighbours1->getFirstGenerator()->dump(std::cout) : pairOfNeighbours1->getSecondGenerator()->dump(std::cout);
1167 std::cout <<
"], VX_IN_sp=" << az <<
" GeneratorOUT ";
1168 (firstState ==
HalfSpace_Rn::hs_IN) ? std::cout << pairOfNeighbours1->getSecondGenerator()->getGeneratorNumber() : std::cout << pairOfNeighbours1->getFirstGenerator()->getGeneratorNumber();
1170 (firstState ==
HalfSpace_Rn::hs_IN) ? pairOfNeighbours1->getSecondGenerator()->dump(std::cout) : pairOfNeighbours1->getFirstGenerator()->dump(std::cout);
1171 std::cout <<
"], VX_OUT_sp= " << ay <<
". This generator is equal to this one: ";
1172 alreadyExistingGenerator->dump(std::cout);
1173 std::cout << std::endl;
1202 std::vector< Segment_Rn >::iterator pairOfNeighbours2;
1204 if (pairOfNeighbours2->getActive()) {
1209 listOfPairsToRemove.push_back( pairOfNeighbours2 -
_allNeighbours.begin() );
1215 listOfPairsToRemove.push_back( pairOfNeighbours2 -
_allNeighbours.begin() );
1221 listOfPairsToRemove.push_back( pairOfNeighbours2 -
_allNeighbours.begin() );
1246 std::vector< boost::shared_ptr<Generator_Rn_SD> >::iterator iteOUT;
1247 for (iteOUT=arrayOfGenOUT.begin(); iteOUT!=arrayOfGenOUT.end(); ++iteOUT) {
1248 std::vector<unsigned int>::const_iterator itFacet;
1249 for (itFacet = (*iteOUT)->facetsBegin(); itFacet != (*iteOUT)->facetsEnd(); ++itFacet)
1253 listOfGeneratorsOUT2recycle.insert(listOfGeneratorsOUT2recycle.end(), arrayOfGenOUT.begin(), arrayOfGenOUT.end());
1254 arrayOfGenOUT.clear();
1264 unsigned int count_new1 = 0, count_new2 = 0;
1265 std::vector< boost::shared_ptr<Generator_Rn_SD> >::const_iterator iteGN_new1, iteGN_new2, iteGN_ON1;
1266 for (iteGN_new1 = GN_new.begin(); iteGN_new1 != GN_new.end(); ++iteGN_new1, ++count_new1) {
1272 for (iteGN_new2 = GN_new.begin(); iteGN_new2 != GN_new.end(); ++iteGN_new2, ++count_new2) {
1273 std::vector< unsigned int > commonPFacets;
1276 if (iteGN_new1 != iteGN_new2 &&
checkNeighbours(poly, *iteGN_new1, *iteGN_new2, commonPFacets) ==
true) {
1293 for (iteGN_ON1 = GN_ON.begin(); iteGN_ON1 != GN_ON.end(); ++iteGN_ON1, ++count_new2) {
1294 std::vector< unsigned int > commonPFacets;
1295 if (
checkNeighbours(poly, *iteGN_new1, *iteGN_ON1, commonPFacets) ==
true) {
1316 std::cout <<
" // Get the new segments //" << std::endl;
1318 for (newGeneratorsTool.
begin(); newGeneratorsTool.
end() !=
true; newGeneratorsTool.
next()) {
1321 unsigned int genInNb = GN_new[gen1]->getGeneratorNumber();
1322 unsigned int genOutNb = (gen2 < GN_new.size()) ? GN_new[gen2]->getGeneratorNumber() : GN_ON[gen2 - GN_new.size()]->getGeneratorNumber();
1323 if (listOfAlreadyCreatedSegments.find( std::make_pair(genInNb, genOutNb) ) == listOfAlreadyCreatedSegments.end() &&
1324 listOfAlreadyCreatedSegments.find( std::make_pair(genOutNb, genInNb) ) == listOfAlreadyCreatedSegments.end()) {
1326 std::cout <<
"NGB1 = ";
1327 GN_new[gen1]->dump(std::cout);
1328 std::cout <<
", NGB2 = ";
1329 (gen2 < GN_new.size()) ? GN_new[gen2]->dump(std::cout) : GN_ON[gen2 - GN_new.size()]->dump(std::cout);
1330 std::cout << std::endl;
1337 if (gen2 < GN_new.size())
1346 if (listOfPairsToRemove.empty() ==
true) {
1352 _allNeighbours[ listOfPairsToRemove[listOfPairsToRemove.size()-1] ] = newSegment;
1353 listOfPairsToRemove.pop_back();
1366 listOfAlreadyCreatedSegments.insert( std::make_pair(genInNb, genOutNb) );
1367 listOfAlreadyCreatedSegments.insert( std::make_pair(genOutNb, genInNb) );
1376 unsigned int count_on1 = 0, count_on2 = 0, count_new1 = 0;
1377 std::vector< boost::shared_ptr<Generator_Rn_SD> >::const_iterator iteGN_new1, iteGN_ON1, iteGN_ON2;
1378 for (iteGN_ON1 = GN_ON.begin(); iteGN_ON1 != GN_ON.end(); ++iteGN_ON1, ++count_on1) {
1382 for (iteGN_ON2 = GN_ON.begin(); iteGN_ON2 != GN_ON.end(); ++iteGN_ON2, ++count_on2) {
1383 std::vector< unsigned int > commonPFacets;
1384 bool toTest =
checkNeighbours(poly, *iteGN_ON1, *iteGN_ON2, commonPFacets);
1385 if (iteGN_ON1 != iteGN_ON2 && toTest ==
true) {
1409 for (iteGN_new1 = GN_new.begin(); iteGN_new1 != GN_new.end(); ++iteGN_new1, ++count_on2) {
1410 std::vector< unsigned int > commonPFacets;
1411 if (
checkNeighbours(poly, *iteGN_ON1, *iteGN_new1, commonPFacets) ==
true) {
1427 std::cout <<
" //Begin dump DS_ //" << std::endl;
1428 newGeneratorsTool.
dump(std::cout);
1429 std::cout <<
" // End dump DS_ //" << std::endl;
1431 for (newGeneratorsTool.
begin(); newGeneratorsTool.
end() !=
true; newGeneratorsTool.
next()) {
1434 unsigned int genInNb = GN_ON[gen1]->getGeneratorNumber();
1435 unsigned int genOutNb = (gen2 < GN_ON.size()) ? GN_ON[gen2]->getGeneratorNumber() : GN_new[gen2 - GN_ON.size()]->getGeneratorNumber();
1436 if (listOfAlreadyCreatedSegments.find( std::make_pair(genInNb, genOutNb) ) == listOfAlreadyCreatedSegments.end() &&
1437 listOfAlreadyCreatedSegments.find( std::make_pair(genOutNb, genInNb) ) == listOfAlreadyCreatedSegments.end()) {
1439 std::cout <<
"NGB1_= ";
1440 GN_ON[gen1]->dump(std::cout);
1441 std::cout <<
", NGB2_= ";
1442 (gen2 < GN_ON.size()) ? GN_ON[gen2]->dump(std::cout) : GN_new[gen2 - GN_ON.size()]->dump(std::cout);
1443 std::cout << std::endl;
1448 if (gen2 < GN_ON.size())
1453 if (listOfPairsToRemove.empty() ==
true) {
1459 _allNeighbours[ listOfPairsToRemove[listOfPairsToRemove.size()-1] ] = newSegment;
1460 listOfPairsToRemove.pop_back();
1469 listOfAlreadyCreatedSegments.insert( std::make_pair(genInNb, genOutNb) );
1470 listOfAlreadyCreatedSegments.insert( std::make_pair(genOutNb, genInNb) );
1479 std::cout <<
"Begin dump DS" << std::endl;
1480 std::vector< Segment_Rn >::iterator pairOfNeighbours2print;
1482 std::cout << pairOfNeighbours2print -
_allNeighbours.begin() <<
". " ;
1483 (pairOfNeighbours2print->getActive() ==
true) ? std::cout <<
"t: " : std::cout <<
"f: ";
1484 std::cout <<
"NGB1= ";
1485 pairOfNeighbours2print->getFirstGenerator()->dump(std::cout);
1486 std::cout <<
", NGB2= ";
1487 pairOfNeighbours2print->getSecondGenerator()->dump(std::cout);
1488 std::cout << std::endl;
1490 std::cout <<
" End dump DS" << std::endl;
1498 std::cout <<
" // Begin redundancy processing //" << std::endl;
1502 std::set< boost::shared_ptr< Generator_Rn_SD > >::iterator itGN1 = listOfGen1->begin();
1503 std::vector< unsigned int > gnNbinLC;
1504 for (; itGN1 != listOfGen1->end(); ++itGN1) {
1507 gnNbinLC.push_back( (*itGN1)->getGeneratorNumber() );
1510 if (!gnNbinLC.empty()) {
1511 std::sort(gnNbinLC.begin(), gnNbinLC.end());
1513 std::copy(gnNbinLC.begin(), gnNbinLC.end(), std::ostream_iterator<unsigned int>(std::cout,
" ") );
1516 std::cout << std::endl;
1520 std::cout <<
" // End redundancy processing //" << std::endl;
1524 unsigned int countLC = 0;
1530 std::set< boost::shared_ptr< Generator_Rn_SD > >::iterator itGN0 = listOfGen->begin();
1531 if (itGN0 != listOfGen->end()) {
1532 for (; itGN0 != listOfGen->end(); ++itGN0) {
1533 (*itGN0)->dump(std::cout);
1536 std::cout << std::endl;
1540 if (listOfGen->size() < poly->numberOfGeneratorsPerFacet()) {
1547 std::cout <<
"Redundant linear constraint " << redundantLC << std::endl;
1549 std::set< boost::shared_ptr< Generator_Rn_SD > >::iterator itGN = listOfGen->begin();
1550 for (; itGN != listOfGen->end(); ++itGN) {
1552 std::cout <<
" To be removed from ";
1553 (*itGN)->dump(std::cout);
1554 std::cout << std::endl;
1556 (*itGN)->removeCurrentLinearConstraint(redundantLC);
1569 std::vector< std::set< boost::shared_ptr< Generator_Rn_SD > > >::iterator listOfGen2 = listOfGen1 + 1;
1572 if (!listOfGen1->empty() && !listOfGen2->empty() &&
1573 listOfGen1->size() > listOfGen2->size() &&
1574 std::includes(listOfGen1->begin(), listOfGen1->end(), listOfGen2->begin(), listOfGen2->end())) {
1577 std::cout <<
"Redundant linear constraint " << redundantLC2 <<
" included into linear constraint " << redundantLC1 << std::endl;
1580 std::set< boost::shared_ptr< Generator_Rn_SD > >::iterator itGN = listOfGen2->begin();
1581 for (; itGN != listOfGen2->end(); ++itGN) {
1583 std::cout <<
" To be removed from ";
1584 (*itGN)->dump(std::cout);
1585 std::cout << std::endl;
1587 (*itGN)->removeCurrentLinearConstraint(redundantLC2);
1590 listOfGen2->clear();
1592 else if (!listOfGen1->empty() && !listOfGen2->empty() &&
1593 listOfGen1->size() <= listOfGen2->size() &&
1594 std::includes(listOfGen2->begin(), listOfGen2->end(), listOfGen1->begin(), listOfGen1->end())) {
1597 std::cout <<
"Redundant linear constraint " << redundantLC1 <<
" included into linear constraint " << redundantLC2 << std::endl;
1600 std::set< boost::shared_ptr< Generator_Rn_SD > >::iterator itGN = listOfGen1->begin();
1601 for (; itGN != listOfGen1->end(); ++itGN) {
1603 std::cout <<
" To be removed from ";
1604 (*itGN)->dump(std::cout);
1605 std::cout << std::endl;
1607 (*itGN)->removeCurrentLinearConstraint(redundantLC1);
1610 listOfGen1->clear();
1644 std::cout <<
"VX_OUT= " << nbGeneratorsOut << std::endl;
1645 std::cout <<
"VX_IN = " << nbGeneratorsIn << std::endl;
1646 std::cout <<
"VX_ON = " << nbGeneratorsOn << std::endl;
1647 std::cout <<
"VX_new= " << GN_new.size() << std::endl;
1649 std::cout <<
"============================================" << std::endl << std::endl;
1663 bool active =
false;
1671 if (active ==
false) {
1673 std::cout <<
"All segments all inactive (2)." << std::endl;
1690 std::cout <<
" // Display redundancy processing //" << std::endl;
1695 std::set< boost::shared_ptr< Generator_Rn_SD > >::iterator itGN1 = listOfGen1->begin();
1696 std::vector< unsigned int > gnNbinLC;
1697 for (; itGN1 != listOfGen1->end(); ++itGN1) {
1700 gnNbinLC.push_back( (*itGN1)->getGeneratorNumber() );
1703 std::sort(gnNbinLC.begin(), gnNbinLC.end());
1704 std::copy(gnNbinLC.begin(), gnNbinLC.end(), std::ostream_iterator< unsigned int >(std::cout,
" ") );
1707 std::cout << std::endl;
1714 listOfGenSD.clear();
1715 std::vector< Segment_Rn >::iterator it_all1;
1716 std::set< boost::shared_ptr<Generator_Rn_SD> > setOfUniqueGen;
1718 if (it_all1->getActive() ==
true) {
1719 if (setOfUniqueGen.find( it_all1->getFirstGenerator() ) == setOfUniqueGen.end()) {
1720 setOfUniqueGen.insert( it_all1->getFirstGenerator() );
1721 listOfGenSD.push_back( it_all1->getFirstGenerator() );
1723 if (setOfUniqueGen.find( it_all1->getSecondGenerator() ) == setOfUniqueGen.end()) {
1724 setOfUniqueGen.insert( it_all1->getSecondGenerator() );
1725 listOfGenSD.push_back( it_all1->getSecondGenerator() );
1736 static void dumpListOfGenerators(POLYHEDRON poly, std::vector< boost::shared_ptr<Generator_Rn_SD> > listOfGenSD, std::ostream &this_ostream,
bool displayAll =
true) {
1737 std::vector< boost::shared_ptr<Generator_Rn_SD> >::iterator it_all1;
1738 for (it_all1 = listOfGenSD.begin(); it_all1 != listOfGenSD.end(); ++it_all1) {
1739 (*it_all1)->dump(this_ostream);
1740 this_ostream << std::endl;
1741 if (displayAll ==
true) {
1742 std::vector< unsigned int >::const_iterator it = (*it_all1)->facetsBegin();
1743 for (; it != (*it_all1)->facetsEnd(); ++it) {
1744 poly->getHalfSpace(*it)->dump(std::cout);
1745 this_ostream <<
" ";
1747 this_ostream << std::endl;
1756 const boost::shared_ptr<Generator_Rn_SD>& genIn,
1757 const boost::shared_ptr<Generator_Rn_SD>& genOut,
1758 std::vector< unsigned int >& commonFacets) {
1760 std::set_intersection(genIn->facetsBegin(), genIn->facetsEnd(), genOut->facetsBegin(), genOut->facetsEnd(), std::inserter(commonFacets, commonFacets.end()));
1770 trackerHdesc.
setNumbersOfEntities(numberOfProcessedLinearConstraints, numberOfProcessedLinearConstraints);
1772 for (
unsigned int i=0; i<truncationStep; ++i)
1774 for (
unsigned int i=truncationStep; i<numberOfProcessedLinearConstraints; ++i)
1784 unsigned int minNbFacetsPerGen = poly->neigbourhoodCondition() + 1;
1785 std::set< boost::shared_ptr<Generator_Rn> > setToRemove;
1787 {
for (iteGN.
begin(); iteGN.
end()!=
true; iteGN.
next()) {
1789 if (iteGN.
current()->numberOfFacets() < minNbFacetsPerGen) {
1790 setToRemove.insert( iteGN.
current() );
1794 if (!setToRemove.empty())
1795 poly->removeGenerators(setToRemove);
1802 const boost::shared_ptr<Generator_Rn_SD>& gen1,
1803 const boost::shared_ptr<Generator_Rn_SD>& fuzz,
1804 std::vector< unsigned int >& commonFacets) {
1806 std::set_intersection(gen1->facetsBegin(), gen1->facetsEnd(), fuzz->fuzzyFacetsBegin(), fuzz->fuzzyFacetsEnd(), std::inserter(commonFacets, commonFacets.end()));
1815 if (commonFacets.empty())
1824 std::vector< std::set< boost::shared_ptr< Generator_Rn_SD > > > list2CompareOfGeneratorsPerLinearConstraint;
1827 for (
unsigned int i=0; i<nbF; ++i) {
1828 std::set< boost::shared_ptr< Generator_Rn_SD > > genSet;
1829 list2CompareOfGeneratorsPerLinearConstraint.push_back(genSet);
1832 std::set< boost::shared_ptr< Generator_Rn_SD > > alreadyProcessedGen;
1833 std::vector< Segment_Rn >::iterator it_GN;
1835 if (it_GN->getActive() ==
true) {
1836 std::pair< std::set< boost::shared_ptr< Generator_Rn_SD > >::iterator,
bool > ret;
1837 ret = alreadyProcessedGen.insert(it_GN->getFirstGenerator());
1839 if (ret.second ==
true) {
1840 for (
unsigned int ii=0; ii<it_GN->getFirstGenerator()->numberOfFacets(); ii++)
1841 list2CompareOfGeneratorsPerLinearConstraint[ it_GN->getFirstGenerator()->getFacet(ii) ].insert( it_GN->getFirstGenerator() );
1844 ret = alreadyProcessedGen.insert(it_GN->getSecondGenerator());
1845 if (ret.second ==
true) {
1846 for (
unsigned int ii=0; ii<it_GN->getSecondGenerator()->numberOfFacets(); ii++)
1847 list2CompareOfGeneratorsPerLinearConstraint[ it_GN->getSecondGenerator()->getFacet(ii) ].insert( it_GN->getSecondGenerator() );
1852 std::cout <<
"======================" << std::endl;
1853 std::cout <<
"= checkConsistency() =" << std::endl;
1854 std::cout <<
"======================" << std::endl;
1857 unsigned int nbF = list2CompareOfGeneratorsPerLinearConstraint.size();
1858 for (
unsigned int i=0; i<nbF; ++i) {
1861 std::cout <<
"Problem found for constraint " << i <<
": ";
1863 std::cout << std::endl <<
"Lists of generators for both data structures: " << std::endl;
1865 std::set< boost::shared_ptr< Generator_Rn_SD > >::iterator itGN1 = list2CompareOfGeneratorsPerLinearConstraint[i].begin();
1866 for (; itGN1 != list2CompareOfGeneratorsPerLinearConstraint[i].end(); ++itGN1)
1867 std::cout << (*itGN1)->getGeneratorNumber() <<
" ";
1868 std::cout << std::endl;
1873 std::cout << (*itGN1)->getGeneratorNumber() <<
" ";
1874 std::cout << std::endl;
1879 (OK1 ==
true) ? std::cout <<
"OK1 " : std::cout <<
"KO1 ";
1880 std::cout << std::endl << std::endl;