Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2014 JMPI - Java Mathematical Programming Interface Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 package net.sf.jmpi.solver.cplex;
 
 import  ilog.concert.IloConstraint;
 import  ilog.concert.IloException;
 import  ilog.concert.IloNumExpr;
 import  ilog.concert.IloNumVar;
 import  ilog.concert.IloNumVarType;
 import  ilog.concert.IloObjectiveSense;
 import  ilog.cplex.IloCplex;
 import  ilog.cplex.IloCplex.DoubleParam;
 
 import java.util.List;
 
 
 public class SolverCPLEX extends AbstractMpSolver<IloNumVar, IloConstraint> implements MpSolver {
 
 	int constraintId = 0;
 	protected final IloCplex cplex;
 
 	public SolverCPLEX() {
 		super();
 		try {
 			 = new IloCplex();
 			.addMinimize();
 		} catch (IloException e) {
 			throw new RuntimeException(e);
 		}
 	}
 
 	protected IloNumVar addVariable(MpVariable variable) {
 		IloNumVarType type;
 		switch (variable.getType()) {
 		case :
 			type = IloNumVarType.Bool;
 			break;
 		case :
 			type = IloNumVarType.Int;
 			break;
 		default// REAL
 			type = IloNumVarType.Float;
 			break;
 		}
 
 		try {
 			double lower = variable.getLower().doubleValue();
 			double upper = variable.getUpper().doubleValue();
 
 			IloNumVar var = .numVar(loweruppertypevariable.getVar().toString());
 
 			return var;
 		} catch (IloException e) {
 			e.printStackTrace();
 			return null;
 		}
 	}
 
 	public double getValue(Object object) {
 		IloNumVar var = .get(object);
 
 		try {
 			double value = .getValue(var);
 			if (.get(.get(object)).getType() != ..) {
 				value = Math.round(value);
 			}
 			return value;
 		} catch (IloException e) {
			..println(object + " " + var);
			throw new RuntimeException(e);
		}
	}
	public boolean getBoolean(Object object) {
		return getValue(object) > 0;
	}
	public MpResult solve() {
		try {
			boolean ret = .solve();
			if (!ret) {
				return null;
			}
			double objValue = 0;
			if (.size() > 0) {
				objValue = .getObjValue();
			}
			MpResult result = new MpResultImpl(objValue);
			for (Object var : .keySet()) {
				double primalValue = getValue(var);
				result.put(varprimalValue);
			}
			return result;
catch (IloException e) {
			throw new RuntimeException(e);
		}
	}
	public void setTimeout(int value) {
		try {
			.setParam(DoubleParam.TiLim, value);
catch (IloException e) {
			e.printStackTrace();
		}
	}
	public void setVerbose(int value) {
		if (value == 0) {
			.setOut(null);
else {
			.setOut(.);
		}
	}
	public void setObjective(MpExpr expressionMpDirection type) {
		try {
			if (type == .) {
				.getObjective().setSense(IloObjectiveSense.Minimize);
else {
				.getObjective().setSense(IloObjectiveSense.Maximize);
			}
			.getObjective().clearExpr();
			.getObjective().setExpr(toExpression(expression));
catch (IloException e) {
			e.printStackTrace();
		}
		 = expression;
	}
	// assume its normalized
	protected IloConstraint addConstraint(MpConstraint constraint) {
		IloNumExpr lhsExpr = toExpression(constraint.getLhs());
		IloNumExpr rhsExpr = toExpression(constraint.getRhs());
		try {
			MpOperator op = constraint.getOperator();
			IloConstraint con = null;
			switch (op) {
			case :
				con = .addGe(lhsExprrhsExpr"c" + (++));
				break;
			case :
				con = .addLe(lhsExprrhsExpr"c" + (++));
				break;
			case :
				con = .addEq(lhsExprrhsExpr"c" + (++));
				break;
			default:
				throw new RuntimeException("Invalid operator " + op);
			}
			return con;
catch (IloException e) {
			throw new RuntimeException(e);
		}
	}
	// assume its normalized
	@SuppressWarnings("unchecked")
	protected IloNumExpr toExpression(MpExpr expression) {
		try {
			List<IloNumExpr> expr = new ArrayList<IloNumExpr>();
			for (MpExprTerm term : expression) {
				double coeff = term.getCoeff().doubleValue();
				switch (term.size()) {
				case 0:
					expr.add(.constant(coeff));
					break;
				case 1:
					expr.add(.prod(coeffgetVar(term.var(0))));
					break;
				case 2:
					expr.add(.prod(coeffgetVar(term.var(0)), getVar(term.var(1))));
					break;
				default:
					throw new IllegalArgumentException("non linear: " + expr);
				}
			}
			IloNumExpr[] array = expr.toArray(new IloNumExpr[0]);
			return .sum(array);
catch (IloException e) {
			throw new RuntimeException(e);
		}
	}
New to GrepCode? Check out our FAQ X