Exemple #1
0
    internal static void BuildModelByRow(IModeler model,
                                         Data data,
                                         INumVar[]  Buy,
                                         NumVarType type)
    {
        int nFoods = data.nFoods;
        int nNutrs = data.nNutrs;

        for (int j = 0; j < nFoods; j++)
        {
            Buy[j] = model.NumVar(data.foodMin[j], data.foodMax[j], type);
        }
        model.AddMinimize(model.ScalProd(data.foodCost, Buy));

        for (int i = 0; i < nNutrs; i++)
        {
            model.AddRange(data.nutrMin[i],
                           model.ScalProd(data.nutrPerFood[i], Buy),
                           data.nutrMax[i]);
        }
    }
Exemple #2
0
   internal static void BuildModelByRow(IModeler   model,
                                        Data       data,
                                        INumVar[]  Buy,
                                        NumVarType type) {
      int nFoods = data.nFoods;
      int nNutrs = data.nNutrs;

      for (int j = 0; j < nFoods; j++) {
         Buy[j] = model.NumVar(data.foodMin[j], data.foodMax[j], type);
      }
      model.AddMinimize(model.ScalProd(data.foodCost, Buy));

      for (int i = 0; i < nNutrs; i++) {
         model.AddRange(data.nutrMin[i],
                        model.ScalProd(data.nutrPerFood[i], Buy),
                        data.nutrMax[i]);
      }
   }
    // The following method populates the problem with data for the
    // following linear program:
    //
    //    Maximize
    //     x1 + 2 x2 + 3 x3
    //    Subject To
    //     - x1 + x2 + x3 <= 20
    //     x1 - 3 x2 + x3 <= 30
    //    Bounds
    //     0 <= x1 <= 40
    //    End
    //
    // using the IModeler API

    internal static void PopulateByRow(IModeler model,
                                       INumVar[][] var,
                                       IRange[][] rng)
    {
        double[]  lb      = { 0.0, 0.0, 0.0 };
        double[]  ub      = { 40.0, System.Double.MaxValue, System.Double.MaxValue };
        string[]  varname = { "x1", "x2", "x3" };
        INumVar[] x       = model.NumVarArray(3, lb, ub, varname);
        var[0] = x;

        double[] objvals = { 1.0, 2.0, 3.0 };
        model.AddMaximize(model.ScalProd(x, objvals));

        rng[0]    = new IRange[2];
        rng[0][0] = model.AddLe(model.Sum(model.Prod(-1.0, x[0]),
                                          model.Prod(1.0, x[1]),
                                          model.Prod(1.0, x[2])), 20.0, "c1");
        rng[0][1] = model.AddLe(model.Sum(model.Prod(1.0, x[0]),
                                          model.Prod(-3.0, x[1]),
                                          model.Prod(1.0, x[2])), 30.0, "c2");
    }
    }     // END WorkerLP

    // This method creates the master ILP (arc variables x and degree constraints).
    //
    // Modeling variables:
    // forall (i,j) in A:
    //    x(i,j) = 1, if arc (i,j) is selected
    //           = 0, otherwise
    //
    // Objective:
    // minimize sum((i,j) in A) c(i,j) * x(i,j)
    //
    // Degree constraints:
    // forall i in V: sum((i,j) in delta+(i)) x(i,j) = 1
    // forall i in V: sum((j,i) in delta-(i)) x(j,i) = 1
    //
    // Binary constraints on arc variables:
    // forall (i,j) in A: x(i,j) in {0, 1}
    //
    internal static void CreateMasterILP(IModeler model,
                                         Data data,
                                         IIntVar[][] x)
    {
        int i, j;
        int numNodes = data.numNodes;

        // Create variables x(i,j) for (i,j) in A
        // For simplicity, also dummy variables x(i,i) are created.
        // Those variables are fixed to 0 and do not partecipate to
        // the constraints.

        for (i = 0; i < numNodes; ++i)
        {
            x[i] = new IIntVar[numNodes];
            for (j = 0; j < numNodes; ++j)
            {
                x[i][j] = model.BoolVar("x." + i + "." + j);
                model.Add(x[i][j]);
            }
            x[i][i].UB = 0;
        }

        // Create objective function: minimize sum((i,j) in A ) c(i,j) * x(i,j)

        ILinearNumExpr objExpr = model.LinearNumExpr();

        for (i = 0; i < numNodes; ++i)
        {
            objExpr.Add(model.ScalProd(x[i], data.arcCost[i]));
        }
        model.AddMinimize(objExpr);

        // Add the out degree constraints.
        // forall i in V: sum((i,j) in delta+(i)) x(i,j) = 1

        for (i = 0; i < numNodes; ++i)
        {
            ILinearNumExpr expr = model.LinearNumExpr();
            for (j = 0; j < i; ++j)
            {
                expr.AddTerm(x[i][j], 1.0);
            }
            for (j = i + 1; j < numNodes; ++j)
            {
                expr.AddTerm(x[i][j], 1.0);
            }
            model.AddEq(expr, 1.0);
        }

        // Add the in degree constraints.
        // forall i in V: sum((j,i) in delta-(i)) x(j,i) = 1

        for (i = 0; i < numNodes; ++i)
        {
            ILinearNumExpr expr = model.LinearNumExpr();
            for (j = 0; j < i; ++j)
            {
                expr.AddTerm(x[j][i], 1.0);
            }
            for (j = i + 1; j < numNodes; ++j)
            {
                expr.AddTerm(x[j][i], 1.0);
            }
            model.AddEq(expr, 1.0);
        }
    } // END CreateMasterILP
    // The following method populates the problem with data for the
    // following linear program:
    //
    //    Maximize
    //     x1 + 2 x2 + 3 x3
    //    Subject To
    //     - x1 + x2 + x3 <= 20
    //     x1 - 3 x2 + x3 <= 30
    //    Bounds
    //     0 <= x1 <= 40
    //    End
    //
    // using the IModeler API
    internal static void PopulateByRow(IModeler model,
                             INumVar[][] var,
                             IRange[][] rng)
    {
        double[]    lb      = {0.0, 0.0, 0.0};
          double[]    ub      = {40.0, System.Double.MaxValue, System.Double.MaxValue};
          string[]    varname = {"x1", "x2", "x3"};
          INumVar[] x       = model.NumVarArray(3, lb, ub, varname);
          var[0] = x;

          double[] objvals = {1.0, 2.0, 3.0};
          model.AddMaximize(model.ScalProd(x, objvals));

          rng[0] = new IRange[2];
          rng[0][0] = model.AddLe(model.Sum(model.Prod(-1.0, x[0]),
                                        model.Prod( 1.0, x[1]),
                                        model.Prod( 1.0, x[2])), 20.0, "c1");
          rng[0][1] = model.AddLe(model.Sum(model.Prod( 1.0, x[0]),
                                        model.Prod(-3.0, x[1]),
                                        model.Prod( 1.0, x[2])), 30.0, "c2");
    }
    // This method creates the master ILP (arc variables x and degree constraints).
    //
    // Modeling variables:
    // forall (i,j) in A:
    //    x(i,j) = 1, if arc (i,j) is selected
    //           = 0, otherwise
    //
    // Objective:
    // minimize sum((i,j) in A) c(i,j) * x(i,j)
    //
    // Degree constraints:
    // forall i in V: sum((i,j) in delta+(i)) x(i,j) = 1
    // forall i in V: sum((j,i) in delta-(i)) x(j,i) = 1
    //
    // Binary constraints on arc variables:
    // forall (i,j) in A: x(i,j) in {0, 1}
    //
    internal static void CreateMasterILP(IModeler model,
                                        Data data,
                                        IIntVar[][] x)
    {
        int i, j;
          int numNodes = data.numNodes;

          // Create variables x(i,j) for (i,j) in A
          // For simplicity, also dummy variables x(i,i) are created.
          // Those variables are fixed to 0 and do not partecipate to
          // the constraints.

          for (i = 0; i < numNodes; ++i)
          {
         x[i] = new IIntVar[numNodes];
         for (j = 0; j < numNodes; ++j)
         {
            x[i][j] = model.BoolVar("x." + i + "." + j);
            model.Add(x[i][j]);
         }
         x[i][i].UB = 0;
          }

          // Create objective function: minimize sum((i,j) in A ) c(i,j) * x(i,j)

          ILinearNumExpr objExpr = model.LinearNumExpr();
          for (i = 0; i < numNodes; ++i)
         objExpr.Add(model.ScalProd(x[i], data.arcCost[i]));
          model.AddMinimize(objExpr);

          // Add the out degree constraints.
          // forall i in V: sum((i,j) in delta+(i)) x(i,j) = 1

          for (i = 0; i < numNodes; ++i)
          {
         ILinearNumExpr expr = model.LinearNumExpr();
         for (j = 0; j < i; ++j) expr.AddTerm(x[i][j], 1.0);
         for (j = i + 1; j < numNodes; ++j) expr.AddTerm(x[i][j], 1.0);
         model.AddEq(expr, 1.0);
          }

          // Add the in degree constraints.
          // forall i in V: sum((j,i) in delta-(i)) x(j,i) = 1

          for (i = 0; i < numNodes; ++i)
          {
         ILinearNumExpr expr = model.LinearNumExpr();
         for (j = 0; j < i; ++j) expr.AddTerm(x[j][i], 1.0);
         for (j = i + 1; j < numNodes; ++j) expr.AddTerm(x[j][i], 1.0);
         model.AddEq(expr, 1.0);
          }
    }