168 #ifndef OR_TOOLS_GRAPH_MIN_COST_FLOW_H_ 169 #define OR_TOOLS_GRAPH_MIN_COST_FLOW_H_ 176 #include "ortools/base/integral_types.h" 177 #include "ortools/base/logging.h" 178 #include "ortools/base/macros.h" 181 #include "ortools/util/stats.h" 182 #include "ortools/util/zvector.h" 187 template <
typename Graph,
typename ArcFlowType,
typename ArcScaledCostType>
240 return SolveWithPossibleAdjustment(SupplyAdjustment::DONT_ADJUST);
249 return SolveWithPossibleAdjustment(SupplyAdjustment::ADJUST);
279 typedef ::util::ReverseArcStaticGraph<NodeIndex, ArcIndex> Graph;
280 enum SupplyAdjustment { ADJUST, DONT_ADJUST };
286 Status SolveWithPossibleAdjustment(SupplyAdjustment adjustment);
289 std::vector<NodeIndex> arc_tail_;
290 std::vector<NodeIndex> arc_head_;
291 std::vector<FlowQuantity> arc_capacity_;
292 std::vector<FlowQuantity> node_supply_;
293 std::vector<CostValue> arc_cost_;
294 std::vector<ArcIndex> arc_permutation_;
295 std::vector<FlowQuantity> arc_flow_;
322 class GenericMinCostFlow :
public MinCostFlowBase {
327 typedef typename Graph::OutgoingOrOppositeIncomingArcIterator
370 std::vector<NodeIndex>*
const infeasible_demand_node);
420 bool IsAdmissible(
ArcIndex arc)
const;
435 bool CheckInputConsistency()
const;
441 bool CheckResult()
const;
445 bool CheckCostRange()
const;
451 bool CheckRelabelPrecondition(
NodeIndex node)
const;
455 std::string DebugString(
const std::string& context,
ArcIndex arc)
const;
459 void ResetFirstAdmissibleArcs();
471 void SaturateAdmissibleArcs();
481 void InitializeActiveNodeStack();
515 bool IsArcDirect(
ArcIndex arc)
const;
516 bool IsArcValid(
ArcIndex arc)
const;
548 ZVector<ArcFlowType> residual_arc_capacity_;
556 std::stack<NodeIndex> active_nodes_;
569 ZVector<ArcScaledCostType> scaled_arc_unit_cost_;
591 int num_relabels_since_last_price_update_;
594 bool feasibility_checked_;
597 bool use_price_update_;
600 bool check_feasibility_;
617 #endif // OR_TOOLS_GRAPH_MIN_COST_FLOW_H_ bool Solve()
Solves the problem, returning true if a min-cost flow could be found.
A simple and efficient min-cost flow interface.
NodeIndex Tail(ArcIndex arc) const
bool MakeFeasible()
Makes the min-cost flow problem solvable by truncating supplies and demands to a level acceptable by ...
void SetNodeSupply(NodeIndex node, FlowQuantity supply)
Sets the supply corresponding to node.
Graph::OutgoingOrOppositeIncomingArcIterator OutgoingOrOppositeIncomingArcIterator
ListGraph Graph
Defining the simplest Graph interface as Graph for convenience.
FlowQuantity MaximumFlow() const
Returns the total flow of the minimum-cost flow found by the algorithm when the returned Status is OP...
Graph::OutgoingArcIterator OutgoingArcIterator
const Graph * graph() const
Returns the graph associated to the current object.
Graph::NodeIndex NodeIndex
Status status() const
Returns the status of last call to Solve().
ZVector< ArcIndex > ArcIndexArray
CostValue UnitCost(ArcIndex arc) const
Different statuses for a solved problem.
FlowQuantity Flow(ArcIndex arc) const
Returns the flow on arc, this only make sense for a successful Solve().
ZVector< FlowQuantity > QuantityArray
FlowQuantity Flow(ArcIndex arc) const
Returns the flow on the given arc using the equations given in the comment on residual_arc_capacity_.
void SetArcCapacity(ArcIndex arc, ArcFlowType new_capacity)
Sets the capacity for the given arc.
void SetArcUnitCost(ArcIndex arc, ArcScaledCostType unit_cost)
Sets the unit cost for the given arc.
FlowQuantity Capacity(ArcIndex arc) const
Returns the capacity of the given arc.
CostValue UnitCost(ArcIndex arc) const
Returns the unscaled cost for the given arc.
CostValue GetOptimalCost() const
Returns the cost of the minimum-cost flow found by the algorithm.
Default MinCostFlow instance that uses StarGraph.
MinCostFlow(const StarGraph *graph)
Status Solve()
Solves the problem, and returns the problem status.
CostValue OptimalCost() const
Returns the cost of the minimum-cost flow found by the algorithm when the returned Status is OPTIMAL.
FlowQuantity InitialSupply(NodeIndex node) const
Returns the initial supply at a given node.
GenericMinCostFlow(const Graph *graph)
Initialize a MinCostFlow instance on the given graph.
SimpleMinCostFlow()
The constructor takes no size.
void SetNodeSupply(NodeIndex node, FlowQuantity supply)
Sets the supply of the given node.
void SetUseUpdatePrices(bool value)
Whether to use the UpdatePrices() heuristic.
void SetArcFlow(ArcIndex arc, ArcFlowType new_flow)
Sets the flow for the given arc.
bool CheckFeasibility(std::vector< NodeIndex > *const infeasible_supply_node, std::vector< NodeIndex > *const infeasible_demand_node)
Checks for feasibility, i.e., that all the supplies and demands can be matched without exceeding bott...
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in c...
FlowQuantity FeasibleSupply(NodeIndex node) const
Returns the largest supply (if > 0) or largest demand in absolute value (if < 0) admissible at node.
ArcIndex AddArcWithCapacityAndUnitCost(NodeIndex tail, NodeIndex head, FlowQuantity capacity, CostValue unit_cost)
Adds a directed arc from tail to head to the underlying graph with a given capacity and cost per unit...
ZVector< CostValue > CostArray
Status SolveMaxFlowWithMinCost()
Same as Solve(), but does not have the restriction that the supply must match the demand or that the ...
FlowQuantity Supply(NodeIndex node) const
Returns the supply at a given node.
FlowQuantity Capacity(ArcIndex arc) const
FlowQuantity Supply(NodeIndex node) const
int32 NodeIndex
Standard instantiation of ForwardEbertGraph (named 'ForwardStarGraph') of EbertGraph (named 'StarGrap...
void SetCheckFeasibility(bool value)
Whether to check the feasibility of the problem with a max-flow, prior to solving it.
NodeIndex Head(ArcIndex arc) const
NodeIndex NumNodes() const
Accessors for the user given data.