[Cmpl] question on syntax for nested loops in cmpl
Thomas Schleiff
t.schleiff at cmpl.de
Thu Oct 12 06:08:47 EDT 2017
You have a wrong semicolon in your nested loop.
You must consider the difference between expressions and statements, as
common in programming languages. "sum { i in export_ports:
route_tonnage[[i, m], j,k] }" is an expression, so a semicolon before
the closing curly bracket would be wrong. But "import_tonnage[m,j,k]=sum
{ i in export_ports: route_tonnage[[i, m], j,k] };" is a statement, so
the semicolon at the end of it is correct.
Thomas Schleiff
Am 11.10.2017 um 17:44 schrieb Richard Males:
>
> 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
>
>
> _______________________________________________
> Cmpl mailing list
> Cmpl at list.coin-or.org
> https://urldefense.proofpoint.com/v2/url?u=https-3A__list.coin-2Dor.org_mailman_listinfo_cmpl&d=DwICAg&c=Ngd-ta5yRYsqeUsEDgxhcqsYYY1Xs5ogLxWPA_2Wlc4&r=bC-ot6cnDq3NJprIa2U54ZW0PXGvQSMdgN_s4GhXuu8&m=5Orel3YhSGsQGudTLGB0FmEPcgSfhnHERZy3COuRlvA&s=nW_9_NLdBPlp0daEmQoOrFCykcKoLtw52gnAlvKu_Yc&e=
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://list.coin-or.org/pipermail/cmpl/attachments/20171012/05152e27/attachment.html>
More information about the Cmpl
mailing list