I'm trying to convert the tutorial's Australia program to use arrays.
I'm having trouble with the output, to wit:
% 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\""]
*/
How can I get show to make the \n a new line, and not escape the quotes in the constants? Like perl double quotes instead of single quotes?
And/or is there any way to define the constants without the quotes? Like perl qw?
You made a small mistake in your code: show is used to output a variable. So it is used like:
output["this is variable x:" ++ show(x)]
Note that \(x)
is a shorthand for "++show(x)++"
The output
statement itself will print the given array as a formatted string. This formatted string can contain escaped character as expected. The correct output statement for your model is thus:
output ["\n\(states[j]) = \(colors[color[j]])" | j in indices];