falcong / or-tools

Automatically exported from code.google.com/p/or-tools
0 stars 0 forks source link

Problem with Element in C# #22

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using Google.OrTools.ConstraintSolver;

public class OrTools
{
  private static long Solve(long num_buses_check = 0)
  {
    SolverParameters sPrm = new SolverParameters();
    sPrm.compress_trail = 0;
    sPrm.trace_level = 0;
    sPrm.profile_level = 0;
    Solver solver = new Solver("OrTools",sPrm);

    //this works
    IntVar[,] x = solver.MakeIntVarMatrix(2,2, new int[] {-2,0,1,2}, "x");

    //this doesn't work
    //IntVar[,] x = solver.MakeIntVarMatrix(2, 2, new int[] { 0, 1, 2 }, "x");

    for (int w = 0; w < 2; w++)
    {
      IntVar[] b = new IntVar[2];
      for (int i = 0; i < 2; i++)
      {
        b[i] = solver.MakeIsEqualCstVar(x[w, i], 0);
      }
      solver.Add(solver.MakeSumGreaterOrEqual(b, 2));
    }

    IntVar[] x_flat = x.Flatten();
    DecisionBuilder db = solver.MakePhase(x_flat,
                                          Solver.CHOOSE_FIRST_UNBOUND,
                                          Solver.ASSIGN_MIN_VALUE);
    solver.NewSearch(db);
    while (solver.NextSolution())
    {
      Console.WriteLine("x: ");
      for (int j = 0; j < 2; j++)
      {
        Console.Write("worker" + (j + 1).ToString() + ":");
        for (int i = 0; i < 2; i++)
        {
          Console.Write(" {0,2} ", x[j, i].Value());
        }
        Console.Write("\n");
      }
      Console.WriteLine("End   at---->" + DateTime.Now);
    }

    Console.WriteLine("\nSolutions: {0}", solver.Solutions());
    Console.WriteLine("WallTime: {0}ms", solver.WallTime());
    Console.WriteLine("Failures: {0}", solver.Failures());
    Console.WriteLine("Branches: {0} ", solver.Branches());

    solver.EndSearch();
    return 1;
  }

  /// <summary>
  ///
  /// </summary>
  /// <param name="args"></param>
  public static void Main(String[] args)
  {
    Console.WriteLine("Check for minimum number of buses: ");
    long num_buses = Solve();
    Console.WriteLine("\n... got {0} as minimal value.", num_buses);
    Console.WriteLine("\nAll solutions: ", num_buses);
    Console.ReadKey();
    //num_buses = Solve(num_buses);
  }
}

Original issue reported on code.google.com by laurent....@gmail.com on 11 Mar 2013 at 10:59

GoogleCodeExporter commented 9 years ago
Working fine with latest SVN.

Original comment by laurent....@gmail.com on 11 Mar 2013 at 2:01