0

I use ojAlgo lib in version 51.4.1. I ve got:

protected final ExpressionsBasedModel model = new ExpressionsBasedModel();
protected Variable[] result;

and some other variables. Each variable is added to the model, same as constrains. When i run:

Optimisation.Result result = model.minimise();

I can get each result value using:

private static double[] getVariablesValues(Variable[] variables) {
    double[] values = new double[variables.length];
    for (int i = 0; i < variables.length; i++) {
        values[i] = variables[i].getValue().doubleValue();
    }
    return values;
}

And now my problem: I want to force optimizer to use ConvexSolver so instead model.minimise() i write:

ConvexSolver solver = new ConvexSolver.ModelIntegration().build(model);
Optimisation.Result result = solver.solve();

Solver returns OPTIMAL solution, but i cannot get variable values as a did before. Is there a possibility to get these values from solver or result? I couldn't find any helpfull resolution with google or chatGPT. I was thinking about getting values by index, but result has other values indexes than model variables.

lukaszP
  • 45
  • 12

1 Answers1

1

There is no need for you to create a new model integration new ConvexSolver.ModelIntegration(). Instead just use the ConvexSolver.INTEGRATION instance.

There are two ways you can use it:

  1. Create a new Optimisation.Integration that simply delegate to that instance except that the isCapable method always returns true. Add this new integration using ExpressionsBasedModel.addIntegration and use model.minimise() the usual way.
  2. Take the Optimisation.Result returned by the solver and feed it to the toModelState method of that integration. This will translate the variable indices to match the model variables.

I think (1) is the better alternative.

Why do you want to force use the ConvexSolver?

apete
  • 1,250
  • 1
  • 10
  • 16
  • Yes, it works thanks. I want to force convex solver becouse it always gives me optimal solution, and for the same model when ExpressionsBasedModel decides to use its default, the result is INFEASIBLE, so propably (i am not specialist but that is how i understand it), the default algorithm found a local optimum that was not passing the criteria. And as i know ConvexSolver always find a global optimum and that is why it returns result OPTIMAL. Mayby there is a better solution for my problem? Generally speaking i ve got linear model minimalizing sum of absolute values with additional constrains. – lukaszP Jun 29 '23 at 08:45
  • Mayby there is a better solver or solution for me? It is important for me to algorithm be efficient. Earlier I minimalized sum of squares, but for more than 100 points efficiency wasn't satisfactional, so i tried to experiment with other objective functions like minimalizing (sum_of_absolute_values) + (number_of_points * max_absolute_value) hoping that fully linear model will be faster but there my problem came that the default solver for that returns INFEASIBLE and force ConvexSolver isnt really faster than minimalizing sum of squares. – lukaszP Jun 29 '23 at 09:28
  • 1
    Forcing the ConvexSolver should not be necessary. Is it possible for you to provide a test case (at GitHub) that demonstrates the problem? – apete Jun 29 '23 at 09:42
  • Unfortunately I cannot show code and data. All I can say is that on the same data set I run my optimizer with different objective function. If my objective is quadratic ExpressionsBasedModel.Integration#getIntegration() returns ConvexSolver and if I optimize absolute sum, it returns LinearSolver. And then optimization via Convex returns OPTIMAL, but Linear INFEASIBLE. When I force to use Convex for linear case, it also returns OPTIMAL. Same result on both 51.4.1 and 53.0.0, but i need to run it on java 8 anyway. If you have an idea why, it it happens, it will be helpfull. Thanks anyway. – lukaszP Jun 29 '23 at 12:30