I'm trying to code MST4 in CPLEX, but because I'm new to code writing I have difficulty in doing it. I would really appreciate if anyone could help me. Thanks
Asked
Active
Viewed 76 times
1 Answers
0
In OPL tips and tricks see Minimum Spanning Tree
tuple edge
{
key int o;
key int d;
int weight;
}
{edge} edges=...;
{int} nodes={i.o | i in edges} union {i.d | i in edges};
range r=1..-2+ftoi(pow(2,card(nodes)));
{int} nodes2 [k in r] = {i | i in nodes: ((k div (ftoi(pow(2,(ord(nodes,i))))) mod 2) == 1)};
dvar boolean x[edges];
minimize sum (e in edges) x[e]*e.weight;
subject to
{
sum(e in edges) x[e]==card(nodes)-1;
// Subtour elimination constraints.
forall(k in r) // all subsets but empty and all
sum(e in edges:(e.o in nodes2[k]) && (e.d in nodes2[k])) x[e]<=card(nodes2[k])-1;
}
{edge} solution={e | e in edges : x[e]==1};
execute
{
writeln("minimum spanning tree ",solution);
}

Alex Fleischer
- 9,276
- 2
- 12
- 15