Skip to content

Commit 2a5fa9d

Browse files
author
matthias.thimm
committed
added customizable options for constructor.
1 parent bcca2d5 commit 2a5fa9d

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

org-tweetyproject-math/src/main/java/org/tweetyproject/math/opt/solver/ApacheCommonsSimplex.java

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,51 @@
5454
* @author Matthias Thimm
5555
*/
5656
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+
5773
/** Constructor */
5874
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);
5983
}
6084

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+
6196

6297
/**
6398
* Logger.
6499
*/
65100
//private Logger log = LoggerFactory.getLogger(ApacheCommonsSimplex.class);
66101

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;
76102

77103
/* (non-Javadoc)
78104
* @see org.tweetyproject.math.opt.Solver#solve()
@@ -156,13 +182,13 @@ public Map<Variable, Term> solve(GeneralConstraintSatisfactionProblem problem) {
156182
// 6.) Optimize.
157183
try{
158184
//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);
161187
RealPointValuePair r = null;
162188
if(problem instanceof OptimizationProblem){
163189
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);
166192
//this.log.info("Parsing output from the Apache Commons Simplex algorithm.");
167193
Map<Variable, Term> result = new HashMap<Variable, Term>();
168194
for(Variable v: origVars2Idx.keySet())

0 commit comments

Comments
 (0)