美文网首页
2019-07-18~gurobi踩过的坑

2019-07-18~gurobi踩过的坑

作者: 生病喝药水 | 来源:发表于2019-07-18 16:47 被阅读0次

\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(变量)

相关文章

  • 2019-07-18~gurobi踩过的坑

    ()git_Python-Gurobi学习资料Get constraints in matrix format f...

  • 交互设计师所要避免的几个坑

    前言 工作中难免会踩到几个坑,即使现在不踩以后还会踩,只有踩过才会深刻记住,踩过说明爱过!但是踩过的坑必须把坑填满...

  • vue踩过的坑

    vue踩过的坑

  • D1094:踩坑的价值最大化

    是人就会踩坑,不踩坑理论上就不属于人类,踩坑是人之常情,能回头站在坑边反思,才是对踩过的的坑价值最大化的体现,要不...

  • 投资避坑指南

    2022年9月14日(第224天) 经常反思踩过的坑,犯过的错,了解别人踩过的坑,犯过的错,思考如何避免自己下次踩...

  • PHP中的数据类型

    一说到数据类型,这个坑就太多了,多到有哪些坑,有多少坑,不知道自己还会踩哪些坑,以及踩过的坑还会不会再踩,我对...

  • 踩过的坑

    1、关于Windows下修改hosts文件无法生效,刚开始直接用浏览器打开发现404,后来ping域名才发现是连接...

  • 踩过的坑

    问题: 虚拟机中一打开metasploit就死机。 答: 发现虚拟机分配内存512M。分配2G后,正常。 问题: ...

  • 踩过的坑

    虚拟机/远程桌面实验室1&7:VNC远程桌面实验室2~6:VM虚拟机 VM虚拟机连接成功后不显示虚拟机资源库方法:...

  • 踩过的坑

    tomcat部署问题 问题概述:1. tomcat部署需要重启两次2. 定时任务要跑两次3. 配置的docbase...

网友评论

      本文标题:2019-07-18~gurobi踩过的坑

      本文链接:https://www.haomeiwen.com/subject/azpllctx.html