# Using binary variables in the Excel solver without making the problem unsolvable

#### Sam Hamels

##### New Member
This is a bit of an unusual post, aimed at people familiar with optimization problems solved by the Excel solver (or Add-ins like OpenSolver)

In the attached Excel file, I simulate the following simple scenario:

• A house consumes electricity from the grid for 24 hours.

• The amount of electricity taken from the grid is shown for every hour (it fluctuates throughout the day)

• The price of electricity is also shown for every hour (it also fluctuates throughout the day)

• The amount paid for electricity is (for each hour) the amount consumed multiplied by the price in that hour

• The house has a battery that is used to change the amount of electricity that is taken from the grid during each hour

• The point of this is to decrease the amount of electricity taken from the grid during hours with a high electricity price, and to increase the amount taken from the grid during hours with a low electricity price

• This way, the total amount paid for electricity can be reduced

• This is an optimization, solved by the basic Excel solver (although I could also use the free Add-in called 'OpenSolver')

• The optimization minimizes the total amount paid, *after* the battery has done its thing.

• Some constraints must me obeyed within the optimization, to be realistic:

1. The battery cannot be charged beyond its maximum energy storage capacity
2. The amount of energy stored in the battery cannot be lower than zero
3. The battery cannot be (dis)charged faster than its maximum (dis)charge rate
4. After the optimization is completed (in other words, "when the battery has done its thing"), the total amount of electricity taken from the grid throughout the day cannot be changed
5. The 'new' amount of electricity taken from the grid during each hour cannot be negative

The problem I have is that I need to add the following detail to the simulation, but I don't know how:

The charging and discharing of the battery causes some 'losses' of electricity. For example, if the battery consumes 10 units of electricity, only 9 actually end up in the battery. Likewise, when the battery delivers 10 units of electricity to the home, 11 units actually go out of the battery. In this example, both the charge efficiency and the discharge efficiency are 90%.

I have tried playing around with binary variables to disconnect charging from discharging, and apply the losses to the battery charging and discharging, but I can't manage to make anything work. It always makes my problem unsolvable.

Intuitively, I basically want this to be to formula of the column with the 'energy stored inside the battery':

• IF the change in electricity taken from the grid is positive, then multiply that change by the battery charging efficiency to arrive at the amount actually stored in the battery

• IF the change in electricity taken from the grid is negative, then multiply that change by the battery discharging efficiency to arrive at the amount actually going out of the battery

The problem is that I can't use an IF function because the solver doesn't allow for that. Hence my experimentation with binary variables (without success).

Any advice would be extremely welcome.

Last edited:

### Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

#### shg

##### MrExcel MVP
Interesting problem. I'd have thought software would exist for this by people that make household batteries, like Tesla.

I think a solution needs to consider several days, so it can stabiiize; starting with an empty battery at peak electricity cost isn't going to work well for a single day.

#### Sam Hamels

##### New Member
In reality I have many hundreds of days of data (home electricity use and electricity prices). The single day in the file is just a sample.

I could easily have the simulation run over periods of two or more days instead of one (this is arbitrary). It would just take longer to solve.

The core problem would remain that I can't find a way to include battery losses without making the problem unsolvable. It seems unlikely to me that it really is impossible to do though. I must just be looking over some obvious way to do it.

#### shg

##### MrExcel MVP
I think that's straightforward:

 A​ B​ C​ D​ E​ F​ I​ 1​ Chg Eff​ 90%​ 2​ Disch Eff​ 90%​ 3​ Max Sorage​ 50​ 4​ 5​ Hour​ Demand​ Unit Cost​ Cost​ (Dis)Charge​ Storage​ 6​ 7​ 3​ 128​ 384​ 10.0​ 9.0​ F6: =IF(E6 > 0, MIN(F\$3, SUM(F5, E6 * F\$1)), MAX(0, SUM(F5, E6 / F\$2))) 7​ 8​ 4​ 130​ 520​ 10.0​ 18.0​ 8​ 9​ 4​ 136​ 544​ 10.0​ 27.0​ 9​ 10​ 4​ 142​ 568​ 10.0​ 36.0​ 10​ 11​ 3​ 139​ 417​ 10.0​ 45.0​ 11​ 12​ 3​ 136​ 408​ -3.0​ 41.7​ 12​ 13​ 3​ 137​ 411​ -3.0​ 38.3​ 13​ 14​ 3​ 134​ 402​ -3.0​ 35.0​ 14​ 15​ 3​ 127​ 381​ 10.0​ 44.0​ 15​ 16​ 3​ 127​ 381​ 10.0​ 50.0​ 16​ 17​ 4​ 122​ 488​ 10.0​ 50.0​ 17​ 18​ 5​ 125​ 625​ 10.0​ 50.0​ 18​ 19​ 3​ 130​ 390​ 9.0​ 50.0​ 19​ 20​ 9​ 132​ 1188​ -9.0​ 40.0​ 20​ 21​ 10​ 136​ 1360​ -10.0​ 28.9​ 21​ 22​ 16​ 143​ 2288​ -10.0​ 17.8​ 22​ 23​ 23​ 173​ 3979​ -10.0​ 6.7​ 23​ 24​ 19​ 169​ 3211​ -10.0​ 0.0​ 24​ 1​ 13​ 159​ 2067​ 0.0​ 0.0​ 25​ 2​ 1​ 158​ 158​ 6.0​ 5.4​ 26​ 3​ 2​ 159​ 318​ -2.0​ 3.2​ 27​ 4​ 1​ 159​ 159​ -1.0​ 2.1​ 28​ 5​ 1​ 163​ 163​ -1.0​ 1.0​ 29​ 6​ 2​ 159​ 318​ -2.0​ 0.0​

#### Sam Hamels

##### New Member
Thanks!

I eventually figured out that I could quite easily include battery losses without using any IF-formula (or MIN or MAX for that matter),
but I simply had to relax the constraint that the 'old' and 'new' electricity consumption (i.e. before and after the battery has done its thing) should always be equal.
They can no longer be equal since the losses of the battery requires a slight increase in the overall electricity consumption.

You're welcome.

1,130,170
Messages
5,640,568
Members
417,151
Latest member
ChickenTenderer

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back