1

I have a very short program which is supposed to find optimize a model using ojAlgo:

package dwarfs

import org.ojalgo.optimisation.ExpressionsBasedModel
import org.ojalgo.optimisation.Variable
import java.util.concurrent.atomic.AtomicInteger

val model = ExpressionsBasedModel()

private val funcId = AtomicInteger(0)
private val variableId = AtomicInteger(0)
fun ExpressionsBasedModel.newVariable() = Variable(variableId.incrementAndGet().toString().let { "Variable$it" }).also(this::addVariable)
fun ExpressionsBasedModel.newExpression() = funcId.incrementAndGet().let { "Expression$it"}.let { this.addExpression(it) }

fun main(args: Array<String>) {
    val works = model.newExpression().upper(1).lower(0).apply {
        dwarfs.forEach { set(it.quata, it.quata, 1) }
    }

    println(model)
    println(works)

    model.maximise().let(::println)
}

class Dwarf(val usefulness: Double) {
    val quata: Variable = model.newVariable().lower(0).upper(1).weight(usefulness)
}

val dwarfs = listOf(
        Dwarf(0.4), Dwarf(0.2), Dwarf(1.1)
)

When I tun it, I get an NPE:

############################################
0 <= Variable1 (0.400000) <= 1.000000
0 <= Variable2 (0.200000) <= 1.000000
0 <= Variable3 (1.100000) <= 1.000000
0 <= Expression1: 0.75 <= 1.000000
############################################

0 <= Expression1 <= 1.000000
Exception in thread "main" java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at org.ojalgo.ProgrammingError.throwIfNull(ProgrammingError.java:83)
at org.ojalgo.optimisation.convex.ConvexSolver$Builder.objective(ConvexSolver.java:318)
at org.ojalgo.optimisation.convex.ConvexSolver.copy(ConvexSolver.java:543)
at org.ojalgo.optimisation.convex.ConvexSolver$ModelIntegration.build(ConvexSolver.java:445)
at org.ojalgo.optimisation.convex.ConvexSolver$ModelIntegration.build(ConvexSolver.java:439)
at org.ojalgo.optimisation.ExpressionsBasedModel.solve(ExpressionsBasedModel.java:762)
at org.ojalgo.optimisation.ExpressionsBasedModel.maximise(ExpressionsBasedModel.java:626)
at dwarfs.DwarfsKt.main(dwarfs.kt:22)

I couldn't find any docs on how to use the quadratic optimizer. Where did I go wrong?

voddan
  • 31,956
  • 8
  • 77
  • 87
  • Just to clarify, so you are doing nonlinear work? It looks like ojAlgo has some quadratic functionality. I'm guessing you are not invoking it. I'll look in more depth later. https://github.com/optimatika/ojAlgo/blob/7a8662eb7d311b88f13b110aace8907855212ab6/src/org/ojalgo/optimisation/Expression.java#L55L:L70 – tmn Nov 19 '17 at 15:44

2 Answers2

1

ExpressionsBasedModel is a modelling tool. It can use many different solvers, and each solver has different capabilities. ojAlgo has a collection of solvers built in. Generally speaking those can handle everything ExpressionsBasedModel can model except one thing - quadratic constraints. This is stated in the javadoc of the ExpressionsBasedModel class. (Getting an NPE is not "nice". I'll fix that.)

To solve quadratically constrained problems you need a solver that can handle that. You could for instance try MOSEK:

https://www.mosek.com/

https://github.com/optimatika/ojAlgo-extensions/tree/master/ojAlgo-mosek

apete
  • 1,250
  • 1
  • 10
  • 16
  • Thanks! Is there a way to set it up using only Gradle? – voddan Nov 20 '17 at 13:15
  • 1
    MOSEK is a commercial product not written in Java. You can't just get it using Maven or Gradle. There is a Java interface, and ojAlgo has an integration using that. – apete Nov 20 '17 at 16:26
0

Ok, found it:

Currently the solvers supplied by ojAlgo can only handle linear constraint expressions.

Pity it wasn't in the documentation or anywhere on the site (the found quote is from a code comment).

voddan
  • 31,956
  • 8
  • 77
  • 87