python time.time()怎么保留整数位_Python-如何设置CVXOPT中的整数线性编程(ILP)函数的时间限制(Python-How to set time limit for The...

Python-如何设置CVXOPT中的整数线性编程(ILP)函数的时间限制(Python-How to set time limit for The integer linear programming(ILP) function in CVXOPT)

如何设置CVXOPT中的整数线性编程(ILP)函数的时间限制? 假设这是我的解算器:

status,solution = glpk.ilp(W, G.T, h,B=set(range(len(W))))

How to set time limit for The integer linear programming(ILP) function in CVXOPT? suppose this is my solver:

status,solution = glpk.ilp(W, G.T, h,B=set(range(len(W))))

原文:https://stackoverflow.com/questions/39389692

更新时间:2020-01-29 02:26

最满意答案

请尝试以下方法:

from cvxopt import solvers

solvers.options['glpk'] = {'tm_lim' : 1000} # time-limit of 1s (glpk expects [ms])

status,solution = glpk.ilp(W, G.T, h,B=set(range(len(W))))

在此处的文档中描述了传递求解器选项(在cvxopt内)。

glpk的可用选项在此处的手册中有所描述

编辑:正如评论中所提到的, tm_lim是要设置的变量,而不是tm lim !

Try the following:

from cvxopt import solvers

solvers.options['glpk'] = {'tm_lim' : 1000} # time-limit of 1s (glpk expects [ms])

status,solution = glpk.ilp(W, G.T, h,B=set(range(len(W))))

Passing solver-options (within cvxopt) is described in the docs here.

The available options of glpk are described in it's manual here

Edit: As mentioned in the comments, tm_lim is the variable to set, not tm lim!

2016-09-08

相关问答

实现此操作的最佳方法是安装预编译的二进制文件。 首先,下载适用于Windows的numpy的MLK版本 。 然后,下载cvxopt的安装程序并运行它。 选择与您的Python版本相对应的构建非常重要。 我链接的版本是针对Windows的标准Python。 他们也应该使用Enthought的发行版。 The best way to get this running is by installing a pre-compiled binary. First, download the MLK buil

...

我最终用元解决方案解决了这个问题,我的一个朋友提出了这个问题。 由于选择位置X的产品推断出其他允许的选择(即X + 1和X + 2),因此优化产品组而不是单个产品是有意义的。 我已经建立了它并且它的工作非常好。 谢谢你的回复! I ended up solving this problem with a meta solution, that a friend of mine came up with. Since choosing the product with position X infe

...

我解决我的问题: conda install -c conda-forge glpk=4.60

conda install -c conda-forge cvxopt=1.1.8

基本上,我们需要使用一贯的公寓锻造的一切。 例如他们的glpk和cvxopt。 I resolve my issue with: conda install -c conda-forge glpk=4.60

conda install -c conda-forge cvxopt=1.1.8

Basically, we

...

请尝试以下方法: from cvxopt import solvers

solvers.options['glpk'] = {'tm_lim' : 1000} # time-limit of 1s (glpk expects [ms])

status,solution = glpk.ilp(W, G.T, h,B=set(range(len(W))))

在此处的文档中描述了传递求解器选项(在cvxopt内)。 glpk的可用选项在此处的手册中有所描述 编辑:正如评论中所提到的, tm_lim是

...

它并不像人们想象的那么简单。 典型的投资组合优化问题是最小化受目标回报影响的风险,目标回报是具有二次目标的线性约束问题; 即,二次程序(QP)。 minimize x^T.P.x

subject to sum(x_i) = 1

avg_ret^T.x >= r_min

x >= 0 (long-only)

你想要的是,在目标风险最大化的情况下最大化回报,是一个二次约束的二次规划(QCQP),看起来像: max

...

你的目标是真的 ExpWin(t) = choose(N,t)*(A-C*t)

其中t是一个整数变量,N,A,C是常数。 这是一个非线性函数,所以线性MIP求解器将无法处理这个问题。 对于这个问题它很愚蠢,但总的来说,我们可以将其线性化。 引入二元变量x(i): x(i) = 1 if tickets=i

0 otherwise

这可以通过执行 sum(i,x(i)) = 1

sum(i,i*x(i)) = tickets

这只有在可变门票的范围很小时才有意义(在你的情况下为

...

问题是矩阵对象的构造函数将其解释为整数类型,而它应该是double。 如果用明确的双数填充列表,它应该可以工作。 从cvxopt的源代码 : if type(c) is not matrix or c.typecode != 'd' or c.size[1] != 1:

raise TypeError("'c' must be a dense column matrix")

检查.. import cvxopt

k = 20

c = [1]*(2*k + 2)

for i in

...

您的代码基本上是正确的,但需要两个小的修改: c-vector也必须是double。 变量x [0]和x [1]应该是整数,而不是二进制。 然后,通过以下方式给出了一个有效的解 import numpy as np

import cvxopt

c=cvxopt.matrix([0,-1],tc='d')

G=cvxopt.matrix([[-1,1],[3,2],[2,3],[-1,0],[0,-1]],tc='d')

h=cvxopt.matrix([1,12,12,0,0],tc='d')

...

这是一个愚蠢的问题,但是由于周围可能会有更多的懒人,请使用这一系列的MATLAB代码(使用带编号的参数来提高可读性): x = quadprog(C1, C2, C3, C4, C5, C6, C7, C8, ...)

Python中的等价物将是: import numpy

import cvxopt

n = C1.shape[1] # This is for readability only

P = C1

q = C2

G = numpy.vstack([C3, -numpy.e

...

至1: 是的,即使用户无权访问php.ini,用户也可以调用该函数。 您可以使用safe_mode(5.4中的DEPRECATED和REMOVED )或者disabled_functions ini指令来避免这种情况。 但是, set_time_limit具有set_time_limit的误导性(如下所示)。 如果您不想更改php.ini,则可以在Web上下文中设置服务器级别的最大响应时间或使用的cgi配置。 To 2也回答3:禁止该功能和/或设置max_execution_time 。 注意 :

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值