## Dynamic set allocation with binary variable

### Dynamic set allocation with binary variable

Hello guys,

currently I'm still working on a GAMS model which allocates a house or a tree to each cell of an empty map.

For example a map contains 10 pixels/cells and the model should fill the map with either tree or house. Each cell must be built once (no cell contains two options and no cell null..)

In my model I have two binary decision variables x(p,h)=1 if cell p is built with house h and y(p,v)=1 if cell p is built with vegetation/tree v.

Since I want to maximize the distance between the houses is it possible to create a dynamic subset which contains only the cells p which have a house on it.

So the syntax should be anything like this:

pbuilt(p)$(x(p,h)=1) =yes;

where pbuilt is a dynamic subset of P.

Do you guys have any idea if this is possible in general or is it impossible and doesn't make any sense to use binary decision variable as an allocation condition for dynamic sets?

Since I'm kind of a beginner in GAMS I would be very thankful for any advice from you guys!

Best regards from Australia

currently I'm still working on a GAMS model which allocates a house or a tree to each cell of an empty map.

For example a map contains 10 pixels/cells and the model should fill the map with either tree or house. Each cell must be built once (no cell contains two options and no cell null..)

In my model I have two binary decision variables x(p,h)=1 if cell p is built with house h and y(p,v)=1 if cell p is built with vegetation/tree v.

Since I want to maximize the distance between the houses is it possible to create a dynamic subset which contains only the cells p which have a house on it.

So the syntax should be anything like this:

pbuilt(p)$(x(p,h)=1) =yes;

where pbuilt is a dynamic subset of P.

Do you guys have any idea if this is possible in general or is it impossible and doesn't make any sense to use binary decision variable as an allocation condition for dynamic sets?

Since I'm kind of a beginner in GAMS I would be very thankful for any advice from you guys!

Best regards from Australia

### Re: Dynamic set allocation with binary variable

Hi,

Your syntax is almost corrrect. It only misses the level attribute of the variable (https://www.gams.com/latest/docs/UG_Var ... Attributes) and it seem that h is a set elemnt which means it should be quoted. Also, I suggest not to test for equality (due to tolerances a binary variable may take values like 0.999999999 in a solution).

would give you such a dynamic set

Also, it seems that either variable x or y is redundant because it seems that for fixed p you have y(p,'v') = 1-x(p,'h').

I hope this helps!

Fred

Your syntax is almost corrrect. It only misses the level attribute of the variable (https://www.gams.com/latest/docs/UG_Var ... Attributes) and it seem that h is a set elemnt which means it should be quoted. Also, I suggest not to test for equality (due to tolerances a binary variable may take values like 0.999999999 in a solution).

Code: Select all

```
pbuilt(p)$(x.l(p,'h')>0.5) = yes;
```

**after**the problem has been solved. Not sure if that is what you need or if you need that information**during**the solution process. It might be easier to help if you share more information and your current code.Also, it seems that either variable x or y is redundant because it seems that for fixed p you have y(p,'v') = 1-x(p,'h').

I hope this helps!

Fred

### Re: Dynamic set allocation with binary variable

Hi Fred,

thank you so much for your answer, really appreciate your help!

The code you mentioned works fine for the set after optimization, but in my example I need the set during the process, is this even possible in general, since I'm not sure if GAMS allows such a dynamic set allocation?

If its possible with GAMS and do you know the syntax to archive this?

If you need the whole model and explanation for it, just let me know and I will introduce it briefly.

thank you so much for your answer, really appreciate your help!

The code you mentioned works fine for the set after optimization, but in my example I need the set during the process, is this even possible in general, since I'm not sure if GAMS allows such a dynamic set allocation?

If its possible with GAMS and do you know the syntax to archive this?

If you need the whole model and explanation for it, just let me know and I will introduce it briefly.

### Re: Dynamic set allocation with binary variable

Hi,

The dynamic set won't help if you need that during the solution process. I guess there are model formulations that do what you want but it is a bit tedious to guess how this could fit into an unknown model.

If you share your model and explain where you are struggling to find the right formulation, that increases the chance to get target-oriented help from other forum users.

Fred

The dynamic set won't help if you need that during the solution process. I guess there are model formulations that do what you want but it is a bit tedious to guess how this could fit into an unknown model.

If you share your model and explain where you are struggling to find the right formulation, that increases the chance to get target-oriented help from other forum users.

Fred

### Re: Dynamic set allocation with binary variable

Hi,

okay sure, I will try to introduce my model and thoughts as compact as possible:

I changed the model a little bit within the last 2 days.

The overall goal is to develop a model for urban city planers, like very basic in the beginning starting with an empty map which should stand for a new suburb for example. In the model we have p=1..P Pixels which the model should fill with one of six options o=1..O. The options are either a house or a tree. Each of the six options has his own properties, the first 3 options are /house1*house3/ which stands for 3 different house types each with individual cost and resident capacity. The second three options are /veg1*veg3/ and have costs as well and a resident capacity of 0. The model should consider Budget and Resident demand constraints but thats easy to implement. The overall goal of the model is an allocation which considers economic and eco goals so every option o=1..O has a property dealding with a temperature delta (the houses leads to a positive temp delta due to Urban heat island effect and the vegetation has an positive impact on the temp, so negative temp delta). The objective function minimizes the overall temp. delta und Budget and Demand restrictions. I have a restriction as well, that every p of P should be allocate exactly one time. So in the end the model tells me after optimizing on which pixel p of P which option o of O is allocated and which ofv (temp delta) the optimzation has.

Hopefully you can follow me till now?

I have one binary variable x(p,o)=1 which is 1 if on pixel p of P option o of O is placed by the model.

As density is one of the leading factors of UHI I definitly want to integrate that in my model and in that way I want to maximze the distance between the pixel allocated with a house either its option 1,2 oder 3.

Do you have any idea how I can archieve this?

Here is my model so far:

okay sure, I will try to introduce my model and thoughts as compact as possible:

I changed the model a little bit within the last 2 days.

The overall goal is to develop a model for urban city planers, like very basic in the beginning starting with an empty map which should stand for a new suburb for example. In the model we have p=1..P Pixels which the model should fill with one of six options o=1..O. The options are either a house or a tree. Each of the six options has his own properties, the first 3 options are /house1*house3/ which stands for 3 different house types each with individual cost and resident capacity. The second three options are /veg1*veg3/ and have costs as well and a resident capacity of 0. The model should consider Budget and Resident demand constraints but thats easy to implement. The overall goal of the model is an allocation which considers economic and eco goals so every option o=1..O has a property dealding with a temperature delta (the houses leads to a positive temp delta due to Urban heat island effect and the vegetation has an positive impact on the temp, so negative temp delta). The objective function minimizes the overall temp. delta und Budget and Demand restrictions. I have a restriction as well, that every p of P should be allocate exactly one time. So in the end the model tells me after optimizing on which pixel p of P which option o of O is allocated and which ofv (temp delta) the optimzation has.

Hopefully you can follow me till now?

I have one binary variable x(p,o)=1 which is 1 if on pixel p of P option o of O is placed by the model.

As density is one of the leading factors of UHI I definitly want to integrate that in my model and in that way I want to maximze the distance between the pixel allocated with a house either its option 1,2 oder 3.

Do you have any idea how I can archieve this?

Here is my model so far:

Code: Select all

```
Set
p
o
h(o)
pbuilt(p)
;
alias(p,j)
Set Nachbar(p,j);
Parameter
c(o)
D
B
kx(p)
ky(p)
t(o)
s(o)
w(p,j)
l(o)
F
a(o)
q(o)
;
binary variable
x(p,o)
;
free variable
ofv
R
Dis
;
equations
ObjectiveFunction
MapDemand
MapBudget
EachPixelOnce
Result
Distance
;
ObjectiveFunction..
sum(p,sum(o,x(p,o)*t(o))) - sum(p,sum(o,x(p,o)*l(o))) + sum(p,sum(o,x(p,o)*a(o)*F)) =e= ofv
;
MapDemand..
sum(p,sum(o$ h(o),x(p,o)*s(o))) =g= D
;
MapBudget..
sum(p,sum(o,x(p,o)*c(o))) =l= B
;
EachPixelOnce(p)..
sum(o,x(p,o)) =e= 1
;
Result..
sum(p,sum(o,x(p,o)*t(o)))+ sum(p,sum(o,x(p,o)*a(o)*F)) =e= R
;
Distance..
sum(p,sum(o,(q(o)*x(p,o)*(sum(j,w(p,j)*q(o)))))) =e= Dis
;
*Distance..
*sum(p,sum(o,x(p,o)*q(o)*sum(j$(x(p,o)*q(o)>0),w(p,j)))) =e= Dis
*;
$include includeModelV2.inc
model problem /all/;
solve problem using mip minimizing ofv;
display ofv.l, x.l, w, h, R.l, Dis.l;
```

Code: Select all

```
Include file:
Set
p /Cell1*Cell9/
o /Option1*Option6/
h(o) /Option1*Option3/
pbuilt(p)
;
Parameter
F
/
2
/
B
/
1000
/
D
/
10
/
t(o)
/
Option1 0.15
Option2 0.2
Option3 0.3
Option4 -0.1
Option5 -0.15
Option6 -0.2
/
c(o)
/
Option1 150
Option2 200
Option3 300
Option4 10
Option5 20
Option6 30
/
l(o)
/
Option1 6
Option2 8
Option3 10
Option4 0
Option5 0
Option6 0
/
q(o)
/
Option1 1
Option2 1
Option3 1
Option4 0
Option5 0
Option6 0
/
a(o)
/
Option1 0.5
Option2 0.35
Option3 0.4
Option4 -0.01
Option5 -0.05
Option6 -0.08
/
s(o)
/
Option1 2
Option2 5
Option3 8
Option4 0
Option5 0
Option6 0
/
kx(p)
/
Cell1 1
Cell2 2
Cell3 3
Cell4 1
Cell5 2
Cell6 3
Cell7 1
Cell8 2
Cell9 3
/
ky(p)
/
Cell1 1
Cell2 1
Cell3 1
Cell4 2
Cell5 2
Cell6 2
Cell7 3
Cell8 3
Cell9 3
/
;
loop((p,j),w(p,j)=abs(kx(p)-kx(j))+abs(ky(p)-ky(j)));
```

Last edited by MatQUT94 1 month ago, edited 3 times in total.

### Re: Dynamic set allocation with binary variable

Hi,

So you want a constraint that calculates the sum of all distances w(p1,p2) between pixels p1 and p2 on which a house h is built?

I think a straightforward (quadratic) formulation could look as follows:
Not sure what exactly the q in your example does, so I just neglected it, but I guess you can adjust the example as needed.

Eventually, it makes sense to linearize this equation with the help of an additional variable y, e.g. as follows:

I hope this helps!

Fred

PS You can also attach files and/or press the </> button to format your code nicely.

So you want a constraint that calculates the sum of all distances w(p1,p2) between pixels p1 and p2 on which a house h is built?

I think a straightforward (quadratic) formulation could look as follows:

Code: Select all

```
equation houseDistNL;
positive variable distNL;
alias(h,h1,h2), (p,p1,p2);
houseDistNL.. distNL =e= sum((p1,h1,p2,h2), w(p1,p2)*x(p1,h2)*x(p2,h2));
```

Eventually, it makes sense to linearize this equation with the help of an additional variable y, e.g. as follows:

Code: Select all

```
equation houseDistL, eq1,eq2,eq3;
positive variable distL;
binary variable y(p1,h1,p2,h2) '1 if house h1 built at p1 and house h2 at p2 ';
*linearized
houseDistL.. distL =e= sum((p1,h1,p2,h2), w(p1,p2)*y(p1,h1,p2,h2));
eq1(p1,h1,p2,h2).. y(p1,h1,p2,h2) =l= x(p1,h1);
eq2(p1,h1,p2,h2).. y(p1,h1,p2,h2) =l= x(p2,h2);
eq3(p1,h1,p2,h2).. y(p1,h1,p2,h2) =g= x(p1,h1) + x(p2,h2) - 1;
```

Fred

PS You can also attach files and/or press the </> button to format your code nicely.

### Re: Dynamic set allocation with binary variable

Hi Fred,

thank you so much!!

This works perfectly for me and does want I want to archive having the distance in my model.

Thanks so much Fred!!

thank you so much!!

This works perfectly for me and does want I want to archive having the distance in my model.

Thanks so much Fred!!

### Re: Dynamic set allocation with binary variable

One last question:

You mentioned in your formula (linear version):

But in the last term in the right?

You mentioned in your formula (linear version):

Code: Select all

```
houseDistL..
distL =e= sum((p1,h1,p2,h2), w(p1,p2)*y(p1,[b]h2[/b],p2,h2))
;
```

*y*it'sCode: Select all

`y(p1,[b]h1[/b],p2,h2)`

### Re: Dynamic set allocation with binary variable

Hi, you are absolutely right. I edited my post accordingly.

Fred

Fred

### Re: Dynamic set allocation with binary variable

Thank you so much Fred!