如何将JuMP约束写入文本文件? [英] How to write a JuMP constraint into a text file?

查看:83
本文介绍了如何将JuMP约束写入文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了,但是没有找到答案.我已经将MPS文件读入JuMP模型.现在,我想将约束子集写入文本文件以进行进一步分析.我知道如何将JuMP模型写入LP或MPS文件.但是在这里,我只想将约束子集写入文本文件.如果我可以解析约束,找出系数是什么,变量是什么,那会更好.预先感谢!

I have searched but didn't find an answer. I have read a MPS file into a JuMP model. Now I would like to write a subset of constraints into a text file for further analysis. I know how to write a JuMP model into a LP or MPS file. But here I just want to write a subset of constraints into a text file. If I can parse the constraints, figuring out what are the coefficients and what are the variables, that would even be better. Thanks in advance!

推荐答案

println(file_stream, m)

println将很好地格式化JuMP模型,并且输出将是文本文件.

println will nicely format a JuMP model and the output will be a text file.

完整代码:

using JuMP
using GLPK
m = Model(optimizer_with_attributes(GLPK.Optimizer))
@variable(m, x1 >= 0)
@variable(m, x2 >= 0)
@constraint(m, x1 + x2 <= 10)
@objective(m, Max, 2x1 + x2)
open("model.txt", "w") do f
   println(f, m)
end

让我们看看文件中的内容:

Let's see what is in the file:

$ more model.txt

Max 2 x1 + x2
Subject to
 x1 + x2 <= 10.0
 x1 >= 0.0
 x2 >= 0.0

如果您只想使用约束,则此代码将起作用:

If you want constraints only this code will do:

open("cons.txt","w") do f
    for c in vcat([all_constraints(m, t...) for t in list_of_constraint_types(m)]...)
       println(f , c)
    end
end

这篇关于如何将JuMP约束写入文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆