[gecode-users] Dynamically constructing a model using solution of an IntVar

Guido Tack tack at gecode.org
Thu Aug 15 05:41:02 CEST 2013


On 15/08/2013, at 1:33 PM, Ajit Dingankar <Ajit.Dingankar at gmail.com> wrote:

> 
>> That's more difficult.  Let's say your variable that contains the required 
> array length is called x.  You
>> either need to decompose your model into two parts (the first part 
> determining x, the second part then
>> using that solution to construct a new model with the right length), or 
> you need to allocate an array with
>> the maximum possible length and use constraints to say that everything 
> beyond index x is ignored.
>> 
>> Cheers,
>> Guido
> 
> Thanks, Guido! I was thinking about an approach similar to your first 
> suggestion which may be better suited for my situation (in addition to 
> being more memory efficient ;-) since it's a natural fit for my solution 
> requirement: I need to solve for every legal length but the data should 
> be randomly picked (one solution for every length) since the data space 
> is very large and most solutions don't add any insight into the problem 
> over that of a single representative solution. 
> 
> I couldn't find an example of model decomposition in MPG, so I'd really 
> appreciate any pointers or hints of handling the underlying space; mainly 
> if I can/must use a different space for the second model.  I think I've 
> to use a different space and the variables in the first model can't be 
> constrained by those in the second model in this approach. If there are 
> any such constraints, I must use the second approach of maximum possible 
> length. 

You can simply add variables and constraints to an existing space. Something like this should work (this is just pseudo code, you may need to use the right options for DFS so that you don't get memory leaks etc):

MySpace* s = new MySpace;
DFS<MySpace> dfs(s);
while (MySpace* s = dfs.next()) {
  s->add_second_level_vars_and_constraints();
  DFS<MySpace> nestedDFS(s);
  while (MySpace* ns = nestedDFS.next()) {
    ns->print();
    delete ns;
  }
}

Cheers,
Guido




More information about the users mailing list