当前位置: 首页>>代码示例>>C#>>正文


C# Solver.MakeFixedDurationIntervalVar方法代码示例

本文整理汇总了C#中Solver.MakeFixedDurationIntervalVar方法的典型用法代码示例。如果您正苦于以下问题:C# Solver.MakeFixedDurationIntervalVar方法的具体用法?C# Solver.MakeFixedDurationIntervalVar怎么用?C# Solver.MakeFixedDurationIntervalVar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Solver的用法示例。


在下文中一共展示了Solver.MakeFixedDurationIntervalVar方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Solve

  /**
   *
   * Moving furnitures (scheduling) problem in Google CP Solver.
   *
   * Marriott & Stukey: 'Programming with constraints', page  112f
   *
   * Also see http://www.hakank.org/or-tools/furniture_moving.py
   *
   */
  private static void Solve()
  {
    Solver solver = new Solver("FurnitureMovingIntervals");

    const int n = 4;
    int[] durations = {30,10,15,15};
    int[] demand = {3, 1, 3, 2};
    const int upper_limit = 160;
    const int max_num_workers = 5;

    //
    // Decision variables
    //
    IntervalVar[] tasks = new IntervalVar[n];
    for (int i = 0; i < n; ++i)
    {
      tasks[i] = solver.MakeFixedDurationIntervalVar(0,
                                                     upper_limit - durations[i],
                                                     durations[i],
                                                     false,
                                                     "task_" + i);
    }

    // Fillers that span the whole resource and limit the available
    // number of workers.
    IntervalVar[] fillers = new IntervalVar[max_num_workers];
    for (int i = 0; i < max_num_workers; ++i)
    {
      fillers[i] = solver.MakeFixedDurationIntervalVar(0,
                                                       0,
                                                       upper_limit,
                                                       true,
                                                       "filler_" + i);
    }

    // Number of needed resources, to be minimized or constrained.
    IntVar num_workers  = solver.MakeIntVar(0, max_num_workers, "num_workers");
    // Links fillers and num_workers.
    for (int i = 0; i < max_num_workers; ++i)
    {
      solver.Add((num_workers > i) + fillers[i].PerformedExpr() == 1);
    }

    // Creates makespan.
    IntVar[] ends = new IntVar[n];
    for (int i = 0; i < n; ++i)
    {
      ends[i] = tasks[i].EndExpr().Var();
    }
    IntVar end_time = ends.Max().VarWithName("end_time");

    //
    // Constraints
    //
    IntervalVar[] all_tasks = new IntervalVar[n + max_num_workers];
    int[] all_demands = new int[n + max_num_workers];
    for (int i = 0; i < n; ++i)
    {
      all_tasks[i] = tasks[i];
      all_demands[i] = demand[i];
    }
    for (int i = 0; i < max_num_workers; ++i)
    {
      all_tasks[i + n] = fillers[i];
      all_demands[i + n] = 1;
    }
    solver.Add(all_tasks.Cumulative(all_demands, max_num_workers, "workers"));

    //
    // Some extra constraints to play with
    //

    // all tasks must end within an hour
    // solver.Add(end_time <= 60);

    // All tasks should start at time 0
    // for(int i = 0; i < n; i++) {
    //   solver.Add(tasks[i].StartAt(0));
    // }


    // limitation of the number of people
    // solver.Add(num_workers <= 3);
    solver.Add(num_workers <= 4);

    //
    // Objective
    //

    // OptimizeVar obj = num_workers.Minimize(1);
    OptimizeVar obj = end_time.Minimize(1);
//.........这里部分代码省略.........
开发者ID:RickOne16,项目名称:or-tools,代码行数:101,代码来源:furniture_moving_intervals.cs


注:本文中的Solver.MakeFixedDurationIntervalVar方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。