有没有人在 OPL 中使用过 Ford Fulkerson 的算法?

问题描述

我有一个问题,可以用图形建模,需要用 opl 实现它,并用 Ford Fulkerson 的算法应用最大化。我没有找到任何用 opl 制作的东西...

解决方法

Ford Fulkerson 是一种解决最大流量的技术。

线性规划是解决最大流量的一种给定技术

https://gist.github.com/ZaydH/b1b09deb1873d8018fdd7cce139d0878 中的 Python CPLEX 示例

可以在 OPL CPLEX 中重写:

{string} nodes={"s","a","b","c","e","t"};

tuple edge
{
  key string o;
  key string d;
  float capacity;
}

{edge} edges with o,d in nodes=
{
  <"s",1>,<"s",9>,9.5>,<"a",2>,<"b",5>,"t",4>,<"c",3.5>,<"e",3>
};

dvar float+ flow[e in edges] in 0..e.capacity;

maximize sum(e in edges:e.o=="s") flow[e];

subject to
{
  // flow conservation
  forall(n in nodes diff {"s","t"}) 
    flowConservation:
      sum(e in edges:e.o==n) flow[e]==sum(e in edges:e.d==n) flow[e];
}

execute
{
  for(var e in edges)
   writeln(e.o," -> ",e.d," : ",flow[e]);
}

给出

// solution (optimal) with objective 10.5
s -> a : 0
s -> b : 7
s -> c : 3.5
a -> e : 0
b -> e : 3
b -> t : 4
c -> b : 0
c -> t : 3.5
e -> t : 3

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...