Exemple #1
0
 /// <summary>
 /// Judges 2 solutions by how many cowboys they have alive in them
 /// </summary>
 /// <param name="negSoln">The solution which if better elicits a -1 response</param>
 /// <param name="posSoln">The solution which if better elicits a 1 response</param>
 /// <returns>-1 if the negative solution is better, 1 if the positive solution is better, 0 if they are equal</returns>
 public static int judgeByNumAlive(Solution negSoln, Solution posSoln)
 {
     //tell the problem we are making a comparison
     posSoln.problem.compare();
     //first check if they are null
     if (posSoln == null && negSoln == null)
     {
         return 0;
     }
     else if (posSoln == null)
     {
         return -1;
     }
     else if (negSoln == null)
     {
         return 1;
     }
     else
     {
         //Here we want to select the one with the maximum number of living cowboys
         int posAlive = posSoln.getNumAlive();
         int negAlive = negSoln.getNumAlive();
         if (posAlive > negAlive)
         {
             return 1;
         }
         else if (posAlive < negAlive)
         {
             return -1;
         }
         else //if posAlive == negAlive
         {
             return 0;
         }
     }
 }
Exemple #2
0
        /// <summary>
        /// Overrides the execute method in Problem, eliminates the worst performing cowboys and
        /// breeds new ones using the visibility as a fitness function
        /// </summary>
        public override void execute()
        {
            Cowboy[] cowboys = solutions[solutions.Count - 1].Cowboys;
            List<Cowboy> sortingList = new List<Cowboy>();
            sortingList.AddRange(cowboys);
            sortingList.Sort(Cowboy.sortByNumSeen);
            sortingList.RemoveRange(NumCowboys / 2, NumCowboys / 2);
            Cowboy[] remaining = sortingList.ToArray<Cowboy>();
            while (sortingList.Count < NumCowboys)
            {

                sortingList.Add(breedNewCowboy(remaining));
            }
            solutions[0] = new Solution(this, sortingList.ToArray<Cowboy>());
        }
Exemple #3
0
 /// <summary>
 /// Breeds a new solution at random from a list of possible parents
 /// </summary>
 /// <param name="solutionsList">The list of potential parent</param>
 /// <returns>A solution gotten by breeding 2 of the solutions on the list</returns>
 public virtual Solution breedNewSolution(Solution[] solutionsList)
 {
     if (Random.Next(MutationChance) == 0)
     {
         return new Solution(this);
     }
     else
     {
         Solution s1 = solutionsList[rand.Next(solutionsList.Length - 1)];
         Solution s2 = solutionsList[rand.Next(solutionsList.Length - 1)];
         while (s1 == s2)
         {
             s2 = solutionsList[rand.Next(solutionsList.Length - 1)];
         }
         List<Cowboy> completeGenes = new List<Cowboy>();
         for (int i = 0; i < NumCowboys; i++)
         {
             if (i % 2 == 0)
             {
                 Cowboy c = s1.Cowboys[i];
                 completeGenes.Add(new Cowboy(this, new decimal[] { c.Location[0], c.Location[1] }));
             }
             else
             {
                 Cowboy c = s2.Cowboys[i];
                 completeGenes.Add(new Cowboy(this, new decimal[] { c.Location[0], c.Location[1] }));
             }
         }
         return new Solution(this, completeGenes.ToArray<Cowboy>());
     }
 }
Exemple #4
0
 /// <summary>
 /// Judges 2 solutions their total visibility, with lower being better
 /// </summary>
 /// <param name="negSoln">The solution which if better elicits a -1 response</param>
 /// <param name="posSoln">The solution which if better elicits a 1 response</param>
 /// <returns>-1 if the negative solution is better, 1 if the positive solution is better, 0 if they are equal</returns>
 public static int judgeByTotalVisibility(Solution negSoln, Solution posSoln)
 {
     posSoln.problem.compare();
     if (posSoln == null && negSoln == null)
     {
         return 0;
     }
     else if (posSoln == null)
     {
         return -1;
     }
     else if (negSoln == null)
     {
         return 1;
     }
     else
     {
         //In this case we want the lowest value for the number of cowboys that can
         //see each other.
         int numSeenPos = posSoln.getVisibility();
         int numSeenNeg = negSoln.getVisibility();
         if (numSeenPos > numSeenNeg)
         {
             return -1;
         }
         else if (numSeenPos < numSeenNeg)
         {
             return 1;
         }
         else //If numSeenPos == numSeenNeg
         {
             return 0;
         }
     }
 }