Kotlin ojAlgo-将变量表示为优化中的边界?
我一直在和ojAlgo一起玩,到目前为止我都很兴奋。我已经用它做了一些研究,但我在这方面遇到了麻烦 我使用的是Kotlin而不是Java,但这不会引起任何问题。我一直试图在模型中输入一个表达式,但却局限于一个变量,而不是一个文字数字值。如何输入 以下是我迄今为止的工作:Kotlin ojAlgo-将变量表示为优化中的边界?,kotlin,mathematical-optimization,linear-programming,ojalgo,Kotlin,Mathematical Optimization,Linear Programming,Ojalgo,我一直在和ojAlgo一起玩,到目前为止我都很兴奋。我已经用它做了一些研究,但我在这方面遇到了麻烦 我使用的是Kotlin而不是Java,但这不会引起任何问题。我一直试图在模型中输入一个表达式,但却局限于一个变量,而不是一个文字数字值。如何输入 以下是我迄今为止的工作: import org.ojalgo.optimisation.ExpressionsBasedModel import org.ojalgo.optimisation.Variable fun main(args: Arra
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
}
import org.ojalgo.optimization.ExpressionsBasedModel
导入org.ojalgo.optimization.Variable
趣味主线(args:Array){
val模型=表达式BasedModel()
val成分=序列(
配料(“猪肉”,4.32,30),
成分(“小麦”,2.46,20),
成分(“淀粉”,1.86,17)
).map{it.name to it}
.toMap()
val香肠类型=sequenceOf(
香肠类型(“经济型”,0.40),
香肠类型(“优质”,0.60)
).map{it.description to it}
.toMap()
//将连接的字符串键映射到变量
val variables=Components.values.asSequence().flatMap{Components->
香肠类型。值。asSequence()
.map{type->Combo(成分,类型)}
}.map{it.toString()到Variable.make(it.toString()).lower(0.weight(it.component.cost)}
.toMap()
//向模型中添加变量
model.addVariables(variables.values)
//Pe+We+Se=350*0.05
模型。添加表达式(“经济需求”)。级别(350.0*0.05)。应用{
集合(变量[“猪肉经济”],1)
集合(变量[“小麦经济”],1)
集合(变量[“淀粉经济”],1)
}
//Pp+Wp+Sp=500*0.05
模型。加法表达式(“前置需求”)。级别(500.0*0.05)。应用{
集合(变量[“猪肉溢价”],1)
集合(变量[“小麦溢价”],1)
集合(变量[“淀粉溢价”],1)
}
//Pe>=0.4(Pe+We+Se)
//编译错误?
model.addExpression(“EconomyGovRestriction”).upper(变量[“猪肉经济])。应用{
集合(变量[“猪肉经济”],.4)
集合(变量[“小麦经济”],.4)
集合(变量[“淀粉经济”],.4)
}
}
数据类组合(val配料:配料,val香肠类型:香肠类型){
重写fun toString()=“$sausageType-$Component”
}
数据类香肠类型(val说明:字符串,val PORK要求:双精度){
重写fun toString()=说明
}
数据类成分(val名称:String,val成本:Double,val可用性:Int){
重写fun toString()=名称
}
你不能那样做。您不能直接建模expr1>=expr2
。相反,您必须建模(expr1-expr2)>=0
。ojAlgo wiki上有一个例子,描述了如何模拟类似的问题:对于未来的读者,这里是我提出的完整的工作解决方案
import org.ojalgo.optimisation.ExpressionsBasedModel
import org.ojalgo.optimisation.Variable
import java.math.RoundingMode
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(17.5).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(25).apply {
set(variables["Pork-Premium"], 1)
set(variables["Wheat-Premium"], 1)
set(variables["Starch-Premium"], 1)
}
// Pe >= 0.4(Pe + We + Se)
model.addExpression("EconomyPorkRatio").upper(0.0).apply {
set(variables["Pork-Economy"], -0.6)
set(variables["Wheat-Economy"], .4)
set(variables["Starch-Economy"], .4)
}
// Pe >= 0.6(Pp + Wp + Sp)
model.addExpression("PremiumPorkRatio").upper(0.0).apply {
set(variables["Pork-Premium"], -0.4)
set(variables["Wheat-Premium"], .6)
set(variables["Starch-Premium"], .6)
}
// Se <= .25(Pe + We + Se)
// Sp <= .25(Pp + Wp + Sp)
sausageTypes.values.forEach {
model.addExpression("${it}StarchRestriction").lower(0.0).apply {
set(variables["Pork-$it"], .25)
set(variables["Wheat-$it"], .25)
set(variables["Starch-$it"], -0.75)
}
}
// Pe + Pp <= 30
// We + Wp <= 20
// Se + Sp <= 17
ingredients.values.forEach { ingredient ->
model.addExpression("${ingredient}SupplyConstraint").upper(ingredient.availability).apply {
sausageTypes.values.forEach { sausageType ->
set(variables["$ingredient-$sausageType"], 1)
}
}
}
// Pe + Pp >= 23
model.addExpression("ContractPorkRestriction").lower(23).apply {
set(variables["Pork-Economy"], 1)
set(variables["Pork-Premium"], 1)
}
// go!
val result = model.minimise()
println("OPTIMIZED COST: ${result.value}")
model.variables.asSequence()
.map { it.name }
.zip(result.asSequence().map { it.setScale(3, RoundingMode.HALF_DOWN) })
.forEach(::println)
}
data class Combo(val ingredient: Ingredient, val sausageType: SausageType) {
override fun toString() = "$ingredient-$sausageType"
}
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
}
我只是在你发帖前想出来的。非常感谢。
OPTIMIZED COST: 140.955
(Pork-Economy, 8.000)
(Pork-Premium, 15.000)
(Wheat-Economy, 5.125)
(Wheat-Premium, 3.750)
(Starch-Economy, 4.375)
(Starch-Premium, 6.250)