static void attempt5(final boolean identity, final boolean addDummyConstraints) { final ConvexSolver cs = P20150809.buildModel(identity, addDummyConstraints); try { final Optimisation.Result solution = cs.solve(); if ((solution.getState() == Optimisation.State.DISTINCT) || (solution.getState() == Optimisation.State.APPROXIMATE) || (solution.getState() == Optimisation.State.OPTIMAL)) { final double[] pt = new double[4]; for (int i = 0; i < pt.length; i++) { pt[i] = solution.doubleValue(i); } System.out.println("Objective " + solution.getValue()); for (int ii = 0; ii < 4; ii++) { System.out.println("x[" + ii + "] = " + solution.doubleValue(ii)); } } else { System.out.println("Failure State = " + solution.getState().name()); } } catch (final Exception e) { System.out.println(e); } }
static ConvexSolver buildModel(final boolean identity, final boolean addDummyConstraints) { if (!identity && !addDummyConstraints) { System.out.println("Zero Q matrix and no constraints -------------------------!"); } else if (!identity) { System.out.println("Zero Q matrix and constraints -------------------------!"); } else if (!addDummyConstraints) { System.out.println("Identity Q matrix and no constraints -------------------------!"); } else { System.out.println("Identity Q matrix and constraints -------------------------!"); } final double[] C = new double[] {0.12, -0.05, 0.08, 0.07}; final RawStore cov = new RawStore(4, 4); if (identity) { for (int i = 0; i < 4; i++) { cov.set(i, i, 1.0); } } final RawStore linPart = new RawStore(C, 4); ConvexSolver.Builder builder = ConvexSolver.getBuilder(cov, linPart); if (addDummyConstraints) { final RawStore ineq = RawStore.FACTORY.rows( new double[][] { {-1.0, 0.0, 0.0, 0.0}, {0.0, -1.0, 0.0, 0.0}, {0.0, 0.0, -1.0, 0.0}, {0.0, 0.0, 0.0, -1.0}, {1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0} }); final RawStore coeff = RawStore.FACTORY.columns( new double[][] {{99999, 99999, 99999, 99999, 99999, 99999, 99999, 99999}}); builder = builder.inequalities(ineq, coeff); } final Optimisation.Options opts = new Optimisation.Options(); opts.iterations_abort = 10000; opts.iterations_suffice = 100; if (OptimisationConvexTests.DEBUG) { opts.debug(ConvexSolver.class); } return builder.build(opts); }