gpt4 book ai didi

kotlin - ojAlgo-在优化中将变量表示为边界?

转载 作者:行者123 更新时间:2023-12-02 13:14:28 31 4
gpt4 key购买 nike

我一直在与ojAlgo玩耍,到目前为止,我对此感到非常激动。我已经对其进行了一些研究,但是使用problem described in this article遇到了麻烦。

我使用的是Kotlin而不是Java,但这不会引起任何问题。我一直试图在模型中输入表达式,但是要限制在变量而不是文字数值上。我该如何输入?

到目前为止,这是我的工作:

import org.ojalgo.optimisation.ExpressionsBasedModel
import org.ojalgo.optimisation.Variable


fun main(args: Array<String>) {

val model = ExpressionsBasedModel()

val ingredients = sequenceOf(
Ingredient("Pork", 4.32, 30),
Ingredient("Wheat", 2.46, 20),
Ingredient("Starch", 1.86, 17)
).map { it.name to it }
.toMap()

val sausageTypes = sequenceOf(
SausageType("Economy", .40),
SausageType("Premium", .60)
).map { it.description to it }
.toMap()

// Map concatenated string keys to variables
val variables = ingredients.values.asSequence().flatMap { ingredient ->
sausageTypes.values.asSequence()
.map { type -> Combo(ingredient,type)}
}.map { it.toString() to Variable.make(it.toString()).lower(0).weight(it.ingredient.cost) }
.toMap()

// add variables to model
model.addVariables(variables.values)

// Pe + We + Se = 350 * 0.05
model.addExpression("EconomyDemand").level(350.0 * 0.05).apply {
set(variables["Pork-Economy"], 1)
set(variables["Wheat-Economy"], 1)
set(variables["Starch-Economy"], 1)
}

// Pp + Wp + Sp = 500 * 0.05
model.addExpression("PremiumDemand").level(500.0 * 0.05).apply {
set(variables["Pork-Premium"], 1)
set(variables["Wheat-Premium"], 1)
set(variables["Starch-Premium"], 1)
}

// Pe >= 0.4(Pe + We + Se)
// compile error?
model.addExpression("EconomyGovRestriction").upper(variables["Pork-Economy"]).apply {
set(variables["Pork-Economy"], .4)
set(variables["Wheat-Economy"], .4)
set(variables["Starch-Economy"], .4)
}
}

data class Combo(val ingredient: Ingredient, val sausageType: SausageType) {
override fun toString() = "$sausageType-$ingredient"
}

data class SausageType(val description: String, val porkRequirement: Double) {
override fun toString() = description
}

data class Ingredient(val name: String, val cost: Double, val availability: Int) {
override fun toString() = name
}

最佳答案

你不能那样做。您不能直接建模expr1 >= expr2。相反,您必须建模(expr1 - expr2) >= 0。 ojAlgo Wiki上有一个示例,描述了如何为类似问题建模:https://github.com/optimatika/ojAlgo/wiki/The-Diet-Problem

关于kotlin - ojAlgo-在优化中将变量表示为边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46798888/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com