Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build-in show cannot convert array of var enum to string #711

Closed
marvinvis opened this issue Jun 19, 2023 · 3 comments
Closed

build-in show cannot convert array of var enum to string #711

marvinvis opened this issue Jun 19, 2023 · 3 comments
Labels
bug resolved Issue is resolved and the feature or fix will be part of next release

Comments

@marvinvis
Copy link

I have the following code:

enum Unit = {Inch, Meter, Foot};
array[1..3] of var Unit: a;

array[1..3] of string: m = [show(a[i]) | i in 1..3];

output([show(m)]);

running this gives:


ffff:4.1-24
  in variable declaration for 'm'
  in array comprehension expression
    with i = 1
  in call 'show'
MiniZinc: type error: no function or predicate with this signature found: `_toString_Unit(var Unit,bool,bool)'
Cannot use the following functions or predicates with the same identifier:
function string : _toString_Unit(array [$U] of set of Unit: x,bool: b,bool: json);
    (argument 1 expects type array[$_] of set of int, but type var Unit given)
function string : _toString_Unit(set of Unit: x,bool: b,bool: json);
    (argument 1 expects type set of Unit, but type var Unit given)
function string : _toString_Unit(opt int: x,bool: b,bool: json);
    (argument 1 expects type opt int, but type var Unit given)
function string : _toString_Unit(array [$U] of opt Unit: x,bool: b,bool: json);
    (argument 1 expects type array[$_] of opt int, but type var Unit given)

Process finished with non-zero exit code 1.
Finished in 340msec.

Version 2.7.5

@Dekker1 Dekker1 added the bug label Jun 19, 2023
@guidotack guidotack removed the bug label Jun 20, 2023
@cyderize cyderize added bug resolved Issue is resolved and the feature or fix will be part of next release labels Jun 20, 2023
@cyderize
Copy link
Member

cyderize commented Jun 20, 2023

This bug will be fixed in the next release. However, it should be noted that in your example, the declaration of m would be evaluated during compilation, and so there would be no actual solution yet, meaning that the strings would just end up being the names of the variables introduced to represent each a[i].

If you want to create a string based on actual solution values and use it from the output item, you can annotate the declaration with :: output_only and in the RHS, use fix:

enum Unit = {Inch, Meter, Foot};
array[1..3] of var Unit: a;

array[1..3] of string: m :: output_only = [show(fix(a[i])) | i in 1..3];

output [show(m), "\n"];

@marvinvis
Copy link
Author

Thanks for explaining this.
The example was created as a minimal case to show the bug. What I'm doing is to create a vis_gantt using the array of string for the labels (3rd argument). For show_gantt it works as expected by your suggestion, because I place it in an output expression.
I can't get it to work for vis_gantt(...), where I use:

constraint vis_gantt(start, duration, names);

could you please show what I'm doing wrong here?

@cyderize
Copy link
Member

cyderize commented Jun 20, 2023

The third argument expects an array mapping each index of the start/duration arrays to the string name to display, so you can get them using a comprehension like [i: show(i) | i in index_set(start)]. So the names have nothing to do with the variables, just the (par) index set of the arrays.

Here's an example:

include "globals.mzn";
include "ide/vis.mzn";

enum Task = { A, B, C, D, E };

array [Task] of var 1..10: start;
array [Task] of var 1..10: duration;

constraint disjunctive(start, duration);

constraint vis_gantt(start, duration, [i: show(i) | i in index_set(start)]);

However, typically you would actually just use the two argument version - it will automatically get the names from the index set, so just using

constraint vis_gantt(start, duration);

Would give the same result.

You could however use the three argument version if you wanted to give different names that weren't just generated from the enum variant names:

include "globals.mzn";
include "ide/vis.mzn";

enum Task = { A, B, C, D, E };

array [Task] of var 1..10: start;
array [Task] of var 1..10: duration;
array [Task] of string: names = [A: "First task", B: "Second task", C: "Third task", D: "Fourth task", E: "Fifth task"];
constraint disjunctive(start, duration);

constraint vis_gantt(start, duration, names);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug resolved Issue is resolved and the feature or fix will be part of next release
Projects
None yet
Development

No branches or pull requests

4 participants