Specifying equations via API

Questions on using the GAMS APIs (Python, .NET, etc.)
Post Reply
nathan
User
User
Posts: 1
Joined: 1 year ago

Specifying equations via API

Post by nathan »

I am learning the GAMS API by attempting to re-implement some of the example programs given in GAMS language.

I seem to be a bit stuck at adding records to equations. In the following code, I am unsure what to do after adding equation `cost` to the `db`.
There does not seem to be a mechanism by which to

Code: Select all

int transpo(void)
{
    static constexpr auto plants = {"seattle", "san-diego"};
    static constexpr double capacity[2] = {350., 600.};
    static constexpr auto markets = {"new-york, chicago, topeka"};
    static constexpr double demand[3] = {325, 300, 275};
    static constexpr double distances[2][3] = {
        {2.5, 1.7, 1.8},
        {2.5, 1.8, 1.4}
    };
    GAMSWorkspace ws;
    GAMSDatabase db = ws.addDatabase();
    auto i = db.addSet("i", 1, "canning plants");
    for (auto& plant : plants) i.addRecord(plant);
    auto j = db.addSet("j", 1, "markets");
    for (auto& market : markets) j.addRecord(market);
    auto a = db.addParameter("a", "capacity of plant i in cases", i);
    for (auto it = plants.begin(); it != plants.end(); ++it) {
        const auto idx = it - plants.begin();
        a.addRecord(*it).setValue(capacity[idx]);
    }
    auto b = db.addParameter("b", "demand at market j in cases", j);
    for (auto it = markets.begin(); it != markets.end(); ++it) {
        const auto idx = it - markets.begin();
        b.addRecord(*it).setValue(capacity[idx]);
    }
    auto d = db.addParameter("d", "distance in thousands of miles", i, j);
    for (auto pit = plants.begin(); pit != plants.end(); ++pit) {
        const auto pidx = pit - plants.begin();
        for (auto mit = markets.begin(); mit != markets.end(); ++mit) {
            const auto midx = mit - markets.begin();
            d.addRecord(*pit, *mit).setValue(distances[pidx][midx]);
        }
    }
    auto f = db.addParameter("f",
                "freight in dollars per case per thousand miles");
    f.addRecord().setValue(90);
    auto c = db.addParameter("c",
            "transport cost in thousands of dollars per case", i, j);
    for (auto pit = plants.begin(); pit != plants.end(); ++pit) {
        const auto pidx = pit - plants.begin();
        for (auto mit = markets.begin(); mit != markets.end(); ++mit) {
            const auto midx = mit - markets.begin();
            c.addRecord(*pit, *mit).setValue(
                    d.findRecord(*pit, *mit).value() * f.findRecord().value());
        }
    }

    auto x = db.addVariable("x", gams::GAMSEnum::Positive,
            "sipment quantities in cases", i, j);
    auto z = db.addVariable("z", gams::GAMSEnum::Free,
                   "total transportation cost in thousands of dollars");

    auto cost = db.addEquation("cost", gams::GAMSEnum::E,
                               "define objective function");
    /* cost.. z =e= sum((i, j), c(i, j)*x(i, j)) ; */
    cost.addRecord(). /* What goes here??? */
    return 0;
}
Post Reply