问题描述
我有一个问题,可以用图形建模,需要用 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