I solved my problem. I discovered make_table and used that to write
the methods
[makePreferencesObjective(prob:choco/Problem): PreferencesObjective
-> // Seed the random number generator with the size of the problem
random!(length(prob.vars)),
// Generate the preferences for each variable and return the objective
let prefTable := make_table(choco/IntVar, table, nil)
in
( for var in prob.vars
( //prefTable[var] := makePreferenceList(var),
//self_print(prefTable[var] as list<integer>)
self_print(makePreferenceList(var))
),
PreferencesObjective(preferences = prefTable)
)
]
// Return a list with a random number between 0 and domain size less 1
for each value
// in the domain of the variable.
[makePreferenceList(var:choco/IntVar) : table
-> let scores := make_table(integer, integer, 0),
domainSize := choco/getDomainSize(var)
in
( printf("Generating preferences for variable ~S\n", var),
for val in choco/domainSet(var.choco/bucket)
( scores[val] := random(domainSize)
),
scores
)
]
Not quite as brief as the other ones but they do the job.
Another question, is it possible to get a list representation of the
range of a table. I tried to cast the table to a list but got a
segmentation fault.
Thanks,
Paidi
--- In claireprogramminglanguage@yahoogroups.com, "paidi_creed"
<paidi_creed@y...> wrote:
> Hi,
>
> I'm trying to implement an object representing an objective in a map
> colouring problem with a set of preferences for each colour at each
> node. I want to be able to have several objectives existing at the
> same time each with their own list of preferences.
>
> This requires a table[range = choco/IntVar, domain = table]
> and an inner table[range = integer, domain = integer]
>
> In the example for tables in the manual it uses
> square[x:(0 .. 20)] : integer := (x * x)
>
> this populates the table.
>
> Would it be possible to say
>
> [makePreferenceObjective(prob:choco/Problem) : PreferenceObjective
> -> preferenceTable[var:prob.vars] : table := makePreferenceList(var),
> PreferenceObjective(preferences = preferenceTable)
> ]
>
> where prob is the problem containing the variables to create a table
> mapping each variable in the problem to a table containing his
> preference for eahc value in that variables domain?
>
> Then can I write
>
> [makePreferenceList(var:choco/IntVar) : table
> -> scores[value: {set of values in the domain}] : integer :=
> random(choco/getDomainSize(var))
> ]
>
> to return a mapping from each value in the variables domain to a
> random number?
>
> When I try to run this I get error message the symbol var is unbound
> and if I just try to run makePreferenceList
> I get an error that value is unbound.
>
> I ran the example in the manual by typing it in to the interpreter and
> it worked fine.
> Am I doing something wrong when I use it inside my own methods.
>
> Thanks,
>
> Paidi