CPLEX OPL约束,计算唯一字符串

发布于 2025-02-10 13:12:20 字数 1353 浏览 3 评论 0原文

OPL模型的目标是选择以最低成本的总成本来履行所有订单,我对货运上的订单限制必须交付给< = 2 ClientId。当前,最佳解决方案是将第三货运用于所有订单(因为它是最便宜的),但是有了新的约束,第三货运最多可以携带两个订单(因为每个订单均适用于不同的客户端ID)。但是如何编码该约束呢?

当前.mod文件(模型文件)

tuple TFreightTypes {
  key string Destination;
  key string VehicleType;
  int TotalWeight;
  key string Company;
  int Cost;
};

tuple TOrders {
  key int OrderNumber;
  float Weight;
  string ClientId;
  string Destination;
  string MaterialCategory;
  int CategoryPriority;
};

{TFreightTypes}    FreightTypes = ...;
{TOrders}    Orders = ...;


dvar boolean Assignment[Orders][FreightTypes];


dexpr float objective = 
  sum(o in Orders, f in FreightTypes)
     Assignment[o][f] * f.Cost;


//choose freight with total minimum cost
minimize objective;


subject to{

  //c1: all order must be fulfilled
  forall(o in Orders)
    sum(f in FreightTypes) Assignment[o][f]==1;
    
  //c2: the order on a freight must be less than 2 destination
   
}

.DAT文件(数据文件)

FreightTypes = {
    <"LONDON","Type1",20000,"SP TRANSPORTS",40000>,
    <"LONDON","Type2",20000,"SP TRANSPORTS",40000>,
    <"DURHAM","Type3",10000,"SP TRANSPORTS",30000>,

};
Orders = {
    <1,5000,"Client1","LONDON","A",0>,
    <2,1000,"Client2","DURHAM","B",1>,
    <3,2000,"Client3","LONDON","C",1>,      

};
 

The goal of the opl model below is to choose the freights with total minimum cost to fulfill all orders, I have a constraint of the order on a freight must be delivered to <= 2 clientId. Currently the optimal solution is to use the third freight for all orders (since it's the cheapest), but with the new constraint, the 3rd freight can carry at most two orders (since each order is for different clientId). but how to encode that constraint?

current .mod file (model file)

tuple TFreightTypes {
  key string Destination;
  key string VehicleType;
  int TotalWeight;
  key string Company;
  int Cost;
};

tuple TOrders {
  key int OrderNumber;
  float Weight;
  string ClientId;
  string Destination;
  string MaterialCategory;
  int CategoryPriority;
};

{TFreightTypes}    FreightTypes = ...;
{TOrders}    Orders = ...;


dvar boolean Assignment[Orders][FreightTypes];


dexpr float objective = 
  sum(o in Orders, f in FreightTypes)
     Assignment[o][f] * f.Cost;


//choose freight with total minimum cost
minimize objective;


subject to{

  //c1: all order must be fulfilled
  forall(o in Orders)
    sum(f in FreightTypes) Assignment[o][f]==1;
    
  //c2: the order on a freight must be less than 2 destination
   
}

.dat file (data file)

FreightTypes = {
    <"LONDON","Type1",20000,"SP TRANSPORTS",40000>,
    <"LONDON","Type2",20000,"SP TRANSPORTS",40000>,
    <"DURHAM","Type3",10000,"SP TRANSPORTS",30000>,

};
Orders = {
    <1,5000,"Client1","LONDON","A",0>,
    <2,1000,"Client2","DURHAM","B",1>,
    <3,2000,"Client3","LONDON","C",1>,      

};
 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝不服输 2025-02-17 13:12:20

您可以写作

//c2: the orders on a freight must be less than 2 
  forall(f in FreightTypes)
    ctMax2:
      sum(o in Orders) Assignment[o][f]<=2;

,2符合两个目的地,完整的模型看起来像

tuple TFreightTypes {
  key string Destination;
  key string VehicleType;
  int TotalWeight;
  key string Company;
  int Cost;
};

tuple TOrders {
  key int OrderNumber;
  float Weight;
  string ClientId;
  string Destination;
  string MaterialCategory;
  int CategoryPriority;
};



{TFreightTypes}    FreightTypes = ...;
{TOrders}    Orders = ...;


dvar boolean Assignment[Orders][FreightTypes];


dexpr float objective = 
  sum(o in Orders, f in FreightTypes)
     Assignment[o][f] * f.Cost;

{string} destinations={i.Destination | i in Orders};

dvar boolean Destination[FreightTypes][destinations];


//choose freight with total minimum cost
minimize objective;


subject to{
  
  forall(f in FreightTypes,d in destinations)
    Destination[f][d]==(1<=sum(o in Orders:o.Destination==d)Assignment[o][f]);

  //c1: all order must be fulfilled
  forall(o in Orders)
    sum(f in FreightTypes) Assignment[o][f]==1;
    
  //c2: the order on a freight must be less than 2 destinations
  forall(f in FreightTypes)
    ctMax2:
      sum(d in destinations) Destination[f][d]<=2;
   
}

You can write

//c2: the orders on a freight must be less than 2 
  forall(f in FreightTypes)
    ctMax2:
      sum(o in Orders) Assignment[o][f]<=2;

and 2 comply with the 2 destinations, the complete model could look like

tuple TFreightTypes {
  key string Destination;
  key string VehicleType;
  int TotalWeight;
  key string Company;
  int Cost;
};

tuple TOrders {
  key int OrderNumber;
  float Weight;
  string ClientId;
  string Destination;
  string MaterialCategory;
  int CategoryPriority;
};



{TFreightTypes}    FreightTypes = ...;
{TOrders}    Orders = ...;


dvar boolean Assignment[Orders][FreightTypes];


dexpr float objective = 
  sum(o in Orders, f in FreightTypes)
     Assignment[o][f] * f.Cost;

{string} destinations={i.Destination | i in Orders};

dvar boolean Destination[FreightTypes][destinations];


//choose freight with total minimum cost
minimize objective;


subject to{
  
  forall(f in FreightTypes,d in destinations)
    Destination[f][d]==(1<=sum(o in Orders:o.Destination==d)Assignment[o][f]);

  //c1: all order must be fulfilled
  forall(o in Orders)
    sum(f in FreightTypes) Assignment[o][f]==1;
    
  //c2: the order on a freight must be less than 2 destinations
  forall(f in FreightTypes)
    ctMax2:
      sum(d in destinations) Destination[f][d]<=2;
   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文