optimize a piece wise linear function in gurobi in csharp

To optimize a piecewise linear function in Gurobi using C#, you can use the Mixed-Integer Linear Programming (MILP) formulation. The idea behind MILP is to replace the non-linearities with linear constraints and binary variables.

Here is an example code that shows how to use MILP to optimize a piecewise linear function:

main.cs
using Gurobi;
using System;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            // Create a new Gurobi environment
            GRBEnv env = new GRBEnv();
            env.Set("OutputFlag", "0");

            // Create a new Gurobi model
            GRBModel model = new GRBModel(env);

            // Define the variables
            GRBVar x = model.AddVar(-GRB.INFINITY, GRB.INFINITY, 0, GRB.CONTINUOUS, "x");
            GRBVar y = model.AddVar(-GRB.INFINITY, GRB.INFINITY, 0, GRB.CONTINUOUS, "y");

            // Define the binary variables and the big-M constant
            int K = 10; // The number of breakpoints in the piecewise function
            GRBVar[] z = new GRBVar[K];
            double M = 1000000; // A big-M constant to represent infinity
            for (int i = 0; i < K; i++)
            {
                z[i] = model.AddVar(0, 1, 0, GRB.BINARY, "z" + i);
            }

            // Add the constraints
            model.AddConstr(x <= y);
            model.AddConstr(x + y >= 2);

            // Add the piecewise linear function constraints
            double[] slope = { 1.5, 0.5, -0.5, -1.5 };
            double[] intercept = { 0, 0.5, 1, 2 };
            for (int i = 0; i < K - 1; i++)
            {
                model.AddConstr(y - intercept[i] <= slope[i] * x + M * (1 - z[i]));
                model.AddConstr(y - intercept[i + 1] >= slope[i] * x - M * (1 - z[i]));
                model.AddConstr(z[i] + z[i + 1] <= 1);
            }
            model.AddConstr(y - intercept[K - 1] >= slope[K - 1] * x - M * (1 - z[K - 1]));

            // Set the objective function
            model.SetObjective(x + y, GRB.MINIMIZE);

            // Optimize the model
            model.Optimize();

            // Print the results
            Console.WriteLine("Optimal objective value: " + model.ObjVal);
            Console.WriteLine("x = " + x.X);
            Console.WriteLine("y = " + y.X);

            // Dispose of the model and the environment
            model.Dispose();
            env.Dispose();

        }
        catch (GRBException e)
        {
            Console.WriteLine("Error code " + e.ErrorCode + ": " + e.Message);
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
        }
    }
}
2334 chars
71 lines

In this example, we have considered a piecewise linear function with four segments. The breakpoints of the function are represented by binary variables z[i] (with i ranging from 0 to K-1). The constraints ensure that only one of these binary variables can be 1 at a time, which means that only one segment of the function is active at any given point.

The optimization problem is to minimize the sum of the x and y variables subject to the piecewise linear function and the other constraints.

Before running this code, you need to download and install the Gurobi optimizer and add a reference to the Gurobi C# interop assembly (gurobi90.dll).

gistlibby LogSnag