在 Cplex 中如何进行条件求和?像 Excel 中的 sumifs? [英] How is a conditional summation possible in Cplex? like sumifs in Excel?

查看:95
本文介绍了在 Cplex 中如何进行条件求和?像 Excel 中的 sumifs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的模型(它是 rcpsp 模型)中对所有使用的资源求和我怎样才能在 CPLEX 中做到这一点?一开始我是这样写的:

I want to sum all used resources among times in my model (it's rcpsp model) how can I do it in CPLEX? at first I wrote this:

forall(k in K)
  forall(t in 1..f[nAct])
    sum(i in I:f[i]-d[i]<=t-1 && t<=f[i]) r[i,k] <= aR[k]; 

(注:K为资源范围,nAct为活动数,f[i]为数组dvar,表示活动i的完成时间,d[i]为i的持续时间,r[i,k]是活动 i 所需的 k 资源,aR[k] 是 k 的可用资源.)

(note: K is a range for resources, nAct is number of activities, f[i] is an array dvar and indicates finishing time of activity i, d[i] is duration of i,r[i,k] is required resource of k for activity i and aR[k] is available resources of k.)

问题是 cplex 不接受 sum 条件下的决策变量.我把它改成:

The problem is that the cplex doesn't accept decision variable in sum's condition. I changed it to:

forall(k in K)
  forall(t in 1..f[nAct])
    sum(i in I) (f[i]-d[i]<=t-1 && t<=f[i])*r[i,k] <= aR[k]; 

但它没有用.它在运行后在问题浏览器中产生了真正的约束(我不知道为什么)并且它使这个约束无效.

But it didn't work. it made True constraints in Problem Browser after run(I don't know why) and it made this constraint ineffective.

知道怎么解决吗?

推荐答案

有几种方法可以将您的问题放入整数编程框架中.关于这个主题有书籍.我认为这是最简单的表述.

There are several ways to put your problem into an integer programming framework. There are books written on this subject. I think this is the simplest formulation.

我假设在您的问题中,r[i,k] 和 d[i] 是已知的,并且时间范围被分成离散的时间段.

I assume that in your problem, r[i,k] and d[i] are known and that the time horizon is broken into discrete time periods.

  • on[i,t] 指示活动 i 在时间 t 处于活动状态
  • start[i,t] 指示活动 i 在周期 t 开始时开始
  • end[i,t] 指示活动 i 在周期 t 结束时完成

所以 in[i,t] 替换了条件 f[i]-d[i]<=t-1 &&t<=f[i])*r[i,k]你的约束变成了

So in[i,t] replaces the condition f[i]-d[i]<=t-1 && t<=f[i])*r[i,k] Your constraint becomes

forall(k in K)
   forall(t in 1..f[nAct])
      sum(i in I : r[i,k] = 1) on[i,t] <= aR[k]; 

您还需要添加约束来强制定义 on、start 和 off.

You also need to add constraints to enforce the definition of on, start and off.

   forall(t in 2..f[nAct])
      forall(i in I)
         on[i,t-1] - on[i,t] = end[i,t-1] - start[i,t];

   forall(i in I)
      on[i,0] = start[i,0];

   forall(i in I)
      sum(t in 1..f[nAct]) start[i,t] = 1;
   forall(i in I)
      sum(t in 1..f[nAct]) end[i,t] = 1;
   forall(i in I)
      sum(t in 1..f[nAct]) on[i,t] = d[i];

这篇关于在 Cplex 中如何进行条件求和?像 Excel 中的 sumifs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆