Пример #1
0
        public void RemoveOrder(OrderPosition order)
        {
            if (order.Active)
            {
                throw new Exception("Nou doe maar eerst inactive alsjeblieft.");
            }
            if (order.Next != null)
            {
                order.Next.Previous = order.Previous;
            }
            if (order.Previous != null)
            {
                order.Previous.Next = order.Next;
            }
            else
            {
                order.cycle.first = order.Next;
            }

            if (order.Next == null && order.Previous == null)
            {
                RemoveCycle(order.cycle);
            }
            //Util.CheckPrevAndNextForLoops(order.next);
            //Util.CheckPrevAndNextForLoops(order.previous);
        }
Пример #2
0
 public Cycle(byte day, byte truck, int cycleWeight, OrderPosition first)
 {
     this.day         = day;
     this.truck       = truck;
     this.cycleWeight = cycleWeight;
     this.first       = first;
 }
Пример #3
0
        public override Solution Generate()
        {
            double declineVal = 0;

            double[,] localtimes   = new double[2, 5];
            List <Cycle>[,] cycles = new List <Cycle> [2, 5];
            for (int d = 0; d < 5; d++)
            {
                for (int t = 0; t < 2; t++)
                {
                    cycles[t, d] = new List <Cycle>();
                }
            }
            Cycle         c    = new Cycle(0, 0, 0, null);
            OrderPosition prev = null;

            foreach (Order o in Program.allOrders)
            {
                declineVal += o.Time * 3 * o.Frequency;
            }
            foreach (OrderPosition op in Program.allPositions)
            {
                op.cycle    = c;
                op.Previous = prev;
                if (prev != null)
                {
                    prev.Next = op;
                }
                prev = op;
            }
            prev.Next = null;
            c.first   = Program.allPositions[0];
            cycles[0, 0].Add(c);
            return(new Solution(0, declineVal, 0, localtimes, cycles));
        }
Пример #4
0
 public static void CheckPrevAndNextForLoops(OrderPosition o)
 {
     if (o == null)
     {
         return;
     }
     if (o.Previous == o)
     {
         throw new Exception("AAAAAAAAA");
     }
     if (o.Next == o)
     {
         throw new Exception("AAaAAAaAA");
     }
     if (o.Previous != null)
     {
         if (o.Previous.Next != o)
         {
             throw new Exception("AAAAAAAAAA?");
         }
     }
     if (o.Next != null)
     {
         if (o.Next.Previous != o)
         {
             throw new Exception("AAaaAAAaAA?");
         }
     }
 }
Пример #5
0
 public Order(int id, string place, byte frequency, byte containers, short containervolume, float time, short location)
 {
     ID              = id;
     Place           = place;
     Frequency       = frequency;
     ContainerVolume = (short)(containervolume * containers / 5);
     Time            = 60 * time;
     Location        = location;
     Positions       = new OrderPosition[frequency];
 }
Пример #6
0
 public static int ShadowDay(OrderPosition op)
 {
     int[] day = new int[1];
     day[0] = op.Day + 1;
     if (InvalidDayPlanning(op.order, day))
     {
         return((int)Program.wrongDayPentalty);
     }
     return(0);
 }
Пример #7
0
        // Om één of andere reden zorgt het aanroepen van de UpdatePenalties-functie voor problemen in Release-modus.
        // Daarom hebben we de inhoud van de functie direct in de SetActive-functie gezet.
        public void UpdatePenalties(OrderPosition op, float time, int weight)
        {
            int   truck = op.truck, day = op.Day;
            Order o = op.order;

            timePen     += Program.overTimePenalty * (Math.Max(localTimes[truck, day] + time - Program.MaxTime, 0) - Math.Max(localTimes[truck, day] - Program.MaxTime, 0));
            weightPen   += Program.overWeightPenalty * (Math.Max(op.cycle.cycleWeight + weight - Program.MaxCarry, 0) - Math.Max(op.cycle.cycleWeight - Program.MaxCarry, 0));
            freqPen     += Program.wrongFreqPenalty * Util.IncreaseFreqPenAmount(o);
            wrongDayPen += Program.wrongDayPentalty * Util.IncreaseInvalidDayPlanning(o);
            penaltyValue = timePen + weightPen + freqPen + wrongDayPen;
        }
Пример #8
0
        public static void SaveSolution(Solution s, string path = "../../Solutions/BestSolution.txt")
        {
            StreamWriter sw = new StreamWriter(path)
            {
                AutoFlush = true
            };
            int counter = 0;

            for (int d = 1; d <= 5; d++)
            {
                for (int t = 1; t <= 2; t++)
                {
                    int max = s.cycles[t - 1, d - 1].Count;
                    for (int c = 0; c < max; c++)
                    {
                        OrderPosition current = s.cycles[t - 1, d - 1][c].first;
                        bool          active  = false;
                        while (current != null)
                        {
                            if (!current.Active)
                            {
                                current = current.Next;
                                continue;
                            }
                            active = true;
                            sw.Write(t);                // Vehicle
                            sw.Write(';');
                            sw.Write(d);                // Day
                            sw.Write(';');
                            sw.Write(counter + 1);      // Sequence number
                            sw.Write(';');
                            sw.Write(current.order.ID); // Order ID
                            sw.WriteLine();
                            counter++;
                            current = current.Next;
                        }
                        if (active)
                        {
                            sw.Write(t);           // Vehicle
                            sw.Write(';');
                            sw.Write(d);           // Day
                            sw.Write(';');
                            sw.Write(counter + 1); // Sequence number
                            sw.Write(';');
                            sw.Write(0);           // Order ID
                            sw.WriteLine();
                            counter++;
                        }
                    }
                    counter = 0;
                }
            }
            sw.Close();
        }
Пример #9
0
        public override void Apply()
        {
            OrderPosition prev1 = op1.Previous;
            Cycle         cycle1 = op1.cycle;
            byte          day1 = op1.Day, truck1 = op1.truck;
            MoveNeighbour move1 = new MoveNeighbour(s, op1, op2.Previous, op2.Day, op2.truck, op2.cycle);

            move1.Apply();
            MoveNeighbour move2 = new MoveNeighbour(s, op2, prev1, day1, truck1, cycle1);

            move2.Apply();
            shadow = move1.ShadowGain() + move2.ShadowGain();
        }
Пример #10
0
 public MoveNeighbour(Solution s, OrderPosition op, OrderPosition newPrevious, byte day, byte truck, Cycle cycle)
 {
     this.s           = s;
     this.op          = op;
     this.newPrevious = newPrevious;
     this.day         = day;
     this.truck       = truck;
     this.cycle       = cycle;
     oldPrevious      = op.Previous;
     oldDay           = op.Day;
     oldTruck         = op.truck;
     oldCycle         = op.cycle;
     initialShadow    = op.Shadow;
 }
Пример #11
0
        public void AddOrder(OrderPosition order, OrderPosition previous, byte truck, byte day, Cycle cycle)
        {
            if (previous == order)
            {
                throw new Exception("Je probeert het order na zichzelf te plaatsen, doe maar niet!");
            }
            if (order.Active)
            {
                throw new Exception("Nou doe maar eerst inactive alsjeblieft.");
            }
            if (cycle != null && cycle.first == null)
            {
                cycle = AddCycle(day, truck);
            }
            order.Previous = previous;
            if (previous != null)
            {
                if (truck != previous.truck ||
                    day != previous.Day ||
                    cycle != previous.cycle)
                {
                    throw new Exception("Truck, day or cycle doesn't match.");
                }
                order.Next    = previous.Next;
                previous.Next = order;
            }
            else
            {
                if (cycle != null)
                {
                    order.Next  = cycle.first;
                    cycle.first = order;
                }
                else   // Create new cycle
                {
                    cycle       = AddCycle(day, truck);
                    cycle.first = order;
                    order.Next  = null;
                }
            }
            if (order.Next != null)
            {
                order.Next.Previous = order;
            }

            order.Day   = day;
            order.truck = truck;
            order.cycle = cycle;
            //Util.CheckPrevAndNextForLoops(order);
        }
Пример #12
0
        public static int ShadowPath(OrderPosition op)
        {
            Order nxt = Program.HomeOrder;
            Order prv = Program.HomeOrder;

            if (op.Previous != null)
            {
                prv = op.Previous.order;
            }
            if (op.Next != null)
            {
                nxt = op.Next.order;
            }
            return(PathValue(prv.Location, op.order.Location) + PathValue(op.order.Location, nxt.Location));
        }
Пример #13
0
        static void ResetOps()
        {
            List <OrderPosition> opList = new List <OrderPosition>();

            foreach (Order o in allOrders)
            {
                for (int i = 0; i < o.Frequency; i++)
                {
                    o.ActiveFreq        = 0;
                    o.LastFreqPenAmount = 0;
                    o.LastValidPlan     = 0;
                    OrderPosition op = new OrderPosition(o, 0, 0, null, false);
                    opList.Add(op);
                    o.Positions[i] = op;
                }
            }
            allPositions = opList.ToArray();
        }
Пример #14
0
        public Order PrevActive(OrderPosition o)
        {
            OrderPosition prev = o.Previous;

            if (prev == null)
            {
                return(Program.HomeOrder);
            }
            if (prev == prev.Previous)
            {
                throw new Exception("previous.previous is equal to previous, that's a problem.");
            }
            while (!prev.Active)
            {
                prev = prev.Previous;
                if (prev == null)
                {
                    return(Program.HomeOrder);
                }
            }
            return(prev.order);
        }
Пример #15
0
        public Order NextActive(OrderPosition o)
        {
            OrderPosition next = o.Next;

            if (next == null)
            {
                return(Program.HomeOrder);
            }
            if (next == next.Next)
            {
                throw new Exception("next.next is equal to next, that's a problem.");
            }
            while (!next.Active)
            {
                next = next.Next;
                if (next == null)
                {
                    return(Program.HomeOrder);
                }
            }
            return(next.order);
        }
Пример #16
0
        public override Solution Generate()
        {
            System.IO.StreamReader sr = new System.IO.StreamReader(path);
            string input;

            string[] inputs;

            List <OrderPosition> plaatsbaar = new List <OrderPosition>();
            double timeVal    = 0;
            double declineVal = 0;
            double penaltyVal = 0;

            double[,] localtimes   = new double[2, 5];
            List <Cycle>[,] cycles = new List <Cycle> [2, 5];
            for (int d = 0; d < 5; d++)
            {
                for (int t = 0; t < 2; t++)
                {
                    cycles[t, d] = new List <Cycle>();
                }
            }
            List <Cycle>  allCycles = new List <Cycle>();
            OrderPosition previous  = null;

            input  = sr.ReadLine();
            inputs = input.Split(';');
            byte  truck   = (byte)(int.Parse(inputs[0]) - 1);
            byte  day     = (byte)(int.Parse(inputs[1]) - 1);
            int   counter = int.Parse(inputs[2]);
            int   nr      = int.Parse(inputs[3]);
            Order order   = Program.orderByID[nr];

            order.Positions[0].Previous = null;
            localtimes[truck, day]     += Util.PathValue(Program.HomeOrder.Location, order.Location) + order.Time;
            timeVal += Util.PathValue(Program.HomeOrder.Location, order.Location) + order.Time;

            Cycle c = new Cycle(day, truck, 0, order.Positions[0]);

            order.Positions[0].cycle = c;
            order.Positions[0].truck = truck;
            order.Positions[0].Day   = day;
            c.cycleWeight           += order.ContainerVolume;
            plaatsbaar.Add(order.Positions[0]);
            order.Positions[0].Active = true;
            previous = order.Positions[0];

            while ((input = sr.ReadLine()) != null)
            {
                inputs  = input.Split(';');
                truck   = (byte)(int.Parse(inputs[0]) - 1);
                day     = (byte)(int.Parse(inputs[1]) - 1);
                counter = int.Parse(inputs[2]);
                nr      = int.Parse(inputs[3]);
                order   = Program.orderByID[nr];

                if (nr == 0)
                {
                    previous.Next = null;
                    cycles[truck, day].Add(c);
                    allCycles.Add(c);
                    localtimes[truck, day] += Util.PathValue(previous.order.Location, order.Location) + Program.DisposalTime;
                    timeVal += Util.PathValue(previous.order.Location, order.Location) + Program.DisposalTime;
                    previous = null;
                }
                else
                {
                    OrderPosition[] positions = order.Positions;
                    for (int i = 0; i < positions.Length; i++)
                    {
                        if (positions[i].cycle == null)
                        {
                            if (previous == null)
                            {
                                c = new Cycle(day, truck, 0, positions[i]);
                                localtimes[truck, day] += Util.PathValue(Program.HomeOrder.Location, order.Location) + order.Time;
                                timeVal += Util.PathValue(Program.HomeOrder.Location, order.Location) + order.Time;
                            }
                            else
                            {
                                previous.Next           = positions[i];
                                localtimes[truck, day] += Util.PathValue(previous.order.Location, order.Location) + order.Time;
                                timeVal += Util.PathValue(previous.order.Location, order.Location) + order.Time;
                            }
                            positions[i].Active   = true;
                            positions[i].Previous = previous;
                            positions[i].cycle    = c;
                            positions[i].Day      = day;
                            positions[i].truck    = truck;
                            c.cycleWeight        += order.ContainerVolume;
                            previous = positions[i];
                            plaatsbaar.Add(positions[i]);
                            break;
                        }
                    }
                }
            }
            int declinedAmount = 0;

            foreach (OrderPosition op in Program.allPositions)
            {
                if (!op.Active)
                {
                    declineVal += op.order.Time * 3;
                    declinedAmount++;
                    int index = (int)(Util.Rnd * (plaatsbaar.Count + allCycles.Count));
                    if (index < plaatsbaar.Count)
                    {
                        op.Previous      = plaatsbaar[index];
                        op.Next          = plaatsbaar[index].Next;
                        op.Previous.Next = op;
                        op.cycle         = plaatsbaar[index].cycle;
                        op.Day           = plaatsbaar[index].Day;
                        op.truck         = plaatsbaar[index].truck;
                        if (op.Next != null)
                        {
                            op.Next.Previous = op;
                        }
                    }
                    else
                    {
                        op.Next  = allCycles[index - plaatsbaar.Count].first;
                        op.cycle = allCycles[index - plaatsbaar.Count].first.cycle;
                        op.Day   = allCycles[index - plaatsbaar.Count].first.Day;
                        op.truck = allCycles[index - plaatsbaar.Count].first.truck;
                        allCycles[index - plaatsbaar.Count].first = op;
                        op.Next.Previous = op;
                    }
                }
            }

            for (int i = 0; i < allCycles.Count; i++)
            {
                if (allCycles[i].cycleWeight > Program.MaxCarry)
                {
                    penaltyVal += (allCycles[i].cycleWeight - Program.MaxCarry) * Program.overWeightPenalty;
                }
            }
            for (int d = 0; d < 5; d++)
            {
                for (int t = 0; t < 2; t++)
                {
                    if (localtimes[t, d] > Program.MaxTime)
                    {
                        penaltyVal += (localtimes[t, d] - Program.MaxTime) * Program.overTimePenalty;
                    }
                }
            }

            sr.Close();
            return(new Solution(timeVal, declineVal, penaltyVal, localtimes, cycles));
        }
Пример #17
0
        public void SetActive(bool setting, OrderPosition op)
        {
            op.Active = setting;
            Order prev        = PrevActive(op);
            Order next        = NextActive(op);
            int   withoutTime = Util.PathValue(prev.Location, next.Location);
            int   withTime    = Util.PathValue(prev.Location, op.order.Location) + Util.PathValue(op.order.Location, next.Location);
            float time;
            int   truck = op.truck;
            int   day   = op.Day;
            int   weight;
            float decline;

            if (setting)
            {
                time    = op.order.Time + withTime - withoutTime;
                weight  = op.order.ContainerVolume;
                decline = -op.order.Time * 3;
                if (prev == Program.HomeOrder && next == Program.HomeOrder)
                {
                    time += Program.HomeOrder.Time;
                }
            }
            else
            {
                time    = -op.order.Time + withoutTime - withTime;
                weight  = -op.order.ContainerVolume;
                decline = op.order.Time * 3;
                if (prev == Program.HomeOrder && next == Program.HomeOrder)
                {
                    time -= Program.HomeOrder.Time;
                }
            }
            timeValue    += time;
            declineValue += decline;

            // Begin UpdatePenalties-functie
            Order o = op.order;

            timePen     += Program.overTimePenalty * (Math.Max(localTimes[truck, day] + time - Program.MaxTime, 0) - Math.Max(localTimes[truck, day] - Program.MaxTime, 0));
            weightPen   += Program.overWeightPenalty * (Math.Max(op.cycle.cycleWeight + weight - Program.MaxCarry, 0) - Math.Max(op.cycle.cycleWeight - Program.MaxCarry, 0));
            freqPen     += Program.wrongFreqPenalty * (double)Util.IncreaseFreqPenAmount(o);
            wrongDayPen += Program.wrongDayPentalty * (double)Util.IncreaseInvalidDayPlanning(o);
            penaltyValue = timePen + weightPen + freqPen + wrongDayPen;
            // Eind UpdatePenalties-functie

            localTimes[truck, day] += time;
            op.cycle.cycleWeight   += weight;
            if (penaltyValue < 0)
            {
                if (timePen < 0)
                {
                    Console.WriteLine("TimePenalty: " + timePen);
                }
                if (weightPen < 0)
                {
                    Console.WriteLine("WeightPenalty: " + weightPen);
                }
                if (freqPen < 0)
                {
                    Console.WriteLine("FrequencyPenalty: " + freqPen);
                }
                if (wrongDayPen < 0)
                {
                    Console.WriteLine("WrongDayPenalty: " + wrongDayPen);
                }
                throw new Exception("Penalty value lager dan nul wtfrick.");
            }
            //string yeet;
            //if (setting) yeet = "Na activatie";
            //else yeet = "Na deactivatie";
            //Util.Test(this, yeet, false);
        }
Пример #18
0
 public SwapNeighbour(Solution s, OrderPosition op1, OrderPosition op2)
 {
     this.s   = s;
     this.op1 = op1;
     this.op2 = op2;
 }
Пример #19
0
 public ActivateNeighbour(Solution s, OrderPosition op)
 {
     this.s  = s;
     this.op = op;
 }
Пример #20
0
 public DisableNeighbour(Solution s, OrderPosition op)
 {
     this.s  = s;
     this.op = op;
 }