[Cmpl] question on syntax for nested loops in cmpl

Richard Males rbmales at gmail.com
Wed Oct 11 11:44:58 EDT 2017


I am currently interested in the syntax for assigning a sum within a loop,
in effect a nested loop.  I have sets of import and export ports and routes
defined on them (thanks to Thomas Schlieff for help on specifying the
routes):
export_ports := set("Santos","NewOrleans","Seattle");
import_ports:= set("ChinaIP", "EuropeIP", "MexicoIP", "JapanIP",
"SoutheastAsiaIP","FSU-MiddleEastIP","KoreaIP");
routes := [export_ports, import_ports];
and variables
route_tonnage[routes,seasons,grains]:real[0..];

export_tonnage[export_ports,seasons,grains]:real[0..];

import_tonnage[import_ports,seasons,grains]:real[0..];

Using this framework, I want to be able to determine the total quantity
coming in from all routes to an import port as an intermediate variable.  I
also have indices of seasons (1 to 4) and grains (currently only
"Soybeans").  I am able to do this by enumerating for each import port,
season, and grain, as follows, but if I try to generalize to loop over the
set of import ports so that I don't need to repeat my lines, I get syntax
errors.

import_tonnage["ChinaIP",1,"Soybeans"]=sum { i in export_ports:
route_tonnage[[i, "ChinaIP"], 1,"Soybeans"] };

import_tonnage["ChinaIP",2,"Soybeans"]=sum { i in export_ports:
route_tonnage[[i, "ChinaIP"], 2,"Soybeans"] };

import_tonnage["ChinaIP",3,"Soybeans"]=sum { i in export_ports:
route_tonnage[[i, "ChinaIP"], 3,"Soybeans"] };

import_tonnage["ChinaIP",4,"Soybeans"]=sum { i in export_ports:
route_tonnage[[i, "ChinaIP"], 4,"Soybeans"] };

import_tonnage["EuropeIP",1,"Soybeans"]=sum { i in export_ports:
route_tonnage[[i, "EuropeIP"], 1,"Soybeans"] };

import_tonnage["EuropeIP",2,"Soybeans"]=sum { i in export_ports:
route_tonnage[[i, "EuropeIP"], 2,"Soybeans"] };

import_tonnage["EuropeIP",3,"Soybeans"]=sum { i in export_ports:
route_tonnage[[i, "EuropeIP"], 3,"Soybeans"] };

import_tonnage["EuropeIP",4,"Soybeans"]=sum { i in export_ports:
route_tonnage[[i, "EuropeIP"], 4,"Soybeans"] };

I want to do something like:

{m in import_ports,j in seasons, k in grains: import_tonnage[m,j,k]=sum { i
in export_ports: route_tonnage[[i, m], j,k];};}


but this yields error messages relating to the double-nested curly braces.

Any suggestions as to a simplifying approach would be appreciated.  Thanks
in advance.

Dick Males
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://list.coin-or.org/pipermail/cmpl/attachments/20171011/5b01f4fc/attachment.html>


More information about the Cmpl mailing list