我有一个很短的程序,应该使用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)
)

调整时,我得到了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)

我找不到有关如何使用二次优化器的任何文档。我哪里做错了?

最佳答案

ExpressionsBasedModel是一个建模工具。它可以使用许多不同的求解器,并且每个求解器具有不同的功能。 ojAlgo内置了一组求解器。通常来说,那些求解器可以处理ExpressionsBasedModel可以建模的所有事物,除了一件事之外-二次约束。这在ExpressionsBasedModel类的javadoc中进行了说明。 (获得NPE并非“好”。我会解决这个问题。)

要解决二次约束问题,您需要一个可以解决该问题的求解器。例如,您可以尝试MOSEK:

https://www.mosek.com/

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

关于kotlin - ojAlgo二次优化器中的NPE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47378153/

10-12 01:41