예제 #1
0
  /**
   *
   * Volsay problem.
   *
   * From the OPL model volsay.mod.
   *
   *
   * Also see http://www.hakank.org/or-tools/volsay.py
   *
   */
  private static void Solve()
  {

    Solver solver = new Solver("Volsay", Solver.CLP_LINEAR_PROGRAMMING);

    //
    // Variables
    //

    Variable Gas = solver.MakeNumVar(0, 100000, "Gas");
    Variable Chloride = solver.MakeNumVar(0, 100000, "Cloride");

    Constraint c1 = solver.Add(Gas + Chloride <= 50);
    Constraint c2 = solver.Add(3 * Gas + 4 * Chloride <= 180);

    solver.Maximize(40 * Gas + 50 * Chloride);

    int resultStatus = solver.Solve();

    if (resultStatus != Solver.OPTIMAL) {
      Console.WriteLine("The problem don't have an optimal solution.");
      return;
    }

    Console.WriteLine("Objective: {0}", solver.ObjectiveValue());

    Console.WriteLine("Gas      : {0} ReducedCost: {1}",
                      Gas.SolutionValue(),
                      Gas.ReducedCost());

    Console.WriteLine("Chloride : {0} ReducedCost: {1}",
                      Chloride.SolutionValue(),
                      Chloride.ReducedCost());

    Console.WriteLine("c1       : DualValue: {0} Activity: {1}",
                      c1.DualValue(),
                      c1.Activity());

    Console.WriteLine("c2       : DualValue: {0} Activity: {1}",
                      c2.DualValue(),
                      c2.Activity());



    Console.WriteLine("\nWallTime: " + solver.WallTime());
    Console.WriteLine("Iterations: " + solver.Iterations());

  }
예제 #2
0
 static void TestObjective()
 {
   Console.WriteLine("Running TestObjective");
   Solver solver = new Solver("TestObjective", Solver.CLP_LINEAR_PROGRAMMING);
   Variable x = solver.MakeNumVar(0.0, 100.0, "x");
   Variable y = solver.MakeNumVar(0.0, 100.0, "y");
   solver.Maximize(x);
   CheckDoubleEq(0.0, solver.Objective().Offset(), "test1");
   CheckDoubleEq(1.0, solver.Objective().GetCoefficient(x), "test2");
   Check(solver.Objective().Maximization(), "test3");
   solver.Minimize(-x - 2 * y + 3);
   CheckDoubleEq(3.0, solver.Objective().Offset(), "test4");
   CheckDoubleEq(-1.0, solver.Objective().GetCoefficient(x), "test5");
   CheckDoubleEq(-2.0, solver.Objective().GetCoefficient(y), "test6");
   Check(solver.Objective().Minimization(), "test7");
 }
예제 #3
0
  /**
   *
   * Volsay problem.
   *
   * From the OPL model volsay.mod.
   * This version use arrays and matrices
   *
   *
   * Also see
   *  http://www.hakank.org/or-tools/volsay2.cs
   *  http://www.hakank.org/or-tools/volsay3.py
   *
   */
  private static void Solve()
  {

    Solver solver = new Solver("Volsay3",
                               Solver.CLP_LINEAR_PROGRAMMING);


    int num_products = 2;
    IEnumerable<int> PRODUCTS = Enumerable.Range(0, num_products);
    String[] products = {"Gas", "Chloride"};
    String[] components = {"nitrogen", "hydrogen", "chlorine"};

    int[,] demand = { {1,3,0}, {1,4,1}};
    int[] profit = {30,40};
    int[] stock = {50,180,40};


    //
    // Variables
    //
    Variable[] production = new Variable[num_products];
    foreach(int p in PRODUCTS) {
      production[p] = solver.MakeNumVar(0, 100000, products[p]);
    }

    //
    // Constraints
    //
    int c_len = components.Length;
    Constraint[] cons = new Constraint[c_len];
    for(int c = 0; c < c_len; c++) {
      cons[c] = solver.Add( (from p in PRODUCTS
                             select (demand[p,c]*production[p])).
                            ToArray().Sum() <= stock[c]);
    }

    //
    // Objective
    //
    solver.Maximize( (from p in PRODUCTS
                      select (profit[p]*production[p])).
                     ToArray().Sum()
                    );



    if (solver.Solve() != Solver.OPTIMAL) {
      Console.WriteLine("The problem don't have an optimal solution.");
      return;
    }

    Console.WriteLine("Objective: {0}", solver.ObjectiveValue());
    foreach(int p in PRODUCTS) {
      Console.WriteLine("{0,-10}: {1} ReducedCost: {2}",
                        products[p],
                        production[p].SolutionValue(),
                        production[p].ReducedCost());
    }


    for(int c = 0; c < c_len; c++) {
      Console.WriteLine("Constraint {0} DualValue {1} Activity: {2} lb: {3} ub: {4}",
                        c,
                        cons[c].DualValue(),
                        cons[c].Activity(),
                        cons[c].Lb(),
                        cons[c].Ub());
    }


    Console.WriteLine("\nWallTime: " + solver.WallTime());
    Console.WriteLine("Iterations: " + solver.Iterations());

  }
예제 #4
0
  /**
   *
   * Volsay problem.
   *
   * From the OPL model volsay.mod.
   *
   * 
   *
   * Also see 
   *  http://www.hakank.org/or-tools/volsay.cs
   *  http://www.hakank.org/or-tools/volsay2.py 
   *
   */
  private static void Solve()
  {

    Solver solver = new Solver("Volsay2",
                               Solver.CLP_LINEAR_PROGRAMMING);


    int num_products = 2;
    IEnumerable<int> PRODUCTS = Enumerable.Range(0, num_products);
    int Gas = 0;
    int Chloride = 1;
    String[] products = {"Gas", "Chloride"};

    //
    // Variables
    //
    Variable[] production = new Variable[num_products];
    foreach(int p in PRODUCTS) {
      production[p] = solver.MakeNumVar(0, 100000, products[p]);
    }

    int num_constraints = 2;
    IEnumerable<int> CONSTRAINTS = Enumerable.Range(0, num_constraints);
    Constraint[] cons = new Constraint[num_constraints];
    cons[0] = solver.Add(production[Gas] + production[Chloride] <= 50);
    cons[1] = solver.Add(3 * production[Gas] + 4 * production[Chloride] <= 180);


    solver.Maximize(40 * production[Gas] + 50 * production[Chloride]);

    Console.WriteLine("NumConstraints: {0}", solver.NumConstraints());

    int resultStatus = solver.Solve();

    if (resultStatus != Solver.OPTIMAL) {
      Console.WriteLine("The problem don't have an optimal solution.");
      return;
    }

    foreach(int p in PRODUCTS) {
      Console.WriteLine("{0,-10}: {1} ReducedCost: {2}", 
                        products[p],
                        production[p].SolutionValue(), 
                        production[p].ReducedCost());
    }


    foreach(int c in CONSTRAINTS) {
      Console.WriteLine("Constraint {0} DualValue {1} Activity: {2} lb: {3} ub: {4}", 
                        c.ToString(),
                        cons[c].DualValue(), 
                        cons[c].Activity(),
                        cons[c].Lb(),
                        cons[c].Ub()
                        );
    }



    Console.WriteLine("\nWallTime: " + solver.WallTime());
    Console.WriteLine("Iterations: " + solver.Iterations());

  }