1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
|
/*
* lib_cone.cpp
*
* Created on: Sep 29, 2010
* Author: anders
*/
#include "gfanlib_zcone.h"
#include <vector>
#include <set>
#include <cdd/setoper.h>
#include <cdd/cdd.h>
namespace gfan{
static void cddinitGmp()
{
static bool initialized;
if(!initialized)
{
dd_set_global_constants(); /* First, this must be called. */
initialized=true;
}
}
class LpSolver
{
static dd_MatrixPtr ZMatrix2MatrixGmp(ZMatrix const &g, dd_ErrorType *Error)
{
int n=g.getWidth();
dd_MatrixPtr M=NULL;
dd_rowrange m_input,i;
dd_colrange d_input,j;
dd_RepresentationType rep=dd_Inequality;
dd_boolean found=dd_FALSE, newformat=dd_FALSE, successful=dd_FALSE;
char command[dd_linelenmax], comsave[dd_linelenmax];
dd_NumberType NT;
(*Error)=dd_NoError;
rep=dd_Inequality; newformat=dd_TRUE;
m_input=g.getHeight();
d_input=n+1;
NT=dd_Rational;
M=dd_CreateMatrix(m_input, d_input);
M->representation=rep;
M->numbtype=NT;
for (i = 0; i < m_input; i++) {
dd_set_si(M->matrix[i][0],0);
for (j = 1; j < d_input; j++) {
g[i][j-1].setGmp(mpq_numref(M->matrix[i][j]));
mpz_init_set_ui(mpq_denref(M->matrix[i][j]), 1);
mpq_canonicalize(M->matrix[i][j]);
}
}
successful=dd_TRUE;
return M;
}
static dd_MatrixPtr ZMatrix2MatrixGmp(ZMatrix const &inequalities, ZMatrix const &equations, dd_ErrorType *err)
{
ZMatrix g=inequalities;
g.append(equations);
int numberOfInequalities=inequalities.getHeight();
int numberOfRows=g.getHeight();
dd_MatrixPtr A=NULL;
cddinitGmp();
A=ZMatrix2MatrixGmp(g, err);
for(int i=numberOfInequalities;i<numberOfRows;i++)
set_addelem(A->linset,i+1);
return A;
}
static ZMatrix getConstraints(dd_MatrixPtr A, bool returnEquations)
{
int rowsize=A->rowsize;
int n=A->colsize-1;
ZMatrix ret(0,n);
for(int i=0;i<rowsize;i++)
{
bool isEquation=set_member(i+1,A->linset);
if(isEquation==returnEquations)
{
QVector v(n);
for(int j=0;j<n;j++)v[j]=Rational(A->matrix[i][j+1]);
ret.appendRow(QToZVectorPrimitive(v));
}
}
return ret;
}
static bool isFacet(ZMatrix const &g, int index)
{
bool ret;
dd_MatrixPtr M=NULL,M2=NULL,M3=NULL;
dd_colrange d;
dd_ErrorType err=dd_NoError;
dd_rowset redrows,linrows,ignoredrows, basisrows;
dd_colset ignoredcols, basiscols;
dd_DataFileType inputfile;
FILE *reading=NULL;
cddinitGmp();
M=ZMatrix2MatrixGmp(g, &err);
if (err!=dd_NoError) goto _L99;
d=M->colsize;
static dd_Arow temp;
dd_InitializeArow(g.getWidth()+1,&temp);
ret= !dd_Redundant(M,index+1,temp,&err);
dd_FreeMatrix(M);
dd_FreeArow(g.getWidth()+1,temp);
if (err!=dd_NoError) goto _L99;
return ret;
_L99:
assert(0);
return false;
}
/*
Heuristic for checking if inequality of full dimensional cone is a
facet. If the routine returns true then the inequality is a
facet. If it returns false it is unknown.
*/
static bool fastIsFacetCriterion(ZMatrix const &normals, int i)
{
int n=normals.getWidth();
for(int j=0;j<n;j++)
if(normals[i][j].sign()!=0)
{
int sign=normals[i][j].sign();
bool isTheOnly=true;
for(int k=0;k<normals.getHeight();k++)
if(k!=i)
{
if(normals[i][j].sign()==sign)
{
isTheOnly=false;
break;
}
}
if(isTheOnly)return true;
}
return false;
}
static bool fastIsFacet(ZMatrix const &normals, int i)
{
if(fastIsFacetCriterion(normals,i))return true;
return isFacet(normals,i);
}
class MyHashMap
{
typedef std::vector<std::set<ZVector> > Container;
Container container;
int tableSize;
public:
class iterator
{
class MyHashMap &hashMap;
int index; // having index==-1 means that we are before/after the elements.
std::set<ZVector>::iterator i;
public:
bool operator++()
{
if(index==-1)goto search;
i++;
while(i==hashMap.container[index].end())
{
search:
index++;
if(index>=hashMap.tableSize){
index=-1;
return false;
}
i=hashMap.container[index].begin();
}
return true;
}
ZVector const & operator*()const
{
return *i;
}
ZVector operator*()
{
return *i;
}
iterator(MyHashMap &hashMap_):
hashMap(hashMap_)
{
index=-1;
}
};
unsigned int function(const ZVector &v)
{
unsigned int ret=0;
int n=v.size();
for(int i=0;i<n;i++)
ret=(ret<<3)+(ret>>29)+v.UNCHECKEDACCESS(i).hashValue();
return ret%tableSize;
}
MyHashMap(int tableSize_):
container(tableSize_),
tableSize(tableSize_)
{
assert(tableSize_>0);
}
void insert(const ZVector &v)
{
container[function(v)].insert(v);
}
void erase(ZVector const &v)
{
container[function(v)].erase(v);
}
iterator begin()
{
iterator ret(*this);
++ ret;
return ret;
}
int size()
{
iterator i=begin();
int ret=0;
do{ret++;}while(++i);
return ret;
}
};
static ZMatrix normalizedWithSumsAndDuplicatesRemoved(ZMatrix const &a)
{
// TODO: write a version of this function which will work faster if the entries fit in 32bit
if(a.getHeight()==0)return a;
int n=a.getWidth();
ZVector temp1(n);
// ZVector temp2(n);
ZMatrix ret(0,n);
MyHashMap b(a.getHeight());
for(int i=0;i<a.getHeight();i++)
{
assert(!(a[i].isZero()));
b.insert(a[i].normalized());
}
{
MyHashMap::iterator i=b.begin();
do
{
MyHashMap::iterator j=i;
while(++j)
{
ZVector const &I=*i;
ZVector const &J=*j;
for(int k=0;k<n;k++)temp1[k]=I.UNCHECKEDACCESS(k)+J.UNCHECKEDACCESS(k);
// normalizedLowLevel(temp1,temp2);
// b.erase(temp2);//this can never remove *i or *j
b.erase(temp1.normalized());//this can never remove *i or *j
}
}
while(++i);
}
ZMatrix original(0,n);
{
MyHashMap::iterator i=b.begin();
do
{
original.appendRow(*i);
}
while(++i);
}
for(int i=0;i!=original.getHeight();i++)
for(int j=0;j!=a.getHeight();j++)
if(!dependent(original[i],a[j]))
{
ZVector const &I=original[i];
ZVector const &J=a[j];
for(int k=0;k<n;k++)temp1[k]=I.UNCHECKEDACCESS(k)+J.UNCHECKEDACCESS(k);
// normalizedLowLevel(temp1,temp2);
// b.erase(temp2);//this can never remove *i or *j
b.erase(temp1.normalized());//this can never remove *i or *j
}
{
MyHashMap::iterator i=b.begin();
do
{
ZVector temp=*i;
ret.appendRow(temp);
}
while(++i);
}
return ret;
}
public:
static ZMatrix fastNormals(ZMatrix const &inequalities)
{
ZMatrix normals=normalizedWithSumsAndDuplicatesRemoved(inequalities);
for(int i=0;i!=normals.getHeight();i++)
if(!fastIsFacet(normals,i))
{
normals[i]=normals[normals.getHeight()-1];
normals.eraseLastRow();
i--;
}
return normals;
}
void removeRedundantRows(ZMatrix &inequalities, ZMatrix &equations, bool removeInequalityRedundancies)
{
cddinitGmp();
int numberOfEqualities=equations.getHeight();
int numberOfInequalities=inequalities.getHeight();
int numberOfRows=numberOfEqualities+numberOfInequalities;
if(numberOfRows==0)return;//the full space, so description is already irredundant
dd_rowset r=NULL;
ZMatrix g=inequalities;
g.append(equations);
dd_LPSolverType solver=dd_DualSimplex;
dd_MatrixPtr A=NULL;
dd_ErrorType err=dd_NoError;
A=ZMatrix2MatrixGmp(g,&err);
if (err!=dd_NoError) goto _L99;
for(int i=numberOfInequalities;i<numberOfRows;i++)
set_addelem(A->linset,i+1);
A->objective=dd_LPmax;
dd_rowset impl_linset;
dd_rowset redset;
dd_rowindex newpos;
if(removeInequalityRedundancies)
dd_MatrixCanonicalize(&A, &impl_linset, &redset, &newpos, &err);
else
dd_MatrixCanonicalizeLinearity(&A, &impl_linset, &newpos, &err);
if (err!=dd_NoError) goto _L99;
{
int n=A->colsize-1;
equations=ZMatrix(0,n); //TODO: the number of rows needed is actually known
inequalities=ZMatrix(0,n); //is known by set_card(). That might save some copying.
{
int rowsize=A->rowsize;
QVector point(n);
for(int i=0;i<rowsize;i++)
{
for(int j=0;j<n;j++)point[j]=Rational(A->matrix[i][j+1]);
((set_member(i+1,A->linset))?equations:inequalities).appendRow(QToZVectorPrimitive(point));
}
}
assert(set_card(A->linset)==equations.getHeight());
assert(A->rowsize==equations.getHeight()+inequalities.getHeight());
set_free(impl_linset);
if(removeInequalityRedundancies)
set_free(redset);
free(newpos);
dd_FreeMatrix(A);
return;
}
_L99:
assert(!"Cddlib reported error when called by Gfanlib.");
}
ZVector relativeInteriorPoint(const ZMatrix &inequalities, const ZMatrix &equations)
{
QVector retUnscaled(inequalities.getWidth());
cddinitGmp();
int numberOfEqualities=equations.getHeight();
int numberOfInequalities=inequalities.getHeight();
int numberOfRows=numberOfEqualities+numberOfInequalities;
dd_rowset r=NULL;
ZMatrix g=inequalities;
g.append(equations);
dd_LPSolverType solver=dd_DualSimplex;
dd_MatrixPtr A=NULL;
dd_ErrorType err=dd_NoError;
A=ZMatrix2MatrixGmp(g,&err);
if (err!=dd_NoError) goto _L99;
{
dd_LPSolutionPtr lps1;
dd_LPPtr lp,lp1;
for(int i=0;i<numberOfInequalities;i++)
dd_set_si(A->matrix[i][0],-1);
for(int i=numberOfInequalities;i<numberOfRows;i++)
set_addelem(A->linset,i+1);
A->objective=dd_LPmax;
lp=dd_Matrix2LP(A, &err);
if (err!=dd_NoError) goto _L99;
lp1=dd_MakeLPforInteriorFinding(lp);
dd_LPSolve(lp1,solver,&err);
if (err!=dd_NoError) goto _L99;
lps1=dd_CopyLPSolution(lp1);
assert(!dd_Negative(lps1->optvalue));
for (int j=1; j <(lps1->d)-1; j++)
retUnscaled[j-1]=Rational(lps1->sol[j]);
dd_FreeLPData(lp);
dd_FreeLPSolution(lps1);
dd_FreeLPData(lp1);
dd_FreeMatrix(A);
return QToZVectorPrimitive(retUnscaled);
}
_L99:
assert(0);
return QToZVectorPrimitive(retUnscaled);
}
void dual(ZMatrix const &inequalities, ZMatrix const &equations, ZMatrix &dualInequalities, ZMatrix &dualEquations)
{
int result;
dd_MatrixPtr A=NULL;
dd_ErrorType err=dd_NoError;
cddinitGmp();
A=ZMatrix2MatrixGmp(inequalities, equations, &err);
dd_PolyhedraPtr poly;
poly=dd_DDMatrix2Poly2(A, dd_LexMin, &err);
if (poly->child==NULL || poly->child->CompStatus!=dd_AllFound) assert(0);
dd_MatrixPtr A2=dd_CopyGenerators(poly);
dualInequalities=getConstraints(A2,false);
dualEquations=getConstraints(A2,true);
dd_FreeMatrix(A2);
dd_FreeMatrix(A);
dd_FreePolyhedra(poly);
return;
_L99:
assert(0);
}
// this procedure is take from cddio.c.
static void dd_ComputeAinc(dd_PolyhedraPtr poly)
{
/* This generates the input incidence array poly->Ainc, and
two sets: poly->Ared, poly->Adom.
*/
dd_bigrange k;
dd_rowrange i,m1;
dd_colrange j;
dd_boolean redundant;
dd_MatrixPtr M=NULL;
mytype sum,temp;
dd_init(sum); dd_init(temp);
if (poly->AincGenerated==dd_TRUE) goto _L99;
M=dd_CopyOutput(poly);
poly->n=M->rowsize;
m1=poly->m1;
/* this number is same as poly->m, except when
poly is given by nonhomogeneous inequalty:
!(poly->homogeneous) && poly->representation==Inequality,
it is poly->m+1. See dd_ConeDataLoad.
*/
poly->Ainc=(set_type*)calloc(m1, sizeof(set_type));
for(i=1; i<=m1; i++) set_initialize(&(poly->Ainc[i-1]),poly->n);
set_initialize(&(poly->Ared), m1);
set_initialize(&(poly->Adom), m1);
for (k=1; k<=poly->n; k++){
for (i=1; i<=poly->m; i++){
dd_set(sum,dd_purezero);
for (j=1; j<=poly->d; j++){
dd_mul(temp,poly->A[i-1][j-1],M->matrix[k-1][j-1]);
dd_add(sum,sum,temp);
}
if (dd_EqualToZero(sum)) {
set_addelem(poly->Ainc[i-1], k);
}
}
if (!(poly->homogeneous) && poly->representation==dd_Inequality){
if (dd_EqualToZero(M->matrix[k-1][0])) {
set_addelem(poly->Ainc[m1-1], k); /* added infinity inequality (1,0,0,...,0) */
}
}
}
for (i=1; i<=m1; i++){
if (set_card(poly->Ainc[i-1])==M->rowsize){
set_addelem(poly->Adom, i);
}
}
for (i=m1; i>=1; i--){
if (set_card(poly->Ainc[i-1])==0){
redundant=dd_TRUE;
set_addelem(poly->Ared, i);
}else {
redundant=dd_FALSE;
for (k=1; k<=m1; k++) {
if (k!=i && !set_member(k, poly->Ared) && !set_member(k, poly->Adom) &&
set_subset(poly->Ainc[i-1], poly->Ainc[k-1])){
if (!redundant){
redundant=dd_TRUE;
}
set_addelem(poly->Ared, i);
}
}
}
}
dd_FreeMatrix(M);
poly->AincGenerated=dd_TRUE;
_L99:;
dd_clear(sum); dd_clear(temp);
}
std::vector<std::vector<int> > extremeRaysInequalityIndices(const ZMatrix &inequalities)
{
int dim2=inequalities.getHeight();
if(dim2==0)return std::vector<std::vector<int> >();
int dimension=inequalities.getWidth();
dd_MatrixPtr A=NULL;
dd_ErrorType err=dd_NoError;
cddinitGmp();
A=ZMatrix2MatrixGmp(inequalities, &err);
dd_PolyhedraPtr poly;
poly=dd_DDMatrix2Poly2(A, dd_LexMin, &err);
if (poly->child==NULL || poly->child->CompStatus!=dd_AllFound) assert(0);
if (poly->AincGenerated==dd_FALSE) dd_ComputeAinc(poly);
std::vector<std::vector<int> > ret;
/*
How do we interpret the cddlib output? For a long ting gfan has
been using poly->n as the number of rays of the cone and thus
returned sets of indices that actually gave the lineality
space. The mistake was then caught later in PolyhedralCone. On Feb
17 2009 gfan was changed to check the length of each set to make
sure that it does not specify the lineality space and only return
those sets giving rise to rays. This does not seem to be the best
strategy and might even be wrong.
*/
for (int k=1; k<=poly->n; k++)
{
int length=0;
for (int i=1; i<=poly->m1; i++)
if(set_member(k,poly->Ainc[i-1]))length++;
if(length!=dim2)
{
std::vector<int> v(length);
int j=0;
for (int i=1; i<=poly->m1; i++)
if(set_member(k,poly->Ainc[i-1]))v[j++]=i-1;
ret.push_back(v);
}
}
dd_FreeMatrix(A);
dd_FreePolyhedra(poly);
return ret;
_L99:
assert(0);
return std::vector<std::vector<int> >();
}
};
LpSolver lpSolver;
bool ZCone::isInStateMinimum(int s)const
{
return state>=s;
}
bool operator<(ZCone const &a, ZCone const &b)
{
assert(a.state>=3);
assert(b.state>=3);
if(a.n<b.n)return true;
if(a.n>b.n)return false;
if(a.equations<b.equations)return true;
if(b.equations<a.equations)return false;
if(a.inequalities<b.inequalities)return true;
if(b.inequalities<a.inequalities)return false;
return false;
}
bool operator!=(ZCone const &a, ZCone const &b)
{
return (a<b)||(b<a);
}
void ZCone::ensureStateAsMinimum(int s)const
{
if((state<1) && (s==1))
{
{
QMatrix m=ZToQMatrix(equations);
m.reduce();
m.removeZeroRows();
ZMatrix newInequalities(0,inequalities.getWidth());
for(int i=0;i<inequalities.getHeight();i++)
{
QVector w=ZToQVector(inequalities[i]);
w=m.canonicalize(w);
if(!w.isZero())
newInequalities.appendRow(QToZVectorPrimitive(w));
}
inequalities=newInequalities;
inequalities.sortAndRemoveDuplicateRows();
equations=QToZMatrixPrimitive(m);
}
if(!(preassumptions&PCP_impliedEquationsKnown))
if(inequalities.getHeight()>1)//there can be no implied equation unless we have at least two inequalities
lpSolver.removeRedundantRows(inequalities,equations,false);
assert(inequalities.getWidth()==equations.getWidth());
}
if((state<2) && (s>=2) && !(preassumptions&PCP_facetsKnown))
{
/* if(inequalities.size()>25)
{
IntegerVectorList h1;
IntegerVectorList h2;
bool a=false;
for(IntegerVectorList::const_iterator i=inequalities.begin();i!=inequalities.end();i++)
{
if(a)
h1.push_back(*i);
else
h2.push_back(*i);
a=!a;
}
PolyhedralCone c1(h1,equations);
PolyhedralCone c2(h2,equations);
c1.ensureStateAsMinimum(2);
c2.ensureStateAsMinimum(2);
inequalities=c1.inequalities;
for(IntegerVectorList::const_iterator i=c2.inequalities.begin();i!=c2.inequalities.end();i++)
inequalities.push_back(*i);
}
*/
if(equations.getHeight())
{
QMatrix m=ZToQMatrix(equations);
m.reduce();
ZMatrix inequalities2(0,equations.getWidth());
for(int i=0;i<inequalities.getHeight();i++)
{
inequalities2.appendRow(QToZVectorPrimitive(m.canonicalize(ZToQVector(inequalities[i]))));
}
inequalities=LpSolver::fastNormals(inequalities2);
goto noFallBack;
fallBack://alternativ (disabled)
lpSolver.removeRedundantRows(inequalities,equations,true);
noFallBack:;
}
else
inequalities=LpSolver::fastNormals(inequalities);
}
if((state<3) && (s>=3))
{
QMatrix equations2=ZToQMatrix(equations);
equations2.reduce();
for(int i=0;i<inequalities.getHeight();i++)
{
inequalities[i]=QToZVectorPrimitive(equations2.canonicalize(ZToQVector(inequalities[i])));
}
inequalities.sortRows();
equations=QToZMatrixPrimitive(equations2);
}
state=s;
}
std::ostream &operator<<(std::ostream &f, ZCone const &c)
{
f<<"Ambient dimension:"<<c.n<<std::endl;
f<<"Inequalities:"<<std::endl;
f<<c.inequalities<<std::endl;
f<<"Equations:"<<std::endl;
f<<c.equations<<std::endl;
}
ZCone::ZCone(int ambientDimension):
n(ambientDimension),
state(1),
preassumptions(PCP_impliedEquationsKnown|PCP_facetsKnown),
multiplicity(1),
haveExtremeRaysBeenCached(false),
linearForms(ZMatrix(0,ambientDimension))
{
}
ZCone::ZCone(ZMatrix const &inequalities_, ZMatrix const &equations_, int preassumptions_):
inequalities(inequalities_),
equations(equations_),
state(0),
preassumptions(preassumptions_),
multiplicity(1),
haveExtremeRaysBeenCached(false),
n(inequalities_.getWidth()),
linearForms(ZMatrix(0,inequalities_.getWidth()))
{
assert(preassumptions_<4);//OTHERWISE WE ARE DOING SOMETHING STUPID LIKE SPECIFYING AMBIENT DIMENSION
assert(equations_.getWidth()==n);
ensureStateAsMinimum(1);
}
void ZCone::canonicalize()
{
ensureStateAsMinimum(3);
}
void ZCone::findFacets()
{
ensureStateAsMinimum(2);
}
ZMatrix ZCone::getFacets()const
{
ensureStateAsMinimum(2);
return inequalities;
}
void ZCone::findImpliedEquations()
{
ensureStateAsMinimum(1);
}
ZMatrix ZCone::getImpliedEquations()const
{
ensureStateAsMinimum(1);
return equations;
}
ZVector ZCone::getRelativeInteriorPoint()const
{
ensureStateAsMinimum(1);
// assert(state>=1);
return lpSolver.relativeInteriorPoint(inequalities,equations);
}
ZVector ZCone::getUniquePoint()const
{
ZMatrix rays=extremeRays();
ZVector ret(n);
for(int i=0;i<rays.getHeight();i++)
ret+=rays[i];
return ret;
}
ZVector ZCone::getUniquePointFromExtremeRays(ZMatrix const &extremeRays)const
{
ZVector ret(n);
for(int i=0;i<extremeRays.getHeight();i++)
if(contains(extremeRays[i]))ret+=extremeRays[i];
return ret;
}
int ZCone::ambientDimension()const
{
return n;
}
int ZCone::codimension()const
{
return ambientDimension()-dimension();
}
int ZCone::dimension()const
{
// assert(state>=1);
ensureStateAsMinimum(1);
return ambientDimension()-equations.getHeight();
}
int ZCone::dimensionOfLinealitySpace()const
{
ZMatrix temp=inequalities;
temp.append(equations);
ZCone temp2(ZMatrix(0,n),temp);
return temp2.dimension();
}
bool ZCone::isOrigin()const
{
return dimension()==0;
}
bool ZCone::isFullSpace()const
{
for(int i=0;i<inequalities.getHeight();i++)
if(!inequalities[i].isZero())return false;
for(int i=0;i<equations.getHeight();i++)
if(!equations[i].isZero())return false;
return true;
}
ZCone intersection(const ZCone &a, const ZCone &b)
{
assert(a.ambientDimension()==b.ambientDimension());
ZMatrix inequalities=a.inequalities;
inequalities.append(b.inequalities);
ZMatrix equations=a.equations;
equations.append(b.equations);
equations.sortAndRemoveDuplicateRows();
inequalities.sortAndRemoveDuplicateRows();
{
ZMatrix Aequations=a.equations;
ZMatrix Ainequalities=a.inequalities;
Aequations.sortAndRemoveDuplicateRows();
Ainequalities.sortAndRemoveDuplicateRows();
if((Ainequalities.getHeight()==inequalities.getHeight()) && (Aequations.getHeight()==equations.getHeight()))return a;
ZMatrix Bequations=b.equations;
ZMatrix Binequalities=b.inequalities;
Bequations.sortAndRemoveDuplicateRows();
Binequalities.sortAndRemoveDuplicateRows();
if((Binequalities.getHeight()==inequalities.getHeight()) && (Bequations.getHeight()==equations.getHeight()))return b;
}
return ZCone(inequalities,equations);
}
/*
PolyhedralCone product(const PolyhedralCone &a, const PolyhedralCone &b)
{
IntegerVectorList equations2;
IntegerVectorList inequalities2;
int n1=a.n;
int n2=b.n;
for(IntegerVectorList::const_iterator i=a.equations.begin();i!=a.equations.end();i++)
equations2.push_back(concatenation(*i,IntegerVector(n2)));
for(IntegerVectorList::const_iterator i=b.equations.begin();i!=b.equations.end();i++)
equations2.push_back(concatenation(IntegerVector(n1),*i));
for(IntegerVectorList::const_iterator i=a.inequalities.begin();i!=a.inequalities.end();i++)
inequalities2.push_back(concatenation(*i,IntegerVector(n2)));
for(IntegerVectorList::const_iterator i=b.inequalities.begin();i!=b.inequalities.end();i++)
inequalities2.push_back(concatenation(IntegerVector(n1),*i));
PolyhedralCone ret(inequalities2,equations2,n1+n2);
ret.setMultiplicity(a.getMultiplicity()*b.getMultiplicity());
ret.setLinearForm(concatenation(a.getLinearForm(),b.getLinearForm()));
ret.ensureStateAsMinimum(a.state);
ret.ensureStateAsMinimum(b.state);
return ret;
}*/
ZCone ZCone::positiveOrthant(int dimension)
{
return ZCone(ZMatrix::identity(dimension),ZMatrix(0,dimension));
}
ZCone ZCone::givenByRays(ZMatrix const &generators, ZMatrix const &linealitySpace)
{
//rewrite modulo lineality space
ZMatrix newGenerators(generators.getHeight(),generators.getWidth());
{
QMatrix l=ZToQMatrix(linealitySpace);
l.reduce();
for(int i=0;i<generators.getHeight();i++)
newGenerators[i]=QToZVectorPrimitive(l.canonicalize(ZToQVector(generators[i])));
}
ZCone dual(newGenerators,linealitySpace);
dual.findFacets();
dual.canonicalize();
ZMatrix inequalities=dual.extremeRays();
ZMatrix span=generators;
span.append(linealitySpace);
QMatrix m2Q=ZToQMatrix(span);
ZMatrix equations=QToZMatrixPrimitive(m2Q.reduceAndComputeKernel());
return ZCone(inequalities,equations);
}
bool ZCone::containsPositiveVector()const
{
ZCone temp=intersection(*this,ZCone::positiveOrthant(n));
return temp.getRelativeInteriorPoint().isPositive();
}
bool ZCone::contains(ZVector const &v)const
{
for(int i=0;i<equations.getHeight();i++)
{
if(!dot(equations[i],v).isZero())return false;
}
for(int i=0;i<inequalities.getHeight();i++)
{
if(dot(inequalities[i],v).sign()<0)return false;
}
return true;
}
bool ZCone::containsRowsOf(ZMatrix const &m)const
{
for(int i=0;i<m.getHeight();i++)
if(!contains(m[i]))return false;
return true;
}
bool ZCone::contains(ZCone const &c)const
{
ZCone c2=intersection(*this,c);
ZCone c3=c;
c2.canonicalize();
c3.canonicalize();
return !(c2!=c3);
}
bool ZCone::containsRelatively(ZVector const &v)const
{
ensureStateAsMinimum(1);
// assert(state>=1);
for(int i=0;i<equations.getHeight();i++)
{
if(!dot(equations[i],v).isZero())return false;
}
for(int i=0;i<inequalities.getHeight();i++)
{
if(dot(inequalities[i],v).sign()<=0)return false;
}
return true;
}
bool ZCone::isSimplicial()const
{
// assert(state>=2);
ensureStateAsMinimum(2);
return codimension()+inequalities.getHeight()+dimensionOfLinealitySpace()==n;
}
ZCone ZCone::linealitySpace()const
{
ZCone ret(ZMatrix(0,n),combineOnTop(equations,inequalities));
// ret.ensureStateAsMinimum(state);
return ret;
}
ZCone ZCone::dualCone()const
{
ensureStateAsMinimum(1);
// assert(state>=1);
ZMatrix dualInequalities,dualEquations;
lpSolver.dual(inequalities,equations,dualInequalities,dualEquations);
ZCone ret(dualInequalities,dualEquations);
ret.ensureStateAsMinimum(state);
return ret;
}
ZCone ZCone::negated()const
{
ZCone ret(-inequalities,equations,(areFacetsKnown()?PCP_facetsKnown:0)|(areImpliedEquationsKnown()?PCP_impliedEquationsKnown:0));
// ret.ensureStateAsMinimum(state);
return ret;
}
ZMatrix ZCone::extremeRays(ZMatrix const *generatorsOfLinealitySpace)const
{
// assert((dimension()==ambientDimension()) || (state>=3));
if(dimension()!=ambientDimension())
ensureStateAsMinimum(3);
if(haveExtremeRaysBeenCached)return cachedExtremeRays;
ZMatrix ret(0,n);
std::vector<std::vector<int> > indices=lpSolver.extremeRaysInequalityIndices(inequalities);
for(int i=0;i<indices.size();i++)
{
/* At this point we know lineality space, implied equations and
also inequalities for the ray. To construct a vector on the
ray which is stable under (or indendent of) angle and
linarity preserving transformation we find the dimension 1
subspace orthorgonal to the implied equations and the
lineality space and pick a suitable primitive generator */
/* To be more precise,
* let E be the set of equations, and v the inequality defining a ray R.
* We wish to find a vector satisfying these, but it must also be orthogonal
* to the lineality space of the cone, that is, in the span of {E,v}.
* One way to get such a vector is to project v to E an get a vector p.
* Then v-p is in the span of {E,v} by construction.
* The vector v-p is also in the orthogonal complement to E by construction,
* that is, the span of R.
* We wish to argue that it is not zero.
* That would imply that v=p, meaning that v is in the span of the equations.
* However, that would contradict that R is a ray.
* In case v-p does not satisfy the inequality v (is this possible?), we change the sign.
*
* As a consequence we need the following procedure
* primitiveProjection():
* Input: E,v
* Output: A primitive representation of the vector v-p, where p is the projection of v onto E
*
* Notice that the output is a Q linear combination of the input and that p is
* a linear combination of E. The check that p has been computed correctly,
* it suffices to check that v-p satisfies the equations E.
* The routine will actually first compute a multiple of v-p.
* It will do this using floating point arithmetics. It will then transform
* the coefficients to get the multiple of v-p into integers. Then it
* verifies in exact arithmetics, that with these coefficients we get a point
* satisfying E. It then returns the primitive vector on the ray v-p.
* In case of a failure it falls back to an implementation using rational arithmetics.
*/
std::vector<int> asVector(inequalities.getHeight());
for(int j=0;j<indices[i].size();j++){asVector[indices[i][j]]=1;}
ZMatrix equations=this->equations;
ZVector theInequality;
for(int j=0;j<asVector.size();j++)
if(asVector[j])
equations.appendRow(inequalities[j]);
else
theInequality=inequalities[j];
assert(!theInequality.isZero());
ZVector thePrimitiveVector;
if(generatorsOfLinealitySpace)
{
QMatrix temp=ZToQMatrix(combineOnTop(equations,*generatorsOfLinealitySpace));
thePrimitiveVector=QToZVectorPrimitive(temp.reduceAndComputeVectorInKernel());
}
else
{
QMatrix linealitySpaceOrth=ZToQMatrix(combineOnTop(this->equations,inequalities));
QMatrix temp=combineOnTop(linealitySpaceOrth.reduceAndComputeKernel(),ZToQMatrix(equations));
thePrimitiveVector=QToZVectorPrimitive(temp.reduceAndComputeVectorInKernel());
}
if(!contains(thePrimitiveVector))thePrimitiveVector=-thePrimitiveVector;
ret.appendRow(thePrimitiveVector);
}
cachedExtremeRays=ret;
haveExtremeRaysBeenCached=true;
return ret;
}
Integer ZCone::getMultiplicity()const
{
return multiplicity;
}
void ZCone::setMultiplicity(Integer const &m)
{
multiplicity=m;
}
ZMatrix ZCone::getLinearForms()const
{
return linearForms;
}
void ZCone::setLinearForms(ZMatrix const &linearForms_)
{
linearForms=linearForms_;
}
ZMatrix ZCone::quotientLatticeBasis()const
{
// assert(isInStateMinimum(1));// Implied equations must have been computed in order to know the span of the cone
ensureStateAsMinimum(1);
int a=equations.getHeight();
int b=inequalities.getHeight();
// Implementation below could be moved to nonLP part of code.
// small vector space defined by a+b equations.... big by a equations.
ZMatrix M=combineLeftRight(combineLeftRight(
equations.transposed(),
inequalities.transposed()),
ZMatrix::identity(n));
M.reduce(false,true);
/*
[A|B|I] is reduced to [A'|B'|C'] meaning [A'|B']=C'[A|B] and A'=C'A.
[A'|B'] is in row echelon form, implying that the rows of C' corresponding to zero rows
of [A'|B'] generate the lattice cokernel of [A|B] - that is the linealityspace intersected with Z^n.
[A'] is in row echelon form, implying that the rows of C' corresponding to zero rows of [A'] generate
the lattice cokernel of [A] - that is the span of the cone intersected with Z^n.
It is clear that the second row set is a superset of the first. Their difference is a basis for the quotient.
*/
ZMatrix ret(0,n);
for(int i=0;i<M.getHeight();i++)
if(M[i].subvector(0,a).isZero()&&!M[i].subvector(a,a+b).isZero())
{
ret.appendRow(M[i].subvector(a+b,a+b+n));
}
return ret;
}
ZVector ZCone::semiGroupGeneratorOfRay()const
{
ZMatrix temp=quotientLatticeBasis();
assert(temp.getHeight()==1);
for(int i=0;i<inequalities.getHeight();i++)
if(dot(temp[0],inequalities[i]).sign()<0)
{
temp[0]=-temp[0];
break;
}
return temp[0];
}
ZCone ZCone::link(ZVector const &w)const
{
/* Observe that the inequalities giving rise to facets
* also give facets in the link, if they are kept as
* inequalities. This means that the state cannot decrease
* when taking links - that is why we specify the PCP flags.
*/
ZMatrix inequalities2(0,n);
for(int j=0;j<inequalities.getHeight();j++)
if(dot(w,inequalities[j]).sign()==0)inequalities2.appendRow(inequalities[j]);
ZCone C(inequalities2,equations,(areImpliedEquationsKnown()?PCP_impliedEquationsKnown:0)|(areFacetsKnown()?PCP_facetsKnown:0));
C.ensureStateAsMinimum(state);
C.setLinearForms(getLinearForms());
C.setMultiplicity(getMultiplicity());
return C;
}
ZCone ZCone::faceContaining(ZVector const &v)const
{
assert(n==v.size());
assert(contains(v));
ZMatrix newEquations=equations;
ZMatrix newInequalities(0,n);
for(int i=0;i<inequalities.getHeight();i++)
if(dot(inequalities[i],v).sign()!=0)
newInequalities.appendRow(inequalities[i]);
else
newEquations.appendRow(inequalities[i]);
ZCone ret(newInequalities,newEquations,(state>=1)?PCP_impliedEquationsKnown:0);
ret.ensureStateAsMinimum(state);
return ret;
}
ZMatrix ZCone::getInequalities()const
{
return inequalities;
}
ZMatrix ZCone::getEquations()const
{
return equations;
}
ZMatrix ZCone::generatorsOfSpan()const
{
ensureStateAsMinimum(1);
QMatrix l=ZToQMatrix(equations);
return QToZMatrixPrimitive(l.reduceAndComputeKernel());
}
ZMatrix ZCone::generatorsOfLinealitySpace()const
{
QMatrix l=ZToQMatrix(combineOnTop(inequalities,equations));
return QToZMatrixPrimitive(l.reduceAndComputeKernel());
}
};
|