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

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

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

This is a forum for the online, data visualization notebook Observable and the technologies it’s based on. That includes at least

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.

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