2019-07-18~gurobi踩过的坑

\color{red}{热的}
git_Python-Gurobi学习资料
Get constraints in matrix format from gurobipy
Sensitivity Analysis with Gurobi

1.安装
  • 注册
    选择academic
  • license
    有命令行,在terminal下直接运行,运行后自动下载证书
  • APP
    用canda自动安装,pip总安装失败
2. Start
  • 看官方document,有相关例子,具体放在Mendelay里(2个)
import gurobipy as bi
m=bi.Model('model name')

#添加变量
m.addVars(变量下标1,变量下标2,...,lb=,ub=,vtype=bi.GRB.CONTINUS,'name')

# or
for i in 下标1:
    for j in 下标2:
        for ...:
              m.addVar()

#更新环境
m.update()

#添加约束,基本将变量表述出来就行
m.addConstrs(fucntion for variable in ...)
#or
m.addConstr()

#添加目标值
m.setObjective(objective function,bi.GRB.MINIMIZE)

#优化
m.optimize()

在表达可能涉及到

bi.quicksum()
tuplelist.select(j,'*')
3. Model infeasible

infeasible analysis1
infeasible analysis - officail

document里有写:workforce1,2,3。
自己在建模过程中出现这个问题是因为出现conflict constraints.(因此,在添加约束和变量的时候最好定义好name,否则conflict cosntraints 会以‘R1’..,出现)

在查找conflict cosntraints的时候,应先用IIS判别模型是否infeasible
两种方式处理conflict constraints

  • Remove
removed=[]
    while True:
        m.computeIIS()
        print('\nthe following constraint cannot be satisfies:')
        for c in m.getConstrs():
            print('%s' % c.constrName)
            removed.append(str(c.constrName))
            m.remove(c)
            break
        print ''
        m.optimize()
        status=m.status

        if status ==bi.GRB.Status.UNBOUNDED:
            print 'unbounded'
            exit(0)
        if status==bi.GRB.Status.OPTIMAL:
            break
        if status==bi.GRB.Status.INF_OR_UNBD and status!=bi.GRB.Status.INFEASIBLE:
            print ('was stopped with %d'% status)
            exit(0)

    print ('\nthe constraint removed:')
    print removed
  • Relax

  • 解决
    查找冲突变量按定义顺序查找
    st1
    st2
    st3
    ...
    若st1与后面的任意变量冲突,输出st1。总之,显示最前的变量。

5. 输出模型结果
    if m.status == bi.GRB.Status.OPTIMAL:
        solution_cache = m.getAttr('x', x)
        solution_deliver = m.getAttr('x', q)
        #print solution_cache
        #print solution_deliver
        for h in range(num_file):
            for i in range(num_sbs):
                print('\nOptimal file %d for node %d:' % (h, i))
                for j, k in bi.tuplelist(depth_first_search(i, num_sbs, edgeWeight)):
                    if solution_cache[i, h] > 0.0:
                        print ('%d cache %d: %f' % (i, h, solution_cache[i, h]))
                    if solution_deliver[h, i, j, k] > 0.0:
                        print('%d -> %d: %f' % (j, k, solution_deliver[h, i, j, k]))
                    if solution_cache[k, h] > 0.0:
                        print ('%d cache %d: %f' % (i, h, solution_cache[i, h]))

    else:
        print m.printStats()

主要为m.getAttr(变量)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 项目管理术语英汉对照表2018-7-20 A Abstract Resource 抽象资源 Abstraction...
    007明_阳阅读 6,518评论 0 51
  • ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。 O...
    我想起个好名字阅读 5,494评论 0 9
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,170评论 1 32
  • 写在前面的话 代码中的# > 表示的是输出结果 输入 使用input()函数 用法 注意input函数输出的均是字...
    FlyingLittlePG阅读 3,046评论 0 9
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,533评论 0 5