I tried to utilize the .getValue() function but the problem ( I imagine?) is that since the optimization isen't completed yet it can't get the value of the expression before that since the variables aren't set. So I wind up with this error AttributeError: Index out of range for attribute 'X'.

The implementation I tried looking at was adding the expression like this.

bat_SOC2 = bat_SOC1.getValue()m.addConstr(bat_SOC2, GRB.EQUAL, bat_SOC)

I'm thinking of perhaps making a function to sum the value every time through using an optimization to give the variables values but that seems quite strange and I suppose it would break the overall optimization...

EDIT: I'm working on a recursive solution that I think might work but as I'm very unfamiliar with this I'll gladly take any help I can get. Will update if I solve the problem.

I belive I've resolved my issue. I created a tupledict and stored the values in the tupledict. Its reduced the non-zeros (logs found below) signifigantly but I'll try utilising it in my bigger model tomorrow. This way the constraint dosen't become so dense atleast.

If someone that is more knowledgable than me about these things tell me if they belive that this is a good solution?

Logs can be seen here.
1# My new version is first.

Old version can be seen here.