26 #define EPSILON_MAXFLOW 1e-10 53 return abs<T>(first) >= abs<T>(second);
64 for (
int i = 0; i<sum_card; ++i) {
68 swap(X[i],X[--sum_card]);
73 memset(X,0,sum_card*
sizeof(T));
82 swap(prU[0],prU[sizeU/2]);
86 for (
int i = 1; i<sizeU; ++i) {
87 if (prU[i] >= prU[0]) {
89 swap(prU[sizeG++],prU[i]);
94 int new_card=sum_card+sizeG;
95 if (new_sum - prU[0]*new_card <= lambda) {
105 T thrs =
MAX(0,(sum-lambda)/sum_card);
106 for (
int i = 0; i<n; ++i)
107 X[i] =
MIN(X[i],thrs);
117 void inline create_tree(
const int N_variables,
int* own_variables,
118 int* N_own_variables, T* lambda,
mwSize* groups_ir,
mwSize* groups_jc,
119 const int N_groups,
const int root_node = 0);
121 int inline perform_order(
const int current_node,
const int pointer);
122 int inline perform_dfs(
const int current_node,
const int pointer);
130 T
inline val_norm(
const T* pr_alpha,
const int current_node,
const bool l1 =
false);
131 T
inline val_norm2(
const T* pr_alpha,
const int current_node, T& tmp,
const bool l1 =
false);
132 T
inline val_zero(
const T* pr_alpha,
const int current_node);
133 T
inline val_zero2(
const T* pr_alpha,
const int current_node,
bool& tmp);
155 template <
typename T>
172 template <
typename T>
183 template <
typename T>
185 int* N_own_variables, T* lambda,
mwSize* groups_ir,
mwSize* groups_jc,
186 const int N_groups,
const int root_node) {
190 _thrs=
new T[N_groups];
202 _work =
new T[
MAX(N_groups,N_variables)];
205 template <
typename T>
207 int cur_pointer=pointer;
215 _order[cur_pointer]=current_node;
216 return cur_pointer+1;
219 template <
typename T>
221 int cur_pointer=pointer;
230 template <
typename T>
233 return this->
val_norm2(pr_alpha,current_node,tmp,l1);
237 template <
typename T>
243 tmp= l1 ?
MAX(tmp,tmp2) : tmp+tmp2;
248 sum+=
_lambda[current_node]*tmp;
252 sum+=
_lambda[current_node]*sqrt(tmp);
257 template <
typename T>
273 template <
typename T>
276 return this->
val_zero2(pr_alpha,current_node,tmp);
279 template <
typename T>
289 for (
int j = 0; j<_size_variables[i];++j) {
290 if (abs<T>(max-abs<T>(pr[j])) < 1e-10) ++num_max;
292 T add=T(1.0)/num_max;
293 for (
int j = 0; j<_size_variables[i];++j) {
294 if (abs<T>(max-abs<T>(pr[j])) < 1e-10 && input[
_pr_variables[i]+j]) {
295 output[
_pr_variables[i]+j]+= input[_pr_variables[i]+j] > 0 ? add : -add;
314 template <
typename T>
327 tmp.l1project_weighted(out,tmpw,fact,
true);
334 template <
typename T>
347 if (
_thrs[node] == 0) {
377 if (
_thrs[node] == 0) {
394 template <
typename T>
410 if (
_work[node] == 0) {
420 template <
typename T>
430 T total=input.
asum();
439 while (!nodes.empty()) {
440 const int node=nodes.
front();
452 tau=sum_variables/sum_weights;
457 if (
_thrs[0] >= old_thrs)
break;
466 MaxFlow(
const int N,
const int* num_edges,
const int s,
const int t);
469 void inline add_edge(
const int u,
const int v,
const T cu,
const T cv);
471 void inline discharge(
const list_int& component,
const int u,
const int max_label);
473 void inline global_relabelling();
475 void inline perform_maxflow();
477 void inline print_excess();
478 void inline print_labels();
480 void inline deactivate();
481 void inline deactivate(
const list_int& component);
485 void inline extractConnexComponents(std::list< list_int* >& connex_components);
486 T
inline project(
const list_int& component,
487 const T* variables_in,T* variables_out,T* work,
489 T
inline project_weighted(
const list_int& component,
490 const T* variables_in,T* variables_out,T* work,
const T* weights,
492 T
inline project_box(
const list_int& component,
const T* variables_in,T*
493 variables_out,T* work,
bool& fusion,
const int Ng);
495 T
inline flow_component(
const list_int& component,
const int Ng);
496 bool inline splitComponent(
const list_int& component,
497 std::list< list_int* >& connex_components,
const int Ng,
bool* positive,
const bool addpos =
true);
498 void inline reset_component(
const list_int& component);
499 void inline perform_maxflow_component(
const list_int& component);
500 void inline component_relabelling(
const list_int& component,
501 const int max_label,
const bool force);
502 void inline gap_relabelling(
const list_int& component,
const int gap,
const int max_label);
503 void inline component_gap(
const list_int& component);
504 void inline update_capacities(
const list_int& component,T* work);
505 void inline set_capacities_variables(
const T* cap,
const int Nv,
const int Ng);
506 void inline set_capacities_groups(
const list_int& component,
507 const Vector<T>& weights,
const T lambda,
const int Ng);
508 void inline update_capacities_aux(
const int node,T* work);
509 void inline restore_capacities(
const list_int& component);
510 T
inline compute_thrs_project_l1(T* X,
const int n,
const T lambda);
512 bool inline check_flow();
513 void inline restore_capacities();
514 void inline restore_flow();
515 void inline reset_flow();
516 void inline scale_flow(
const T scal);
517 void inline save_capacities();
518 void inline save_flow();
519 void inline set_weights(
const T lambda);
520 void inline set_weights(
const T* weights,
const T lambda);
521 void inline print_graph();
522 inline void init_split_variables(
SpMatrix<T>& splitted_w,
const int Ng,
const int Nv);
523 inline void init_split_variables_aux(
const int node,
int& current_counter,
Vector<int>& counter_node,
list_int** splitted_w,
524 const int Ng,
const int Nv);
525 void inline print_component(
const list_int& component);
526 void inline print_component2(
const list_int& component);
527 void inline print_sink();
528 void inline print_graph_aux(
const int node);
529 T
inline norm(
const T* variables, T* work,
const T* weights,
const int Ng,
const bool linf =
true);
530 inline int nzmax()
const {
return _nzmax; };
533 const int node,
list_int& list,
const int Ng);
564 template <
typename T>
571 memset(_labels,0,N*
sizeof(
int));
573 memset(_excess,0,N*
sizeof(T));
577 _num_edges=
new int[N];
578 _current_edges=
new int[N];
579 memset(_num_edges,0,N*
sizeof(
int));
580 memset(_current_edges,0,N*
sizeof(
int));
581 _max_num_edges=
new int[N];
582 for (
int i = 0; i<N; ++i) _max_num_edges[i]=num_edges[i];
583 _pr_node=
new int[N+1];
585 for (
int i = 1; i<=N; ++i) _pr_node[i]=_pr_node[i-1]+_max_num_edges[i-1];
587 _children=
new int[_nzmax];
588 _reverse_address=
new int[_nzmax];
589 _capacity=
new T[_nzmax];
590 _copycapacity=
new T[_nzmax];
591 _flow=
new T[_nzmax];
592 memset(_flow,0,_nzmax*
sizeof(T));
593 _current_max_label=0;
595 _all_nodes=
new int[N+1];
596 for (
int i = 0; i<=N; ++i) _active_nodes[i]=
new list_int();
599 template <
typename T>
605 delete[](_num_edges);
606 delete[](_current_edges);
607 delete[](_max_num_edges);
609 delete[](_reverse_address);
611 delete[](_copycapacity);
613 for (
int i = 0; i<=_N; ++i)
delete(_active_nodes[i]);
614 delete[](_active_nodes);
615 delete[](_all_nodes);
619 template <
typename T>
622 const int pu=_pr_node[u];
623 const int pv=_pr_node[v];
624 const int nu=_num_edges[u]+pu;
625 const int nv=_num_edges[v]+pv;
630 _reverse_address[nu]=nv;
631 _reverse_address[nv]=nu;
637 template <
typename T>
640 cerr <<
"Discharge " << u << endl;
642 const T* capacity=_capacity+_pr_node[u];
643 T* flow=_flow+_pr_node[u];
644 const int* children=_children+_pr_node[u];
645 const int* reverse=_reverse_address+_pr_node[u];
647 const int curr=_current_edges[u];
648 const int nn=_num_edges[u];
652 const int num_c=(pr+curr) % nn;
653 const int v=children[num_c];
654 if (capacity[num_c] > flow[num_c]) {
655 if (_labels[u] > _labels[v]) {
657 const T delta=
MIN(_excess[u],capacity[num_c]-flow[num_c]);
661 cerr <<
"Send " << delta <<
" from " << u <<
" to " << v << endl;
663 if (!_active[v] && v != _t) {
665 _active_nodes[_labels[v]]->push_back(v);
669 _flow[reverse[num_c]]-=delta;
671 m=
MIN(m,_labels[v]+1);
680 _all_nodes[_labels[u]]--;
681 if (_all_nodes[_labels[u]]==0) {
682 this->gap_relabelling(component,_labels[u],max_label);
683 _labels[u]=max_label;
686 cerr <<
"relabel " << u <<
" from " << _labels[u] <<
" to " <<
MIN(m,max_label) << endl;
688 _labels[u]=
MIN(m,max_label);
689 _all_nodes[_labels[u]]++;
693 cerr <<
"relabel " << u <<
" from " << _labels[u] <<
" to " <<
MIN(m,max_label) << endl;
695 _labels[u]=
MIN(m,max_label);
699 _current_edges[u]=((pr+curr) % nn);
703 template <
typename T>
706 while (_current_max_label > 0 || !_active_nodes[0]->empty()) {
707 if (counter % 2*_N == 0) this->global_relabelling();
708 if (_active_nodes[_current_max_label]->empty()) {
709 _current_max_label--;
711 const int current_node=_active_nodes[_current_max_label]->front();
712 _active_nodes[_current_max_label]->pop_front();
713 _active[current_node]=
false;
715 this->discharge(NULL,current_node,_N);
716 if (_excess[current_node] >
EPSILON_MAXFLOW && _labels[current_node] < _N) {
717 _active_nodes[_labels[current_node]]->push_back(current_node);
718 _active[current_node]=
true;
719 if (_labels[current_node] > _current_max_label)
720 _current_max_label=_labels[current_node];
723 _excess[current_node]=0;
730 template <
typename T>
732 cerr <<
"Excess: " << endl;
733 for (
int i= 0; i<_N; ++i) {
734 cerr << _excess[i] <<
" ";
741 template <
typename T>
743 for (
int i= 0; i<_N; ++i) {
750 template <
typename T>
753 it != component.
end(); ++it) {
761 template <
typename T>
763 cerr <<
"Labels: " << endl;
764 for (
int i= 0; i<_N; ++i)
765 cerr << _labels[i] <<
" ";
770 template <
typename T>
772 cerr <<
"Number of nodes: " << _N << endl;
773 cerr <<
"Source: " << _s << endl;
774 cerr <<
"Sink: " << _t << endl;
775 for (
int i = 0; i<_N; ++i) _seen[i]=
false;
776 this->print_graph_aux(_s);
779 template <
typename T>
781 for (
int i = 0; i<_N; ++i) _seen[i]=
false;
785 for (
int i = 0; i<Ng; ++i) tab_list[i] =
new list_int();
786 this->init_split_variables_aux(_s,current,count,tab_list,Ng,Nv);
788 for (
int i = 0; i<Ng; ++i) {
789 nzmax += tab_list[i]->
size();
792 splitted_w.
resize(Nv,Ng,nzmax);
793 int* pB = splitted_w.
pB();
794 int* r = splitted_w.
r();
795 T* v = splitted_w.
v();
798 for (
int i = 0; i<Ng; ++i) {
799 pB[i+1]=pB[i]+tab_list[i]->
size();
805 for (
int i = 0; i<Ng; ++i)
delete(tab_list[i]);
809 template <
typename T>
811 for (
int i = 0; i<_nzmax; ++i) _copycapacity[i]=_capacity[i];
813 template <
typename T>
815 _copyflow=
new T[_nzmax];
816 for (
int i = 0; i<_nzmax; ++i) _copyflow[i]=_flow[i];
817 _copyexcess=
new T[_N];
818 for (
int i = 0; i<_N; ++i) _copyexcess[i]=_excess[i];
820 template <
typename T>
822 for (
int i = 0; i<_nzmax; ++i) _flow[i]=_copyflow[i];
824 for (
int i = 0; i<_N; ++i) _excess[i]=_copyexcess[i];
825 delete[](_copyexcess);
829 template <
typename T>
831 for (
int i = 0; i<_nzmax; ++i) _capacity[i]=_copycapacity[i];
834 template <
typename T>
837 for (
int i = 0; i<_N; ++i) _seen[i]=
false;
844 while (!tmp.
empty()) {
845 const int node = tmp.
front();
846 const int ind = _pr_node[node];
848 if (_excess[node] < 0) {
849 cerr <<
"negative excess: " <<_excess[node] <<
" on node " << node << endl;
853 for (
int i = 0; i<_num_edges[node]; ++i) {
854 totalflow+=_flow[ind+i];
855 if (_flow[ind+i] > _capacity[ind+i]) {
857 cerr <<
"exceed capacity on node " << node <<
" to " << _children[ind+i] <<
". Flow: " << _flow[ind+i] <<
", capacity: " << _capacity[ind+i] << endl;
858 total_excess += _flow[ind+i]-_capacity[ind+i];
860 if (!_seen[_children[ind+i]]) {
862 _seen[_children[ind+i]]=
true;
866 cerr <<
"prb on node " << node <<
", excess: " << _excess[node] <<
", totalflow: " << totalflow << endl;
868 if (node != _s && node != _t)
869 total_excess2+=
abs(totalflow+_excess[node]);
874 template <
typename T>
876 memset(_excess,0,_N*
sizeof(T));
877 memset(_flow,0,_nzmax*
sizeof(T));
881 template <
typename T>
883 for (
int i = 0; i<_N; ++i) _excess[i]*=scal;
884 for (
int i = 0; i<_nzmax; ++i) _flow[i]*=scal;
888 template <
typename T>
890 for (
int j = 0; j<_num_edges[_s]; ++j) {
891 _capacity[_pr_node[_s]+j]=lambda;
895 template <
typename T>
898 for (
int j = 0; j<_num_edges[_s]; ++j) {
899 _capacity[_pr_node[_s]+j]=lambda*weights[j];
903 template <
typename T>
906 for (
int j = 0; j<_num_edges[_t]; ++j) {
907 cerr << _flow[_reverse_address[_pr_node[_t]+j]] <<
" ";
910 cerr <<
"Capacity: ";
911 for (
int j = 0; j<_num_edges[_t]; ++j) {
912 cerr << _capacity[_reverse_address[_pr_node[_t]+j]] <<
" ";
917 template <
typename T>
920 it != component.
end(); ++it) {
921 cerr <<
"Node: " << *it << endl;
922 cerr <<
"Children: ";
923 for (
int j = 0; j<_num_edges[*it]; ++j) {
924 cerr << _children[_pr_node[*it]+j] <<
" ";
928 for (
int j = 0; j<_num_edges[*it]; ++j) {
929 cerr << _flow[_pr_node[*it]+j] <<
" ";
932 cerr <<
"Capacity: ";
933 for (
int j = 0; j<_num_edges[*it]; ++j) {
934 cerr << _capacity[_pr_node[*it]+j] <<
" ";
940 template <
typename T>
942 const int node,
list_int& variables,
const int Ng) {
944 const int ind = _pr_node[node];
945 const int* children = _children+ind;
946 const T* capacity = _capacity+ind;
947 for (
int i = 0; i<_num_edges[node]; ++i) {
948 const int child=children[i];
949 if (child != _s && child != _t) {
951 if (capacity[i] > 0 && !_seen[child]) {
953 this->sub_gradient_aux(input,output,weights,child,new_var,Ng);
954 variables.
fusion(new_var);
963 if (abs<T>(input[*it-Ng]) > max_abs) max_abs=abs<T>(input[*it-Ng]);
969 if (abs<T>(abs<T>(input[*it-Ng])-max_abs) < 1e-15)
972 T scal = weights[node]/var_max.
size();
974 output[*it] += input[*it] > 0 ? scal : -scal;
978 template <
typename T>
982 for (
int i = 0; i<_N; ++i) {
986 while (!tmp.
empty()) {
987 const int node=tmp.
front();
990 this->sub_gradient_aux(input,output,weights,node,variables,Ng);
996 template <
typename T>
997 T
inline MaxFlow<T>::norm(
const T* variables, T* work,
const T* weights,
const int Ng,
const bool linf) {
999 for (
int i = 0; i<_N; ++i) {
1005 while (!tmp.
empty()) {
1006 const int node = tmp.
front();
1010 if (node >= Ng && node != _s && node != _t) {
1011 work[node]= linf ?
abs(variables[node-Ng]) : variables[node-Ng]*variables[node-Ng];
1015 const int ind = _pr_node[node];
1016 const int* children = _children+ind;
1017 const T* capacity = _capacity+ind;
1018 bool all_child=
true;
1019 for (
int i = 0; i<_num_edges[node]; ++i) {
1020 const int child=children[i];
1021 if (child != _s && child != _t && capacity[i] > 0 && !_seen[child]) {
1028 for (
int i = 0; i<_num_edges[node]; ++i) {
1029 const int child=children[i];
1030 if (child != _s && child != _t && capacity[i] > 0) {
1031 max_var = linf ?
MAX(max_var,work[child]) : max_var + work[child];
1042 for (
int i = 0; i<Ng; ++i) {
1043 sum+=weights[i]*work[i];
1046 for (
int i = 0; i<Ng; ++i) {
1047 sum+=weights[i]*sqrt(work[i]);
1053 template <
typename T>
1055 cerr <<
"*********Print component***********" << endl;
1057 it != component.
end(); ++it) {
1061 cerr <<
"Excess" << endl;
1063 it != component.
end(); ++it) {
1064 cerr << _excess[*it] <<
" ";
1066 cerr <<
" " << _excess[_s] <<
" " << _excess[_t];
1068 cerr <<
"Labels" << endl;
1070 it != component.
end(); ++it) {
1071 cerr << _labels[*it] <<
" ";
1073 cerr <<
" " << _labels[_s] <<
" " << _labels[_t];
1080 template <
typename T>
1082 if (_seen[i])
return;
1084 cerr <<
"Node: " << i << endl;
1086 if (i == _t)
return;
1087 cerr <<
"Children: ";
1088 for (
int j = 0; j<_num_edges[i]; ++j) {
1089 cerr << _children[_pr_node[i]+j] <<
" ";
1092 cerr <<
"Capacity: ";
1093 for (
int j = 0; j<_num_edges[i]; ++j) {
1094 cerr << _capacity[_pr_node[i]+j] <<
" ";
1098 for (
int j = 0; j<_num_edges[i]; ++j) {
1099 cerr << _flow[_pr_node[i]+j] <<
" ";
1102 cerr <<
"Rverse Flow: ";
1103 for (
int j = 0; j<_num_edges[i]; ++j) {
1104 cerr << _flow[_reverse_address[_pr_node[i]+j]] <<
" ";
1108 for (
int j = 0; j<_num_edges[i]; ++j) {
1109 this->print_graph_aux(_children[_pr_node[i]+j]);
1113 template <
typename T>
1116 if (_seen[i] || (i >= Ng && i != _s))
return;
1118 const int ind = _pr_node[i];
1119 const int* children = _children+ind;
1120 const T* capacity = _capacity+ind;
1122 for (
int j = 0; j<_num_edges[i]; ++j) {
1123 if (capacity[j] > 0) {
1124 this->init_split_variables_aux(children[j],current,count,splitted_w,Ng,Nv);
1131 for (
int j = 0; j<_num_edges[i]; ++j) {
1132 const int child=children[j];
1133 if (child != _s && child != _t && capacity[j] > 0) {
1138 it != splitted_w[count[child]]->
end(); ++it)
1143 for (
int j = 0; j<tmp.
n(); ++j) {
1144 if (tmp[j]) splitted_w[current]->
push_back(j);
1151 template <
typename T>
1155 for (
int i = 0; i<_N; ++i) _labels[i]=_N;
1156 for (
int i = 0; i<_N; ++i) _seen[i]=
false;
1160 while (!nodes.
empty()) {
1161 const int node=nodes.
front();
1162 const int* children=_children+_pr_node[node];
1163 const int* reverse=_reverse_address+_pr_node[node];
1164 for (
int i = 0; i<_num_edges[node]; ++i) {
1165 const int child=children[i];
1166 if (!_seen[child] && _capacity[reverse[i]] > _flow[reverse[i]]) {
1168 const int new_label=_labels[node]+1;
1169 if (new_label != _labels[child] && _excess[child] >
EPSILON_MAXFLOW) {
1170 _active_nodes[new_label]->push_back(child);
1171 _active[child]=
true;
1172 if (new_label > _current_max_label)
1173 _current_max_label=new_label;
1175 _labels[child]=new_label;
1180 _active[node]=
false;
1184 template <
typename T>
1186 std::list< list_int* >& connex_components) {
1188 for (
int i = 0; i<_N; ++i) _seen[i]=
false;
1192 for (
int i = 0; i<_N; ++i) {
1197 while (!tmp.
empty()) {
1198 int node=tmp.
front();
1202 const int* children=_children+_pr_node[node];
1203 for (
int i = 0; i<_num_edges[node]; ++i) {
1204 const int child=children[i];
1205 if (!_seen[child]) {
1211 connex_components.push_back(component);
1216 template <
typename T>
1218 const T* variables_in,T* variables_out,T* work,
const T* weights,
1224 it != component.
end(); ++it) {
1226 lambda+=_capacity[_reverse_address[_pr_node[*it]]];
1228 ww[num_var]=T(1.0)/weights[*it-Ng];
1229 work[num_var++]=variables_in[*it-Ng];
1239 it != component.
end(); ++it) {
1240 const int ind = _pr_node[*it];
1242 const int nv=*it-Ng;
1243 variables_out[nv]=out[count];
1244 const T diff = (variables_in[nv]-variables_out[nv])*ww[count++];
1246 _capacity[ind]=diff;
1247 if (_flow[ind] > diff) {
1248 _excess[*it]+=_flow[ind]-diff;
1250 _flow[_reverse_address[ind]]=-diff;
1259 template <
typename T>
1261 const T* variables_in,T* variables_out, T* work,
1270 it != component.
end(); ++it) {
1272 lambda+=_capacity[_reverse_address[_pr_node[*it]]];
1274 work[num_var++]=variables_in[*it-Ng];
1279 const T thrs=this->compute_thrs_project_l1(work,num_var,lambda);
1281 it != component.
end(); ++it) {
1282 const int ind = _pr_node[*it];
1284 const int nv=*it-Ng;
1285 variables_out[nv]=
MIN(variables_in[nv],thrs);
1286 const T diff = variables_in[nv]-variables_out[nv];
1288 _capacity[ind]=diff;
1289 if (_flow[ind] > diff) {
1290 _excess[*it]+=_flow[ind]-diff;
1292 _flow[_reverse_address[ind]]=-diff;
1300 template <
typename T>
1302 const T* variables_in,T* variables_out,T* work,
bool& fusion,
1311 it !=component.
end(); ++it) {
1312 const int node = *it;
1313 const int ind = _pr_node[node];
1317 work[node]=_capacity[_reverse_address[ind]];
1328 fusion = num_nodes > 1;
1329 while (!nodes.
empty()) {
1330 const int node = nodes.
front();
1332 const int ind = _pr_node[node];
1333 const int* childrens=_children+ind;
1334 const int* reverse=_reverse_address+ind;
1335 for (
int& i = _all_nodes[node]; i<_num_edges[node]; ++i) {
1336 const int child = childrens[i];
1337 if (_active[child] && !_seen[child]
1338 && _capacity[reverse[i]] > 0) {
1343 if (_all_nodes[node]==_num_edges[node]) {
1345 for (
int i = 1; i<_num_edges[node]; ++i) {
1346 const int child = childrens[i];
1347 if (_active[child] && _capacity[ind+i] > 0) {
1348 work[child]+=work[node];
1362 it !=variables.
end(); ++it) {
1363 if (variables_in[*it-Ng] > 0) {
1364 var.push_back(variables_in[*it-Ng]);
1365 T diff = variables_in[*it-Ng]-work[*it];
1367 var.push_back(-diff);
1370 var.sort(compare_abs<T>);
1375 for (
typename std::list<T>::const_iterator it=var.begin();
1376 it != var.end(); ++it) {
1379 num+= pivot > 0 ? 1 : -1;
1380 if (sum-abs<T>(*it)*num > lambda) {
1382 num-= (*it > 0 ? 1 : -1);
1384 thrs= num==0 ? pivot : (sum-lambda)/num;
1388 if (!br) thrs=
MAX(0,num==0 ? pivot : (sum-lambda)/num);
1391 it !=variables.
end(); ++it) {
1392 variables_out[*it-Ng] =
MIN(
MAX(thrs,variables_in[*it-Ng]-work[*it]),
1393 variables_in[*it-Ng]);
1397 it != component.
end(); ++it) {
1398 const int ind = _pr_node[*it];
1400 const int nv=*it-Ng;
1401 const T diff = variables_in[nv]-variables_out[nv];
1403 _capacity[ind]=diff;
1404 if (_flow[ind] > diff) {
1405 _excess[*it]+=_flow[ind]-diff;
1407 _flow[_reverse_address[ind]]=-diff;
1416 template <
typename T>
1421 it != component.
end(); ++it) {
1423 max_flow+=_flow[_pr_node[*it]];
1436 template <
typename T>
1438 std::list< list_int* >& connex_components,
const int Ng,
bool* positive,
const bool addpos) {
1441 it != component.
end(); ++it) {
1443 positive[*it]=
false;
1453 it != component.
end(); ++it) {
1458 while (!tmp.
empty()) {
1459 int node=tmp.
front();
1461 const int ind=_pr_node[node];
1462 const int* children=_children+ind;
1463 const T* flow=_flow+ind;
1464 const T* capacity=_capacity+ind;
1465 for (
int i = 0; i<_num_edges[node]; ++i) {
1466 const int child=children[i];
1467 if (!_seen[child] && !positive[child] && (flow[i] < capacity[i]-
EPSILON_MAXFLOW)) {
1468 positive[child]=
true;
1495 it != component.
end(); ++it) {
1496 if (positive[*it] && !_seen[*it]) {
1501 while (!tmp.
empty()) {
1502 int node=tmp.
front();
1505 const int ind=_pr_node[node];
1506 const int* children=_children+ind;
1507 for (
int i = 0; i<_num_edges[node]; ++i) {
1508 const int child=children[i];
1509 if (!positive[child] && child != _t) {
1510 _capacity[ind+i]=_capacity[ind+i] > 0 ? -0.5 : 0;
1512 if (positive[child] && !_seen[child]) {
1519 connex_components.push_back(new_component);
1521 delete(new_component);
1528 it != component.
end(); ++it) {
1529 if (!positive[*it] && !_seen[*it]) {
1534 while (!tmp.
empty()) {
1535 int node=tmp.
front();
1538 const int ind=_pr_node[node];
1539 const int* children=_children+ind;
1540 for (
int i = 0; i<_num_edges[node]; ++i) {
1541 const int child=children[i];
1542 if (positive[child] && child != _t) {
1544 _capacity[ind+i]=_capacity[ind+i] > 0 ? -0.5 : 0;
1546 if (!positive[child] && !_seen[child]) {
1552 connex_components.push_back(new_component);
1556 if (num_comp == 1 && connex_components.size() != 0) {
1557 list_int* comp = connex_components.back();
1559 connex_components.pop_back();
1561 return num_comp > 1;
1565 template <
typename T>
1568 it != component.
end(); ++it) {
1569 const int ind = _pr_node[*it];
1571 for (
int i = 0; i<_num_edges[*it]; ++i) {
1573 _flow[_reverse_address[i+ind]]=0;
1578 template <
typename T>
1581 int size_component=component.
size();
1582 const int max_label=size_component+2;
1584 this->component_relabelling(component,max_label,
true);
1587 this->print_component2(component);
1588 this->print_component(component);
1595 while (_current_max_label > 0 || !_active_nodes[0]->empty()) {
1597 this->print_component2(component);
1600 if (global_heuristic && (counter % (size_component+1)) == 0) {
1601 this->component_relabelling(component,max_label,
false);
1604 if (_active_nodes[_current_max_label]->empty()) {
1605 _current_max_label--;
1607 cerr <<
"current max label decreased to " << _current_max_label << endl;
1610 const int current_node=_active_nodes[_current_max_label]->front();
1611 _active_nodes[_current_max_label]->pop_front();
1612 _active[current_node]=
false;
1614 this->discharge(component,current_node,max_label);
1615 if (_excess[current_node] >
EPSILON_MAXFLOW && _labels[current_node] < max_label) {
1616 _active_nodes[_labels[current_node]]->push_back(current_node);
1617 _active[current_node]=
true;
1618 if (_labels[current_node] > _current_max_label) {
1619 _current_max_label=_labels[current_node];
1623 _excess[current_node]=0;
1630 cerr <<
"END max flow" << endl;
1631 this->print_excess();
1637 template <
typename T>
1640 cerr <<
"Gap relabelling " << gap << endl;
1646 if (_labels[*it] > gap) {
1647 _labels[*it]=max_label;
1650 for (
int i = gap; i<max_label; ++i) {
1656 template <
typename T>
1658 const int max_label,
const bool force) {
1661 for (
int i = 0; i<=component.
size(); ++i)
1662 _active_nodes[i]->clear();
1664 for (
int i = 0; i<=component.
size(); ++i)
1666 _current_max_label=0;
1667 num_global_relabels++;
1672 _labels[_s]=max_label;
1681 it != component.
end(); ++it) {
1682 const int ind = _pr_node[*it];
1683 const int first_child=_children[ind];
1684 if (first_child==_t && _flow[ind] < _capacity[ind]) {
1688 _active_nodes[1]->push_back(*it);
1689 _current_max_label=1;
1699 if (first_child == _s && force) {
1700 const T delta = _capacity[_reverse_address[ind]] - _flow[_reverse_address[ind]];
1702 _excess[*it] += delta;
1703 _flow[_reverse_address[ind]]=_capacity[_reverse_address[ind]];
1708 _labels[*it]=max_label;
1711 while (!nodes.
empty()) {
1712 const int node=nodes.
front();
1713 const int* children=_children+_pr_node[node];
1714 const int* reverse=_reverse_address+_pr_node[node];
1715 for (
int i = 0; i<_num_edges[node]; ++i) {
1716 const int child=children[i];
1717 if (!_seen[child] && _capacity[reverse[i]] > _flow[reverse[i]]) {
1719 const int new_label=_labels[node]+1;
1720 if (new_label != _labels[child] && _excess[child] >
EPSILON_MAXFLOW) {
1721 _active_nodes[new_label]->push_back(child);
1722 _active[child]=
true;
1723 if (new_label > _current_max_label)
1724 _current_max_label=new_label;
1726 _labels[child]=new_label;
1728 _all_nodes[new_label]++;
1741 template <
typename T>
1745 it != component.
end(); ++it) {
1746 const int ind = _pr_node[*it];
1747 const int first_child=_children[ind];
1750 if (first_child == _t) {
1752 work[*it]=_capacity[ind];
1759 while (!comp_nodes.
empty()) {
1760 const int new_node=comp_nodes.
front();
1762 if (!_seen[new_node]) {
1764 while (!nodes.
empty()) {
1765 const int node = nodes.
front();
1767 const int ind = _pr_node[node];
1768 const int* children=_children+ind;
1769 for ( ; _all_nodes[node] < _num_edges[node]; ++_all_nodes[node]) {
1770 const int child=children[_all_nodes[node]];
1771 if (_active[child] && !_seen[child] &&_capacity[ind+_all_nodes[node]] > 0) {
1776 if (_all_nodes[node]==_num_edges[node]) {
1778 for (
int i = 0; i < _num_edges[node]; ++i) {
1779 const int child=children[i];
1780 if (_active[child] && _capacity[ind+i] > 0) {
1781 if (work[child] > 0) {
1782 work[node]+=work[child];
1783 _capacity[ind+i] =
MAX(_flow[ind+i],work[child]);
1785 _capacity[ind+i]=-2;
1795 template <
typename T>
1798 for (
int i = 0; i<Nv; ++i) {
1799 const int ind = _pr_node[Ng+i];
1800 _capacity[ind]=
abs(cap[i]);
1804 template <
typename T>
1806 const Vector<T>& weights,
const T lambda,
const int Ng) {
1810 _capacity[_reverse_address[_pr_node[*it]]]=lambda*weights[*it];
1816 template <
typename T>
1823 it != component.
end(); ++it) {
1827 it != component.
end(); ++it) {
1828 const int* children=_children+_pr_node[*it];
1829 T* capacity=_capacity+_pr_node[*it];
1830 for (
int i = 0; i<_num_edges[*it]; ++i) {
1831 const int child=children[i];
1832 if (!_seen[child] && (capacity[i] > 0 || capacity[i] < -1))
1841 template <
typename T>
1847 for (
int i = 0; i<sum_card; ++i) {
1851 swap(X[i],X[--sum_card]);
1856 memset(X,0,sum_card*
sizeof(T));
1859 int sizeU = sum_card;
1865 swap(prU[0],prU[sizeU/2]);
1869 for (
int i = 1; i<sizeU; ++i) {
1870 if (prU[i] >= prU[0]) {
1872 swap(prU[sizeG++],prU[i]);
1877 int new_card=sum_card+sizeG;
1878 if (new_sum - prU[0]*new_card <= lambda) {
1879 sum_card = new_card;
1888 return MAX(0,(sum-lambda)/sum_card);
1896 void inline create_graph(
const int Nv,
const int Ng,
1898 void inline create_graph(
const int Nv,
const int Ng,
1901 void inline proximal_operator(
const T* variables_in, T* variables_out,
const bool clever =
false,
const T* weights = NULL);
1907 void inline scale_flow(
const T scal) { _maxflow->scale_flow(scal); };
1909 _maxflow->set_weights(lambda); };
1912 _maxflow->set_weights(weights,lambda); };
1913 void inline print() { _maxflow->print_graph(); };
1914 T
inline norm(
const T* variables, T* work,
const T* weights,
const bool linf=
true) {
return _maxflow->norm(variables,work,weights,_Ng,linf); };
1917 _maxflow->sub_gradient(input,output,weights,_Ng);
1920 _maxflow->init_split_variables(splitted_w,_Ng,_Nv);
1931 template <
typename T>
1939 template <
typename T>
1945 template <
typename T>
1950 _weights=
new T[_Ng];
1951 for (
int i = 0; i<_Ng; ++i) _weights[i]=weights[i];
1952 const int N = _Ng+_Nv+2;
1953 int* num_edges=
new int[N];
1954 for (
int i = 0; i<N; ++i) num_edges[i]=1;
1955 for (
int i = 0; i<Ng; ++i) {
1956 for (
int j = var_jc[i]; j<var_jc[i+1]; ++j) {
1958 num_edges[Ng+var_ir[j]]++;
1961 const int s=_Ng+_Nv;
1962 const int t=_Ng+_Nv+1;
1967 for (
int i = 0; i<_Ng; ++i)
1968 _maxflow->
add_edge(s,i,_weights[i],0);
1969 for (
int i = 0; i<_Nv; ++i)
1970 _maxflow->add_edge(_Ng+i,t,
INFINITY,0);
1971 for (
int i = 0; i<_Ng; ++i) {
1972 for (
int j = var_jc[i]; j<var_jc[i+1]; ++j) {
1973 _maxflow->add_edge(i,_Ng+static_cast<int>(var_ir[j]),_weights[i],0);
1976 _maxflow->save_capacities();
1977 delete[](num_edges);
1980 template <
typename T>
1985 _weights=
new T[_Ng];
1986 for (
int i = 0; i<_Ng; ++i) _weights[i]=weights[i];
1987 const int N = _Ng+_Nv+2;
1988 int* num_edges=
new int[N];
1989 for (
int i = 0; i<N; ++i) num_edges[i]=1;
1990 for (
int i = 0; i<Ng; ++i) {
1991 for (
int j = gv_jc[i]; j<static_cast<int>(gv_jc[i+1]); ++j) {
1993 num_edges[Ng+gv_ir[j]]++;
1996 for (
int i = 0; i<Ng; ++i) {
1997 for (
int j = gg_jc[i]; j<static_cast<int>(gg_jc[i+1]); ++j) {
1998 if (i != static_cast<int>(gg_ir[j])) {
2000 num_edges[gg_ir[j]]++;
2005 const int s=_Ng+_Nv;
2006 const int t=_Ng+_Nv+1;
2012 for (
int i = 0; i<_Ng; ++i)
2013 _maxflow->
add_edge(s,i,_weights[i],0);
2014 for (
int i = 0; i<_Nv; ++i)
2015 _maxflow->add_edge(_Ng+i,t,0,0);
2016 for (
int i = 0; i<_Ng; ++i) {
2017 for (
int j = gv_jc[i]; j<static_cast<int>(gv_jc[i+1]); ++j) {
2018 _maxflow->add_edge(i,_Ng+static_cast<int>(gv_ir[j]),
INFINITY,0);
2021 for (
int i = 0; i<_Ng; ++i) {
2022 for (
int j = gg_jc[i]; j<static_cast<int>(gg_jc[i+1]); ++j) {
2023 if (i != static_cast<int>(gg_ir[j])) {
2024 _maxflow->add_edge(i,static_cast<int>(gg_ir[j]),
INFINITY,0);
2028 _maxflow->save_capacities();
2029 delete[](num_edges);
2032 template <
typename T>
2038 T* work =
new T[_Nv+_Ng+2];
2039 bool* positive =
new bool[_Ng+_Nv+2];
2040 _maxflow->set_capacities_variables(input.
rawX(),_Nv,_Ng);
2041 std::list< list_int* > connex_components;
2042 _maxflow->extractConnexComponents(connex_components);
2043 _maxflow->deactivate();
2051 while (!connex_components.empty()) {
2053 list_int* component=connex_components.front();
2054 connex_components.pop_front();
2055 if (component->
size() != 1) {
2061 it != component->
end(); ++it) {
2063 sum_weights+=weights[*it];
2066 sum_variables+=abs<T>(input[*it-_Ng]);
2069 tau =
MAX(tau,sum_variables/sum_weights);
2070 _maxflow->set_capacities_groups(*component,weights,tau,_Ng);
2072 _maxflow->update_capacities(*component,work);
2074 num_global_relabels=0;
2076 _maxflow->perform_maxflow_component(*component);
2079 num3+=component->
size();
2081 T flow=_maxflow->flow_component(*component,_Ng);
2082 _maxflow->restore_capacities(*component);
2084 _maxflow->splitComponent(*component,connex_components,_Ng, positive,
false);
2086 _maxflow->deactivate(*component);
2104 template <
typename T>
2117 cap_heuristic =
true;
2118 global_heuristic =
true;
2119 gap_heuristic =
true;
2120 std::list< list_int* > connex_components;
2121 _maxflow->extractConnexComponents(connex_components);
2122 T* work =
new T[_Nv+_Ng+2];
2123 T* variables_bis =
new T[_Nv];
2124 for (
int i = 0; i<_Nv; ++i) variables_bis[i]=abs<T>(variables_in[i]);
2125 for (
int i = 0; i<_Nv; ++i) variables_out[i]=variables_bis[i];
2131 bool* positive =
new bool[_Ng+_Nv+2];
2132 _maxflow->deactivate();
2140 Timer tsplit, tproj, tcap;
2142 while (!connex_components.empty()) {
2143 list_int* component=connex_components.front();
2144 connex_components.pop_front();
2145 if (component->
size() != 1) {
2151 budget=_maxflow->project_weighted(*component,variables_bis,variables_out,work,weights,_Ng);
2154 budget=_maxflow->project_box(*component,variables_bis,variables_out,work,fusion,_Ng);
2156 budget=_maxflow->project(*component,variables_bis,variables_out,work,_Ng);
2165 _maxflow->update_capacities(*component,work);
2168 num_global_relabels=0;
2170 _maxflow->perform_maxflow_component(*component);
2173 num3+=component->
size();
2175 _maxflow->restore_capacities(*component);
2176 T flow=_maxflow->flow_component(*component,_Ng);
2180 if (!_maxflow->splitComponent(*component,connex_components,_Ng, positive,
true)) {
2181 flow_missed+=abs<T>(budget-flow);
2187 if (component->
size() > 100000) {
2189 cerr <<
"num_comp: " << num << endl;
2190 cerr <<
"size_component: " << num3 << endl;
2191 cerr <<
"num_relabels: " << num1 << endl;
2192 cerr <<
"global: " << num2 << endl;
2193 cerr <<
"gap:: " << num4 << endl;
2202 _maxflow->deactivate(*component);
2207 cerr <<
"num_comp: " << num << endl;
2208 cerr <<
"size_component: " << num3 << endl;
2209 cerr <<
"num_relabels: " << num1 << endl;
2210 cerr <<
"global: " << num2 << endl;
2211 cerr <<
"gap:: " << num4 << endl;
2212 cerr <<
"Time global" << endl;
2226 for (
int i = 0; i<_Nv; ++i) variables_out[i] = variables_in[i] >= 0 ?
MAX(variables_out[i],0) : -
MAX(variables_out[i],0);
2229 delete[](variables_bis);
2256 start_weights=NULL; stop_weights=NULL;
2271 template <
typename Int=
long long>
2281 MinCostFlow(
const int n,
const int* max_num_arcs);
2284 void inline add_edge(
const int u,
const int v,
const Int cost,
const double double_cost,
const Int cap);
2285 void inline set_demand(
const int node,
const Int dem) { _demand[node]=dem; };
2286 void inline set_edge(
const int node,
const int num_arc,
const Int cost,
const Int cap);
2287 void inline set_capacity(
const int node,
const int num_arc,
const Int cap);
2288 void inline add_flow(
const int node,
const int num_arc,
const Int flow);
2289 void inline set_quad_cost(
const int node,
const int num_arc,
const bool quad_cost) {
2290 _quad_cost[_pr_node[node]+num_arc]=quad_cost;
2291 _quad_cost[_reverse[_pr_node[node]+num_arc]]=quad_cost;
2294 void inline discharge(
const int node,
const Int eps);
2296 void restore_costs();
2297 void scale_costs(
const double scal);
2298 Int compute_cost()
const;
2299 double compute_cost_double()
const;
2300 Int compute_uncap_cost()
const;
2301 Int
inline get_flow(
const int node,
const int num_arc)
const {
return _flow[_pr_node[node]+num_arc]; };
2303 void compute_min_cost(
const bool scale_data =
true,
const bool verbose=
false);
2304 Int refine(Int eps,
const bool price_refine =
false);
2305 void price_update(
const Int eps);
2306 bool price_refine(
const Int eps);
2308 bool test_optimality_conditions()
const;
2310 void inline print_excess()
const;
2311 void inline print_prices()
const;
2312 void inline print_graph()
const;
2313 bool inline topological_sort(
const bool admissible =
false,
bool* admiss = NULL, Int* rcosts = NULL);
2314 Int
inline reduced_cost(
const int node,
const int child,
const int arc)
const;
2316 Int cost_shortest_path_in_dag(
list_int& path);
2317 void st_flow_decomposition_dag(
List<
Path<Int>*>& list,
const int s,
const int t)
const;
2318 void print_dimacs(
const char* name)
const;
2354 template <
typename Int>
2360 _is_quadratic_cost=
false;
2363 memset(_prices,0,n*
sizeof(Int));
2365 memset(_excess,0,n*
sizeof(Int));
2367 memset(_demand,0,n*
sizeof(Int));
2368 _active=
new bool[n];
2369 memset(_active,
false,n*
sizeof(
bool));
2371 _topological_order=
new int[n];
2372 memset(_topological_order,0,n*
sizeof(
int));
2373 _topologically_sorted=
false;
2374 _num_arcs=
new int[n];
2375 memset(_num_arcs,0,n*
sizeof(
int));
2376 _max_num_arcs=
new int[n];
2377 memcpy(_max_num_arcs,max_num_arcs,n*
sizeof(
int));
2378 _pr_node=
new int[n];
2380 for (
int i = 0; i<n; ++i) {
2382 _maxm+=_max_num_arcs[i];
2384 _children=
new int[_maxm];
2385 memset(_children,-1,_maxm*
sizeof(
int));
2386 _reverse=
new int[_maxm];
2387 memset(_reverse,-1,_maxm*
sizeof(
int));
2388 _flow=
new Int[_maxm];
2389 memset(_flow,0,_maxm*
sizeof(Int));
2390 _capacity=
new Int[_maxm];
2391 memset(_capacity,0,_maxm*
sizeof(Int));
2392 _cost=
new Int[_maxm];
2393 memset(_cost,0,_maxm*
sizeof(Int));
2394 _save_cost=
new Int[_maxm];
2395 memset(_save_cost,0,_maxm*
sizeof(Int));
2396 _init_double_cost=
new double[_maxm];
2397 memset(_init_double_cost,0,_maxm*
sizeof(
double));
2398 _quad_cost=
new bool[_maxm];
2399 memset(_quad_cost,
false,_maxm*
sizeof(
bool));
2402 template <
typename Int>
2407 delete[](_topological_order);
2408 delete[](_num_arcs);
2409 delete[](_max_num_arcs);
2411 delete[](_children);
2414 delete[](_capacity);
2416 delete[](_save_cost);
2417 delete[](_init_double_cost);
2419 delete[](_quad_cost);
2422 template <
typename Int>
2424 const int pu=_pr_node[u];
2425 const int pv=_pr_node[v];
2426 const int nu=_num_arcs[u]+pu;
2427 const int nv=_num_arcs[v]+pv;
2434 _init_double_cost[nu]=double_cost;
2435 _init_double_cost[nv]=-double_cost;
2442 template <
typename Int>
2444 const Int cost,
const Int cap) {
2445 const int pu=_pr_node[node];
2446 const int nu=pu+num_arc;
2449 _cost[_reverse[nu]]=-cost;
2450 _capacity[_reverse[nu]]=0;
2453 template <
typename Int>
2456 const int pu=_pr_node[node];
2457 const int nu=pu+num_arc;
2459 _capacity[_reverse[nu]]=0;
2462 template <
typename Int>
2464 const int nu=_pr_node[node]+num_arc;
2466 _flow[_reverse[nu]]-=flow;
2469 template <
typename Int>
2471 memcpy(_save_cost,_cost,_maxm*
sizeof(Int));
2474 template <
typename Int>
2476 memcpy(_cost,_save_cost,_maxm*
sizeof(Int));
2479 template <
typename Int>
2482 for (
int i = 0;i<_maxm; ++i) {
2483 _cost[i]=
static_cast<Int
>(ceil(scal*_init_double_cost[i]));
2487 template <
typename Int>
2503 for (
int i = 0; i<_maxm; ++i) _cost[i] *= _n;
2504 for (
int i = 0; i<_maxm; ++i) _capacity[i] *= _n;
2505 for (
int i = 0; i<_n; ++i) _demand[i] *= _n;
2508 for (
int i=0; i< _maxm; ++i) if (_cost[i] > eps) eps=_cost[i];
2509 memset(_prices,0,_n*
sizeof(Int));
2510 memset(_flow,0,_maxm*
sizeof(Int));
2511 memset(_active,
false,_n*
sizeof(
bool));
2512 for (
int i=0; i<_n; ++i) _excess[i]=-_demand[i];
2516 bool price_refine=
false;
2519 eps=this->refine(eps,price_refine);
2523 for (
int i = 0; i<_maxm; ++i) _cost[i] /= _n;
2524 for (
int i = 0; i<_maxm; ++i) _capacity[i] /= _n;
2525 for (
int i = 0; i<_n; ++i) _demand[i] /= _n;
2526 for (
int i = 0; i<_maxm; ++i) _flow[i] /= _n;
2527 for (
int i = 0; i<_n; ++i) _prices[i] /= _n;
2532 cerr <<
"Num pushes: " << num_pushes <<
", num relabels: " << num_relabels << endl;
2534 cerr <<
"Time for price updates" << endl;
2536 cerr <<
"Time for price refines" << endl;
2541 template <
typename Int>
2545 eps=
static_cast<Int
>(ceil(static_cast<double>(eps)/_alpha));
2546 if (price_refine_heuristic && price_refine && this->price_refine(eps))
return eps;
2548 for (
int i = 0; i<_n; ++i) {
2549 const int pr_begin=_pr_node[i];
2550 const int pr_end=_pr_node[i]+_num_arcs[i];
2551 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
2552 const int child=_children[pointer];
2553 if (_is_quadratic_cost && _quad_cost[pointer]) {
2554 const Int reduced_cost=_flow[pointer]+_cost[pointer]+_prices[i]-_prices[child];
2555 if (reduced_cost < 0) {
2556 const Int delta=
MIN(_capacity[pointer]-_flow[pointer],-reduced_cost);
2560 _excess[child]+=delta;
2561 _flow[pointer] += delta;
2562 _flow[_reverse[pointer]] -=delta;
2566 const Int reduced_cost=_cost[pointer]+_prices[i]-_prices[child];
2567 if (reduced_cost < 0) {
2568 const Int delta=_capacity[pointer]-_flow[pointer];
2571 _excess[child]+=delta;
2572 _flow[pointer] = _capacity[pointer];
2573 _flow[_reverse[pointer]]=-_capacity[pointer];
2580 for (
int i = 0; i<_n; ++i)
2581 if (_excess[i] > 0 && !_active[i]) {
2582 _list_active.push_back(i);
2586 while (!_list_active.empty()) {
2587 if (price_heuristic && (_time2.getElapsed()/_time1.getElapsed() < 0.5)) this->price_update(eps);
2588 const int node = _list_active.front();
2589 _list_active.pop_front();
2590 _active[node]=
false;
2591 this->discharge(node,eps);
2596 template <
typename Int>
2601 Int* rank =
new Int[_n];
2602 Int* scanned =
new Int[_n];
2603 Int* temp_scanned =
new Int[_n];
2605 memset(scanned,
false,_n*
sizeof(Int));
2606 memset(temp_scanned,
false,_n*
sizeof(Int));
2608 for (
int i = 0; i<_n; ++i) {
2609 if (_excess[i] < 0) {
2611 temp_scanned[i]=
true;
2613 total_excess-=_excess[i];
2625 if (_excess[node] > 0) total_excess-=_excess[node];
2626 if (total_excess==0)
break;
2627 const int pr_begin=_pr_node[node];
2628 const int pr_end=_pr_node[node]+_num_arcs[node];
2629 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
2630 const int child = _children[pointer];
2631 if (!scanned[child] && _flow[_reverse[pointer]] < _capacity[_reverse[pointer]]) {
2632 const Int reduced_cost=this->reduced_cost(node,child,pointer);
2633 if (reduced_cost >= 0) {
2634 if (temp_scanned[child]) {
2635 if (rank[node]<rank[child])
2638 heap.
insert(child,rank[node]);
2639 temp_scanned[child]=
true;
2641 rank[child]=rank[node];
2643 const Int new_rank=rank[node]-reduced_cost;
2644 if (temp_scanned[child]) {
2645 if (new_rank < rank[child]) {
2646 rank[child]=new_rank;
2650 temp_scanned[child]=
true;
2651 rank[child]=new_rank;
2652 heap.
insert(child,rank[child]);
2660 for (
int i = 0; i<_n; ++i) {
2661 if (scanned[i] && rank[i] > max_rank) max_rank=rank[i];
2665 const Int max_increase=max_rank;
2666 for (
int i = 0; i<_n; ++i) {
2667 assert(rank[i] >= 0);
2668 _prices[i] -= rank[i] > max_rank ? max_increase : rank[i];
2673 delete[](temp_scanned);
2679 template <
typename Int>
2681 if (_excess[node] <= 0)
return;
2684 const int pr_begin=_pr_node[node];
2685 const int pr_end=_pr_node[node]+_num_arcs[node];
2686 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
2687 const Int cap_residual=_capacity[pointer]-_flow[pointer];
2688 const int child=_children[pointer];
2689 if (cap_residual > 0) {
2690 if (_is_quadratic_cost && _quad_cost[pointer]) {
2691 const Int reduced_cost=_flow[pointer]+_cost[pointer]+_prices[node]-_prices[child];
2692 if (reduced_cost < 0) {
2694 const Int delta=
MIN(
MIN(cap_residual,-reduced_cost),_excess[node]);
2695 _excess[node]-=delta;
2696 _excess[child]+=delta;
2697 _flow[pointer] += delta;
2698 _flow[_reverse[pointer]] -= delta;
2699 if (!_active[child]) {
2700 _active[child]=
true;
2701 _list_active.push_back(child);
2703 if (delta==-reduced_cost) {
2704 max_cmp_cost=
MAX(max_cmp_cost,_prices[node]);
2707 max_cmp_cost=
MAX(max_cmp_cost,_prices[node]-reduced_cost);
2709 if (!_excess[node])
break;
2711 const Int compare_cost=_prices[child]-_cost[pointer];
2712 if (compare_cost > _prices[node]) {
2715 if (_excess[node] > cap_residual) {
2717 _excess[node]-=delta;
2719 delta=_excess[node];
2722 _excess[child]+=delta;
2723 _flow[pointer] += delta;
2724 _flow[_reverse[pointer]] -= delta;
2725 if (!_active[child]) {
2726 _active[child]=
true;
2727 _list_active.push_back(child);
2729 if (!_excess[node])
break;
2731 max_cmp_cost=
MAX(max_cmp_cost,compare_cost);
2737 if (_excess[node] > 0) {
2739 _prices[node]=max_cmp_cost-eps;
2740 _list_active.push_front(node);
2745 template <
typename Int>
2748 for (
int i = 0; i<_n; ++i) {
2749 const int pr_begin=_pr_node[i];
2750 const int pr_end=_pr_node[i]+_num_arcs[i];
2751 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
2752 const Int cap_residual=_capacity[pointer]-_flow[pointer];
2753 if (cap_residual > 0) {
2754 const int child=_children[pointer];
2755 const Int reduced_cost=this->reduced_cost(i,child,pointer);
2756 min_prb=
MIN(min_prb,reduced_cost);
2760 cerr <<
"Flow is " << -min_prb <<
"-optimal" << endl;
2764 template <
typename Int>
2767 for (
int i = 0; i<_n; ++i) {
2768 const int pr_begin=_pr_node[i];
2769 const int pr_end=_pr_node[i]+_num_arcs[i];
2770 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
2771 cost+=_flow[pointer]*_cost[pointer];
2777 template <
typename Int>
2780 for (
int i = 0; i<_n; ++i) {
2781 const int pr_begin=_pr_node[i];
2782 const int pr_end=_pr_node[i]+_num_arcs[i];
2783 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
2784 cost+=
static_cast<double>(_flow[pointer])*static_cast<double>(_cost[pointer]);
2791 template <
typename Int>
2794 for (
int i = 0; i<_n; ++i) {
2795 cost += _prices[i]*_demand[i];
2800 template <
typename Int>
2804 bool* admiss =
new bool[_maxm];
2805 Int* reduced_costs =
new Int[_maxm];
2806 Int* distances =
new Int[_n];
2807 bool* scanned =
new bool[_n];
2816 acyclic=this->topological_sort(
true,admiss,reduced_costs);
2817 if (!acyclic)
break;
2819 for (
int i = 0; i<_maxm; ++i) {
2820 if (admiss[i] && reduced_costs[i] < -eps) { optimal=
false;
break; }
2824 memset(distances,0,_n*
sizeof(Int));
2825 distances[_topological_order[0]]=0;
2826 for (
int i = 0; i<_n; ++i) {
2827 const int node = _topological_order[i];
2828 const int pr_begin=_pr_node[node];
2829 const int pr_end=_pr_node[node]+_num_arcs[node];
2830 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
2831 if (admiss[pointer]) {
2832 const int child = _children[pointer];
2833 const Int new_cost=distances[node] + reduced_costs[pointer];
2834 if (distances[child] > new_cost) {
2835 distances[child]=new_cost;
2839 heap.
insert(node,distances[node]);
2842 memset(scanned,
false,_n*
sizeof(
bool));
2849 const int pr_begin=_pr_node[node];
2850 const int pr_end=_pr_node[node]+_num_arcs[node];
2851 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
2852 const int child = _children[pointer];
2853 const Int reduced_cost=reduced_costs[pointer];
2854 if (!scanned[child] && _capacity[pointer] > _flow[pointer]) {
2855 if (reduced_cost < 0) {
2856 if (distances[child] > distances[node]) {
2857 distances[child]=distances[node];
2861 const Int new_dist = distances[node]+eps*(reduced_cost/eps);
2862 if (distances[child] > new_dist) {
2863 distances[child]=new_dist;
2870 Int max_distances=-infinity;
2871 Int min_distances=infinity;
2872 for (
int i = 0; i<_n; ++i) {
2873 if (distances[i] < min_distances) min_distances=distances[i];
2874 if (distances[i] > max_distances) max_distances=distances[i];
2878 if (min_distances==max_distances)
break;
2879 for (
int i = 0; i<_n; ++i) {
2880 _prices[i] += distances[i]-max_distances;
2886 delete[](reduced_costs);
2887 delete[](distances);
2893 template <
typename Int>
2895 if (!_topologically_sorted) this->topological_sort();
2896 Int* distances =
new Int[_n];
2897 int* prec =
new int[_n];
2898 for (
int i = 0; i < _n; ++i) prec[i]=-1;
2900 for (
int i = 0; i < _n; ++i) distances[i]=infinity;
2901 distances[_topological_order[0]]=0;
2902 for (
int i = 0; i < _n; ++i) {
2903 const int node = _topological_order[i];
2904 const int pr_begin=_pr_node[node];
2905 const int pr_end=_pr_node[node]+_num_arcs[node];
2906 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
2907 if (_capacity[pointer] > 0) {
2908 const int child = _children[pointer];
2909 const Int new_cost= distances[node] + _cost[pointer];
2910 if (distances[child] > new_cost) {
2911 distances[child]=new_cost;
2917 const Int shortest_path=distances[_topological_order[_n-1]];
2918 int current=_topological_order[_n-1];
2920 while (current != -1) {
2922 current=prec[current];
2924 delete[](distances);
2926 return shortest_path;
2929 template <
typename Int>
2931 const int pr_begin=_pr_node[s];
2932 const int pr_end=_pr_node[s]+_num_arcs[s];
2934 int * sj_arcs =
new int[_n];
2935 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
2936 if (_capacity[pointer] >0 && _flow[pointer] > 0) {
2937 heap.
insert(_children[pointer],-_flow[pointer]);
2938 sj_arcs[_children[pointer]]=pointer;
2943 decomposition.push_back(path);
2956 const int pr_begin=_pr_node[node];
2957 const int pr_end=_pr_node[node]+_num_arcs[node];
2958 int max_pointer=pr_begin;
2960 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
2961 if (_capacity[pointer] >0 && _flow[pointer] > max_flow) {
2962 max_pointer=pointer;
2963 max_flow=_flow[pointer];
2966 flow=
MIN(flow,max_flow);
2968 node=_children[max_pointer];
2973 _flow[_reverse[*it]]+=flow;
2975 if (init_flow!=flow) {
2976 heap.
insert(init_node,-init_flow+flow);
2982 template <
typename Int>
2986 stream <<
"p min " << _n <<
" " << _m << endl;
2987 for (
int i = 0; i<_n; ++i) {
2988 if (_demand[i] != 0)
2989 stream <<
"n " << i+1 <<
" " << _demand[i] << endl;
2991 for (
int i = 0; i<_n; ++i) {
2992 const int pr_begin=_pr_node[i];
2993 const int pr_end=_pr_node[i]+_num_arcs[i];
2994 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
2995 if (_capacity[pointer] > 0) {
2996 stream <<
"a " << i+1 <<
" " << _children[pointer]+1 <<
" " << 0 <<
" " << _capacity[pointer] <<
" " << _cost[pointer] << endl;
3003 template <
typename Int>
3005 return (_is_quadratic_cost && _quad_cost[pointer]) ? _cost[pointer]+_flow[pointer] + _prices[node]-_prices[child] :
3006 _cost[pointer] + _prices[node]-_prices[child];
3010 template <
typename Int>
3012 const bool extern_admiss_node=admiss_node != NULL;
3013 const bool extern_reduced_costs=reduced_costs != NULL;
3014 int* indegree =
new int[_n];
3015 for (
int i = 0; i<_n; ++i) indegree[i]=0;
3017 if (!extern_admiss_node)
3018 admiss_node=
new bool[_maxm];
3019 for (
int i = 0; i<_n; ++i) {
3020 const int pr_begin=_pr_node[i];
3021 const int pr_end=_pr_node[i]+_num_arcs[i];
3022 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
3023 const int child=_children[pointer];
3024 const int rcost=this->reduced_cost(i,child,pointer);
3025 if (extern_reduced_costs) reduced_costs[pointer]=rcost;
3026 admiss_node[pointer]=(_capacity[pointer] > _flow[pointer] && rcost < 0);
3027 if (admiss_node[pointer]) indegree[child]++;
3031 for (
int i = 0; i<_n; ++i) {
3032 const int pr_begin=_pr_node[i];
3033 const int pr_end=_pr_node[i]+_num_arcs[i];
3034 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
3035 if (_capacity[pointer] > 0)
3036 indegree[_children[pointer]]++;
3042 for (
int i = 0; i<_n; ++i) {
3045 while (!list.
empty()) {
3046 int node=list.
front();
3048 _topological_order[next++]=node;
3050 const int pr_begin=_pr_node[node];
3051 const int pr_end=_pr_node[node]+_num_arcs[node];
3052 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
3053 const int child=_children[pointer];
3054 if (admiss_node[pointer]) {
3056 if (!indegree[child]) list.
push_back(child);
3060 const int pr_begin=_pr_node[node];
3061 const int pr_end=_pr_node[node]+_num_arcs[node];
3062 for (
int pointer = pr_begin; pointer<pr_end; ++pointer) {
3063 if (_capacity[pointer] > 0) {
3064 const int child=_children[pointer];
3066 if (!indegree[child]) list.
push_back(child);
3073 _topologically_sorted=!admissible;
3075 if (!extern_admiss_node)
3076 delete[](admiss_node);
3080 template <
typename Int>
3082 cerr <<
"Excess: " << endl;
3083 for (
int i= 0; i<_n; ++i) {
3084 cerr << _excess[i] <<
" ";
3089 template <
typename Int>
3091 cerr <<
"Prices: " << endl;
3092 for (
int i= 0; i<_n; ++i) {
3093 cerr << _prices[i] <<
" ";
3098 template <
typename Int>
3100 cerr <<
"Graph: " << _n <<
" x " << _m << endl;
3101 for (
int i= 0; i<_n; ++i) {
3102 cerr <<
"***********************" << endl;
3103 cerr <<
"Node: " << i <<
", e(i): " << _excess[i] <<
", pi(i): " << _prices[i] <<
", d(i): " << _demand[i] << endl;
3104 const int pr=_pr_node[i];
3105 for (
int j = 0; j<_num_arcs[i]; ++j) {
3106 cerr <<
" child: " << _children[pr+j] <<
", cap: " << _capacity[pr+j] <<
", cost: " << _cost[pr+j] <<
", flow: " << _flow[pr+j] << endl;
3112 template <
typename T =
double,
typename Int =
long long>
class GraphPath {
3118 T eval_l0(
const T* variables,
List<
Path<Int>*>* decomposition = NULL);
3119 T eval_conv(
const T* variables,
List<
Path<Int>*>* decomposition = NULL);
3120 T eval_dual_norm(
const T* variables,
list_int* path_out = NULL);
3121 void proximal_l0(T* variables,
const T lambda);
3122 void proximal_conv(T* variables,
const T lambda);
3123 int n()
const {
return _n; };
3126 void flow_decomposition(
List<
Path<Int>*>& decomposition)
const;
3127 void scale_costs(
const T lambda);
3150 template <
typename T,
typename Int>
3157 const int n2=_n*2+2;
3158 _infinite_capacity=_big_integer/n2;
3159 int* num_arcs=
new int[n2];
3160 for (
int i = 0; i<_n; ++i) {
3164 for (
int i = 0; i<_n; ++i) {
3167 num_arcs[n2-2]=_n+1;
3168 num_arcs[n2-1]=_n+1;
3169 for (
int i = 0; i<_n; ++i) {
3170 for (
int j = graph.
jc[i]; j<graph.
jc[i+1]; ++j) {
3172 num_arcs[graph.
ir[j]]++;
3183 _sf=
MIN(graph.
precision,static_cast<T>(_big_integer)/(max_weight*1000000.0*n2));
3187 _min_cost_flow->add_edge(2*_n,2*_n+1,0,0,_big_integer);
3190 for (
int i = 0; i<_n; ++i) {
3191 _min_cost_flow->add_edge(i,i+_n,0,0,_infinite_capacity);
3192 _min_cost_flow->add_edge(i,i+_n,0,0,0);
3195 for (
int i = 0; i<_n; ++i) {
3197 const Int cost=
static_cast<Int
>(ceil(graph.
start_weights[i]*_sf));
3198 const double double_cost=
static_cast<double>((graph.
start_weights[i]));
3199 _min_cost_flow->add_edge(2*_n,i,cost,double_cost,_infinite_capacity);
3203 for (
int i = 0; i<_n; ++i) {
3205 const Int cost=
static_cast<Int
>(ceil(graph.
stop_weights[i]*_sf));
3206 const double double_cost=
static_cast<double>((graph.
stop_weights[i]));
3207 _min_cost_flow->add_edge(i+_n,2*_n+1,cost,double_cost,_infinite_capacity);
3211 for (
int i = 0; i<_n; ++i) {
3212 for (
int j= graph.
jc[i]; j<graph.
jc[i+1]; ++j) {
3213 const Int cost=
static_cast<Int
>(ceil(graph.
weights[j]*_sf));
3214 const double double_cost=
static_cast<double>((graph.
weights[j]));
3215 _min_cost_flow->add_edge(i+_n,graph.
ir[j],cost,double_cost,_infinite_capacity);
3219 _min_cost_flow->set_demand(2*_n,-_big_integer);
3220 _min_cost_flow->set_demand(2*_n+1,_big_integer);
3224 template <
typename T,
typename Int>
3226 for (
int i = 0; i<_n; ++i) {
3227 const Int dem= variables[i] ?
static_cast<Int
>(_sf) : 0;
3228 _min_cost_flow->set_demand(i,dem);
3229 _min_cost_flow->set_demand(i+_n,-dem);
3231 _min_cost_flow->compute_min_cost(
false,
false);
3232 const T val =
static_cast<T
>(_min_cost_flow->compute_cost_double())/(2*_sf*_sf);
3233 if (decomposition) {
3234 for (
int i = 0; i<_n; ++i) {
3235 const Int dem= variables[i] ?
static_cast<Int
>(_sf) : 0;
3236 _min_cost_flow->set_demand(i,0);
3237 _min_cost_flow->set_demand(i+_n,0);
3238 _min_cost_flow->add_flow(i,0,dem);
3240 this->flow_decomposition(*decomposition);
3247 template <
typename T,
typename Int>
3249 for (
int i = 0; i<_n; ++i) {
3250 const Int dem=
static_cast<Int
>(_sf*abs<T>(variables[i]));
3251 _min_cost_flow->set_demand(i,dem);
3252 _min_cost_flow->set_demand(i+_n,-dem);
3254 _min_cost_flow->compute_min_cost(
false,
false);
3255 const T val =
static_cast<T
>(_min_cost_flow->compute_cost_double())/(2*_sf*_sf);
3256 if (decomposition) {
3257 for (
int i = 0; i<_n; ++i) {
3258 const Int dem=
static_cast<Int
>(_sf*abs<T>(variables[i]));
3259 _min_cost_flow->set_demand(i,0);
3260 _min_cost_flow->set_demand(i+_n,0);
3261 _min_cost_flow->add_flow(i,0,dem);
3263 this->flow_decomposition(*decomposition);
3269 template <
typename T,
typename Int>
3273 bool exit_loop=
false;
3275 _min_cost_flow->set_edge(2*_n,0,0,0);
3277 while (!exit_loop) {
3278 for (
int i = 0; i<_n; ++i) {
3279 const Int fact=
static_cast<Int
>(_sf*abs<T>(variables[i]/tau));
3280 _min_cost_flow->set_edge(i,0,-fact,_infinite_capacity);
3281 _min_cost_flow->set_edge(i,1,0,0);
3283 T delta=
static_cast<T
>(_min_cost_flow->cost_shortest_path_in_dag(path))/_sf;
3286 if (*it < _n) gamma+=abs<T>(variables[*it]);
3288 T new_tau=gamma/(delta+gamma/tau);
3289 if (abs<T>(new_tau) < 1e-12 || abs<T>(delta) < 1e-12 || abs<T>(new_tau-tau) < 1e-12 || (!first && (new_tau <= tau))) exit_loop=
true;
3294 _min_cost_flow->set_edge(2*_n,0,0,_big_integer);
3295 for (
int i = 0; i<_n; ++i) {
3296 _min_cost_flow->set_edge(i,0,0,_infinite_capacity);
3297 _min_cost_flow->set_edge(i,1,0,0);
3306 template <
typename T,
typename Int>
3308 _min_cost_flow->save_costs();
3310 this->scale_costs(lambda);
3311 const Int unit=
static_cast<Int
>(_sf);
3312 for (
int i = 0; i<2*_n; ++i) {
3313 _min_cost_flow->set_demand(i,0);
3315 for (
int i = 0; i<_n; ++i) {
3316 const Int fact=
static_cast<Int
>(_sf*(0.5*variables[i]*variables[i]));
3317 _min_cost_flow->set_edge(i,0,-fact,unit);
3318 _min_cost_flow->set_edge(i,1,0,_infinite_capacity);
3320 _min_cost_flow->compute_min_cost(
false,
false);
3325 for (
int i = 0; i<_n; ++i) {
3327 variables[i]= _min_cost_flow->get_flow(i,0) > 0 ? variables[i] : 0;
3330 for (
int i = 0; i<_n; ++i) {
3331 _min_cost_flow->set_edge(i,0,0,_infinite_capacity);
3332 _min_cost_flow->set_edge(i,1,0,0);
3336 _min_cost_flow->restore_costs();
3351 template <
typename T,
typename Int>
3353 _min_cost_flow->set_is_quad_cost(
true);
3354 _min_cost_flow->save_costs();
3356 this->scale_costs(lambda);
3358 for (
int i = 0; i<2*_n; ++i) {
3359 _min_cost_flow->set_demand(i,0);
3361 for (
int i = 0; i<_n; ++i) {
3362 const Int fact=
static_cast<Int
>(_sf*(abs<T>(variables[i])));
3363 _min_cost_flow->set_edge(i,0,-fact,fact);
3364 _min_cost_flow->set_quad_cost(i,0,
true);
3365 _min_cost_flow->set_edge(i,1,0,_infinite_capacity);
3367 _min_cost_flow->compute_min_cost(
false,
false);
3369 for (
int i = 0; i<_n; ++i) {
3370 variables[i]= variables[i] > 0 ?
static_cast<T
>(_min_cost_flow->get_flow(i,0))/_sf : -static_cast<T>(_min_cost_flow->get_flow(i,0))/_sf;
3372 for (
int i = 0; i<_n; ++i) {
3373 _min_cost_flow->set_edge(i,0,0,_infinite_capacity);
3374 _min_cost_flow->set_quad_cost(i,0,
false);
3375 _min_cost_flow->set_edge(i,1,0,0);
3377 _min_cost_flow->set_is_quad_cost(
false);
3378 _min_cost_flow->restore_costs();
3382 template <
typename T,
typename Int>
3384 _min_cost_flow->set_edge(2*_n,0,0,0);
3385 _min_cost_flow->st_flow_decomposition_dag(decomposition,2*_n,2*_n+1);
3386 _min_cost_flow->set_edge(2*_n,0,0,_big_integer);
3387 for (
ListIterator<
Path<Int>*> it_path = decomposition.begin(); it_path != decomposition.end(); ++it_path) {
3389 for (
const_iterator_int it = it_path->nodes.begin(); it != it_path->nodes.end(); ++it) {
3392 it_path->nodes.clear();
3393 it_path->nodes.fusion(new_nodes);
3394 it_path->flow =
static_cast<T
>(it_path->flow_int)/_sf;
3398 template <
typename T,
typename Int>
3400 Vector<T> start_weights(_init_start_weights,_n);
3401 Vector<T> stop_weights(_init_stop_weights,_n);
3403 const int n2=_n*2+2;
3405 _sf=
MIN(_graphprecision,static_cast<T>(_big_integer)/(max_weight*1000000.0*n2));
3407 _min_cost_flow->scale_costs(_sf*lambda);
void set_capacity(const int node, const int num_arc, const Int cap)
void proximal_conv(T *variables, const T lambda)
void set_edge(const int node, const int num_arc, const Int cost, const Int cap)
double compute_cost_double() const
void init_split_variables(SpMatrix< T > &splitted_w, const int Ng, const int Nv)
void init_graph(const GraphPathStruct< T > &graph)
T val_zero(const T *pr_alpha, const int current_node)
void proximal_l0(T *variables, const T lambda)
T project_tree_l1(T *variables, const int n, const T lambda)
int pB(const int i) const
returns pB[i]
void copy(const Vector< T > &x)
make a copy of x
T flow_component(const list_int &component, const int Ng)
void set_weights(const T lambda)
void perform_maxflow_component(const list_int &component)
T dual_norm_inf(const Vector< T > &input, const Vector< T > &weights)
Int reduced_cost(const int node, const int child, const int arc) const
void inv(const Vector< T > &x)
A <- 1./x.
void scale_costs(const T lambda)
T eval_dual_norm(const T *variables, list_int *path_out=NULL)
void init_split_variables_aux(const int node, int ¤t_counter, Vector< int > &counter_node, list_int **splitted_w, const int Ng, const int Nv)
void extractConnexComponents(std::list< list_int * > &connex_components)
void proximal_operator(const T *variables_in, T *variables_out, const bool clever=false, const T *weights=NULL)
T fmaxval() const
returns the maximum magnitude
bool price_refine(const Int eps)
void proj_zero(Vector< T > &input, const T fact=1.0)
void component_relabelling(const list_int &component, const int max_label, const bool force)
void print_excess() const
MaxFlow(const int N, const int *num_edges, const int s, const int t)
void print_dimacs(const char *name) const
void scale_flow(const T scal)
T norm(const T *variables, T *work, const T *weights, const int Ng, const bool linf=true)
void decrease_key(const int node, const T val)
void st_flow_decomposition_dag(List< Path< Int > * > &list, const int s, const int t) const
T * rawX() const
returns a modifiable reference of the data, DANGEROUS
void sub_gradient(const Vector< T > &input, Vector< T > &output, const Vector< T > &weights, const int Ng)
MinCostFlow< Int > * _min_cost_flow
T eval_conv(const T *variables, List< Path< Int > * > *decomposition=NULL)
void restore_capacities()
void flow_decomposition(List< Path< Int > * > &decomposition) const
void scale_costs(const double scal)
void update_capacities(const list_int &component, T *work)
const T & max(const T &a, const T &b)
Return the maximum between a and b.
void scale_flow(const T scal)
T compute_thrs_project_l1(T *X, const int n, const T lambda)
T project_box(const list_int &component, const T *variables_in, T *variables_out, T *work, bool &fusion, const int Ng)
void reset_component(const list_int &component)
T project_weighted(const list_int &component, const T *variables_in, T *variables_out, T *work, const T *weights, const int Ng)
static void stop()
a useful debugging function
ListIterator< T > & begin() const
int n() const
returns the size of the vector
bool compare_abs(T first, T second)
void discharge(const list_int &component, const int u, const int max_label)
T val_zero2(const T *pr_alpha, const int current_node, bool &tmp)
void setZeros()
Set all values to zero.
T asum() const
computes the sum of the magnitudes of the vector
T v(const int i) const
returns v[i]
void set_is_quad_cost(const bool is_quad_cost)
T abs(const T x)
template version of the fabs function
void set_capacities_groups(const list_int &component, const Vector< T > &weights, const T lambda, const int Ng)
T project(const list_int &component, const T *variables_in, T *variables_out, T *work, const int Ng)
void create_tree(const int N_variables, int *own_variables, int *N_own_variables, T *lambda, mwSize *groups_ir, mwSize *groups_jc, const int N_groups, const int root_node=0)
int perform_order(const int current_node, const int pointer)
void init_split_variables(SpMatrix< T > &splitted_w)
void create_graph(const int Nv, const int Ng, T *weights, mwSize *var_ir, mwSize *var_jc)
T val_norm2(const T *pr_alpha, const int current_node, T &tmp, const bool l1=false)
bool _topologically_sorted
void price_update(const Int eps)
void set_demand(const int node, const Int dem)
Int get_flow(const int node, const int num_arc) const
void insert(const int node, const T val)
void add_edge(const int u, const int v, const T cu, const T cv)
void proj_weighted_linf(Vector< T > &input, const Vector< T > &weights, const T fact=1.0)
bool splitComponent(const list_int &component, std::list< list_int * > &connex_components, const int Ng, bool *positive, const bool addpos=true)
bool test_optimality_conditions() const
list_int ** _active_nodes
void gap_relabelling(const list_int &component, const int gap, const int max_label)
void start()
start the time
void sub_grad(const Vector< T > &input, Vector< T > &output, const bool linf)
bool price_refine_heuristic
void sub_gradient_aux(const Vector< T > &input, Vector< T > &output, const Vector< T > &weights, const int node, list_int &list, const int Ng)
Int cost_shortest_path_in_dag(list_int &path)
void sub_gradient(const Vector< T > &input, Vector< T > &output, const Vector< T > &weights)
void proj(Vector< T > &input, const bool l1=false, const T fact=1.0)
T dual_norm_inf(const Vector< T > &input)
int r(const int i) const
returns r[i]
void printElapsed()
print the elapsed time
void print_component(const list_int &component)
void resize(const int m, const int n, const int nzmax)
resize the matrix
void discharge(const int node, const Int eps)
void print_component2(const list_int &component)
MinCostFlow(const int n, const int *max_num_arcs)
void set_quad_cost(const int node, const int num_arc, const bool quad_cost)
void add_edge(const int u, const int v, const Int cost, const double double_cost, const Int cap)
double getElapsed() const
print the elapsed time
void reset()
reset the timer
int * _size_own_variables
void add_flow(const int node, const int num_arc, const Int flow)
double * _init_double_cost
int perform_dfs(const int current_node, const int pointer)
T norm(const T *variables, T *work, const T *weights, const bool linf=true)
void find_min(int &node, T &val) const
void fusion(const List< T > &list)
T val_norm(const T *pr_alpha, const int current_node, const bool l1=false)
void restore_capacities()
void set_capacities_variables(const T *cap, const int Nv, const int Ng)
void set_weights(const T lambda)
void global_relabelling()
void set_weights(const T *weights, const T lambda)
bool topological_sort(const bool admissible=false, bool *admiss=NULL, Int *rcosts=NULL)
the function assumes the graph is a DAG
void print_prices() const
T eval_l0(const T *variables, List< Path< Int > * > *decomposition=NULL)
void compute_min_cost(const bool scale_data=true, const bool verbose=false)
void setData(T *X, const int n)
Int refine(Int eps, const bool price_refine=false)
Int compute_uncap_cost() const
void print_graph_aux(const int node)
void l1project_weighted(Vector< T > &out, const Vector< T > &weights, const T thrs, const bool residual=false) const