use of org.tweetyproject.math.term.IntegerVariable in project TweetyProject by TweetyProjectTeam.
the class ApacheCommonsSimplexEx method createConstraintSatProb1.
/**
* constructor
* @return the problem
*/
public static ConstraintSatisfactionProblem createConstraintSatProb1() {
// Define the constraints (all are equations)
IntegerVariable m1 = new IntegerVariable("Maschine 1");
IntegerVariable m2 = new IntegerVariable("Maschine 2");
Inequation constr1 = new Inequation(m1, new IntegerConstant(10), 1);
Inequation constr2 = new Inequation(m2, new IntegerConstant(12), 1);
Inequation constr3 = new Inequation(m1, new IntegerConstant(0), 3);
Inequation constr4 = new Inequation(m2, new IntegerConstant(0), 3);
Inequation constr5 = new Inequation(m1.add(m2), new IntegerConstant(16), 0);
Collection<Statement> constraints = new ArrayList<Statement>();
constraints.add(constr1);
constraints.add(constr2);
constraints.add(constr3);
constraints.add(constr4);
constraints.add(constr5);
OptimizationProblem prob = new OptimizationProblem(1);
prob.addAll(constraints);
// Define targetfunction
Term opt = new Sum(new Sum(m1, new FloatConstant(1)), m2);
prob.setTargetFunction(opt);
return prob;
}
use of org.tweetyproject.math.term.IntegerVariable in project TweetyProject by TweetyProjectTeam.
the class LpSolve method solve.
/* (non-Javadoc)
* @see org.tweetyproject.math.opt.Solver#solve()
*/
@Override
public Map<Variable, Term> solve(GeneralConstraintSatisfactionProblem problem) {
if (!((ConstraintSatisfactionProblem) problem).isLinear())
throw new IllegalArgumentException("The solver \"lpsolve\" needs linear optimization problems.");
// check existence of lp_solve first
if (!LpSolve.isInstalled()) {
System.out.println("LpSolve is not installed \n");
return null;
}
String output = new String();
try {
File lpFile = File.createTempFile("lptmp", null);
// File lpFile = new File("lptmp2");
// Delete temp file when program exits.
// lpFile.deleteOnExit();
// Write to temp file
BufferedWriter out = new BufferedWriter(new FileWriter(lpFile));
out.write(((OptimizationProblem) problem).convertToLpFormat());
out.close();
// execute lp_solve on problem in lp format and retrieve console output
output = NativeShell.invokeExecutable(LpSolve.binary + " " + lpFile.getAbsolutePath());
// lpFile.delete();
} catch (IOException e) {
// TODO add error handling
e.printStackTrace();
return null;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
// parse output
String delimiter = "Actual values of the variables:";
String assignments = output.substring(output.indexOf(delimiter) + delimiter.length(), output.length());
StringTokenizer tokenizer = new StringTokenizer(assignments, "\n");
Set<Variable> variables = ((ConstraintSatisfactionProblem) problem).getVariables();
Map<Variable, Term> result = new HashMap<Variable, Term>();
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
StringTokenizer subTokenizer = new StringTokenizer(token, " ");
String varName = subTokenizer.nextToken().trim();
String varValue = subTokenizer.nextToken().trim();
Variable v;
Constant c;
try {
c = new IntegerConstant(Integer.valueOf(varValue));
v = new IntegerVariable(varName);
} catch (NumberFormatException e) {
c = new FloatConstant(Float.valueOf(varValue));
v = new FloatVariable(varName);
}
if (!variables.contains(v)) {
// Check on: v might be a float variable that has been assigned an integer value
v = new FloatVariable(v.getName());
if (!variables.contains(v))
throw new IllegalStateException("Something is terribly wrong: optimization problem contains variables it is not supposed to contain.");
}
variables.remove(v);
result.put(v, c);
}
if (!variables.isEmpty())
throw new IllegalStateException("Not every variable has been assigned a value. This shouldn't happen.");
return result;
}
use of org.tweetyproject.math.term.IntegerVariable in project TweetyProject by TweetyProjectTeam.
the class TestToQuadraticForm method main.
/**
* main method
* @param args arguments
*/
public static void main(String[] args) {
IntegerVariable m1 = new IntegerVariable("Maschine_A");
ArrayList<Term> opts = new ArrayList<Term>();
opts.add(new Product(new Sum(m1, new FloatConstant(4)), new FloatConstant(2)));
opts.add(new Product(new Sum(m1, new FloatConstant(4)), new Sum(m1, new FloatConstant(4))));
opts.add(new Product(new Product(m1, new FloatConstant(4)), new Product(m1, new FloatConstant(4))));
opts.add(new Product(new Product(m1, new FloatConstant(4)), new Sum(m1, new FloatConstant(4))));
opts.add(new Product(new Sum(m1, new FloatConstant(4)), new Product(m1, new FloatConstant(4))));
opts.add(new Sum(new Sum(m1, new FloatConstant(4)), new Sum(m1, new FloatConstant(4))));
opts.add(new Sum(new Product(m1, new FloatConstant(4)), new Product(m1, new FloatConstant(4))));
opts.add(new Sum(new Product(m1, new FloatConstant(4)), new Sum(m1, new FloatConstant(4))));
opts.add(new Sum(new Sum(m1, new FloatConstant(4)), new Product(m1, new FloatConstant(4))));
// Term opt = new Sum(new Product(m2, new IntegerConstant(3)), new Sum(m1, new IntegerConstant(3)));
for (Term t : opts) {
System.out.println(t.toQuadraticForm().simplify().toString() + "\n");
// System.out.println(t.getSums().toString());
}
// for(Term t : opts) {
// ArrayList<Sum> sums = new ArrayList<Sum>();
// sums.addAll(t.toQuadraticForm().getSums());
// for(Sum s : sums)
// System.out.println(s.toString());
// System.out.print("\n");
//
// }
//
}
use of org.tweetyproject.math.term.IntegerVariable in project TweetyProject by TweetyProjectTeam.
the class LagrangeSolverEx method createConstraintSatProb1.
/**
* constructor
* @return problem
*/
public static ConstraintSatisfactionProblem createConstraintSatProb1() {
// Define the constraints (all are equations)
IntegerVariable m1 = new IntegerVariable("Maschine 1");
IntegerVariable m2 = new IntegerVariable("Maschine 2");
Equation constr1 = new Equation(m1, new IntegerConstant(10));
Equation constr2 = new Equation(m2, new IntegerConstant(12));
Equation constr3 = new Equation(m1, new IntegerConstant(0));
Equation constr4 = new Equation(m2, new IntegerConstant(0));
Equation constr5 = new Equation(m1.add(m2), new IntegerConstant(16));
Collection<Statement> constraints = new ArrayList<Statement>();
constraints.add(constr1);
constraints.add(constr2);
constraints.add(constr3);
constraints.add(constr4);
constraints.add(constr5);
OptimizationProblem prob = new OptimizationProblem(0);
prob.addAll(constraints);
// Define targetfunction
Term opt = new Sum(new Power(new Sum(m1, new FloatConstant(1)), new IntegerConstant(2)), new Power(m2, new IntegerConstant(2)));
prob.setTargetFunction(opt);
return prob;
}
use of org.tweetyproject.math.term.IntegerVariable in project TweetyProject by TweetyProjectTeam.
the class LagrangeSolverEx2 method createConstraintSatProb1.
/**
* constructor
* @return problem
*/
public static ConstraintSatisfactionProblem createConstraintSatProb1() {
// Define the constraints (all are equations)
IntegerVariable m1 = new IntegerVariable("Maschine 1");
IntegerVariable m2 = new IntegerVariable("Maschine 2");
Equation constr1 = new Equation(m1, new IntegerConstant(10));
Equation constr2 = new Equation(m2, new IntegerConstant(12));
Equation constr3 = new Equation(m1, new IntegerConstant(0));
Equation constr4 = new Equation(m2, new IntegerConstant(0));
Equation constr5 = new Equation(m1.add(m2), new IntegerConstant(16));
Collection<Statement> constraints = new ArrayList<Statement>();
constraints.add(constr1);
constraints.add(constr2);
constraints.add(constr3);
constraints.add(constr4);
constraints.add(constr5);
OptimizationProblem prob = new OptimizationProblem(0);
prob.addAll(constraints);
// Define targetfunction
Term opt = new Sum(new Power(new Sum(m1, new FloatConstant(1)), new IntegerConstant(2)), new Power(m2, new IntegerConstant(2)));
prob.setTargetFunction(opt);
return prob;
}
Aggregations