Exemplo n.º 1
0
        public void optimize(int depth)
        {
            Analyzation before = manager.analyzeSolution();

            MissionaryManager resultMM = independentOptimizerRandomOrder(manager);

            Analyzation after = resultMM.analyzeSolution();
            Console.WriteLine(after.conflictCount);
            Console.WriteLine("---");
            Console.WriteLine(before.conflictCount.ToString() + " -> " + after.conflictCount.ToString() + " conflicts.");
            Console.WriteLine(before.cost.ToString() + " -> " + after.cost.ToString() + " cost.");
            Console.WriteLine(before.time.ToString() + " -> " + after.time.ToString() + " total time.");
            Console.WriteLine(before.null_trains.ToString() + " -> " + after.null_trains.ToString() + " null trains.");
            Console.WriteLine("---");
            while (!after.Equals(before))
            {
                manager = resultMM;
                before = manager.analyzeSolution();
                resultMM = independentOptimizerRandomOrder(manager);
                after = manager.analyzeSolution();
                Console.WriteLine("---");
                Console.WriteLine(before.conflictCount.ToString() + " -> " + after.conflictCount.ToString());
                Console.WriteLine(before.cost.ToString() + " -> " + after.cost.ToString());
                Console.WriteLine(before.time.ToString() + " -> " + after.time.ToString());
                Console.WriteLine("---");
                Console.WriteLine(after.conflictCount);
                resultMM = independentOptimizerRandomOrder(manager);
            }
            Console.WriteLine("We got done! There are "+after.conflictCount.ToString()+" conflicts.");
        }
Exemplo n.º 2
0
        public MissionaryManager independentOptimizerRandomOrder(MissionaryManager inputMM)
        {
            MissionaryManager mm = inputMM.Clone();
            Random rand = new Random();
            List<Missionary> ms = mm.missionaries;
            int numberOfMissionaries = ms.Count;
            List<int> missionaryIndices = new List<int>();
            for (int i = 0; i < numberOfMissionaries; i++)
            {
                missionaryIndices.Add(i);
            }

            // until we've done every missionary...
            while (missionaryIndices.Count > 0)
            {
                int randomInd = rand.Next(0,missionaryIndices.Count);
                int missInd = missionaryIndices[randomInd];
                Missionary currentMissionary = ms[missInd];
                missionaryIndices.Remove(missInd);

                // If we're already getting up early enough, start at the end (or, if we're going to a sleepover, which means we need to always squish forward.)
                if (currentMissionary.legs[0].time_start < 9 * 60 || currentMissionary.legs[0].going_to_sleepover)
                {
                    // For each train...
                    for (int i = currentMissionary.legs.Length - 1; i >= 0; i--)
                    {
                        // As long as it conflicts with that before it, move it forward.
                        while (currentMissionary.legs[i].conflict_before == true)
                        {
                            Train replacement = manager.trainStation.getNextTrain(currentMissionary.legs[i].train);
                            /* The fillLeg should do this...
                             if (i > 0 && replacement.timeMinutes > currentMissionary.legs[i - 1].train.arrivalTimeMinutes)
                            {
                                currentMissionary.legs[i].conflict_before = false;
                                currentMissionary.legs[i - 1].conflict_after = false;
                            }
                            else
                            {
                                currentMissionary.legs[i].conflict_before = true;
                                currentMissionary.legs[i - 1].conflict_after = true;
                            }

                            if (i < currentMissionary.legs.Length - 1 && replacement.arrivalTimeMinutes > currentMissionary.legs[i + 1].train.timeMinutes)
                            {
                                currentMissionary.legs[i].conflict_after = true;
                                currentMissionary.legs[i+1].conflict_before = true;
                            }
                            else
                            {
                                currentMissionary.legs[i].conflict_after = false;
                                currentMissionary.legs[i+1].conflict_before = false;
                            } */
                            //currentMissionary.legs[i].train = replacement;
                            currentMissionary.fillLeg(replacement);
                        }
                    }
                }
                else
                {
                    // For each train...
                    for (int i = 0; i < currentMissionary.legs.Length-1; i++)
                    {
                        // As long as it conflicts with that after it, move it back.
                        while (currentMissionary.legs[i].conflict_after == true)
                        {
                            Train replacement = manager.trainStation.getPreviousTrain(currentMissionary.legs[i].train);
                            /* The fillLeg should do this
                            if (i > 0 && replacement.timeMinutes > currentMissionary.legs[i - 1].train.arrivalTimeMinutes)
                            {
                                currentMissionary.legs[i].conflict_before = false;
                                currentMissionary.legs[i - 1].conflict_after = false;
                            }
                            else
                            {
                                if (i > 0)
                                {
                                    currentMissionary.legs[i].conflict_before = true;
                                    currentMissionary.legs[i - 1].conflict_after = true;
                                }
                            }

                            if (i < currentMissionary.legs.Length - 1 && replacement.arrivalTimeMinutes > currentMissionary.legs[i + 1].train.timeMinutes)
                            {
                                currentMissionary.legs[i].conflict_after = true;
                                currentMissionary.legs[i+1].conflict_before = true;
                            }
                            else
                            {
                                if (i < currentMissionary.legs.Length)
                                {
                                    currentMissionary.legs[i].conflict_after = false;
                                    currentMissionary.legs[i + 1].conflict_before = false;
                                }
                            } */
                            //currentMissionary.legs[i].train = replacement;
                            currentMissionary.fillLeg(replacement);
                        }
                    }
                }

                // We've done the whole missionaries travel now.
                ms[missInd] = currentMissionary; // put it back.
            }
            // We've done all the missinaries.

            // Compare it to that with which we started, and if it's better, return it, otherwise the original.
            mm.missionaries = ms;
            Analyzation inputA = inputMM.analyzeSolution();
            Analyzation outputA = mm.analyzeSolution();

            if (compareAnalyzations(inputA, outputA) == 0)
            {
                return inputMM;
            }
            else
            {
                return mm;
            }

            return mm;
        }