- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Minizinc 程序,可以根据时间间隔给定一组价格,为并网电池生成最佳充电/放电时间表。
我的程序可以运行(有点;有一些注意事项),但我的问题是关于两个“约束”语句,它们实际上只是赋值语句:
constraint forall(t in 2..T)(MW_SETPOINT[t-1] - SALE[t] = MW_SETPOINT[t]);
constraint forall(t in 1..T)(PROFIT[t] = SALE[t] * PRICE[t]);
这些只是意味着能量 SALES
是 MW_SETPOINT
从 t-1
到 1
的增量,并且 PROFIT
是每个间隔的 SALE
* PRICE
。因此,将它们声明为“约束”对我来说似乎违反直觉。但是我无法在不抛出语法错误的情况下将它们表述为赋值语句。
问题:
constraint
中为数组赋值是在 Minizinc 中推荐/惯用的方式?上下文的完整程序:
% PARAMS
int: MW_CAPACITY = 10;
array[int] of float: PRICE;
% DERIVED PARAMS
int: STARTING_MW = MW_CAPACITY div 2; % integer division
int: T = length(PRICE);
% DECISION VARIABLE - MW SETPOINT EACH INTERVAL
array[1..T] of var 0..MW_CAPACITY: MW_SETPOINT;
% DERIVED/INTERMEDIATE VARIABLES
array[1..T] of var -1*MW_CAPACITY..MW_CAPACITY: SALE;
array[1..T] of var float: PROFIT;
var float: NET_PROFIT = sum(PROFIT);
% CONSTRAINTS
%% If start at 5MW, and sell 5 first interval, setpoint for first interval is 0
constraint MW_SETPOINT[1] = STARTING_MW - SALE[1];
%% End where you started; opt schedule from arbitrage means no net MW over time
constraint MW_SETPOINT[T] = STARTING_MW;
%% these are really justassignment statements for SALE & PROFIT
constraint forall(t in 2..T)(MW_SETPOINT[t-1] - SALE[t] = MW_SETPOINT[t]);
constraint forall(t in 1..T)(PROFIT[t] = SALE[t] * PRICE[t]);
% OBJECTIVE: MAXIMIZE REVENUE
solve maximize NET_PROFIT;
output["DAILY_PROFIT: " ++ show(NET_PROFIT) ++
"\nMW SETPOINTS: " ++ show(MW_SETPOINT) ++
"\nMW SALES: " ++ show(SALE) ++
"\n$/MW PRICES: " ++ show(PRICE)++
"\nPROFITS: " ++ show(PROFIT)
];
它可以运行
minizinc opt_sched_hindsight.mzn --solver org.minizinc.mip.coin-bc -D "PRICE = [29.835, 29.310470000000002, 28.575059999999997, 28.02416, 28.800690000000003, 32.41052, 34.38542, 29.512390000000003, 25.66587, 25.0499, 26.555529999999997, 28.149440000000002, 30.216509999999996, 32.32415, 31.406609999999997, 36.77642, 41.94735, 51.235209999999995, 50.68137, 64.54481, 48.235170000000004, 40.27663, 34.93675, 31.10404];"```
最佳答案
你可以玩Array Comprehensions : (引自文档)
Array comprehensions have this syntax:
<array-comp> ::= "[" <expr> "|" <comp-tail> "]"
For example (with the literal equivalents on the right):
[2*i | i in 1..5] % [2, 4, 6, 8, 10]
Array comprehensions have more flexible type and inst requirements than set comprehensions (see Set Comprehensions).
Array comprehensions are allowed over a variable set with finite type, the result is an array of optional type, with length equal to the cardinality of the upper bound of the variable set. For example:
var set of 1..5: x;
array[int] of var opt int: y = [ i * i | i in x ];The length of array will be 5.
Array comprehensions are allowed where the where-expression is a
var bool
. Again the resulting array is of optional type, and of length equal to that given by the generator expressions. For example:var int x;
array[int] of var opt int: y = [ i | i in 1..10 where i != x ];The length of the array will be 10.
The indices of an evaluated simple array comprehension are implicitly
1..n
, wheren
is the length of the evaluated comprehension.
示例:
int: MW_CAPACITY = 10;
int: STARTING_MW = MW_CAPACITY div 2;
array [int] of float: PRICE = [1.0, 2.0, 3.0, 4.0];
int: T = length(PRICE);
array [1..T] of var -1*MW_CAPACITY..MW_CAPACITY: SALE;
array [1..T] of var 0..MW_CAPACITY: MW_SETPOINT = let {
int: min_i = min(index_set(PRICE));
} in
[STARTING_MW - sum([SALE[j] | j in min_i..i])
| i in index_set(PRICE)];
array [1..T] of var float: PROFIT =
[SALE[i] * PRICE[i]
| i in index_set(PRICE)];
solve satisfy;
输出:
~$ minizinc test.mzn
SALE = array1d(1..4, [-10, -5, 0, 0]);
----------
请注意 index_set(PRICE)
只是 1..T
而 min(index_set(PRICE))
也不是别的但是 1
,所以可以将上面的 array comprehensions 也写成
array [1..T] of var 0..MW_CAPACITY: MW_SETPOINT =
[STARTING_MW - sum([SALE[j] | j in 1..i])
| i in 1..T];
array [1..T] of var float: PROFIT =
[SALE[i] * PRICE[i]
| i in 1..T];
关于optimization - 在 minizinc 中使用 forall() 谓词作为没有 'constraint' 的赋值语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61104833/
我想在 MiniZinc 中用相同的项目创建两个数组,不一定按相同的顺序。在这里,A0 中的每一项也应该在 A1 : array[1..3] of var int:A0; array[1..3] of
假设我想计算 {1,2,..100} 的 80 个元素子集的数量,使它们的总和为 3690。 我有以下模型: array[1..100] of var 0..1: b; constraint (sum
我看到 MiniZinc Handbook 中的各个地方都使用了 $ (主要在 Reference Manual 部分),但我一直找不到定义。有人可以向我解释一下吗?谢谢。 最佳答案 MiniZinc
在斑马谜题 ( http://rosettacode.org/wiki/Zebra_puzzle#MiniZinc ) 的解决方案中,有一个约束条件,规定其中一只宠物必须是斑马: var 1..5:
在斑马谜题 ( http://rosettacode.org/wiki/Zebra_puzzle#MiniZinc ) 的解决方案中,有一个约束条件,规定其中一只宠物必须是斑马: var 1..5:
我正在尝试解决 MiniZinc 中的一个练习,其中部分排序关系由二维数组给出: enum NODE = { A, B, C, D, E }; int : SOURCE = 1; int : TARG
我正在熟悉 MiniZinc 的基础知识。因此,借助 MiniZinc IDE,我编写了如下代码片段 solve satisfy; string: s1 = "hello"; string: s2 =
锌规范是这样说的: If no output item is present, the implementation should print all the global variables and
MiniZinc 中的 channel 是什么?你能提供一个简单的例子来解释 channel 吗?最后,什么是逆? 最佳答案 两者都用于建立两个数组之间的双向关系。 设 f 是一个数组,index_s
FlatZinc 文档说 只有非标谓词必须在 FlatZinc 模型的顶部声明: Predicates used in the model that are not standard FlatZinc
我有一个二维网格,其中某些单元格值会产生不同的分数,我想通过为单元格分配值来最大化分数。是否可以跟踪求解器在求解过程中尝试的每个网格?下面是模型的一个片段,只是为了演示这个想法。 int: i_cou
picat求解器 (v. 2.6#2) 指出示例模型 knights.mzn包含在 minizinc 存储库中,特此复制粘贴: % RUNS ON mzn20_fd % RUNS ON mzn-fzn
尝试学习 minizinc,但在完成示例之后,我是否可以确认,如果我想获得多个输出,或者有一种更“自然的 minizinc”方式来获得它,我实际上必须编写一些程序语言。 例如,假设我想让所有不同的数字
练习是: n个人想要拍一张合影。每个人都可以给出他或她旁边的偏好想要放置在照片上。要解决的问题是找到满足最大数量的放置位置偏好。 到目前为止我编写的代码: include "globals.mzn";
我正在尝试编写一个带有嵌套循环的输出语句,以及在外部级别的重要输出。如果 Minizinc 有一个顶级 for 命令,我会做类似的事情 for (f in Foo) ( output(["Foo:
我想检查 1 到 5 之间的哪个数字没有出现在数组组中,并将这个(或多个)数字放在另一个数组中。 g=2; set of int: GROUPS = 1..g; groups = [{1, 3}, {
问题 给定一个 MiniZinc 字符串数组: int: numStats; set of int: Stats = 1..numStats; array[Stats] of string: stat
我有一个变量,它应该只在特定条件下有一个值,所以我想它需要是一个可选变量。如果满足该条件,则可选变量应限制为集合的元素。 问题是,MiniZinc 似乎不喜欢可选变量和集合。 如何重写,使 MiniZ
我如何使用嵌套 for 循环(就像下面 java 所做的那样)在 Minizinc 中生成/填充数组? int[][] input1 = {{1,1,1}, {3,3,3}, {5,5,5} }; i
我正在使用 minizinc 和 gecode 以分布式方式解决最小化问题。我有多个分布式服务器,它们使用相同的输入来解决相同的模型,并且我希望所有服务器都能获得相同的解决方案。 问题是模型有多个解决
我是一名优秀的程序员,十分优秀!