示例#1
0
        public void Step()
        {
            if (State == SimplexState.FirstPhase || State == SimplexState.SecondPhase)
            {
                Arc    current = EnteringArcEnumerator.Current;
                Arc    arc     = Arc.Invalid;
                double num     = double.NaN;
                bool   flag    = false;
                do
                {
                    Arc current2 = EnteringArcEnumerator.Current;
                    if (!Tree.ContainsKey(current2))
                    {
                        bool   flag2 = Saturated.Contains(current2);
                        double num2  = ActualCost(current2) - (Potential[MyGraph.V(current2)] - Potential[MyGraph.U(current2)]);
                        if ((num2 < 0.0 - Epsilon && !flag2) || (num2 > Epsilon && (flag2 || ActualLowerBound(current2) == -9223372036854775808L)))
                        {
                            arc  = current2;
                            num  = num2;
                            flag = flag2;
                            break;
                        }
                    }
                    if (!EnteringArcEnumerator.MoveNext())
                    {
                        EnteringArcEnumerator = MyGraph.Arcs(ArcFilter.All).GetEnumerator();
                        EnteringArcEnumerator.MoveNext();
                    }
                }while (!(EnteringArcEnumerator.Current == current));
                if (arc == Arc.Invalid)
                {
                    if (State == SimplexState.FirstPhase)
                    {
                        State = SimplexState.SecondPhase;
                        foreach (Arc artificialArc in ArtificialArcs)
                        {
                            if (Flow(artificialArc) > 0)
                            {
                                State = SimplexState.Infeasible;
                                break;
                            }
                        }
                        if (State == SimplexState.SecondPhase)
                        {
                            RecalculatePotentialDfs recalculatePotentialDfs = new RecalculatePotentialDfs();
                            recalculatePotentialDfs.Parent = this;
                            recalculatePotentialDfs.Run(TreeSubgraph, null);
                        }
                    }
                    else
                    {
                        State = SimplexState.Optimal;
                    }
                }
                else
                {
                    Node       node  = MyGraph.U(arc);
                    Node       node2 = MyGraph.V(arc);
                    List <Arc> list  = new List <Arc>();
                    List <Arc> list2 = new List <Arc>();
                    IPath      path  = TreeSubgraph.FindPath(node2, node, Dfs.Direction.Undirected);
                    foreach (Node item in path.Nodes())
                    {
                        Arc arc2 = path.NextArc(item);
                        ((!(MyGraph.U(arc2) == item)) ? list2 : list).Add(arc2);
                    }
                    ulong num3  = (!(num < 0.0)) ? MySubtract(Flow(arc), ActualLowerBound(arc)) : MySubtract(ActualUpperBound(arc), Flow(arc));
                    Arc   arc3  = arc;
                    bool  flag3 = !flag;
                    foreach (Arc item2 in list)
                    {
                        ulong num4 = (!(num < 0.0)) ? MySubtract(Tree[item2], ActualLowerBound(item2)) : MySubtract(ActualUpperBound(item2), Tree[item2]);
                        if (num4 < num3)
                        {
                            num3  = num4;
                            arc3  = item2;
                            flag3 = (num < 0.0);
                        }
                    }
                    foreach (Arc item3 in list2)
                    {
                        ulong num5 = (!(num > 0.0)) ? MySubtract(Tree[item3], ActualLowerBound(item3)) : MySubtract(ActualUpperBound(item3), Tree[item3]);
                        if (num5 < num3)
                        {
                            num3  = num5;
                            arc3  = item3;
                            flag3 = (num > 0.0);
                        }
                    }
                    long num6 = 0L;
                    switch (num3)
                    {
                    case ulong.MaxValue:
                        State = SimplexState.Unbounded;
                        return;

                    default:
                        num6 = (long)((!(num < 0.0)) ? (0L - num3) : num3);
                        foreach (Arc item4 in list)
                        {
                            Dictionary <Arc, long> tree;
                            Arc key;
                            (tree = Tree)[key = item4] = tree[key] + num6;
                        }
                        foreach (Arc item5 in list2)
                        {
                            Dictionary <Arc, long> tree;
                            Arc key2;
                            (tree = Tree)[key2 = item5] = tree[key2] - num6;
                        }
                        break;

                    case 0uL:
                        break;
                    }
                    if (arc3 == arc)
                    {
                        if (flag)
                        {
                            Saturated.Remove(arc);
                        }
                        else
                        {
                            Saturated.Add(arc);
                        }
                    }
                    else
                    {
                        Tree.Remove(arc3);
                        TreeSubgraph.Enable(arc3, false);
                        if (flag3)
                        {
                            Saturated.Add(arc3);
                        }
                        double num7 = ActualCost(arc) - (Potential[node2] - Potential[node]);
                        if (num7 != 0.0)
                        {
                            UpdatePotentialDfs updatePotentialDfs = new UpdatePotentialDfs();
                            updatePotentialDfs.Parent = this;
                            updatePotentialDfs.Diff   = num7;
                            updatePotentialDfs.Run(TreeSubgraph, new Node[1]
                            {
                                node2
                            });
                        }
                        Tree[arc] = Flow(arc) + num6;
                        if (flag)
                        {
                            Saturated.Remove(arc);
                        }
                        TreeSubgraph.Enable(arc, true);
                    }
                }
            }
        }
示例#2
0
        /// Performs an iteration in the simplex algorithm.
        /// Modifies the State field according to what happened.
        public void Step()
        {
            if (State != SimplexState.FirstPhase && State != SimplexState.SecondPhase)
            {
                return;
            }

            // calculate reduced costs and find an entering arc
            Arc    firstArc            = EnteringArcEnumerator.Current;
            Arc    enteringArc         = Arc.Invalid;
            double enteringReducedCost = double.NaN;
            bool   enteringSaturated   = false;

            while (true)
            {
                Arc arc = EnteringArcEnumerator.Current;
                if (!Tree.ContainsKey(arc))
                {
                    bool   saturated   = Saturated.Contains(arc);
                    double reducedCost = ActualCost(arc) - (Potential[MyGraph.V(arc)] - Potential[MyGraph.U(arc)]);
                    if ((reducedCost < -Epsilon && !saturated) ||
                        (reducedCost > Epsilon && (saturated || ActualLowerBound(arc) == long.MinValue)))
                    {
                        enteringArc         = arc;
                        enteringReducedCost = reducedCost;
                        enteringSaturated   = saturated;
                        break;
                    }
                }

                // iterate
                if (!EnteringArcEnumerator.MoveNext())
                {
                    EnteringArcEnumerator = MyGraph.Arcs().GetEnumerator();
                    EnteringArcEnumerator.MoveNext();
                }
                if (EnteringArcEnumerator.Current == firstArc)
                {
                    break;
                }
            }

            if (enteringArc == Arc.Invalid)
            {
                if (State == SimplexState.FirstPhase)
                {
                    State = SimplexState.SecondPhase;
                    foreach (var arc in ArtificialArcs)
                    {
                        if (Flow(arc) > 0)
                        {
                            State = SimplexState.Infeasible;
                            break;
                        }
                    }
                    if (State == SimplexState.SecondPhase)
                    {
                        new RecalculatePotentialDfs()
                        {
                            Parent = this
                        }
                    }