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
|
#include "lp.h"
#include <assert.h>
#include <math.h>
#include <string>
#include "linalg.h"
#include "timer.h"
static Timer lpTimer("LP",100);
static Timer lpTimer2("LP2",100);
static Timer lpTimer3("LP3 - rank of matrix",100);
LpSolver *LpSolver::list;
LpSolver::LpSolver()
{
next=list;
list=this;
}
LpSolver *LpSolver::find(const char *name)
{
LpSolver *l=list;
while(l)
{
if(std::string(l->name())==std::string(name))break;
l=l->next;
}
return l;
}
void LpSolver::printList(FILE *f)
{
fprintf(f,"List of linked LP solvers:\n");
LpSolver *l=list;
while(l)
{
fprintf(f," %s\n",l->name());
l=l->next;
}
}
bool LpSolver::interiorPoint(const IntegerVectorList &g, IntegerVector &result, bool strictlyPositive, IntegerVector const *equalitySet)
{
fprintf(stderr,"interiorPoint method not supported in \"%s\" LP class\n",name());
assert(0);
return false;
}
bool LpSolver::hasInteriorPoint(const IntegerVectorList &g, bool strictlyPositive, IntegerVector const *equalitySet)
{
fprintf(stderr,"hasInteriorPoint method not supported in \"%s\" LP class\n",name());
assert(0);
}
IntegerVectorList::const_iterator LpSolver::shoot(const IntegerVectorList &g)
{
fprintf(stderr,"shoot method not supported in \"%s\" LP class\n",name());
assert(0);
return g.begin();
}
bool LpSolver::positiveVectorInKernel(const IntegerVectorList &g, IntegerVector *result)
{
fprintf(stderr,"positiveVectorInKernel method not supported in \"%s\" LP class\n",name());
assert(0);
return false;
}
int LpSolver::rankOfMatrix(const IntegerVectorList &g)
{
fprintf(stderr,"rankOfMatrix method not supported in \"%s\" LP class\n",name());
assert(0);
return 0;
}
IntegerVectorList LpSolver::extremeRaysInequalityIndices(const IntegerVectorList &inequalityList)
{
fprintf(stderr,"extremeRaysInequalityIndices not supported in \"%s\" LP class\n",name());
assert(0);
return IntegerVectorList();
}
void LpSolver::removeRedundantRows(IntegerVectorList *inequalities, IntegerVectorList *equalities, bool removeInequalityRedundancies)
{
fprintf(stderr,"removeRedundantRows method not supported in \"%s\" LP class\n",name());
assert(0);
}
IntegerVector LpSolver::relativeInteriorPoint(int n, const IntegerVectorList &g, IntegerVector const *equalitySet)
{
fprintf(stderr,"relativeInteriorPoint method not supported in \"%s\" LP class\n",name());
assert(0);
return IntegerVector();
}
void LpSolver::dual(int n, const IntegerVectorList &inequalities, const IntegerVectorList &equations, IntegerVectorList *dualInequalities, IntegerVectorList *dualEquations)
{
fprintf(stderr,"dual method not supported in \"%s\" LP class\n",name());
assert(0);
}
bool LpSolver::hasHomogeneousSolution(int n, const IntegerVectorList &inequalities, const IntegerVectorList &equations)
{
fprintf(stderr,"hasHomogeneousSolution method not supported in \"%s\" LP class\n",name());
assert(0);
}
static LpSolver *soplex,*soplexCddGmp,*huber,*cdd,*cddgmp,*default_;
static bool initialized;
bool lpSetSolver(const char *name)
{
soplexCddGmp=LpSolver::find("SoPlexCddGmp");
soplex=LpSolver::find("SoPlex");
huber=LpSolver::find("Huber's");
cdd=LpSolver::find("cdd");
cddgmp=LpSolver::find("cddgmp");
LpSolver *selected=LpSolver::find(name);
default_=huber;
if(soplex)default_=soplex;
if(cdd)default_=cdd;
if(cddgmp)default_=cddgmp;
if(soplexCddGmp)default_=soplexCddGmp;
if(selected)default_=selected;
initialized=true;
assert(default_);
fprintf(stderr,"LP algorithm being used: \"%s\".\n",default_->name()); //TODO: change to debug printer
//if(default_==soplexCddGmp)fprintf(stderr,"USING SoPlexCddGmp\n");
return selected;
}
static void LpInit()
{
if(!initialized)
{
lpSetSolver("");
}
}
bool isFacet(const IntegerVectorList &g, IntegerVectorList::const_iterator i)
{
TimerScope ts(&lpTimer);
LpInit();
return default_->isFacet(g,i);
}
bool interiorPoint(const IntegerVectorList &g, IntegerVector &result, bool strictlyPositive, IntegerVector const *equalitySet)
{
LpInit();
return default_->interiorPoint(g,result,strictlyPositive,equalitySet);
}
bool hasInteriorPoint(const IntegerVectorList &g, bool strictlyPositive, IntegerVector const *equalitySet)
{
LpInit();
return default_->hasInteriorPoint(g,strictlyPositive, equalitySet);
}
IntegerVectorList::const_iterator shootRay(const IntegerVectorList &g)
{
TimerScope ts(&lpTimer2);
LpInit();
if(g.empty())return g.end();
return default_->shoot(g);
}
bool positiveVectorInKernel(const IntegerVectorList &g, IntegerVector *result)
{
LpInit();
return default_->positiveVectorInKernel(g,result);
}
int rankOfMatrix(const IntegerVectorList &g)
{
TimerScope ts(&lpTimer3);
LpInit();
return default_->rankOfMatrix(g);
}
IntegerVectorList extremeRaysInequalityIndices(const IntegerVectorList &inequalityList)
{
/* If cone is simplicial, then the rays are easy to find...*/
if(rankOfMatrix(inequalityList)==inequalityList.size())
{
IntegerVectorList ret;
int m=inequalityList.size();
for(int i=0;i<m;i++)
{
IntegerVector v(m-1);
for(int j=0;j<i;j++)
v[j]=j;
for(int j=i+1;j<m;j++)
v[j-1]=j;
ret.push_back(v);
}
return ret;
}
LpInit();
return default_->extremeRaysInequalityIndices(inequalityList);
}
void removeRedundantRows(IntegerVectorList *inequalities, IntegerVectorList *equalities, bool removeInequalityRedundancies)
{
LpInit();
return default_->removeRedundantRows(inequalities,equalities,removeInequalityRedundancies);
}
IntegerVector relativeInteriorPoint(int n, const IntegerVectorList &g, IntegerVector const *equalitySet)
{
LpInit();
return default_->relativeInteriorPoint(n,g,equalitySet);
}
void dual(int n, const IntegerVectorList &inequalities, const IntegerVectorList &equations, IntegerVectorList *dualInequalities, IntegerVectorList *dualEquations)
{
LpInit();
return default_->dual(n,inequalities,equations,dualInequalities,dualEquations);
}
bool hasHomogeneousSolution(int n, const IntegerVectorList &inequalities, const IntegerVectorList &equations)
{
LpInit();
for(IntegerVectorList::const_iterator i=inequalities.begin();i!=inequalities.end();i++)
if(i->size()!=n)
{
AsciiPrinter(Stderr) << "Inequality length does not match. n="<<n<<" *i="<<*i<<"\n";
assert(0);
}
for(IntegerVectorList::const_iterator i=equations.begin();i!=equations.end();i++)
if(i->size()!=n)
{
AsciiPrinter(Stderr) << "Equation length does not match. n="<<n<<" *i="<<*i<<"\n";
assert(0);
}
return default_->hasHomogeneousSolution(n, inequalities, equations);
}
bool isInNonNegativeSpan(IntegerVector const &v, IntegerVectorList const &rays, IntegerVectorList const &linealitySpace)
{
int n=v.size();
/*
Solve Ax>=0 with A being:
0| 1 | 000
0| 1 | 000
0| 1| 000
-v| rrr| lll\
-v| aaa| iii \ Added as
-v| yyy| nnn / equations
-v| sss| eee/
*/
FieldMatrix A1=combineLeftRight(combineLeftRight(FieldMatrix(Q,rays.size(),1),FieldMatrix::identity(Q,rays.size())),FieldMatrix(Q,rays.size(),linealitySpace.size()));
FieldMatrix temp=FieldMatrix(Q,1,n);
temp[0]=integerVectorToFieldVector(-v,Q);
FieldMatrix A2=combineLeftRight(combineLeftRight(temp.transposed(),integerMatrixToFieldMatrix(rowsToIntegerMatrix(rays,n),Q).transposed()),
integerMatrixToFieldMatrix(rowsToIntegerMatrix(linealitySpace,n),Q).transposed());
return hasHomogeneousSolution(1+rays.size()+linealitySpace.size(),fieldMatrixToIntegerMatrixPrimitive(A1).getRows(),fieldMatrixToIntegerMatrixPrimitive(A2).getRows());
}
|