# glpk.js and variable Sets - restrict to "either or"

**URL:** <https://talk.observablehq.com/t/glpk-js-and-variable-sets-restrict-to-either-or/8153>\
**Category:** Help\
**Created:** [August 3, 2023, 5:26pm UTC](https://talk.observablehq.com/t/glpk-js-and-variable-sets-restrict-to-either-or/8153 "2023-08-03T17:26:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![seb2010](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@seb2010](https://talk.observablehq.com/u/seb2010)\
**Post date:** [August 3, 2023, 5:26pm UTC](https://talk.observablehq.com/t/glpk-js-and-variable-sets-restrict-to-either-or/8153/1 "2023-08-03T17:26:29Z")

</div>

Hi guys,

I am not totally new to optimization usage but I am struggeling with the usage within node.js and the GLPK.js implementation.  
I do have a problem, where I basically want to introduce constraints, which allows only a set of variables in the subject or a constraint to be non-zero, while others then need to be zero. So either-or.

To be more specific, imagine the following subject:

x1 \* 2 - x2 \* 1 + x3 \* 3 - x4 \* 1.5 → maximize  
constraints:  
0 \<= x1 - x2 \<= 1000  
0 \<= x1 - x2 + x3 - x4 \<= 1000  
0 \<= x1 \<= 32000  
0 \<= x2 \<= 32000  
0 \<= x3 \<= 32000  
0 \<= x4 \<= 32000  
The physicallity behind this is a battery, which can either draw energy from the net or feed energy in. The two prices to draw energy are 1 and 1.5 and the variable for the power to be drawn are x2 and x4.  
The infeed prices are 2 and 3 (so higher that the price to draw energy) and the corresponding variables are x1 and x3.  
If nothing else is introduced, the optimizer with put all variables to maximum. But this is physically not possible at the same power-meter. Either can energy be drawn ot fed-in and only the net balance for each step shoud get honored with the price of the direction. So only one variable of each tuple x1,x2 and x3,x4 is allowed to be non-zero. Not x1,x2 or x3,x4 at the same time…  
How can I achieve this?

I read something about “Sets” for CPLEX files, but is something similar possible with GLPK.js or node in combination with GLPK?

Hope you have a hint for me!  
KR  
SEB

---

<div class="post-metadata">

**Author:** ![seb2010](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@seb2010](https://talk.observablehq.com/u/seb2010)\
**Post date:** [August 3, 2023, 5:38pm UTC](https://talk.observablehq.com/t/glpk-js-and-variable-sets-restrict-to-either-or/8153/2 "2023-08-03T17:38:49Z")

</div>

I just asked ChatGPT this question, and it came up with the following:  
Maximize  
2 x1 - 1 x2 + 3 x3 - 1.5 x4

Subject to  
Binary1: x1\_bin + x2\_bin \<= 1  
Binary2: x3\_bin + x4\_bin \<= 1  
NonZero1: x1 \<= Large\_number \* x1\_bin  
NonZero2: x2 \<= Large\_number \* x2\_bin  
NonZero3: x3 \<= Large\_number \* x3\_bin  
NonZero4: x4 \<= Large\_number \* x4\_bin

Bounds  
0 \<= x1 \<= U1  
0 \<= x2 \<= U1  
0 \<= x3 \<= U2  
0 \<= x4 \<= U2  
0 \<= x1\_bin \<= 1  
0 \<= x2\_bin \<= 1  
0 \<= x3\_bin \<= 1  
0 \<= x4\_bin \<= 1

Generals  
x1\_bin  
x2\_bin  
x3\_bin  
x4\_bin

End

Now the question is, how can I set a variable as a boundary with GLPK.js as in  
NonZero1: x1 \<= Large\_number \* **x1\_bin**

---

<div class="post-metadata">

**Author:** ![mcmcclur](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mcmcclur/32/2364_2.png) [@mcmcclur](https://talk.observablehq.com/u/mcmcclur)\
**Post date:** [August 3, 2023, 5:46pm UTC](https://talk.observablehq.com/t/glpk-js-and-variable-sets-restrict-to-either-or/8153/3 "2023-08-03T17:46:25Z")

</div>

This is a forum for the online, data visualization notebook [Observable](https://observablehq.com/) and the technologies it’s based on. That includes at least

- The Observable notebook,
- [Observable Plot](https://observablehq.com/plot/), and
- [D3](https://d3js.org/).

There are a few other tools that you occasionally see mentioned here but not many. There are certainly some bright folks with a wide range of interest here so you might get a response. I don’t think this is the ideal location for your question, though.

---

<div class="post-metadata">

**Author:** ![seb2010](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@seb2010](https://talk.observablehq.com/u/seb2010)\
**Post date:** [August 3, 2023, 7:40pm UTC](https://talk.observablehq.com/t/glpk-js-and-variable-sets-restrict-to-either-or/8153/4 "2023-08-03T19:40:45Z")

</div>

thanks mcmcclur. Was worth a shot.  
I solved it now though.
