当前位置:   article > 正文

mosek使用记录_mosek的说明文档

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")))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

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")))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

[未完待续]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/593115
推荐阅读
相关标签
  

闽ICP备14008679号