Search in sources :

Example 1 with IntegerVariable

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;
}
Also used : IntegerVariable(org.tweetyproject.math.term.IntegerVariable) Inequation(org.tweetyproject.math.equation.Inequation) Statement(org.tweetyproject.math.equation.Statement) FloatConstant(org.tweetyproject.math.term.FloatConstant) ArrayList(java.util.ArrayList) OptimizationProblem(org.tweetyproject.math.opt.problem.OptimizationProblem) Sum(org.tweetyproject.math.term.Sum) Term(org.tweetyproject.math.term.Term) IntegerConstant(org.tweetyproject.math.term.IntegerConstant)

Example 2 with IntegerVariable

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;
}
Also used : IntegerVariable(org.tweetyproject.math.term.IntegerVariable) Variable(org.tweetyproject.math.term.Variable) FloatVariable(org.tweetyproject.math.term.FloatVariable) ConstraintSatisfactionProblem(org.tweetyproject.math.opt.problem.ConstraintSatisfactionProblem) GeneralConstraintSatisfactionProblem(org.tweetyproject.math.opt.problem.GeneralConstraintSatisfactionProblem) HashMap(java.util.HashMap) IntegerConstant(org.tweetyproject.math.term.IntegerConstant) Constant(org.tweetyproject.math.term.Constant) FloatConstant(org.tweetyproject.math.term.FloatConstant) FileWriter(java.io.FileWriter) FloatConstant(org.tweetyproject.math.term.FloatConstant) IOException(java.io.IOException) Term(org.tweetyproject.math.term.Term) IntegerConstant(org.tweetyproject.math.term.IntegerConstant) BufferedWriter(java.io.BufferedWriter) StringTokenizer(java.util.StringTokenizer) IntegerVariable(org.tweetyproject.math.term.IntegerVariable) FloatVariable(org.tweetyproject.math.term.FloatVariable) File(java.io.File)

Example 3 with IntegerVariable

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");
// 
// }
// 
}
Also used : IntegerVariable(org.tweetyproject.math.term.IntegerVariable) FloatConstant(org.tweetyproject.math.term.FloatConstant) ArrayList(java.util.ArrayList) Sum(org.tweetyproject.math.term.Sum) Term(org.tweetyproject.math.term.Term)

Example 4 with IntegerVariable

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;
}
Also used : IntegerVariable(org.tweetyproject.math.term.IntegerVariable) Statement(org.tweetyproject.math.equation.Statement) FloatConstant(org.tweetyproject.math.term.FloatConstant) ArrayList(java.util.ArrayList) Equation(org.tweetyproject.math.equation.Equation) OptimizationProblem(org.tweetyproject.math.opt.problem.OptimizationProblem) Sum(org.tweetyproject.math.term.Sum) Term(org.tweetyproject.math.term.Term) IntegerConstant(org.tweetyproject.math.term.IntegerConstant) Power(org.tweetyproject.math.term.Power)

Example 5 with IntegerVariable

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;
}
Also used : IntegerVariable(org.tweetyproject.math.term.IntegerVariable) Statement(org.tweetyproject.math.equation.Statement) FloatConstant(org.tweetyproject.math.term.FloatConstant) ArrayList(java.util.ArrayList) Equation(org.tweetyproject.math.equation.Equation) OptimizationProblem(org.tweetyproject.math.opt.problem.OptimizationProblem) Sum(org.tweetyproject.math.term.Sum) Term(org.tweetyproject.math.term.Term) IntegerConstant(org.tweetyproject.math.term.IntegerConstant) Power(org.tweetyproject.math.term.Power)

Aggregations

FloatConstant (org.tweetyproject.math.term.FloatConstant)5 IntegerVariable (org.tweetyproject.math.term.IntegerVariable)5 Term (org.tweetyproject.math.term.Term)5 ArrayList (java.util.ArrayList)4 IntegerConstant (org.tweetyproject.math.term.IntegerConstant)4 Sum (org.tweetyproject.math.term.Sum)4 Statement (org.tweetyproject.math.equation.Statement)3 OptimizationProblem (org.tweetyproject.math.opt.problem.OptimizationProblem)3 Equation (org.tweetyproject.math.equation.Equation)2 Power (org.tweetyproject.math.term.Power)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 StringTokenizer (java.util.StringTokenizer)1 Inequation (org.tweetyproject.math.equation.Inequation)1 ConstraintSatisfactionProblem (org.tweetyproject.math.opt.problem.ConstraintSatisfactionProblem)1 GeneralConstraintSatisfactionProblem (org.tweetyproject.math.opt.problem.GeneralConstraintSatisfactionProblem)1 Constant (org.tweetyproject.math.term.Constant)1