bus_schedule.cs 3.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
//
// Copyright 2012 Hakan Kjellerstrand
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Google.OrTools.ConstraintSolver;

public class BusSchedule
{


  /**
   *
   * Bus scheduling.
   *
   * Minimize number of buses in timeslots.
   *
   * Problem from Taha "Introduction to Operations Research", page 58.
   * 
   * This is a slightly more general model than Taha's.
   *
   * Also see, http://www.hakank.org/or-tools/bus_schedule.py
   *
   */
  private static long Solve(long num_buses_check = 0)
  {

    Solver solver = new Solver("BusSchedule");

    //
    // data
    //
    int time_slots = 6;
    int[] demands = {8, 10, 7, 12, 4, 4};
    int max_num = demands.Sum();

    //
    // Decision variables
    //

    // How many buses start the schedule at time slot t
    IntVar[] x = solver.MakeIntVarArray(time_slots, 0, max_num, "x");
    // Total number of buses
    IntVar num_buses = x.Sum().VarWithName("num_buses");

    //
    // Constraints
    //

    // Meet the demands for this and the next time slot.
    for(int i = 0; i < time_slots - 1; i++) {
      solver.Add(x[i]+x[i+1] >= demands[i]);
    }

    // The demand "around the clock"
    solver.Add(x[time_slots-1] + x[0] - demands[time_slots-1] == 0);

    // For showing all solutions of minimal number of buses
    if (num_buses_check > 0) {
      solver.Add(num_buses == num_buses_check);
    }


    //
    // Search
    //
    DecisionBuilder db = solver.MakePhase(x,
                                          Solver.CHOOSE_FIRST_UNBOUND,
                                          Solver.ASSIGN_MIN_VALUE);

    if (num_buses_check == 0) {

      // Minimize num_buses
      OptimizeVar obj = num_buses.Minimize(1);
      solver.NewSearch(db, obj);

    } else {

      solver.NewSearch(db);

    }

    long result = 0;
    while (solver.NextSolution()) {
      result = num_buses.Value();
      Console.Write("x: ");
      for(int i = 0; i < time_slots; i++) {
        Console.Write("{0,2} ", x[i].Value());
      }
      Console.WriteLine("num_buses: " + num_buses.Value());
    }

    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 result;

  }



  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);
    num_buses = Solve(num_buses);

  }
}