int Flow::createVarW() { int nvars = 0; double coeff = 0.0; double lb = 0.; double ub = 1e20; VariableHash::iterator vit; Variable::VARTYPE varType = Variable::V_W; Column::COLTYPE colType = Column::COLTYPE::CONTINUOUS; for (int sIt = 0; sIt < g->nTerminals; ++sIt) { Vertex s = g->terminals[sIt]; for (int i = 1; i <= g->nVertices; ++i) { for (int j = 0; j < g->adjList[i].size(); ++j) { Arc arc = g->adjList[i][j]; Variable w(colType, coeff, lb, ub); w.setType(varType); w.setCategory('p'); w.setVertex1(s); w.setArc(arc.toEdge()); vit = vHash[varType].find(w); if (vit != vHash[varType].end()) continue; bool isInserted = addCol(&w); if (isInserted) { w.setColIdx(getNCols() - 1); vHash[varType][w] = w.getColIdx(); ++nvars; } w.setCategory('m'); vit = vHash[varType].find(w); if (vit != vHash[varType].end()) continue; isInserted = addCol(&w); if (isInserted) { w.setColIdx(getNCols() - 1); vHash[varType][w] = w.getColIdx(); ++nvars; } } } } return nvars; }
int Flow::createVarY() { int nvars = 0; double coeff = 0.0; double lb = 0.; double ub = 1.; VariableHash::iterator vit; Variable::VARTYPE varType = Variable::V_Y; Column::COLTYPE colType = Column::COLTYPE::BINARY; for (int sIt = 0; sIt < g->nTerminals; ++sIt) { Vertex s = g->terminals[sIt]; for (int tIt = 0; tIt < g->nTerminals; ++tIt) { Vertex t = g->terminals[tIt]; if (s.getCode() == t.getCode()) continue; for (int i = 1; i <= g->nVertices; ++i) { for (int j = 0; j < g->adjList[i].size(); ++j) { Arc arc = g->adjList[i][j]; Variable y(colType, coeff, lb, ub); y.setType(varType); y.setVertex1(s); y.setVertex2(t); y.setArc(arc); vit = vHash[varType].find(y); if (vit != vHash[varType].end()) continue; bool isInserted = addCol(&y); if (isInserted) { y.setColIdx(getNCols() - 1); vHash[varType][y] = y.getColIdx(); ++nvars; } } } } } return nvars; }
int FacilityLocation::createVarY() { int nVars = 0; double coeff = 0.0; double lb = 0.; double ub = 1.; VariableHash::iterator vit; Variable::VARTYPE varType = Variable::V_Y; Column::COLTYPE colType = choseColType(pType, lb, ub); // @teste colType = Column::CONTINUOUS; // * Variable y_i: // * // * Used to indicate whether the vertex i \in I is a core vertex (Steiner // * vertex) in the solution. for (int i = 1; i <= g->nVertices; ++i) { Vertex* v = &(g->vertices[i]); // @annotation: comment next filter to resolve instances where // the vpn terminals may be considered internal nodes // 20160125 if (v->isTerminal()) continue; Variable var(colType, coeff, lb, ub); var.setType(varType); var.setVertex1(*v); vit = vHash[varType].find(var); if (vit != vHash[varType].end()) continue; bool isInserted = addCol(&var); if (isInserted) { var.setColIdx(getNCols() - 1); vHash[varType][var] = var.getColIdx(); ++nVars; } } return nVars; }
void MtxLP::addStoich(stomap* sto, double lb, double ub, string name){ name = newColName(name); int len = sto->size(); int* ind = new int[len + 1]; double* val = new double[len + 1]; int j = addCol(name, lb, ub); int i = 1; for (stomap::iterator it = sto->begin(); it != sto->end(); ++it){ string row = it->first; int n = nrow(row); if (n == 0) n = addRow(row); ind[i] = n; val[i] = it->second; i++; } set_mat_col(j, len, ind, val); delete [] ind; delete [] val; if (getKind() == MILP) attachIntVar(name); }
int Flow::createVarX() { int nvars = 0; double coeff = 1.0; double lb = 0.; double ub = 1e20; VariableHash::iterator vit; Variable::VARTYPE varType = Variable::V_X; Column::COLTYPE colType = Column::COLTYPE::CONTINUOUS; for (int i = 1; i <= g->nVertices; ++i) { for (int j = 0; j < g->adjList[i].size(); ++j) { Arc arc = g->adjList[i][j]; Variable x(colType, coeff, lb, ub); x.setType(varType); x.setArc(arc.toEdge()); vit = vHash[varType].find(x); if (vit != vHash[varType].end()) continue; bool isInserted = addCol(&x); if (isInserted) { x.setColIdx(getNCols() - 1); vHash[varType][x] = x.getColIdx(); ++nvars; } } } return nvars; }
void MtxLP::attachIntVar(string name){ string intname = get_int_name(name); int i = addCol(intname); set_col_kind(i, INT); }
// solves problem using ben-tal nemirovski approximation // v is approximation parameter // resolve is false, then solve from scratch // resolve is true, use Clp's resolve. ProblemStatus ColaModel::solve_with_bn(bool resolve, int v) { // if we have rotated cones bail out, // create approximation // = first reduce all cones to 3 dimensional cones. // conic constraints obtained by reducing cone i std::vector<Cone*> reduced_cones_i; // set of conic constraints we get by reducing all cones of original problem std::vector<Cone*> reduced_cones; int num_cones = cones_.size(); int num_var = getNumCols(); for (int i=0; i<num_cones; ++i) { if (cones_[i]->size()<=3) { continue; } // reduce conic constraint i to smaller cones, save them in reduced_cc_i LorentzCone * c = dynamic_cast<LorentzCone*>(cones_[i]); reduce_cone (c->size(), c->members(), reduced_cones_i, num_var); // add new cones to problem int num_cones_i = reduced_cones_i.size(); for (int i=0; i<num_cones_i; ++i) { reduced_cones.push_back(reduced_cones_i[i]); } // std::copy(reduced_cones_i, reduced_cones_i+reduced_cones_i.size(), // reduced_cones+reduced_cones.size()); // reset reduced_cc_i reduced_cones_i.clear(); } // print new cones of the problem //reduced_cc->dump_cones(); // = add new variables to the model int diff = num_var - getNumCols(); double infinity = getInfinity(); for(int i=0; i<diff; ++i) { addCol(0, NULL, NULL, 0.0, infinity, 0.0); } // int num_rows = getNumRows(); // int diff = num_var - getNumCols(); // double * collb = new double[diff](); // double * colub = new double[diff](); // double * obj = new double[diff](); // double infinity = getInfinity(); // std::fill(colub, colub+diff, infinity); // CoinPackedVectorBase ** cols = 0; // cols = new CoinPackedVectorBase*[diff]; // int * inds = 0; // for (int i=0; i<diff; ++i) { // cols[i] = new CoinPackedVector(num_rows, inds, 0.0); // } // addCols(diff, cols, collb, colub, obj); // std::cout << "Num Cols: " << getNumCols(); // delete[] collb; // delete[] colub; // delete[] obj; //writeMps("reduced", "mps"); // if reduced cone is not empty if (!reduced_cones.empty()) { setConicConstraints(reduced_cones); } int nOfCones = getNumCones(); if (num_cuts_!=0) { delete[] num_cuts_; } if (num_supports_!=0) { delete num_supports_; } num_cuts_ = new int[nOfCones](); num_supports_ = new int[nOfCones](); solve(resolve); // set columns for new variables to 0 }
// first reduces all conic constraints to 3 dimentional second order conic // constraints as described in ben-tal nemirovski, then solves the conic // problem using liner approximations ProblemStatus ColaModel::solve_reducing_cones(bool resolve) { int num_cones = cones_.size(); // if we have Scaled cones bail out, std::vector<Cone*>::const_iterator it; for (it=cones_.begin(); it!=cones_.end(); it++) { if ((*it)->type()==SCALED) { std::cerr << "Cola: This method is only for Lorentz cones." << std::endl; std::cerr << "Cola: Terminating..." << std::endl; soco_status_ = ABANDONED; return ABANDONED; } } // if we have rotated cones bail out, for (int i=0; i<num_cones; ++i) { if (cones_[i]->type()==SCALED or cones_[i]->type()==RLORENTZ) { std::cerr << "Cola: This method is only for canonical cones." << std::endl; std::cerr << "Cola: Terminating..." << std::endl; soco_status_ = ABANDONED; return ABANDONED; } } // create approximation // = first reduce all cones to 3 dimensional cones. // conic constraints obtained by reducing cone i std::vector<Cone*> reduced_cones_i; // set of conic constraints we get by reducing all cones of original problem std::vector<Cone*> reduced_cones; int num_var = getNumCols(); for (int i=0; i<num_cones; ++i) { LorentzCone * c = dynamic_cast<LorentzCone*>(cones_[i]); if (c->size()<=3) { continue; } // reduce conic constraint i to smaller cones, save them in reduced_cc_i reduce_cone (c->size(), c->members(), reduced_cones_i, num_var); // add new cones to problem int num_cones_i = reduced_cones_i.size(); for (int i=0; i<num_cones_i; ++i) { reduced_cones.push_back(reduced_cones_i[i]); } // copy all from reduced_cones_i vector to reduced_cones // std::copy(reduced_cones_i, reduced_cones_i+reduced_cones_i.size(), // reduced_cones+reduced_cones.size()); // reset reduced_cc_i reduced_cones_i.clear();; } // print new cones of the problem //reduced_cc->dump_cones(); // = add new variables to the model int diff = num_var - getNumCols(); double infinity = getInfinity(); for(int i=0; i<diff; ++i) { addCol(0, NULL, NULL, 0.0, infinity, 0.0); } // int num_rows = getNumRows(); // int diff = num_var - getNumCols(); // double * collb = new double[diff](); // double * colub = new double[diff](); // double * obj = new double[diff](); // double infinity = getInfinity(); // std::fill(colub, colub+diff, infinity); // CoinPackedVectorBase ** cols = 0; // cols = new CoinPackedVectorBase*[diff]; // int * inds = 0; // for (int i=0; i<diff; ++i) { // cols[i] = new CoinPackedVector(num_rows, inds, 0.0); // } // addCols(diff, cols, collb, colub, obj); // std::cout << "Num Cols: " << getNumCols(); // delete[] collb; // delete[] colub; // delete[] obj; //writeMps("reduced", "mps"); // if reduced cone is not empty if (!reduced_cones.empty()) { setConicConstraints(reduced_cones); } int nOfCones = getNumCones(); if (num_cuts_!=0) { delete[] num_cuts_; } if (num_supports_!=0) { delete num_supports_; } num_cuts_ = new int[nOfCones](); num_supports_ = new int[nOfCones](); solve(resolve); // set columns for new variables to 0 }
int FacilityLocation::createVarZ() { int nVars = 0; double coeff = 0.0; double lb = 0.; double ub = 1.; VariableHash::iterator vit; Variable::VARTYPE varType = Variable::V_Z; Column::COLTYPE colType = choseColType(pType, lb, ub); // @teste //colType = Column::CONTINUOUS; double bMinus = 0.; double bPlus = 0.; for (int i = 1; i <= g->nVertices; ++i) { bMinus += g->vertices[i].getIngree(); bPlus += g->vertices[i].getEgree(); } (bMinus < bPlus) ? coeff = bMinus : coeff = bPlus; // * Variable z_e: // * // * Used to indicate whether the edges e = (i, j) is used to connect the // * two core vertex i \in I and j \in I for (int i = 1; i <= g->nVertices; ++i) { Vertex* v = &(g->vertices[i]); // @annotation: comment next filter to resolve instances where // the vpn terminals may be considered internal nodes // 20160125 if (v->isTerminal()) continue; for (int j = 0; j < g->adjList[i].size(); ++j) { Vertex* u = &(g->adjList[i][j].getTail()); Arc* arc = &(g->adjList[i][j]); // @annotation: comment next filter to resolve instances where // the vpn terminals may be considered internal nodes // 20160125 if (arc->getHead().isTerminal() || arc->getTail().isTerminal()) continue; Variable var(colType, coeff, lb, ub); var.setType(varType); var.setArc(arc->toEdge()); vit = vHash[varType].find(var); if (vit != vHash[varType].end()) continue; bool isInserted = addCol(&var); if (isInserted) { var.setColIdx(getNCols() - 1); vHash[varType][var] = var.getColIdx(); ++nVars; } } } return nVars; }
int FacilityLocation::createVarX() { int nVars = 0; double coeff = 0.0; double lb = 0.; double ub = 1.; VariableHash::iterator vit; Variable::VARTYPE varType = Variable::V_X; Column::COLTYPE colType = choseColType(pType, lb, ub); // @teste //colType = Column::CONTINUOUS; std::vector<std::vector<int> > dist((g->nVertices + 1), std::vector<int>((g->nVertices + 1), 0)); clock_t start, end; start = clock(); bfs(g, dist); end = clock(); #ifdef VERBOSE std::cout << "\nBFS_time: " << (end - start) / (double)CLOCKS_PER_SEC << "\n"; #endif #ifdef DEBUG for (int i = 0; i <= g->nVertices; ++i) { for (int j = 0; j <= g->nVertices; ++j) { std::cout << i << " " << j << ": " << dist[i][j] << std::endl; } } #endif // * Variable x_ip: // * // * Used to indicate whether the terminal vertex (client) p \in P is connected // * (or assigned) to the core vertex i \in I. double totalIngree = 0.; double totalEgree = 0.; for (int i = 1; i <= g->nVertices; ++i) { totalIngree += g->vertices[i].getIngree(); totalEgree += g->vertices[i].getEgree(); } for (int i = 1; i <= g->nVertices; ++i) { Vertex client = g->vertices[i]; if (!client.isTerminal()) continue; double tmp1 = std::min(g->vertices[i].getEgree(), totalIngree - g->vertices[i].getIngree()); double tmp2 = std::min(g->vertices[i].getIngree(), totalEgree- g->vertices[i].getEgree()); double b = tmp1 + tmp2; for (int j = 1; j <= g->nVertices; ++j) { Vertex router = g->vertices[j]; // @annotation: comment next filter to resolve instances where // the vpn terminals may be considered internal nodes // 20160125 if (client == router || router.isTerminal()) continue; coeff = b * dist[client.getCode()][router.getCode()]; Variable var(colType, coeff, lb, ub); var.setType(varType); var.setArc(Arc(router, client, 0.)); vit = vHash[varType].find(var); if (vit != vHash[varType].end()) continue; bool isInserted = addCol(&var); if (isInserted) { var.setColIdx(getNCols() - 1); vHash[varType][var] = var.getColIdx(); ++nVars; } } } return nVars; }
void SuperastCPP::addPos(rapidjson::Value& object, clang::Decl* decl) { addLine(object, decl); addCol(object, decl); }
void SuperastCPP::addPos(rapidjson::Value& object, clang::Stmt* stmt) { addLine(object, stmt); addCol(object, stmt); }