示例#1
0
 public individual(individual old)
 {
     this.path = new int[old.path.Length];
     for (int x = 0; x < old.path.Length; x++)
     {
         this.path[x] = old.path[x];
     }
     this.cost = old.cost;
 }
示例#2
0
        public void solve(ref City[] Cities, int maxTime, ref string timeOut, ref int bssfUpdates, ref ProblemAndSolver.TSPSolution bssf, ref double bssfCost)
        {
            Stopwatch timer = new Stopwatch();

            timer.Start();

            // At the very least, stop when you run out of time
            while (timer.ElapsedMilliseconds < maxTime)
            {
                init(Cities);

                // Pick two parents
                int   indexA  = parentSelection(-1, Cities.Length);
                int[] parentA = species[indexA].path;

                int[] parentB = species[parentSelection(indexA, Cities.Length)].path;


                // Have them reproduce
                individual child = new individual();
                child.path = reproduce(ref parentA, ref parentB, Cities.Length);
                child.cost = determineFitness(ref Cities, child.path);

                // Store the child somewhere

                // TODO - DO SOMETHING COOL
                // for now just store at least cost guy. If none, don't keep
                for (int i = 0; i < Cities.Length; i++)
                {
                    if (species[i].cost > child.cost)
                    {
                        species[i] = new individual(child);
                        bssfUpdates++;
                        break;
                    }
                }
            }

            // Find out the best
            int best = 0;

            for (int i = 1; i < Cities.Length; i++)
            {
                if (species[i].cost < species[best].cost)
                {
                    best = i;
                }
            }

            ArrayList Route = new ArrayList();

            Route.Clear();
            for (int i = 0; i < species[best].path.Length; i++)
            {
                Route.Add(Cities[species[best].path[i]]);
            }
            bssf     = new ProblemAndSolver.TSPSolution(Route);
            bssfCost = bssf.costOfRoute();

            // Return variables
            timer.Stop();
            timeOut = timer.Elapsed.ToString();
        }