List of usage examples for org.apache.commons.math3.optim.linear Relationship EQ
Relationship EQ
To view the source code for org.apache.commons.math3.optim.linear Relationship EQ.
Click Source Link
From source file:com.datumbox.framework.core.mathematics.linearprogramming.LPSolver.java
/** * Solves the LP problem and returns the result. * /*from w w w .ja va 2 s . co m*/ * @param linearObjectiveFunction * @param linearConstraintsList * @param nonNegative * @param maximize * @return */ public static LPResult solve(double[] linearObjectiveFunction, List<LPSolver.LPConstraint> linearConstraintsList, boolean nonNegative, boolean maximize) { int m = linearConstraintsList.size(); List<LinearConstraint> constraints = new ArrayList<>(m); for (LPSolver.LPConstraint constraint : linearConstraintsList) { String sign = constraint.getSign(); Relationship relationship = null; if (LPSolver.GEQ.equals(sign)) { relationship = Relationship.GEQ; } else if (LPSolver.LEQ.equals(sign)) { relationship = Relationship.LEQ; } else if (LPSolver.EQ.equals(sign)) { relationship = Relationship.EQ; } constraints .add(new LinearConstraint(constraint.getContraintBody(), relationship, constraint.getValue())); } SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(new LinearObjectiveFunction(linearObjectiveFunction, 0.0), new LinearConstraintSet(constraints), maximize ? GoalType.MAXIMIZE : GoalType.MINIMIZE, new NonNegativeConstraint(nonNegative), PivotSelectionRule.BLAND); LPResult result = new LPResult(); result.setObjectiveValue(solution.getValue()); result.setVariableValues(solution.getPoint()); return result; }
From source file:edu.mit.isos.app.water.WaterControllerState.java
@Override public void iterateTick(ElementImpl element, long duration) { Set<WaterPlant> plants = new HashSet<WaterPlant>(); Set<WaterPipeline> pipelines = new HashSet<WaterPipeline>(); Set<WaterElementImpl> systems = new HashSet<WaterElementImpl>(); for (ElementImpl e : elements) { if (e instanceof WaterPlant && ((WaterPlant) e).isOperating()) { plants.add((WaterPlant) e);/* w ww .j av a2 s . c om*/ } if (e instanceof WaterPipeline) { pipelines.add((WaterPipeline) e); } if (e instanceof WaterElementImpl) { systems.add((WaterElementImpl) e); } } List<LinearConstraint> constraints = new ArrayList<LinearConstraint>(); double[] costCoefficients = new double[elements.size()]; double[] initialValues = new double[elements.size()]; for (WaterElementImpl e : systems) { // cost of lifting aquifer is 1 costCoefficients[elements.indexOf(e)] = 1; initialValues[elements.indexOf(e)] = ((WaterElementState) e.getState()).getProduced(e, duration) .getQuantity(ResourceType.WATER); } for (WaterPlant e : plants) { initialValues[elements.indexOf(e)] = e.getOperatingState().getProduced(e, duration) .getQuantity(ResourceType.WATER); double[] productionCoefficients = new double[elements.size()]; productionCoefficients[elements.indexOf(e)] = 1; constraints.add(new LinearConstraint(productionCoefficients, Relationship.LEQ, e.getOperatingState().productionCapacity.multiply(duration).getQuantity(ResourceType.WATER))); } for (WaterPipeline e : pipelines) { initialValues[elements.indexOf(e)] = e.getOperatingState().getOutput(e, duration) .getQuantity(ResourceType.WATER); double[] outputCoefficients = new double[elements.size()]; outputCoefficients[elements.indexOf(e)] = 1; constraints.add(new LinearConstraint(outputCoefficients, Relationship.LEQ, e.getOperatingState().outputCapacity.multiply(duration).getQuantity(ResourceType.WATER))); } for (WaterElementImpl e : systems) { double[] flowCoefficients = new double[elements.size()]; flowCoefficients[elements.indexOf(e)] = 1; // system production for (WaterPlant plant : plants) { if (plant.getLocation().equals(e.getLocation())) { flowCoefficients[elements.indexOf(plant)] = 1; // plant production } } for (WaterPipeline pipeline : pipelines) { if (pipeline.getLocation().getOrigin().equals(e.getLocation().getOrigin())) { flowCoefficients[elements.indexOf(pipeline)] = -1 / pipeline.getOperatingState().eta; // pipeline input } else if (pipeline.getLocation().getDestination().equals(e.getLocation().getOrigin())) { flowCoefficients[elements.indexOf(pipeline)] = 1; // pipeline output } } constraints.add(new LinearConstraint(flowCoefficients, Relationship.EQ, ((WaterElementState) e.getState()).getSent(e, duration).getQuantity(ResourceType.WATER))); } try { // Run optimization and get results. PointValuePair output = new SimplexSolver().optimize(GoalType.MINIMIZE, new MaxIter(1000), new NonNegativeConstraint(true), new LinearConstraintSet(constraints), new LinearObjectiveFunction(costCoefficients, 0d), new InitialGuess(initialValues)); for (WaterElementImpl e : systems) { e.getOperatingState().setProduced(e, ResourceFactory.create(ResourceType.WATER, output.getPoint()[elements.indexOf(e)]), duration); } for (WaterPlant e : plants) { e.getOperatingState().setProduced(e, ResourceFactory.create(ResourceType.WATER, output.getPoint()[elements.indexOf(e)]), duration); } for (WaterPipeline e : pipelines) { e.getOperatingState().setOutput(e, ResourceFactory.create(ResourceType.WATER, output.getPoint()[elements.indexOf(e)]), duration); } } catch (TooManyIterationsException ignore) { // Don't overwrite existing values. ignore.printStackTrace(); } catch (NoFeasibleSolutionException ignore) { // Don't overwrite existing values. ignore.printStackTrace(); } for (WaterElementImpl system : systems) { Resource received = system.getOperatingState().getConsumed(system, duration) .get(ResourceType.ELECTRICITY); for (WaterPlant plant : plants) { if (plant.getLocation().equals(system.getLocation())) { received = received.add( plant.getOperatingState().getConsumed(plant, duration).get(ResourceType.ELECTRICITY)); } } for (WaterPipeline pipeline : pipelines) { if (pipeline.getLocation().getOrigin().equals(system.getLocation().getOrigin())) { received = received.add(pipeline.getOperatingState().getInput(pipeline, duration) .get(ResourceType.ELECTRICITY)); } } system.getOperatingState().setReceived(system, received, duration); } }
From source file:org.lightjason.agentspeak.action.buildin.math.linearprogram.IConstraint.java
/** * returns the enum of a relationship by a string value * * @param p_symbol string symbol//from w ww . j a v a 2 s . co m * @return relationship */ protected final Relationship getRelation(final String p_symbol) { switch (p_symbol.trim().toLowerCase(Locale.ROOT)) { case "less": case "less-equal": case "leq": case "<": case "<=": return Relationship.LEQ; case "greater": case "greater-equal": case "geq": case ">": case ">=": return Relationship.GEQ; case "equal": case "eq": case "=": case "==": return Relationship.EQ; default: throw new CIllegalArgumentException(CCommon.languagestring(IConstraint.class, "relation", p_symbol)); } }
From source file:org.lightjason.agentspeak.action.builtin.math.linearprogram.IConstraint.java
/** * returns the enum of a relationship by a string value * * @param p_symbol string symbol//w w w .j a va 2s .c om * @return relationship */ @Nonnull protected final Relationship getRelation(@Nonnull final String p_symbol) { switch (p_symbol.trim().toLowerCase(Locale.ROOT)) { case "less": case "less-equal": case "leq": case "<": case "<=": return Relationship.LEQ; case "greater": case "greater-equal": case "geq": case ">": case ">=": return Relationship.GEQ; case "equal": case "eq": case "=": case "==": return Relationship.EQ; default: throw new CIllegalArgumentException(CCommon.languagestring(IConstraint.class, "relation", p_symbol)); } }
From source file:org.lightjason.agentspeak.action.builtin.TestCActionMathLinearprogram.java
/** * test equation constraint// w w w .ja va 2 s.c om */ @Test public final void equationconstraint() { final LinearConstraint l_result = new LinearConstraint(new double[] { 2, 7, 12 }, 19.0, Relationship.EQ, new double[] { 1, 2, 3 }, 5.0); new CEquationConstraint().execute(false, IContext.EMPTYPLAN, Stream.of(m_linearprogram, 2, 7, 12, 19.0, "=", 1, 2, 3, 5.0).map(CRawTerm::from) .collect(Collectors.toList()), Collections.emptyList()); Assert.assertEquals(m_linearprogram.getRight().size(), 1); Assert.assertArrayEquals( Stream.of(l_result.getValue(), l_result.getRelationship(), l_result.getCoefficients()).toArray(), Stream.of(m_linearprogram.getRight().iterator().next().getValue(), m_linearprogram.getRight().iterator().next().getRelationship(), m_linearprogram.getRight().iterator().next().getCoefficients()).toArray()); }
From source file:uk.ac.tgac.metaopt.external.Apache.java
protected Relationship convertRelationship(Constraint.Relation relation) { if (relation == Constraint.Relation.EQUAL) { return Relationship.EQ; } else if (relation == Constraint.Relation.GREATER_THAN_OR_EQUAL_TO) { return Relationship.GEQ; } else if (relation == Constraint.Relation.LESS_THAN_OR_EQUAL_TO) { return Relationship.LEQ; }/*from w w w.jav a2s.co m*/ throw new UnsupportedOperationException("Apache cannot handle this Relation: " + relation.toString()); }
From source file:uk.ac.tgac.metaopt.external.Apache.java
protected void addDecisionVariables(Collection<LinearConstraint> constraints, Problem problem) { List<Variable> variables = problem.getVariables(); // Add restriction constraint for (int i = 0; i < variables.size(); i++) { Variable var = variables.get(i); double[] coefficients = new double[variables.size()]; for (int j = 0; j < variables.size(); j++) { coefficients[j] = i == j ? 1.0 : 0.0; }//from ww w .j a v a 2s.com Bounds.BoundType boundType = var.getBounds().getBoundType(); if (boundType == Bounds.BoundType.FREE) { // Do nothing in this case } else if (boundType == Bounds.BoundType.LOWER) { constraints.add(new LinearConstraint(coefficients, Relationship.GEQ, var.getBounds().getLower())); } else if (boundType == Bounds.BoundType.UPPER) { constraints.add(new LinearConstraint(coefficients, Relationship.LEQ, var.getBounds().getUpper())); } else if (boundType == Bounds.BoundType.DOUBLE) { constraints.add(new LinearConstraint(coefficients, Relationship.GEQ, var.getBounds().getLower())); constraints.add(new LinearConstraint(coefficients, Relationship.LEQ, var.getBounds().getUpper())); } else if (boundType == Bounds.BoundType.FIXED) { constraints.add(new LinearConstraint(coefficients, Relationship.EQ, var.getBounds().getLower())); } else { throw new IllegalArgumentException("Unknown bound type encountered: " + boundType.toString()); } } }