1

I am trying to reproduce the result of the fzero's MATLAB function in JAVA. After searching around, I found the library JOM(Java Optimization Modeler). I wrote a very simple program in order to test its efficiency and I am not able to find the correct value, I am using a very simple evaluation function : x - 100 with the objective to minimize it. I would like to know if my code is wrong or if you could suggest me another library doing better ? The below code found the optimal solution to be 0.0, I was expecting 100.

    /* Create the optimization problem object */
    OptimizationProblem op = new OptimizationProblem();
    op.addDecisionVariable("x", false, new int[] { 1 , 1 }, 0 , Double.MAX_VALUE);
    /* Sets the objective function */
    op.setObjectiveFunction("minimize", "x-100");
    op.solve("ipopt");
    if (!op.solutionIsOptimal ()) throw new RuntimeException ("An optimal solution was not found");
    System.out.println (op.getPrimalSolution("x").toValue());
    System.out.println("Total cost obtained: " + op.getOptimalCost());
oro777
  • 1,110
  • 1
  • 14
  • 29

1 Answers1

1

I think that there is an issue here that you have not though about. If you think about it again you would see that minimum of the equation is really at 0.0 in the interval [0,Double.MAX_VALUE). This because the function is monotonically increasing and start with its minimum value -100. To solve the equation you need to minimize the absolute value.

However, is the functions that you are going to solve continous over the interval you try to solve it? In that case I would recommend you to use Newtons method, which have been shown to have good performance for equation solving. The method can be implemented quite easy if you have a function for the equation you wants to solve (eg f(x) = x-100). The derivate can be found using different approximations, which some of them can be found on wikipedia.

patrik
  • 4,506
  • 6
  • 24
  • 48
  • Unfortunately, the library does not support the ABS function, is there a way to minimize the absolute value easily using this library ? Thank you for your suggestion, I will consider it. – oro777 Feb 26 '15 at 03:23
  • 1
    @oro777 Unfortunately I do not work with Java very much, but I would guess that the optimization that is done in the library should be much heavier than just finding a root anyway. I would also guess that Newtons method would be closest to what matlab implemented in `fzero`. However, the signbit is the highest order bit which means you can genereate an abs function using bitwise operators. It may be a pain to do but if you can manage fairly easy you have some tips [here](http://stackoverflow.com/questions/9772348/get-absolute-value-without-using-abs-function-nor-if-statement). – patrik Feb 26 '15 at 06:56