|
54 | 54 | * @author Matthias Thimm |
55 | 55 | */ |
56 | 56 | public class ApacheCommonsSimplex extends Solver { |
| 57 | + |
| 58 | + /** |
| 59 | + * The maximum number of iterations of the simplex algorithm. |
| 60 | + */ |
| 61 | + private int maxiterations; |
| 62 | + |
| 63 | + /** |
| 64 | + * The precision |
| 65 | + */ |
| 66 | + private double precision; |
| 67 | + |
| 68 | + /** |
| 69 | + * Whether only positive solutions are allowed. |
| 70 | + */ |
| 71 | + private boolean restrictToNonNegative; |
| 72 | + |
57 | 73 | /** Constructor */ |
58 | 74 | public ApacheCommonsSimplex() { |
| 75 | + this(0.01); |
| 76 | + } |
| 77 | + |
| 78 | + /** Constructor |
| 79 | + * @param precision The precision |
| 80 | + */ |
| 81 | + public ApacheCommonsSimplex(double precision) { |
| 82 | + this(50000,precision,false); |
59 | 83 | } |
60 | 84 |
|
| 85 | + /** Constructor |
| 86 | + * @param maxiterations The maximum number of iterations of the simplex algorithm. |
| 87 | + * @param precision The precision |
| 88 | + * @param restrictToNonNegative Whether only positive solutions are allowed. |
| 89 | + */ |
| 90 | + public ApacheCommonsSimplex(int maxiterations, double precision, boolean restrictToNonNegative) { |
| 91 | + this.maxiterations = maxiterations; |
| 92 | + this.precision = precision; |
| 93 | + this.restrictToNonNegative = restrictToNonNegative; |
| 94 | + } |
| 95 | + |
61 | 96 |
|
62 | 97 | /** |
63 | 98 | * Logger. |
64 | 99 | */ |
65 | 100 | //private Logger log = LoggerFactory.getLogger(ApacheCommonsSimplex.class); |
66 | 101 |
|
67 | | - /** |
68 | | - * The maximum number of iterations of the simplex algorithm. |
69 | | - */ |
70 | | - public int MAXITERATIONS = 50000; |
71 | | - |
72 | | - /** |
73 | | - * Whether only positive solutions are allowed. |
74 | | - */ |
75 | | - public boolean onlyPositive = false; |
76 | 102 |
|
77 | 103 | /* (non-Javadoc) |
78 | 104 | * @see org.tweetyproject.math.opt.Solver#solve() |
@@ -156,13 +182,13 @@ public Map<Variable, Term> solve(GeneralConstraintSatisfactionProblem problem) { |
156 | 182 | // 6.) Optimize. |
157 | 183 | try{ |
158 | 184 | //this.log.info("Calling the Apache Commons Simplex algorithm."); |
159 | | - SimplexSolver solver = new SimplexSolver(0.01); |
160 | | - solver.setMaxIterations(this.MAXITERATIONS); |
| 185 | + SimplexSolver solver = new SimplexSolver(this.precision); |
| 186 | + solver.setMaxIterations(this.maxiterations); |
161 | 187 | RealPointValuePair r = null; |
162 | 188 | if(problem instanceof OptimizationProblem){ |
163 | 189 | int type = ((OptimizationProblem)problem).getType(); |
164 | | - r = solver.optimize(target, finalConstraints, (type == OptimizationProblem.MINIMIZE)?(GoalType.MINIMIZE):(GoalType.MAXIMIZE), this.onlyPositive); |
165 | | - }else r = solver.optimize(target, finalConstraints, GoalType.MINIMIZE, this.onlyPositive); |
| 190 | + r = solver.optimize(target, finalConstraints, (type == OptimizationProblem.MINIMIZE)?(GoalType.MINIMIZE):(GoalType.MAXIMIZE), this.restrictToNonNegative); |
| 191 | + }else r = solver.optimize(target, finalConstraints, GoalType.MINIMIZE, this.restrictToNonNegative); |
166 | 192 | //this.log.info("Parsing output from the Apache Commons Simplex algorithm."); |
167 | 193 | Map<Variable, Term> result = new HashMap<Variable, Term>(); |
168 | 194 | for(Variable v: origVars2Idx.keySet()) |
|
0 commit comments