Пример #1
0
        public void Reverse(cTSP_LIST_NODE from, cTSP_LIST_NODE to)
        {
            cTSP_LIST_NODE l1, l2, l3, l4, l5, l6;

            l1 = from;
            l2 = to;
            l3 = from.Next;
            l4 = to.Prev;

            while (((l3.Prev != l4) && (l4.Next != l3)) && (l1 != l2))
            {
                //assign
                l1.Next = l4;
                l2.Prev = l3;
                l5      = l4.Prev;
                l4.Prev = l1;
                l6      = l3.Next;
                l3.Next = l2;
                //shift
                l1 = l4;
                l2 = l3;
                l4 = l5;
                l3 = l6;
            }
        }
Пример #2
0
        bool std3opt()
        {
            //do
            // {
            ret = false;
            float best = float.MaxValue;

            cTSP_LIST_NODE b1 = null, b2 = null, b3 = null;

            l1  = list_tour.First;
            l1b = l1.Prev.Prev;     //n-1
            for (; !l1.Equals(l1b); l1 = l1.Next)
            {
                t.a = l1.Value;
                t.b = l1.Next.Value;
                l2b = list_tour.First.Prev;     //n-1
                l3b = list_tour.First;

                for (l2 = l1.Next; !l2.Equals(l2b); l2 = l2.Next)
                {
                    t.c = l2.Value;
                    t.d = l2.Next.Value;
                    if (t.d == t.a)
                    {
                        break;
                    }

                    for (l3 = l2.Next; !l3.Equals(l3b); l3 = l3.Next)
                    {
                        t.e = l3.Value;
                        t.f = l3.Next.Value;

                        if ((t.swap = Best3optSwap2(/*min+tsp.distance[t.e,t.f]*/)) > 0.0)
                        {
                            if (best > gain)
                            {
                                best      = gain;
                                best_swap = t;
                                b1        = l1;
                                b2        = l2;
                                b3        = l3;
                            }
                        }
                    } //for
                }     //for
            }         //for


            if (best < EPSILON)
            {
                ret = true;
                swapcase2(best_swap, b1, b2, b3);
            }
            //} while (ret);

            return(ret);
        } //std3opt
Пример #3
0
        public c3opt(cTSP _tsp, byte _algo, bool _show, int _nn, bool _fast)
        {
            tsp      = _tsp;
            n        = tsp.GetN();
            algo     = _algo;
            showprog = _show;
            nn       = _nn;
            fast     = _fast;

            list_tour = new cTSP_LIST(_tsp);
            l1        = list_tour.First;
        }
Пример #4
0
        private void BuildFromTour()
        {
            int            i;
            cTSP_LIST_NODE l;

            l = list = first = new cTSP_LIST_NODE();
            for (i = 0; i < tsp.GetN() - 1; i++, l = l.Next)
            {
                l.Value     = tsp.GetTourNode(i);
                l.Next      = new cTSP_LIST_NODE();
                l.Next.Prev = l;
            }
            l.Value    = tsp.GetTourNode(i);
            l.Next     = first;
            first.Prev = l;
            last       = l;
        }
Пример #5
0
        bool std3optCandidateSet()
        {
            ret = false;

            l1  = list_tour.First;
            l1b = l1.Prev;

            for (; !l1.Equals(l1b); l1 = l1.Next)
            {
                t.a = l1.Value;
                t.b = l1.Next.Value;
                l2b = l1b.Prev.Prev;

                for (i = 0; i < nn; i++)
                {
                    l2 = list_tour.Find(tsp.GetNN_Node(l1.Value, i));
                    if (l2 == null)
                    {
                        throw new NotSupportedException("nodo non trovato");
                    }
                    if (l2.Equals(l1))
                    {
                        continue;
                    }
                    if ((l2.Equals(l1.Next)) || (l2.Next.Equals(l1)))
                    {
                        continue;
                    }

                    t.c = l2.Value;
                    t.d = l2.Next.Value;

                    //da mettere come opzione!!!
                    //Fast
                    if ((fast) && (tsp.distance[t.a, t.b] <= tsp.distance[t.b, t.c]))
                    {
                        continue;
                    }


                    l3  = l2.Next.Next;
                    min = tsp.distance[t.a, t.b] + tsp.distance[t.c, t.d];

                    for (; !l3.Equals(l1); l3 = l3.Next)
                    {
                        if ((l3.Equals(l1.Next)) ||
                            (l3.Equals(l2)) || (l3.Equals(l2.Next)) ||
                            (l3.Next.Equals(l2)))
                        {
                            continue;
                        }

                        t.e = l3.Value;
                        t.f = l3.Next.Value;

                        if ((t.swap = Best3optSwap()) > 0)
                        {
                            ret = true;

                            swapcase1();

                            if ((t.swap != 5) && (i != 9))
                            {
                                i = 0;
                            }
                            min = tsp.distance[t.a, t.b] + tsp.distance[t.c, t.d];
                        }
                    } //for
                }     //for
            }         //for
            return(ret);
        }
Пример #6
0
        private void swapcase2(TSWAP t, cTSP_LIST_NODE l1, cTSP_LIST_NODE l2, cTSP_LIST_NODE l3)
        {
            int            etmp;
            cTSP_LIST_NODE b, d, f, a, c, e;

            a = l1;
            c = l2;
            e = l3;
            b = a.Next;
            d = c.Next;
            f = e.Next;

            switch (t.swap)
            {
            case 1:     //t2
                list_tour.Reverse(a, d);
                t.b = t.c;
                t.c = b.Value;
                list_tour.Reverse(d.Prev, f);
                t.d = t.e;
                break;

            case 2:     //t3
                list_tour.Reverse(a, d);
                a.Next = d; d.Prev = a;
                e.Next = c; c.Prev = e;
                b.Next = f; f.Prev = b;
                t.b    = t.d;
                t.d    = t.c;
                t.c    = t.e;
                break;

            case 3:     // t4
                list_tour.Reverse(c, f);
                a.Next = e; e.Prev = a;
                d.Next = b; b.Prev = d;
                c.Next = f; f.Prev = c;
                etmp   = t.b;
                t.b    = t.e;
                t.c    = t.d;
                t.d    = etmp;
                break;

            case 4:     // t5
                a.Next = d; d.Prev = a;
                e.Next = b; b.Prev = e;
                c.Next = f; f.Prev = c;
                etmp   = t.b;
                t.b    = t.d;
                t.d    = etmp;
                t.c    = t.e;
                break;

            case 5:     //t6
                list_tour.Reverse(c, f);
                c.Next = e; e.Prev = c;
                d.Next = f; f.Prev = d;
                t.d    = t.e;
                break;

            case 6:     //t7
                list_tour.Reverse(a, f);
                etmp = t.c;
                t.c  = t.d;
                t.d  = etmp;
                t.b  = t.e;
                break;

            case 7:     //t8
                list_tour.Reverse(a, d);
                a.Next = c; c.Prev = a;
                b.Next = d; d.Prev = b;
                etmp   = t.b;
                t.b    = t.c;
                t.c    = etmp;
                break;
            }
        }