marci@1017: // -*- c++ -*- marci@1017: #ifndef LEMON_MIN_COST_GEN_FLOW_H marci@1017: #define LEMON_MIN_COST_GEN_FLOW_H marci@1017: //#include marci@1017: //#include marci@1017: marci@1017: //#include marci@1017: //#include marci@1017: //#include marci@1017: //#include marci@1017: //#include marci@1017: //#include marci@1017: //#include marci@1017: //#include marci@1017: #include marci@1017: marci@1017: namespace lemon { marci@1017: marci@1017: template marci@1017: class PrimalMap { marci@1017: protected: marci@1017: LPSolverWrapper* lp; marci@1017: EdgeIndexMap* edge_index_map; marci@1017: public: marci@1017: PrimalMap(LPSolverWrapper& _lp, EdgeIndexMap& _edge_index_map) : marci@1017: lp(&_lp), edge_index_map(&_edge_index_map) { } marci@1017: double operator[](Edge e) const { marci@1017: return lp->getPrimal((*edge_index_map)[e]); marci@1017: } marci@1017: }; marci@1017: marci@1017: // excess: rho-delta marci@1017: template , marci@1017: typename LCapMap=typename Graph::template EdgeMap, marci@1017: typename CapMap=typename Graph::template EdgeMap, marci@1017: typename FlowMap=typename Graph::template EdgeMap, marci@1017: typename CostMap=typename Graph::template EdgeMap > marci@1017: class MinCostGenFlow { marci@1017: protected: marci@1017: const Graph& g; marci@1017: const Excess& excess; marci@1017: const LCapMap& lcapacity; marci@1017: const CapMap& capacity; marci@1017: FlowMap& flow; marci@1017: const CostMap& cost; marci@1017: public: marci@1017: MinCostGenFlow(const Graph& _g, const Excess& _excess, marci@1017: const LCapMap& _lcapacity, const CapMap& _capacity, marci@1017: FlowMap& _flow, marci@1017: const CostMap& _cost) : marci@1017: g(_g), excess(_excess), lcapacity(_lcapacity), marci@1017: capacity(_capacity), flow(_flow), cost(_cost) { } marci@1017: void run() { marci@1017: LPSolverWrapper lp; marci@1017: lp.setMinimize(); marci@1017: typedef LPSolverWrapper::ColIt ColIt; marci@1017: typedef LPSolverWrapper::RowIt RowIt; marci@1017: typedef typename Graph::template EdgeMap EdgeIndexMap; marci@1017: EdgeIndexMap edge_index_map(g); marci@1017: PrimalMap lp_flow(lp, edge_index_map); marci@1017: for (typename Graph::EdgeIt e(g); e!=INVALID; ++e) { marci@1017: ColIt col_it=lp.addCol(); marci@1017: edge_index_map.set(e, col_it); marci@1017: if (lcapacity[e]==capacity[e]) marci@1017: lp.setColBounds(col_it, LPX_FX, lcapacity[e], capacity[e]); marci@1017: else marci@1017: lp.setColBounds(col_it, LPX_DB, lcapacity[e], capacity[e]); marci@1017: lp.setObjCoef(col_it, cost[e]); marci@1017: } marci@1017: for (typename Graph::NodeIt n(g); n!=INVALID; ++n) { marci@1017: typename Graph::template EdgeMap coeffs(g, 0); marci@1017: for (typename Graph::InEdgeIt e(g, n); e!=INVALID; ++e) marci@1017: coeffs.set(e, coeffs[e]+1); marci@1017: for (typename Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) marci@1017: coeffs.set(e, coeffs[e]-1); marci@1017: RowIt row_it=lp.addRow(); marci@1017: typename std::vector< std::pair > row; marci@1017: //std::cout << "node:" <