ojAlgo 二次优化器中的 NPE
NPE in ojAlgo quadratic optimizer
我有一个非常短的程序,它应该使用 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)
我找不到任何关于如何使用二次优化器的文档。我哪里错了?
好的,找到了:
Currently the solvers supplied by ojAlgo can only handle linear constraint expressions.
遗憾的是它不在文档中或网站上的任何地方(找到的引用来自代码注释)。
ExpressionsBasedModel 是一种建模工具。它可以使用许多不同的求解器,每个求解器都有不同的能力。 ojAlgo 内置了一组求解器。一般来说,这些求解器可以处理 ExpressionsBasedModel 可以建模的所有内容,除了一件事 - 二次约束。这在 ExpressionsBasedModel class 的 javadoc 中有说明。 (获得 NPE 不是 "nice"。我会解决的。)
要求解二次约束问题,您需要一个可以处理该问题的求解器。例如,您可以尝试 MOSEK:
https://github.com/optimatika/ojAlgo-extensions/tree/master/ojAlgo-mosek
我有一个非常短的程序,它应该使用 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)
我找不到任何关于如何使用二次优化器的文档。我哪里错了?
好的,找到了:
Currently the solvers supplied by ojAlgo can only handle linear constraint expressions.
遗憾的是它不在文档中或网站上的任何地方(找到的引用来自代码注释)。
ExpressionsBasedModel 是一种建模工具。它可以使用许多不同的求解器,每个求解器都有不同的能力。 ojAlgo 内置了一组求解器。一般来说,这些求解器可以处理 ExpressionsBasedModel 可以建模的所有内容,除了一件事 - 二次约束。这在 ExpressionsBasedModel class 的 javadoc 中有说明。 (获得 NPE 不是 "nice"。我会解决的。)
要求解二次约束问题,您需要一个可以处理该问题的求解器。例如,您可以尝试 MOSEK:
https://github.com/optimatika/ojAlgo-extensions/tree/master/ojAlgo-mosek