From b19bd8d8a41664328f33c9b459b2b0100f0b303f Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Fri, 9 Nov 2012 12:17:46 +1100 Subject: Add an MCF operator to the separate solver For the solver utility it'd be good to have MCF problems, so here they are! Format is: MCF(cost*) Supplies is a [int,int,int,...], where each int represents a new node Arcs is [int:int, int:int, int:int, ...] where each int:int pair represents an edge from the first to the second (1 indexed from the "supplies" array). Costs is the argument to the function. There must be as many costs as arcs, and they are set from left to right, in order. --- impl/main.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 2 deletions(-) (limited to 'impl/main.cpp') diff --git a/impl/main.cpp b/impl/main.cpp index be9cc82..0daa72a 100644 --- a/impl/main.cpp +++ b/impl/main.cpp @@ -19,6 +19,59 @@ extern "C" { using namespace std; +template +vector toSupplies(pANTLR3_BASE_TREE node) { + ANTLR3_UINT32 num = node->getChildCount(node); + T output; + vector supplies; + pANTLR3_BASE_TREE childNode; + + for (int i = 0; i < num; ++i) { + childNode = (pANTLR3_BASE_TREE) node->getChild(node, i); + stringstream stream((char*)childNode->getText(childNode)->chars); + assert(stream >> output); + supplies.push_back(output); + } + + return supplies; +} + +pair toArc(pANTLR3_BASE_TREE node) { + pair result; + int output; + pANTLR3_BASE_TREE childNode; + + ANTLR3_UINT32 num = node->getChildCount(node); + assert(num == 2); + + { + childNode = (pANTLR3_BASE_TREE) node->getChild(node, 0); + stringstream stream((char*)childNode->getText(childNode)->chars); + assert(stream >> output); + result.first = output; + } + + { + childNode = (pANTLR3_BASE_TREE) node->getChild(node, 1); + stringstream stream((char*)childNode->getText(childNode)->chars); + assert(stream >> output); + result.second = output; + } + + return result; +} + +vector > toArcs(pANTLR3_BASE_TREE node) { + vector > arcs; + ANTLR3_UINT32 num = node->getChildCount(node); + pANTLR3_BASE_TREE childNode; + for (int i = 0; i < num; ++i) { + childNode = (pANTLR3_BASE_TREE) node->getChild(node, i); + arcs.push_back(toArc(childNode)); + } + return arcs; +} + template Expression& treeToExpression(pANTLR3_BASE_TREE node, EquationSystem& system) { ANTLR3_UINT32 num = node->getChildCount(node); @@ -39,7 +92,23 @@ Expression& treeToExpression(pANTLR3_BASE_TREE node, EquationSystem& syste } } - // anything that's not a constant/variable + if (name == "MCF") { + pANTLR3_BASE_TREE childNode; + + childNode = (pANTLR3_BASE_TREE) node->getChild(node, 0); + vector supplies = toSupplies(childNode); + childNode = (pANTLR3_BASE_TREE) node->getChild(node, 1); + vector > arcs = toArcs(childNode); + + vector*> args; + for (unsigned int i = 2; i < num; ++i) { + childNode = (pANTLR3_BASE_TREE) node->getChild(node, i); + args.push_back(&treeToExpression(childNode, system)); + } + return system.expression(new MinCostFlow(supplies, arcs), args); + } + + // anything that's not a constant/variable, or an MCF expr vector*> args; pANTLR3_BASE_TREE childNode; for (unsigned int i = 0; i < num; ++i) { @@ -70,7 +139,7 @@ Expression& treeToExpression(pANTLR3_BASE_TREE node, EquationSystem& syste } else if (name == "GUARD" || name == "guard") { op = new Guard(); } else { - std::cout << "throw exception" << *(char*)NULL; + assert(false); //throw "Parse error: Unknown operator"; } return system.expression(op, args); -- cgit v1.2.3