//append item to end of linked list int addParam(Object * tree, char *type) { ListString *node = malloc(sizeof(ListString)); if (node == 0) { warningMsg("Allocation failed in addParam. (ObjectTree.c)\n"); return 1; } node->value = strdup(type); node->next = 0; if (node->value == 0) { warningMsg("strdup failed in addParam. (ObjectTree.c)\n"); return 2; } if (tree->paramTypes == 0) { tree->paramTypes = node; return 0; } ListString *tail = tree->paramTypes; while (tail->next != 0) { tail = tail->next; } tail->next = node; return 0; }
void writeTreeHelper(FILE * outc, FILE * outh, Object * tree, int indent) { ListObject *oIter; ListString *sIter; if (tree == 0) { warningMsg("tree was null in writeTree. (ObjectTree.c)\n"); return; } if (outc == 0 || outh == 0) { warningMsg("output file was null in writeTree. (ObjectTree.c)\n"); return; } oIter = tree->definedSymbols; sIter = tree->paramTypes; //construct and print function header if (isVerb(tree) && !getFlag(tree, FLAG_EXTERNAL)) { compilerDebugPrintf("Writing function %s\n",tree->fullname); writeFunction(outh, tree, indent, false); } else if (tree->category == Type && !getFlag(tree, FLAG_EXTERNAL)) { writeClass(outc, outh, tree, indent); } else if (tree->category == Dummy) { //Dummy } else { writeOther(outc, outh, tree, indent); } }
ListString *addCode(Object * tree, char *line) { ListString *node = malloc(sizeof(ListString)); if (node == 0) { warningMsg("Allocation failed in addCode. (ObjectTree.c)\n"); return 0; } node->value = strdup(line); node->next = 0; if (node->value == 0) { warningMsg("strdup failed in addCode. (ObjectTree.c)\n"); return 0; } if (tree->code == 0) { tree->code = node; return node; } ListString *tail = tree->code; while (tail->next != 0) { tail = tail->next; } tail->next = node; return node; }
ListString *insertCode(Object * tree, char *line) { ListString *node = malloc(sizeof(ListString)); if (node == 0) { warningMsg("Allocation failed in addCode. (ObjectTree.c)\n"); return 0; } node->value = strdup(line); if (node->value == 0) { warningMsg("strdup failed in addCode. (ObjectTree.c)\n"); return 0; } if (tree->code == 0) { tree->code = node; return node; } node->next = tree->code; tree->code = node; return node; }
int addSymbol(Object * tree, Object * leaf) { ListObject *node = malloc(sizeof(ListObject)); if (node == 0) { warningMsg("Allocation failed in addSymbol. (ObjectTree.c)\n"); return 1; } node->value = leaf; node->next = 0; if (tree->definedSymbols == 0) { tree->definedSymbols = node; return 0; } ListObject *tail = tree->definedSymbols; while (tail->next != 0) { tail = tail->next; } tail->next = node; return 0; }
void findShortestPath(Vertex & _root, Vertex & _destination, Graph* _g, std::vector<Arc> & _path) { int root = _root.getCode(); bool isVertexFound = false; std::list<int> f; std::vector<bool> visited(_g->nVertices + 1, false); std::vector<Arc> pred(_g->nVertices + 1); // BFS from root vertex towards destination vertex f.push_back(root); while (!isVertexFound && f.size() != 0) { int v1 = *f.begin(); visited[v1] = true; for (int j = 0; j < _g->adjList[v1].size(); ++j) { int v2 = _g->adjList[v1][j].getTail().getCode(); if (!visited[v2]) { pred[v2] = _g->adjList[v1][j]; f.push_back(v2); visited[v2] = true; if (v2 == _destination.getCode()) { isVertexFound = true; break; } } } f.pop_front(); } // Retrieving the path from predecessors int predV = -1; int element = _destination.getCode(); do { predV = pred[element].getHead().getCode(); _path.push_back(pred[element]); element = predV; } while (element != _root.getCode() && element != -1); if (element == -1) { int warnCode = 970; std::string msg = "Problem retrieving the shortest path"; warningMsg(NULL, __func__, msg.data(), warnCode); _path.clear(); } printf(""); return; }
Object *findByNameInScope(Object * scope, char *name, int bUseFullName) { if (scope == 0 || name == 0) { warningMsg("Object or name was null in findByNameInScope. (ObjectTree.c)\n"); return 0; } //printf("in findByName, searching %s for %s\n", scope->fullname, name); Object *result; switch (scope->category) { case CodeBlock: result = searchCodeBlock(scope, name, bUseFullName); break; case Function: //compilerDebugPrintf("Searching %s in function %s\n",name,scope->name); result = searchFunction(scope, name, bUseFullName); break; case Constructor: result = searchConstructor(scope, name, bUseFullName); break; case Type: result = searchType(scope, name, bUseFullName); break; default: warningMsg("cannot search within category %d\n", scope->category); break; } //printf("exiting findByName (%s), found %s\n", scope->fullname, result? result->fullname : "nothing."); return result; }
//mallocs memory and returns a pointer to a new Object Object *CreateObject(char *name, char *fullname, Object * parentScope, OBJ_TYPE type, char *returnType) { Object *result = (Object *) malloc(sizeof(Object)); if (result == 0) { warningMsg("%s", "CreateObject couldn't allocate a new object. (ObjectTree.c)\n"); return 0; //malloc failed. } result->name = name ? strdup(name) : 0; result->fullname = fullname ? strdup(fullname) : 0; result->parentClass = 0; result->parentScope = parentScope; result->category = type; result->returnType = returnType ? strdup(returnType) : 0; result->paramTypes = 0; result->definedSymbols = 0; result->code = 0; return result; }
SolverMIP::SOLVERSTAT FacilityLocation::solveLRExtentedFormulations(MulltipleCutSetSeparation _cutSetSepFunc, double _tol) { int pos = 0; int nbCuts = 0; int sentinel = 0; int MAX_ITER = 100; int tailOffCounter = 0; int tailOffTol = 3; int printInterval = 5; double currentLp = 0., lastLp = 0.; bool noViolatedCutFound = true; std::set<long> hashTable; TypeVariableHashPtr vHashPtr = &(vHash); std::vector<std::set<int>*> cutSets; SolverMIP::SOLVERSTAT ret = SolverMIP::SOLVERSTAT::SOLVERSTAT_UNKNOWN; if (xSol != NULL) delete[] xSol; xSol = new double[getNCols()]; clock_t start = clock(); do { lastLp = currentLp; status = SolverMIP::solve(SolverMIP::METHOD::METHOD_DUAL); if (status == SolverMIP::SOLVERSTAT::SOLVERSTAT_MIPOPTIMAL || status == SolverMIP::SOLVERSTAT::SOLVERSTAT_LPOPTIMAL || status == SolverMIP::SOLVERSTAT::SOLVERSTAT_FEASIBLE) { ret = SolverMIP::SOLVERSTAT::SOLVERSTAT_FEASIBLE; currentLp = getObjVal(); // Getting fractional node solution getX(); if (sentinel % printInterval == 0) { printf("\n---- iter: %d\n", sentinel + 1); printf("OBJ_VAL = %lf\n", currentLp); } #ifndef DEBUG //Printing xNode solution printf("\n\n---- iter: %d\n", sentinel + 1); for (int varType = Variable::V_X; varType != Variable::V_UNKNOWN; varType += 10) { VariableHash::iterator vit = vHashPtr->at((Variable::VARTYPE)varType).begin(); for (; vit != vHashPtr->at((Variable::VARTYPE)varType).end(); ++vit) { int idx = vit->second; if (xSol[idx] > SOLVER_EPS) std::cout << (vit->first).toString() << "(" << idx << "); " << xSol[idx] << std::endl; printf(""); } printf(""); } #endif // Verifying optimality conditions if (fabs(currentLp - lastLp) < _tol) { ++tailOffCounter; if (tailOffCounter > tailOffTol) { ret = SolverMIP::SOLVERSTAT::SOLVERSTAT_LPOPTIMAL; break; } } else tailOffCounter = 0; // Calling the separation routine pos = cutSets.size(); int cutSize = _cutSetSepFunc(g, vHashPtr, xSol, cutSets, true); // If a fractional cycle is found... if (cutSets.size() - pos > 0) { noViolatedCutFound = false; for (int i = pos; i < cutSets.size(); ++i) { std::set<int>* sPtr = cutSets[i]; // Check whether the cut has already been generated unsigned long hashVal = hashFunc(*sPtr); std::set<long>::iterator it = hashTable.find(hashVal); if (it != hashTable.end()) { #ifdef DEBUG int warnCode = 990; std::string aux = convertSetToString(s); std::string msg = "The identified cut set was already separated " + convertSetToString(s); warningMsg(NULL, __func__, msg.data(), warnCode); #endif } else hashTable.insert(hashVal); // ... we must find the cut set ... std::vector<Variable> cutSet; for (VariableHash::iterator vit = vHashPtr->at(Variable::V_Z).begin(); vit != vHashPtr->at(Variable::V_Z).end(); ++vit) { Variable z = vit->first; int head = z.getArc().getHead().getCode(); int tail = z.getArc().getTail().getCode(); std::set<int>::iterator hIt = sPtr->find(head); std::set<int>::iterator tIt = sPtr->find(tail); // ... which is composed by those arcs with one endpoint in S bool isHeadInS = hIt != sPtr->end(); bool isTailInS = tIt != sPtr->end(); if (!(isHeadInS && isTailInS) && (isHeadInS || isTailInS)) { cutSet.push_back(z); } } // Identifying the y-variables involved in cut set constraints // And split them into two sets std::vector<Variable> sVec; std::vector<Variable> sCompVec; for (VariableHash::iterator vit = vHashPtr->at(Variable::V_Y).begin(); vit != vHashPtr->at(Variable::V_Y).end(); ++vit) { Variable v = vit->first; int nodeIdx = v.getVertex1().getCode(); if (sPtr->find(nodeIdx) != sPtr->end()) { sVec.push_back(v); } else { sCompVec.push_back(v); } } // Translating valid inequalities found into cplex/matrix representation int nzcnt = cutSet.size() + 2; std::vector<int> idx(nzcnt); std::vector<double> val(nzcnt); for (int i = 0; i < sVec.size(); ++i) { idx[0] = sVec[i].getColIdx(); val[0] = -1.0; for (int j = 0; j < sCompVec.size(); ++j) { idx[1] = sCompVec[j].getColIdx(); val[1] = -1.0; for (int k = 0; k < cutSet.size(); ++k) { idx[k + 2] = cutSet[k].getColIdx(); val[k + 2] = 1.0; } // Adding user generated cut int nRows = 1; double rhs = -1; char sense = 'G'; int rmatbeg = 0; int newColsAdded = 0; status = CPXaddrows(env, lp, newColsAdded, nRows, nzcnt, &rhs, &sense, &rmatbeg, &idx[0], &val[0], NULL, NULL); //status = CPXcutcallbackadd(_env, _cbdata, _wherefrom, nzcnt, -1, 'G', &idx[0], &val[0], CPX_USECUT_FORCE); if (status) { int warnCode = 999; std::string msg = "Failed to add integer cut."; warningMsg(NULL, __func__, msg.data(), warnCode); } else nbCuts++; printf(""); } } } #ifdef DEBUG // salva um arquivo .lp com o LP atual writeProbLP(".\\lpRelax"); #endif } else { // No violated cut was found noViolatedCutFound = true; } // If no violated cut was found if (noViolatedCutFound) { ret = SolverMIP::SOLVERSTAT::SOLVERSTAT_LPOPTIMAL; break; } } else { int warnCode = 201; std::string msg = "Model is infeasible"; warningMsg(typeid(*this).name(), __func__, msg.data(), warnCode); // salva um arquivo .lp com o LP atual writeProbLP(".\\infeasible"); ret = SolverMIP::SOLVERSTAT::SOLVERSTAT_INFEASIBLE; break; } } while (++sentinel < MAX_ITER); // Deallocating memory for (int i = 0; i < cutSets.size(); ++i) { if (cutSets[i] != NULL) { delete cutSets[i]; cutSets[i] = NULL; } } clock_t end = clock(); printf("\n-----"); printf("\n----- iter: %d", sentinel + 1); printf("\n----- OBJ_VAL = %lf", currentLp); printf("\n----- Exectution time: %.4f", (end - start) / (double)CLOCKS_PER_SEC); return ret; }
void ProblemDataLoader::load(void) { int nNodes, nTerminals, nArcs; std::ifstream fin(file, std::ios::in); if (!fin.good()) { std::string msg = "Cannot open " + file; errorMsg(typeid(*this).name(), __func__, msg.c_str(), 1); exit(EXIT_FAILURE); } #ifdef VERBOSE std::cout << "Reading file: " << file << "\n";; #endif //reading general instance information fin >> nNodes >> nTerminals >> nArcs; #ifdef VERBOSE std::cout << "\nNodes(" << nNodes << "), Terminals("<< nTerminals << "), Arcs(" << nArcs << ")\n"; #endif g->nVertices = nNodes; g->nTerminals = nTerminals; g->nArcs = nArcs; g->reserve(nNodes + 1); //reading terminal nodes information for (int i = 0; i < nTerminals; ++i) { int code; double ingree, egree; fin >> code >> ingree >> egree; #ifdef VERBOSE std::cout << "\nterminal " << code << ", ingree " << ingree << ", egree " << egree; #endif Vertex terminal(code, ingree, egree, true); g->addVertex(terminal); g->addTerminal(terminal); } //reading graph edges (network links) for (int i = 0; i < nArcs; ++i) { int head, tail; double capacity; fin >> head >> tail >> capacity; Arc aux(g->vertices[head], g->vertices[tail], capacity); if (head == tail) { int warnCode = 010; std::string msg = "Self arc : (" + aux.toString() + ")"; warningMsg(typeid(*this).name(), __func__, msg.data(), warnCode); continue; } g->addArc(aux); #ifdef VERBOSE std::cout << "\nArc(" << aux.toString() << ")"; #endif } return; }
Object *findFunctionMatch(Object * scope, char *name, int paramc, char **params) { if (scope == 0 || name == 0) { warningMsg("Object or name was null in findFunctionMatch. (ObjectTree.c)\n"); return 0; } int i; char *s; if (scope->definedSymbols == 0 && scope->parentScope == 0) { return 0; } else if (scope->definedSymbols == 0) { return findFunctionMatch(scope->parentScope, name, paramc, params); } ListObject *iter = scope->definedSymbols; while (iter != 0) { if (!strcmp(name, iter->value->name)) { ListString *iter_param = iter->value->paramTypes; for (i = 0; i < paramc && iter_param != 0; i++) { if (strcmp(params[i], iter_param->value)) { break; } iter_param = iter_param->next; } if (i == paramc && iter_param == 0) { return iter->value; } } iter = iter->next; } if (scope->parentScope != 0) { return findFunctionMatch(scope->parentScope, name, paramc, params); } return 0; }