List of usage examples for org.apache.commons.math3.optim.linear Relationship GEQ
Relationship GEQ
To view the source code for org.apache.commons.math3.optim.linear Relationship GEQ.
Click Source Link
From source file:com.datumbox.framework.core.mathematics.linearprogramming.LPSolver.java
/** * Solves the LP problem and returns the result. * //from w ww . j a v a 2 s . c o 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:org.aika.experiments.ComputeWeightsExperiment.java
@Test public void testOptimization2() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 7, 3, 0, 0 }, 0); LinearConstraintSet constraints = new LinearConstraintSet( new LinearConstraint(new double[] { 1, 1, 1, 0 }, Relationship.GEQ, 0.5), new LinearConstraint(new double[] { 1, 0, 1, 1 }, Relationship.GEQ, 0.5), new LinearConstraint(new double[] { 0, 1, 0, 1 }, Relationship.LEQ, -0.5), new LinearConstraint(new double[] { 1, 0, 0, 0 }, Relationship.LEQ, -0.5), new LinearConstraint(new double[] { 0, 1, 0, 0 }, Relationship.LEQ, -0.5)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE); Assert.assertEquals(10.0, solution.getValue(), .0000001); }
From source file:org.aika.experiments.ComputeWeightsExperiment.java
@Test public void testOptimization1() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 1, 0, 0, 0, 0, 0 }, 0); LinearConstraintSet constraints = new LinearConstraintSet( new LinearConstraint(new double[] { 1, 1, 1, 0, 1, 1 }, Relationship.GEQ, 0.5), new LinearConstraint(new double[] { 1, 1, 1, 1, 0, 1 }, Relationship.GEQ, 0.5), new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 1 }, Relationship.LEQ, -0.5), new LinearConstraint(new double[] { 1, 1, 0, 0, 1, 1 }, Relationship.LEQ, -0.5), new LinearConstraint(new double[] { 1, 1, 1, 0, 0, 1 }, Relationship.LEQ, -0.5), new LinearConstraint(new double[] { 1, 1, 1, 0, 1, 0 }, Relationship.LEQ, -0.5), new LinearConstraint(new double[] { 1, 0, 1, 1, 0, 1 }, Relationship.LEQ, -0.5), new LinearConstraint(new double[] { 1, 1, 0, 1, 0, 1 }, Relationship.LEQ, -0.5), new LinearConstraint(new double[] { 1, 1, 1, 1, 0, 0 }, Relationship.LEQ, -0.5)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, new NonNegativeConstraint(false)); Assert.assertEquals(10.0, solution.getValue(), .0000001); }
From source file:org.aika.network.neuron.lattice.AndNode.java
private Neuron computeNeuron(TreeSet<AndNode> significantLowerBound, TreeSet<Node> nonSignificantUpperBound) { Map<InputNode, Integer> indexes = new TreeMap<>(); ArrayList<InputNode> revIndexes = new ArrayList<>(); for (AndNode n : significantLowerBound) { for (InputNode in : n.parents.keySet()) { if (!indexes.containsKey(in)) { indexes.put(in, indexes.size()); revIndexes.add(in);/*from www .j av a 2s. c om*/ } } } double[] objF = new double[indexes.size() + 1]; objF[0] = 1; LinearObjectiveFunction f = new LinearObjectiveFunction(objF, 0); LinearConstraint[] constraintArr = new LinearConstraint[significantLowerBound.size() + nonSignificantUpperBound.size()]; int i = 0; for (AndNode n : significantLowerBound) { double[] c = new double[indexes.size() + 1]; c[0] = 1; for (InputNode in : n.parents.keySet()) { c[indexes.get(in) + 1] = in.key.isNeg ? -1 : 1; } constraintArr[i] = new LinearConstraint(c, Relationship.GEQ, 0.5); i++; } for (Node n : nonSignificantUpperBound) { double[] c = new double[indexes.size() + 1]; c[0] = 1; if (n instanceof InputNode) { c[indexes.get(n) + 1] = ((InputNode) n).key.isNeg ? -1 : 1; } else if (n instanceof AndNode) { for (InputNode in : ((AndNode) n).parents.keySet()) { c[indexes.get(in) + 1] = in.key.isNeg ? -1 : 1; } } constraintArr[i] = new LinearConstraint(c, Relationship.LEQ, -0.5); i++; } LinearConstraintSet constraints = new LinearConstraintSet(constraintArr); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, new NonNegativeConstraint(false)); double bias = solution.getKey()[0]; TreeSet<Synapse> synapses = new TreeSet<>(); for (int j = 1; j < solution.getKey().length; j++) { InputNode in = revIndexes.get(j - 1); Synapse s = new Synapse(in.inputNeuron, in.key.posDelta); s.w = (float) solution.getKey()[j]; synapses.add(s); } return Neuron.createNeuron(new Neuron(), bias, synapses, false); }
From source file:org.arrowhead.wp5.market.impl.Market.java
public void clear(List<Bid> demList, List<Bid> supList, List<Bid> winningDemandBids, List<Bid> winningSupplyBids) { if (demList.isEmpty() || supList.isEmpty()) { logger.debug("Empty list, no clearing."); return;/*from www .j a v a 2 s . c o m*/ } List<Bid> demandList = new ArrayList<Bid>(); demandList.addAll(demList); List<Bid> supplyList = new ArrayList<Bid>(); supplyList.addAll(supList); long totalDemand = getTotalQuantity(demandList); long totalSupply = getTotalQuantity(supplyList); long minQuantity = Math.min(totalDemand, totalSupply); Bid fakeBid = new Bid(0, totalSupply - totalDemand, true, "", ""); if (totalDemand < totalSupply) { demandList.add(fakeBid); totalDemand = getTotalQuantity(demandList); totalSupply = getTotalQuantity(supplyList); minQuantity = Math.min(totalDemand, totalSupply); } int numDemand = demandList.size(); int numSupply = supplyList.size(); int demandIdx = 0; int supplyIdx = numDemand; int total = numDemand + numSupply; logger.debug("demand: {} supply: {} total: {}", numDemand, numSupply, total); double[] demand = getPrice(demandList); double[] supply = getPrice(supplyList); logger.debug("demand: {}", Arrays.toString(demand)); logger.debug("supply: {}", Arrays.toString(supply)); double[] empty = new double[total]; double[] objective = empty.clone(); System.arraycopy(demand, 0, objective, demandIdx, numDemand); System.arraycopy(supply, 0, objective, supplyIdx, numSupply); for (int i = 0; i < supply.length; i++) { objective[supplyIdx + i] = -supply[i]; } logger.debug("Empty: {} - obj: {}", Arrays.toString(empty), Arrays.toString(objective)); // describe the optimization problem LinearObjectiveFunction f = new LinearObjectiveFunction(objective, 0); Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>(); for (int i = 0; i < demandList.size(); i++) { generateConstraint(demandList, demandIdx, total, constraints, i, Relationship.LEQ); } for (int i = 0; i < supplyList.size(); i++) { generateConstraint(supplyList, supplyIdx, total, constraints, i, Relationship.GEQ); } double[] constraint = empty.clone(); for (int i = 0; i < demandList.size(); i++) { constraint[i + demandIdx] = 1; } logger.debug("Total quantity constraint: {} {} {}", Arrays.toString(constraint), Relationship.LEQ, minQuantity); constraints.add(new LinearConstraint(constraint, Relationship.LEQ, minQuantity)); constraint = empty.clone(); for (int i = 0; i < supplyList.size(); i++) { constraint[i + supplyIdx] = 1; } logger.debug("Total quantity constraint: {} {} {}", Arrays.toString(constraint), Relationship.LEQ, minQuantity); constraints.add(new LinearConstraint(constraint, Relationship.LEQ, minQuantity)); for (int i = 0; i < demandList.size(); i++) { constraint = empty.clone(); constraint[i + demandIdx] = 1; logger.debug("Non zero constraint: {} {} {}", Arrays.toString(constraint), Relationship.GEQ, 0); constraints.add(new LinearConstraint(constraint, Relationship.GEQ, 0)); } for (int i = 0; i < supplyList.size(); i++) { constraint = empty.clone(); constraint[i + supplyIdx] = 1; logger.debug("Non zero constraint: {} {} {}", Arrays.toString(constraint), Relationship.GEQ, 0); constraints.add(new LinearConstraint(constraint, Relationship.GEQ, 0)); } constraint = empty.clone(); for (int i = 0; i < demandList.size(); i++) { constraint[i + demandIdx] = 1; } for (int i = 0; i < supplyList.size(); i++) { constraint[i + supplyIdx] = -1; } // logger.debug("Equilibrium constraint: {} {} {}", Arrays.toString(constraint), Relationship.LEQ, totalSupply-totalDemand); // constraints.add(new LinearConstraint(constraint, Relationship.LEQ, totalSupply-totalDemand)); LinearConstraintSet set = new LinearConstraintSet(constraints); // create and run the solver PointValuePair solution; try { solution = new SimplexSolver().optimize(f, set, GoalType.MAXIMIZE); } catch (NoFeasibleSolutionException e) { logger.info("No feasuble solution!!"); return; } // get the solution logger.debug("solution: "); double[] solutionPoint = solution.getPoint(); List<Bid> all = new ArrayList<Bid>(); all.addAll(demandList); all.addAll(supplyList); for (int i = 0; i < solutionPoint.length; i++) { logger.debug("{} - {} - {}", solutionPoint[i], all.get(i).getQuantity(), all.get(i).getPrice()); } double max = solution.getValue(); logger.debug("value: {}", max); demandList.remove(fakeBid); double price = findPrice(demandList, supplyList, solutionPoint); logger.debug("win price: {}", price); for (int i = 0; i < demandList.size(); i++) { if (solutionPoint[i + demandIdx] > 0) { Bid d = demandList.get(i); if (d == fakeBid) { continue; } if (price <= d.getPrice()) { d.setWinPrice(price); // d.setWinQuantity(solutionPoint[i+demandIdx]); logger.debug("Win d: p: {} - q: {} - wq: {}", d.getWinPrice(), d.getQuantity(), d.getWinQuantity()); winningDemandBids.add(d); } } } for (int i = 0; i < supplyList.size(); i++) { if (solutionPoint[i + supplyIdx] > 0) { Bid d = supplyList.get(i); if (price >= d.getPrice()) { d.setWinPrice(price); // d.setWinQuantity(solutionPoint[i+supplyIdx]); logger.debug("Win s: p: {} - q: {} - wq: {}", d.getWinPrice(), d.getQuantity(), d.getWinQuantity()); winningSupplyBids.add(d); } } } logger.debug("winning demand: {} - total quantity: {}", winningDemandBids, getTotalWinQuantity(winningDemandBids)); logger.debug("winning supply: {} - total quantity: {}", winningSupplyBids, getTotalWinQuantity(winningSupplyBids)); }
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 ww w . java 2s. 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 ww. ja v a2s. c o m * @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 solve maximum/*from w w w . j a v a2 s . co m*/ */ @Test public final void solvemaximize() { final List<ITerm> l_return = new ArrayList<>(); final ImmutablePair<LinearObjectiveFunction, Collection<LinearConstraint>> l_linearprogrammax = new ImmutablePair<>( new LinearObjectiveFunction(new double[] { 3, 5 }, 0.0), new HashSet<LinearConstraint>()); l_linearprogrammax.getRight().add(new LinearConstraint(new double[] { 2, 8 }, Relationship.LEQ, 13)); l_linearprogrammax.getRight().add(new LinearConstraint(new double[] { 5, -1 }, Relationship.LEQ, 11)); l_linearprogrammax.getRight().add(new LinearConstraint(new double[] { 1, 0 }, Relationship.GEQ, 0)); l_linearprogrammax.getRight().add(new LinearConstraint(new double[] { 0, 1 }, Relationship.GEQ, 0)); new CSolve().execute(false, IContext.EMPTYPLAN, Stream.of(l_linearprogrammax, "maximize", "non-negative") .map(CRawTerm::from).collect(Collectors.toList()), l_return); Assert.assertArrayEquals(l_return.stream().map(ITerm::raw).toArray(), Stream.of(12.333333333333332, 2, 2.4047619047619047, 1.0238095238095237).toArray()); }
From source file:org.lightjason.agentspeak.action.builtin.TestCActionMathLinearprogram.java
/** * test solve minimize//w ww.j a va 2s .co m */ @Test public final void solveminimize() { final List<ITerm> l_return = new ArrayList<>(); final ImmutablePair<LinearObjectiveFunction, Collection<LinearConstraint>> l_linearprogrammin = new ImmutablePair<>( new LinearObjectiveFunction(new double[] { -2, 15 }, 0.0), new HashSet<LinearConstraint>()); l_linearprogrammin.getRight().add(new LinearConstraint(new double[] { -6, 8 }, Relationship.GEQ, 3)); l_linearprogrammin.getRight().add(new LinearConstraint(new double[] { 5, -1 }, Relationship.GEQ, 11)); l_linearprogrammin.getRight().add(new LinearConstraint(new double[] { 1, 0 }, Relationship.GEQ, 0)); l_linearprogrammin.getRight().add(new LinearConstraint(new double[] { 0, 1 }, Relationship.GEQ, 0)); new CSolve().execute(false, IContext.EMPTYPLAN, Stream.of(l_linearprogrammin, "minimize", "non-negative") .map(CRawTerm::from).collect(Collectors.toList()), l_return); Assert.assertArrayEquals(l_return.stream().map(ITerm::raw).toArray(), Stream.of(30.38235294117647, 2, 2.676470588235294, 2.3823529411764706).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; }/* ww w. ja v a 2s .c o m*/ throw new UnsupportedOperationException("Apache cannot handle this Relation: " + relation.toString()); }