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
|
#include "scarf.h"
#include <vector>
#include <set>
#include <algorithm>
#include "latticeideal.h"
#include "subspace.h"
#include "printer.h"
#include "polyhedralcone.h"
IntegerVectorList neighbours(IntegerMatrix const &A)
{
IntegerVectorList testSet=latticeIdealRevLex(A);
IntegerMatrix T=A.transposed();
IntegerMatrix B=IntegerMatrix::identity(T.getWidth());
B.append(T);
B=B.transposed();
// AsciiPrinter(Stderr).printVectorList(B.getRows());
// AsciiPrinter Q(Stderr);
IntegerVectorList temp;
PolyhedralCone c(temp,B.getRows(),B.getWidth());
// c.canonicalize();
c=c.dualCone();
Subspace U(c.getEquations(),c.ambientDimension());
IntegerVectorList neighbours;
for(IntegerVectorList::const_iterator i=testSet.begin();i!=testSet.end();i++)
{
IntegerVector v=*i;
IntegerVector w=v;
w.grow(c.ambientDimension());
IntegerVector u=U.canonicalizeVector(w).subvector(A.getHeight(),w.size());
neighbours.push_back(u);
// Q.printVector(v);
// Q.printVector(A.vectormultiply(u));
assert(A.vectormultiply(u)==v); //the method depends on the implementation of Subspace
}
return neighbours;
}
bool satisfiesA1(IntegerMatrix const &A)
{
IntegerMatrix T=A.transposed();
IntegerVectorList temp;
PolyhedralCone c(temp,T.getRows(),T.getWidth());
return intersection(PolyhedralCone::positiveOrthant(T.getWidth()),c).containsPositiveVector();
//PolyhedralCone d=intersection(PolyhedralCone::positiveOrthant(T.getWidth()),c);
//AsciiPrinter Q(Stderr);
//d.print(&Q);
//return intersection(PolyhedralCone::positiveOrthant(T.getWidth()),c).containsPositiveVector();
}
bool satisfiesA2(IntegerMatrix const &A)
{
int n=A.getWidth();
int m=A.getHeight();
assert(m>=n);
IntegerVector v(m);
for(int i=0;i<n;i++)v[i]=-1;
do
{
IntegerVectorList l;
IntegerVectorList k=A.transposed().getRows();
for(int i=0;i<m;i++)
if(v[i])
l.push_back(IntegerVector::standardVector(m,i));
else
k.push_back(IntegerVector::standardVector(m,i));
PolyhedralCone c(l,k,m);
if(!c.isZero())return false;
}
while(v.nextPermutation());
return true;
}
bool satisfiesA3(IntegerMatrix const &A, IntegerVectorList const *N)
{
IntegerVectorList N2;
if(!N)
{
N2=neighbours(A);
N=&N2;
}
for(IntegerVectorList::const_iterator i=N->begin();i!=N->end();i++)
for(int j=0;j<A.getHeight();j++)
{
if(dot(A[j],*i)==0)return false;
}
return true;
}
IntegerVectorList orientedNeighbours(IntegerVectorList const& N, IntegerVector const &v)
{
IntegerVectorList ret;
for(IntegerVectorList::const_iterator i=N.begin();i!=N.end();i++)
{
if(dot(v,*i)>0)
ret.push_back(*i);
else
ret.push_back(-*i);
}
return ret;
}
static IntegerVector shift(IntegerMatrix const &N, IntegerVector simplex, IntegerVector const &offset)
{
{
for(int i=0;i<simplex.size();i++)
{
IntegerVector newNeighbour=N[simplex[i]]+offset;
int newIndex=-1;
for(int j=0;j<N.getHeight();j++)
if(N[j]==newNeighbour)newIndex=j;
assert(newIndex!=-1);
simplex[i]=newIndex;
}
}
return simplex;
}
IntegerVector kFlip(IntegerMatrix const &A, IntegerMatrix const &N, IntegerVector simplex, int vertex)
{
int vertex2=-1;
int best=-100000;
for(int i=0;i<simplex.size();i++)
{
if(i!=vertex)
{
int d=dot(N[simplex[i]],A[vertex]);
if(d>best)
{
best=d;
vertex2=i;
}
}
}
assert(vertex2!=-1);
simplex[vertex]=simplex[vertex2];
simplex=shift(N,simplex,-N[simplex[0]]);
best=10000;
int newRow=-1;
for(int i=0;i<N.getHeight();i++)
{
bool inside=true;
for(int j=0;j<simplex.size();j++)
{
if(j!=vertex2)
if(dot(N[i]-N[simplex[j]],A[j])>=0)inside=false;
}
if(inside)
{
int d=dot(N[i],A[vertex2]);
if(d<best)
{
best=d;
newRow=i;
}
}
}
assert(newRow!=-1);
simplex[vertex2]=newRow;
simplex=shift(N,simplex,-N[simplex[0]]);
return simplex;
}
void traverseScarfComplex(IntegerMatrix const &A, IntegerMatrix const &N, IntegerVector simplex)
{
set<IntegerVector> simplices;
IntegerVectorList active;
active.push_back(simplex);
while(!active.empty())
{
IntegerVector s=active.front();
if(simplices.count(s)==0)
{
fprintf(Stderr,"processing:");
AsciiPrinter(Stderr).printVector(s);
simplices.insert(s);
fprintf(Stderr,"\n");
for(int i=0;i<simplex.size();i++)
{
IntegerVector s2=kFlip(A,N,s,i);
active.push_back(s2);
}
}
active.pop_front();
}
}
int growPolytope(IntegerMatrix const &A, IntegerMatrix const &N, IntegerVector simplex, int inequality, int n)
{
int best=10000;
int newRow=-1;
for(int i=0;i<N.getHeight();i++)
{
bool inside=true;
for(int j=0;j<n;j++)
{
if(j!=inequality)
if(dot(N[i]-N[simplex[j]],A[j])>=0)inside=false;
}
/* fprintf(Stderr,"Testing ");
AsciiPrinter(Stderr).printVector(N[i]);
fprintf(Stderr," inside:%i\n",inside);
*/
for(int j=0;j<n;j++)
if(dot(N[i]-N[simplex[j]],A[inequality])<0)inside=false;
// fprintf(Stderr," inside:%i\n",inside);
if(inside)
{
int d=dot(N[i],A[inequality]);
if(d<best)
{
best=d;
newRow=i;
}
}
}
/* fprintf(Stderr,"Best ");
AsciiPrinter(Stderr).printVector(N[newRow]);
fprintf(Stderr,"\n");
*/
assert(newRow!=-1);
return newRow;
}
IntegerVector computeMaximalScarfSimplex(IntegerMatrix const &A, IntegerMatrix const &N)
{
IntegerVector s(A.getHeight());
for(int i=1;i<s.size();i++)
s[i]=growPolytope(A,N,s,i,i);
return s;
}
|