我想求解具有以下目标函数的混合整数线性规划:
J = 最大化 (f1(x) + f2(x)) 受约束:成本(x) <= 阈值
其中 x 是选定变量的集合,f1 和 f2 是两个评分函数,cost 是成本函数。
f2 是基于所选变量之间相似性的函数。我不知道如何在纸浆中制定这个功能。
这是我的最小工作示例,其中函数 f2 是两种成分之间的相似度,我想添加similarity[i][j]到目标函数 ifj已经在选定的变量中,但不知道该怎么做。
import numpy as np
import pulp
threshold = 200
model = pulp.LpProblem('selection', pulp.LpMaximize)
similarity = np.array([[1., 0.08333333, 0.1, 0., 0., 0.0625],
[0.08333333, 1., 0.33333333,
0., 0.11111111, 0.07692308],
[0.1, 0.33333333, 1., 0.2, 0., 0.09090909],
[0., 0., 0.2, 1., 0., 0.],
[0., 0.11111111, 0., 0., 1., 0.27272727],
[0.0625, 0.07692308, 0.09090909, 0., 0.27272727, 1.]])
ingredients = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 CVXOPT 解决线性程序。有 2n 个变量,x_1,...,x_2n。LP的形式为
最小 x_{n+1}+...+x_{2n}
st Ax \leq b
这里,A 和 b 是固定的。从这里看起来很简单。由于我正在优化后半部分变量的总和,因此我创建了一个包含 n 个零和 n 个 1 的向量:c = (0,...,0,1,...,1) 并具有以下代码 (假设 A 和 b 已经计算):
c = [1]*(2*k + 2)
for i in range(k + 1):
c[i] = 0
c = matrix(c)
sol=solvers.lp(c,A,b)
print(sol['x'])
Run Code Online (Sandbox Code Playgroud)
此代码直接来自 CVXOPT 文档。但是,我收到一条错误消息:
TypeError: 'c' must be a dense column matrix
Run Code Online (Sandbox Code Playgroud)
我查了一下,但在我看来,调用 matrix() 应该已将 c 转换为适当的类型。有谁知道如何解决这一问题?
我使用 PuLP 库编写了以下 Python 代码,用于使用整数规划公式解决背包问题。我使用字符串生成 LpVariable 命令并添加约束,然后使用 eval 执行它们。有没有办法在不使用 eval 的情况下做到这一点?
from pulp import *
#Knapsack problem
items = input ('Enter the number of items :')
items = int(items)
#print('Enter %d items one by one')
print ('Enter {0} items profit one by one'.format(items))
obj = []
weight = []
knapweight = 0
for i in range(0,items):
print('Enter {0} item profit : '.format(i+1))
obj.append(input())
for i in range(0, items):
print('The profit at {0} is {1}'.format(i, obj[i]))
print ('\nEnter {0} items weights …Run Code Online (Sandbox Code Playgroud) 我在 Python 上使用 Gurobi,我的代码需要一个 Model.optimize() 函数循环。有没有办法让这个功能静音,这样它就不会输出段落?
谢谢。
当有N个变量和R个约束时,整数线性规划(ILP) 问题的运行时间复杂度是多少?出于编码目的,我使用了 Matlab 的intlinprog函数。任何参考都会有所帮助。
我有一个受线性约束的优化问题。如何知道哪种方法更适合建模和解决问题。我通常会询问将问题作为可满足性问题(SAT 或 SMT)来解决还是作为线性规划问题(ILP 或 MILP)来解决。
我对两者都没有太多了解。因此,如果您有任何答案,请简化您的答案。
我需要为线性规划问题实现一个求解器。所有的限制都是 <= 的,例如
5x + 10y <= 10
可以有任意数量的这些限制。此外,x>=0 y>=0 隐式。
我需要找到最优解(最大值)并在 matplotlib 中显示可行区域。我通过实施单纯形方法找到了最佳解决方案,但我不知道如何绘制图形。
我发现的一些方法:
我当时正在开发一个示例 MIP 程序,该程序选择了男女混合运动队的首发阵容,并发现了一个在 Google OR 工具中得出非最佳结果的小示例。
基础知识:从 n>5 名玩家组成的团队中选择 5 名首发。至少两名首发必须是女性。被划伤的玩家无法启动。目标函数是初学者技能水平的简单总和。代码是:
import pandas as pd
from ortools.linear_solver import pywraplp
# %% Read the data from an external file
pname = ["Tom", "Joe", "Bill", "Mike", "Frank", "Mary", "Sue", "JoAnn"]
skill = [ 11.0, 13.0, 11.0, 12.0, 14.0, 10.0, 10.0, 7.0]
female = [ 0, 0, 0, 0, 0, 1, 1, 1]
scratch = [ 0, 0, 0, 0, 1, 0, 0, 0]
# %% Create the mip solver with the SCIP …Run Code Online (Sandbox Code Playgroud) python linear-programming python-3.x or-tools mixed-integer-programming
我有许多变量表示我可以更换的设备。更换后,它们会改善影响指标[I]。每个还具有相关的年度成本节省[S]和更换成本[C]。
n <- 1000 # variable count
# impact
# negative for use in minimization function
I <- -rnorm(n, mean=20000, sd=8000)
# cost savings
s <- rnorm(n, mean=2500, sd=1000)
# replacement cost
c <- rnorm(n, mean=15000, sd=5000)
Run Code Online (Sandbox Code Playgroud)
我想选择要替换的组件,以在预算范围内最大限度地发挥全面影响,同时仍然确保整个项目(作为一个整体)满足简单的投资回收目标。
payback_goal <- 3
budget <- 1000000
Run Code Online (Sandbox Code Playgroud)
这个问题由下面的方程描述。
我正在努力设置这个lpSolveAPI。具体来说,我不知道如何合并方程式。3.
library(lpSolveAPI)
m <- 2 # number of constraints, disregarding binary constraint set by type
my.lp <- make.lp(m, n)
set.row(my.lp, 1, c)
# i don't think this …Run Code Online (Sandbox Code Playgroud) 我今天可能需要实现整数线性规划,我想知道是否有任何伪代码或相对无痛(评论很好)的源代码解释如何实现它?强烈偏爱伪代码.
请注意,我并不是在寻找一个具有所有"微调"的严肃完整项目,以获得最佳性能.我正在寻找最基本的求解器,它演示整数线性编程如何工作与逐个尝试所有选项.
谢谢.
python ×5
pulp ×2
python-3.x ×2
algorithm ×1
cvxopt ×1
gurobi ×1
integer ×1
lpsolve ×1
matlab ×1
matplotlib ×1
numpy ×1
optimization ×1
or-tools ×1
pseudocode ×1
r ×1
sat ×1
solver ×1