mosek使用记录

mosek

需求

求解mixed-integer problem

Introduction

优秀的数学优化求解器

官方网址:https://www.mosek.com/
其中,重要说明:

  1. 认证文件下载:https://docs.mosek.com/9.3/licensing/quickstart.html#i-don-t-have-a-license-file-yet
    按照要求放在指定的文件夹位置。python正常方法按照mosek包即可使用
  2. https://www.mosek.com/documentation/
    有比较比较多的使用说明,主要是两个API文档FusionAPIOptimizerAPI,一个是面向对象的一个是面向过程的用法,fusion更直观好用。尾页有一cookbook也可以更深入了解模型理论
    使用求解器首先要自己完成建模过程,分清楚三个要素:variable、constraint、cost

Study

FusionAPI整数线性规划的例题
例题

from mosek.fusion import *
def main(args):
    # constraint的系数,每行指代一个不等式
	A = [[50.0, 31.0],
		 [3.0, -2.0]]
	# cost的系数
	c = [1.0, 0.64]
	with Model('milo1') as M:
		# 1.设置变量,名称x,数量为2,限制为非负整数。greaterThan和lessThan都是包含了“=”
		x = M.variable('x', 2, Domain.integral(Domain.greaterThan(0.0)))
		
		# 2.设置constraints
		# 50.0 x[0] + 31.0 x[1] <= 250.0
		# 3.0 x[0] - 2.0 x[1] >= -4.0
		M.constraint('c1', Expr.dot(A[0], x), Domain.lessThan(250.0))
		M.constraint('c2', Expr.dot(A[1], x), Domain.greaterThan(-4.0))
		
		# 3.设置求解器terminate的条件约束,
		# 3.1 time-out时间为60(单位未知,maybe60ms,超时会返回目前suboptimal的答案)
		# Set max solution time
		M.setSolverParam('mioMaxTime', 60.0)
		# 3.2 类似精度的约束,详见FusionAPI13.4.5
		# Set max relative gap (to its default value)
		M.setSolverParam('mioTolRelGap', 1e-4)
		# Set max absolute gap (to its default value)
		M.setSolverParam('mioTolAbsGap', 0.0)
		
		# 4.键入目标函数cost为Maximize或者Minimize
		# Set the objective function to (c^T * x)
		M.objective('obj', ObjectiveSense.Maximize, Expr.dot(c, x))
		
		# Solve the problem
		M.solve()
		
		# x.level()返回求解情况即x值为多少的list,其余的也是求解精度
		print('[x0, x1] = ', x.level())
		print("MIP rel gap = %.2f (%f)" % (M.getSolverDoubleInfo(
		"mioObjRelGap"), M.getSolverDoubleInfo("mioObjAbsGap")))

Practice

网上随便找一道有答案的自我验证例题
网上随便找的一道例题

def mosek_fusion_integer_test():
    """
    网上找的游泳问题,测试一下是否正确
    测试完成,可以解
    求解结果
    0     0     0     1    甲自由泳
    1     0     0     0    乙蝶泳
    0     1     0     0    丙仰泳
    0     0     1     0    丁蛙泳
    0     0     0     0    戊不参加
    :return:
    """
    c = [[66.8, 75.6, 87, 58.6],
         [57.2, 66, 66.4, 53],
         [78, 67.8, 84.6, 59.4],
         [70, 74.2, 69.6, 57.2],
         [67.4, 71, 83.8, 62.4]]

    A = [[1,1,1,1, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0],
         [0,0,0,0, 1,1,1,1, 0,0,0,0, 0,0,0,0, 0,0,0,0],
         [0,0,0,0, 0,0,0,0, 1,1,1,1, 0,0,0,0, 0,0,0,0],
         [0,0,0,0, 0,0,0,0, 0,0,0,0, 1,1,1,1, 0,0,0,0],
         [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 1,1,1,1],
         [1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0],
         [0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0],
         [0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,0],
         [0,0,0,1, 0,0,0,1, 0,0,0,1, 0,0,0,1, 0,0,0,1],
         ]
    # Create a model with the name 'swim'
    with Model("swim") as M:
        x = M.variable('x', 20, Domain.integral(Domain.greaterThan(0.0)))
        M.constraint(x, Domain.lessThan(1.0))

        # 每个队员最多只能选一种泳姿
        M.constraint("c1", Expr.dot(A[0], x), Domain.lessThan(1.0))
        M.constraint("c2", Expr.dot(A[1], x), Domain.lessThan(1.0))
        M.constraint("c3", Expr.dot(A[2], x), Domain.lessThan(1.0))
        M.constraint("c4", Expr.dot(A[3], x), Domain.lessThan(1.0))
        M.constraint("c5", Expr.dot(A[4], x), Domain.lessThan(1.0))

        # 每种泳姿必须一个人选
        M.constraint("ca", Expr.dot(A[5], x), Domain.equalsTo(1.0))
        M.constraint("cb", Expr.dot(A[6], x), Domain.equalsTo(1.0))
        M.constraint("cc", Expr.dot(A[7], x), Domain.equalsTo(1.0))
        M.constraint("cd", Expr.dot(A[8], x), Domain.equalsTo(1.0))

        # Set max solution time
        M.setSolverParam('mioMaxTime', 60.0)
        # Set max relative gap (to its default value)
        M.setSolverParam('mioTolRelGap', 1e-4)
        # Set max absolute gap (to its default value)
        M.setSolverParam('mioTolAbsGap', 0.0)

        # Set the objective function to (c^T * x)
        cc = [ii for i in c for ii in i]
        M.objective('obj', ObjectiveSense.Minimize, Expr.dot(cc, x))
        # Solve the problem
        M.solve()

        # Get the solution values
        print('[x] = ', x.level())
        print("MIP rel gap = %.2f (%f)" % (M.getSolverDoubleInfo(
            "mioObjRelGap"), M.getSolverDoubleInfo("mioObjAbsGap")))

[未完待续]

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值