0

我正在做一个生产计划项目,所以我想在一些机器(A,B)中找到一个订单生产开始时间。

为简化起见,我假设我有 2 台机器(机器 A 和机器 B)我有以下约束:

  • 出于简化目的,我认为每台机器只能在一天中的某些时间(A_TimeLimitation, )工作B_TimeLimitation

    • 机器A可以在以下范围内工作:

      (2014/12/4 00:00:00 - 2014/12/4 02:00:00) (2014/12/4 04:00:00 - 2014/12/4 06:00:00)

    • B机可在以下范围内工作:

      (2014/12/4 02:00:00 - 2014/12/4 04:00:00)(2014/12/4 05:00:00 - 2014/12/4 07:00:00)

  • 每个产品必须经过机器 A,然后经过机器 B。

  • 每个产品在每台机器上都需要一些时间(在我的示例中为 60 分钟)。
  • 机器 B 应在机器 A 完成工作后启动。
  • B 可以在 A 完成工作后最多 10 分钟开始工作。[更新]
  • 目标是最小化订单生产时间。

我使用以下代码将 a 转换DateTime为分钟:

var minutes = new TimeSpan(dateTime.Ticks).TotalMinutes;

所以使用上面的代码我的DateTime范围转换为Double范围。

我正在使用Microsoft Solver Foundation来解决问题,所以我使用了以下代码:

var context = SolverContext.GetContext();
var model   = context.CreateModel();
var a_OperationTime = 60;
var b_OperationTime = 60;
var tolerance = 10;

//Decision
Decision A_StartTime = new Decision(Domain.IntegerNonnegative, "A_StartTime");
model.AddDecision(A_StartTime);
Decision B_StartTime = new Decision(Domain.IntegerNonnegative, "B_StartTime");
model.AddDecision(B_StartTime);

 //Constraints
model.AddConstraint("A_TimeLimitations" ,
                 (1059220800 <= A_StartTime <= 1059220860) | 
                 (1059221160 <= A_StartTime <= 1059221220));

model.AddConstraint("B_TimeLimitations" , 
                 (1059220920 <= B_StartTime <= 1059220980) | 
                 (1059221100 <= B_StartTime <= 1059221160));
model.AddConstraint("B_ContiguousLimitations" , 
                 B_StartTime - (A_StartTime + 60) <= tolerence);

//Goal
var A_StartTime = model.Decisions.First(x => x.Name == "A_StartTime");
var B_StartTime = model.Decisions.First(x => x.Name == "B_StartTime");
model.AddGoals("OrderDuration", GoalKind.Minimize, B_StartTime - A_StartTime);

//Solve
var directive = new ConstraintProgrammingDirective();
//directive.TimeLimit = 10000;
var solution = context.Solve(directive);

但是当我运行代码时,它似乎进入了一个无限循环,当我指定TimeLimitdirective的 10000 毫秒后,solution.Quality是否Unknown意味着MSF找不到任何解决方案,如果我删除DateTime机器中的第二个范围,A代码可以找到解决方案。

有谁知道问题出在哪里?

4

1 回答 1

1

我发现了问题

  1. 我应该在我的模型中定义更多约束
  2. 我的时间限制非常大,我在hankank指导下更改DateTimedouble转换方法

所以最终的解决方案在这里:

//extention method for converting DateTime to normalized double
public static double ToMinutes(this DateTime value)
{
   return (value-new DateTime(2014, 12, 3)).TotalMinutes;
}

var a1_start = new DateTime(2014, 12, 4).ToMinutes();
var a1_finish = new DateTime(2014, 12, 4, 2, 0, 0).ToMinutes();
var a2_start = new DateTime(2014, 12, 4, 4, 0, 0).ToMinutes();
var a2_finish = new DateTime(2014, 12, 4, 6, 0, 0).ToMinutes();
var b1_start = new DateTime(2014, 12, 4,1,0,0).ToMinutes();
var b1_finish = new DateTime(2014, 12, 4, 1, 55, 0).ToMinutes();
var B2_start = new DateTime(2014, 12, 4, 5, 0, 0).ToMinutes();
var B2_finish = new DateTime(2014, 12, 4, 6, 0, 0).ToMinutes();

var context = SolverContext.GetContext();
var model = context.CreateModel();
var a_OperationTime = 60;
var b_OperationTime = 60;
var tolerance = 10;

//Decision
Decision A_StartTime = new Decision(Domain.IntegerNonnegative,"A_StartTime");
model.AddDecision(A_StartTime);
Decision B_StartTime = new Decision(Domain.IntegerNonnegative,"B_StartTime");
model.AddDecision(B_StartTime);

//Constraints
model.AddConstraint("A_TimeLimitations",
                        ((a1_start <= A_StartTime <= a1_finish) & 
                         (a1_start + 60 <= A_StartTime + 60 <= a1_finish)) |
                        ((a2_start <= A_StartTime <= a2_finish) & 
                         (a2_start + 60 <= A_StartTime + 60 <= a2_finish)));

model.AddConstraint("B_TimeLimitations",
                       ((b1_start <= B_StartTime <= b1_finish) & 
                        ((b1_start + 60 <= B_StartTime + 60 <= b1_finish))) |
                       ((B2_start <= B_StartTime <= B2_finish) & 
                        (B2_start + 60 <= B_StartTime + 60 <= B2_finish)));
model.AddConstraint("B_ContiguousLimitations",
                          B_StartTime - (A_StartTime + 60) <= tolerance);

model.AddConstraint("B_GreaterThan_A",
                     B_StartTime >= A_StartTime + 60);
//Goal
A_StartTime = model.Decisions.First(x => x.Name == "A_StartTime");
B_StartTime = model.Decisions.First(x => x.Name == "B_StartTime");
model.AddGoals("OrderDuration",GoalKind.Minimize, B_StartTime - A_StartTime);

//Solve
var directive = new ConstraintProgrammingDirective();
var solution = context.Solve(directive);
于 2014-12-03T19:42:58.107 回答