- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 Microsoft Solver Foundation 解决 C# 中的二进制线性问题。我不知道为什么我得到错误的答案。目标值应该是 41.1,但我得到了 213。5 个变量的值应该是 1,其他变量应该是 0。但是我得到了很多错误值的变量。
矩阵每一行的总和应 <= 1。这是我的约束,正如您在 Constraint_arr 中看到的那样,我得到了正确的约束。
感谢您的帮助。
定义决策变量:
SolverContext context = SolverContext.GetContext();
Model model = context.CreateModel();
Decision[,] x = new Decision[name_column.Length, 7];
for (int i = 0; i < name_column.Length; i++)
{
for (int j = 0; j < 7; j++)
{
x[i, j] = new Decision(Domain.IntegerRange(0,1), "x" + i + j);
}
}
for (int i = 0; i < name_column.Length; i++)
{
for (int j = 0; j < 7; j++)
{
model.AddDecisions(x[i, j]);
}
}
创建约束并将其添加到模型中:
Term[] Constraint_arr = new Term[name_column.Length];
Term tempC;
int jj;
for (int i = 0; i < name_column.Length; i++)
{
tempC = 0;
for (jj= 0; jj < 7; jj++)
{
if(vars_Matrix[i,jj] == 1)
{
tempC += x[i,jj];
}
}
Constraint_arr[i] = tempC;
model.AddConstraints("constraint" + i, Constraint_arr[i] <= 1);
}
创建目标函数:
Term objective_Func = 0;
Term tempZ;
for (int i = 0; i < name_column.Length; i++)
{
tempZ = 0;
for (int j = 0; j < 7; j++)
{
tempZ += x[i, j] * ratio[i];
}
objective_Func+= tempZ;
}
model.AddGoal("Goal", GoalKind.Maximize, objective_Func);
打印答案:
Solution solution = context.Solve(new SimplexDirective());
Report report = solution.GetReport();
for (int i = 0; i < name_column.Length; i++)
{
for (int j = 0; j < 7; j++)
{
Console.Write(x[i, j]);
}
Console.WriteLine();
}
Console.Write("{0}", report);
Console.ReadLine();
最佳答案
以下 MiniZinc 模型的目标最大值为 14:
set of int: rows = 1..5;
set of int: cols = 1..7;
array[rows, cols] of 0..1: vars_Matrix = [|0, 0, 1, 0, 1, 1, 1
|0, 0, 1, 1, 0, 1, 1
|0, 0, 1, 0, 0, 0, 0
|0, 0, 1, 1, 0, 1, 1
|0, 0, 0, 0, 1, 0, 0|];
array[cols] of var 0..1: c;
var int: obj;
% constraint
% obj = sum(i in rows)(
% sum(j in cols) (
% c[i] * vars_Matrix[i, j]
% )
% );
constraint
obj = sum([ sum([ c[i] * vars_Matrix[i, j] | j in cols ]) | i in rows ]);
solve maximize(obj);
输出
c = array1d(1..7, [1, 1, 1, 1, 1, 1, 1]);
obj = 14;
从以下 Z3py 模型获得相同的结果:
from z3 import *
s = Optimize()
Rows = range(5);
Cols = range(7);
vars_Matrix = [[0, 0, 1, 0, 1, 1, 1],
[0, 0, 1, 1, 0, 1, 1],
[0, 0, 1, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 1, 1],
[0, 0, 0, 0, 1, 0, 0]]
c = [Int("c" + str(i+1)) for i in Rows]
obj = Int("obj")
for i in Rows:
s.add(c[i] >= 0, c[i] <= 1)
s.add(obj == Sum( [ Sum( [ c[i] * vars_Matrix[i][j] for j in Cols ] ) for i in Rows ] ))
s.maximize(obj)
if sat == s.check():
print(s.model())
else:
print("No solution. Sorry!")
关于c# - Microsoft Solver Foundation 为简单的 ILP 给出了错误的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70628769/
打开几个编写宏/代码的示例,我突然被一个似乎安全的代码卡住了。求解器 (SOLVER.XLAM) 这是值得注意的,我想从我的系统中删除它。我想这不是一个相关的代码。谁能告诉我我应该怎么做?我已经安装了
是否可以使用 MSF api 将变量指定为半整数(V = 0 或 a = 5; Q1 = 5; Q2 10 * VPositive<= V <= 20 * VPositive ] ] 如果您使用
我正在研究线性和非线性编程(优化)框架的选项。 要求是: 支持大约线性和非线性规划问题。 100-1000 个变量和最多约 1000 个约束(我认为这很简单)。非线性问题具有涉及多个变量的乘法或除法的
SMT-Solver 可用于约束求解。众所周知,CSP 求解器也用于约束求解多年。那么 SMT 求解器与 CSP 求解器相比有什么优势呢? 最佳答案 这完全取决于你想做什么。您可以将两者都转换为 SA
我正在查看 Caffe LeNet 教程 here我想到了一个问题: 这两个代码有什么区别: self.solver.step(1) 和 self.solver.net.forward() # tr
我正在使用 CBC 求解器在 PyCharm 上解决优化问题,但一直遇到此错误: 错误:求解器 (cbc) 返回非零返回代码 (3221225781) 和 pyutilib.common._excep
我是 microsoft excels 求解器的用户,我很确定不可能求解最大化两个值。我想知道是否有人可能有另一种聪明的方法来做到这一点。 基本上,我有一列介于 1 到 30 之间的数字,我需要查看它
我想从数值的角度理解混合建模(特别是状态事件)背后的一般思想(尽管我不是数学家 :))。鉴于以下 Modelica 模型: model BouncingBall constant Real g
最近,我开始研究形式验证技术。在文献中,模型检查器和求解器可以以某种方式互换使用。 但是,模型检查器和求解器如何相互连接? p.s.如果建议提供一些论文或链接,我将不胜感激。 最佳答案 为了执行模型检
我希望这对某人来说是显而易见的。我只使用过 GLPK/MathProg。 我无法弄清楚 GNU MathProg(在 GLPK 内)中的语法来执行以下操作,例如: set PartsOfWeek; s
我有一个(对我来说)非常复杂的问题。我有两个向量: vectora <- c(111, 245, 379, 516, 671) vectorb <- c(38, 54, 62, 67, 108) 此外
我在 VBA 循环中使用 Excel 2007 中的内置求解器来解决许多不同的问题。有时,求解器会达到最大时间,这会导致出现弹出对话框,询问用户是否要继续、停止或结束。在所有情况下,我都希望它结束
我在 PC 上的 Excel 2010 中创建了一个程序,该程序依赖于 Excel 的内置求解器。我确保它适用于 PC 版 Excel 2010 和 Excel 2013。然后我尝试在 Excel 2
在汽车行业,当您购买汽车时,您有数千种不同的组件可供选择。并非每个组件都是可组合的,因此对于每辆汽车,都存在许多用命题逻辑表达的规则。就我而言,每辆车都有 2000 到 4000 条规则。 它们看起来
有没有办法让我们知道解算器中添加了多少约束?例如,我们初始化一个 z3 求解器 s = Solver(),然后使用 s.add() 添加约束。我们如何获得最终添加到求解器中的约束数量? 最佳答案 您可
我正在尝试修改 ceres 的默认行为,即计算残差的平方和作为成本函数。我希望它只计算总和(残差已经以只能为正的方式计算) 根据文档,我应该使用 ConditionedCostFunction 这是我
我有这个方法来求解根据增量返回 2 种类型数组的二次方程(如果小于零则有复数解) public static final String[] quadEquationSolver(double a, d
p = Int('p') q = Int('q') s = Solver() s.add(1<=p<=9, 1<=q<=19, 5<(3*p-4*q)<10) s.check() print s.mo
“问题”在标题中不应该是问题的情况。 我想为一组问题(类 Problem 的所有子项)实现一个求解器(类 Solver),这些问题或多或少共享同一组方法。我目前的设计是这样的: 在solver.h :
我为所有方向创建了一个单词求解器。它可以水平、垂直和反向查找单词。然而,我在让它走向各个方向时遇到了问题。所以把“你好”放在: H E i l x L p q c L O m 任何人
我是一名优秀的程序员,十分优秀!