我怎样才能让 MiniZinc 输出来解释转义,而不是按字面打印它们?
How can I get MiniZinc output to interpret escapes, instead of printing them literally?
我正在尝试将教程的澳大利亚程序转换为使用数组。
我在输出方面遇到问题,即:
% Coloring Australia using nc colors
int: nc = 3; % number of colors
int: ns = 7; % number of states
array[1..nc] of string: colors = ["red", "green", "blue"];
array[1..ns] of string: states = ["wa","nt","sa","q","nsw","v","t"];
array[1..ns] of var 1..ns: indices = [1,2,3,4,5,6,7];
array[1..ns] of var 1..nc: color; % computed color for each state
% I want to use the name as a mnemonic for the index of the state
var int: wa=1; % I know of no alternative to the brute force method
var int: nt=2; var int: sa=3; var int: q=4;
var int: nsw=5; var int: v=6; var int: t=7;
constraint color[wa] != color[nt]; % abutting states
constraint color[wa] != color[sa];
constraint color[nt] != color[sa];
constraint color[nt] != color[q];
constraint color[sa] != color[q];
constraint color[sa] != color[nsw];
constraint color[sa] != color[v];
constraint color[q] != color[nsw];
constraint color[v] != color[nsw];
solve satisfy;
/*
I want a loop to print it out like this:
"wa" = "blue"
"nt" = "green"
...
*/
output [
show( ["\n\(states[j]) = \(colors[color[j]])" | j in indices])
];
/* prints
["\n\"wa\" = \"blue\"", "\n\"nt\" = \"green\"", "\n\"sa\" = \"red\"", "\n\"q\" = \"blue\"", "\n\"nsw\" = \"green\"", "\n\"v\" = \"blue\"", "\n\"t\" = \"red\""]
*/
如何让 \n 换行,而不转义常量中的引号?喜欢 perl 双引号而不是单引号?
And/or 有什么方法可以定义不带引号的常量吗?喜欢 perl qw?
你的代码犯了一个小错误:show是用来输出一个变量的。所以它像这样使用:
output["this is variable x:" ++ show(x)]
请注意 \(x)
是 "++show(x)++"
的 shorthand
output
语句本身会将给定的数组打印为格式化字符串。此格式化字符串可以按预期包含转义字符。因此,您的模型的正确输出语句是:
output ["\n\(states[j]) = \(colors[color[j]])" | j in indices];
我正在尝试将教程的澳大利亚程序转换为使用数组。
我在输出方面遇到问题,即:
% Coloring Australia using nc colors
int: nc = 3; % number of colors
int: ns = 7; % number of states
array[1..nc] of string: colors = ["red", "green", "blue"];
array[1..ns] of string: states = ["wa","nt","sa","q","nsw","v","t"];
array[1..ns] of var 1..ns: indices = [1,2,3,4,5,6,7];
array[1..ns] of var 1..nc: color; % computed color for each state
% I want to use the name as a mnemonic for the index of the state
var int: wa=1; % I know of no alternative to the brute force method
var int: nt=2; var int: sa=3; var int: q=4;
var int: nsw=5; var int: v=6; var int: t=7;
constraint color[wa] != color[nt]; % abutting states
constraint color[wa] != color[sa];
constraint color[nt] != color[sa];
constraint color[nt] != color[q];
constraint color[sa] != color[q];
constraint color[sa] != color[nsw];
constraint color[sa] != color[v];
constraint color[q] != color[nsw];
constraint color[v] != color[nsw];
solve satisfy;
/*
I want a loop to print it out like this:
"wa" = "blue"
"nt" = "green"
...
*/
output [
show( ["\n\(states[j]) = \(colors[color[j]])" | j in indices])
];
/* prints
["\n\"wa\" = \"blue\"", "\n\"nt\" = \"green\"", "\n\"sa\" = \"red\"", "\n\"q\" = \"blue\"", "\n\"nsw\" = \"green\"", "\n\"v\" = \"blue\"", "\n\"t\" = \"red\""]
*/
如何让 \n 换行,而不转义常量中的引号?喜欢 perl 双引号而不是单引号?
And/or 有什么方法可以定义不带引号的常量吗?喜欢 perl qw?
你的代码犯了一个小错误:show是用来输出一个变量的。所以它像这样使用:
output["this is variable x:" ++ show(x)]
请注意 \(x)
是 "++show(x)++"
output
语句本身会将给定的数组打印为格式化字符串。此格式化字符串可以按预期包含转义字符。因此,您的模型的正确输出语句是:
output ["\n\(states[j]) = \(colors[color[j]])" | j in indices];