使用JuMP时如何转换变量的类型 [英] How to convert type of a variable when using JuMP
问题描述
我正在使用Julia/JuMP来实现算法.在一部分中,我定义了一个带有连续变量的模型并求解线性模型.我进行了一些其他计算,在这些计算的基础上,我向模型添加了一些约束,然后我想使用整数变量来解决相同的问题.我不能使用convert()
函数,因为它没有变量.
I am using Julia/JuMP to implement an algorithm. In one part, I define a model with continues variables and solve the linear model. I do some other calculations based on which I add a couple constraints to the model, and then I want to solve the same problem but with integer variables. I could not use convert()
function as it does not take variables.
我试图再次将变量定义为整数,但是模型似乎没有考虑它!我在这里提供了示例代码:
I tried to define the variable again as integer, but the model did not seem to consider it! I provide a sample code here:
m = Model()
@defVar(m, 0 <= x <= 5)
@setObjective(m, Max, x)
@addConstraint(m, con, x <= 3.1)
solve(m)
println(getValue(x))
@defVar(m, 0 <= x <= 1, Bin)
solve(m)
println(getValue(x))
请您帮我完成一次转换吗?
Would you please help me do this conversion?
推荐答案
问题是第二个@variable(m, 0 <= x <= 1, Bin)
实际上在模型中创建了一个新变量,但在Julia中具有相同的名称.
The problem is that the second @variable(m, 0 <= x <= 1, Bin)
actually creates a new variable in the model, but with the same name in Julia.
要将变量从连续变量更改为二进制变量,可以执行
To change a variable from a continuous to a binary, you can do
setcategory(x, :Bin)
在再次调用Solve之前更改变量范围和类.
to change the variable bounds and class before calling solve again.
这篇关于使用JuMP时如何转换变量的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!