本文整理汇总了C++中Digraph::runningNode方法的典型用法代码示例。如果您正苦于以下问题:C++ Digraph::runningNode方法的具体用法?C++ Digraph::runningNode怎么用?C++ Digraph::runningNode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Digraph
的用法示例。
在下文中一共展示了Digraph::runningNode方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
//.........这里部分代码省略.........
if (!found) {cout<<"Could not find target node "<<target_node_name<<endl;exit(0);}
kPaths_Instance T(g,vname,px,py,weight,source,target,k);
//for (DiNodeIt v(g);v!=INVALID;++v){ if(v==T.V[0])vcolor[v]=RED; else vcolor[v]=BLUE;}
//for (int i=1;i<T.nt;i++) vcolor[T.V[i]] = MAGENTA;
//for (ArcIt e(g); e != INVALID; ++e) ecolor[e] = BLUE;
//ViewListDigraph(g,vname,px,py,vcolor,ecolor,"Triangulated graph");
// Generate the binary variables and the objective function
// Add one binary variable for each edge and set its cost in the objective function
for (Digraph::ArcIt e(g); e != INVALID; ++e) {
char name[100];
sprintf(name,"X_%s_%s",vname[g.source(e)].c_str(),vname[g.target(e)].c_str());
x[e] = model.addVar(0.0, 1.0, weight[e],GRB_CONTINUOUS,name); }
model.update(); // run update to use model inserted variables
try {
//if (time_limit >= 0) model.getEnv().set(GRB_DoubleParam_TimeLimit,time_limit);
//model.getEnv().set(GRB_DoubleParam_ImproveStartTime,10); //try better sol. aft. 10s
// if (cutoff > 0) model.getEnv().set(GRB_DoubleParam_Cutoff, cutoff );
//model.write("model.lp"); system("cat model.lp");
// Add degree constraint for each node (sum of solution edges incident to a node is 2)
for (Digraph::NodeIt v(g); v!=INVALID; ++v) {
GRBLinExpr exprin, exprout;
for (Digraph::InArcIt e(g,v); e != INVALID; ++e) exprin += x[e];
for (Digraph::OutArcIt e(g,v); e != INVALID; ++e) exprout += x[e];
if (v==source) model.addConstr(exprout - exprin == k );
else if (v==target) model.addConstr(exprin - exprout == k );
else model.addConstr(exprin - exprout == 0 );
}
model.optimize();
double soma=0.0;
for (DiNodeIt v(g);v!=INVALID;++v) vcolor[v]=BLUE; // all nodes BLUE
vcolor[source]=RED; // change the colors of the source node
vcolor[target]=RED; // and the target node to RED
for (Digraph::ArcIt e(g); e!=INVALID; ++e) {
lpvar[e] = x[e].get(GRB_DoubleAttr_X);
if (lpvar[e] > 1.0 - EPS) soma += weight[e];
}
cout << "kPaths Tree Value = " << soma << endl;
//-----------------------------------------------------------------
// By Lucas Prado Melo: coloring paths by bfs
bool ok = true;
for(int i=0; i < k && ok; i++) {
queue<DiNode> q;
Digraph::NodeMap<Arc> pre(g, INVALID);
q.push(source);
while (!q.empty()) {
DiNode cur = q.front();
q.pop();
for(Digraph::OutArcIt e(g, cur); e!=INVALID; ++e) {
DiNode nxt = g.runningNode(e);
if (pre[nxt] == INVALID && ecolor[e] == NOCOLOR && lpvar[e] > 1.0 - EPS) {
pre[nxt] = e;
q.push(nxt);
}
}
}
if (pre[target] == INVALID) ok = false;
else {
DiNode x = target;
while (x != source) {
ecolor[pre[x]] = 3+i%6; // use colors 3(RED) to 8(CYAN), see myutils.h
x = g.oppositeNode(x, pre[x]);
}
}
}
if (!ok) {cout << "Nao eh possivel encontrar os " << k << " caminhos!" << endl;}
for(Digraph::ArcIt e(g); e!=INVALID; ++e) {
if (lpvar[e] > 1.0 - EPS && ecolor[e] == NOCOLOR ) {
cout << "Alguma(s) aresta(s) nao pertencem a qualquer caminho!" << endl;
break;
}
}
for(Digraph::ArcIt e(g); e!=INVALID; ++e) {
if (lpvar[e] < 1.0-EPS && lpvar[e] > EPS) {
ecolor[e] = GRAY;
}
}
for(Digraph::ArcIt e(g); e!=INVALID; ++e) {
if (lpvar[e] < 1.0-EPS && lpvar[e] > EPS) {
cout << "Alguma(s) aresta(s) possuem valor fracionario! (marcadas com cor cinza claro)" << endl;
break;
}
}
//-----------------------------------------------------------------
cout << "kPaths Tree Value = " << soma << endl;
ViewListDigraph(g,vname,px,py,vcolor,ecolor,
"minimum kPaths cost in graph with "+IntToString(T.nnodes)+
" nodes and "+IntToString(k)+" paths: "+DoubleToString(soma));
} catch (...) {cout << "Error during callback..." << endl; }
return 0;
}