javaoptimizationcplexobjective-function

Add quadratic penalty term to objective function in cplex (Java)


I'm developing an Optimization tool for a domestic energy system that also contains a battery. All values are correct and the solution makes sense. The problem is that the solution contains very strong fluctuations. Meaning that the decision variable is often either 0 or the maximum value. In order to avoid that I would like to add a quadratic constraint that penalizes the difference of two values (something like the derivative). Should look something like this:

((x[t] - x[t-1]) / stepsize) ^ 2

Where x is the decision variable of interest. E.g. power_g_h[t].

My objective function (so far) ist definded as follows:

IloLQNumExpr expr = model.lqNumExpr();

        for (int t = 0; t < timesteps; t++) {
            expr.addTerm(problem.getCosts().getElectricityCosts(t), power_g_h[t]);
            expr.addTerm(problem.getCosts().getElectricityCosts(t), power_g_b[t]);
            expr.addTerm(problem.getCosts().getElectricityCosts(t), power_g_bev[t]);
            expr.addTerm(problem.getCosts().getFeedCompensation(), power_pv_g[t]);

        } 

I hope this was somewhat understandable and someone is able to tell whether or not this is even possible in CPLEX.

If this is not possible, I would be very happy about hints on how to "smoothen" a solution in CPLEX.

With kind regards,

L.


Solution

  • The problem was solved as follows:

    It does not seem to be possible to add an expression like x * ((a - b) ^ 2). Instead the solution was to write the above as x*a*a - 2x*a*b + x*b*b. Where x is the penalty factor and a & b are the decision variables. This way it was possible to add the term to the objective function in cplex. In code it looks something like this:

    IloCplex model = new IloCplex();
    ...
    IloLQNumExpr expr = model.lqNumExpr();
    
    expr.addTerm(x, a, a);
    expr.addTerm(x, b, b);
    expr.addTerm(-2 * x, a, b);
    

    In my case a and b was the same variable for two consecutive timestep, s.t. the change over time was kept small.