/// <summary> /// Initialises the solution and associates it with a specified problem. /// </summary> /// <param name="problem">The problem to associate this solution with</param> public Solution(Problem problem) { this.problem = problem; cowboys = new Cowboy[Problem.NumCowboys]; for (int i = 0; i < Problem.NumCowboys; i++) { cowboys[i] = new Cowboy(problem); } checkCowboys(); }
/// <summary> /// Sorts the cowboys by if they are alive or not /// </summary> /// <param name="negCowboy">The cowboy that gives a -1 response if it is alive and the other isn't</param> /// <param name="posCowboy">The cowboy that gives a 1 response if it is alive and the other isn't</param> /// <returns>-1 or 1 if one cowboy is alive and the other isnt 0 otherwise</returns> public static int sortAlive(Cowboy negCowboy, Cowboy posCowboy) { negCowboy.problem.compare(); int ret = 0; if ((negCowboy.isDead && posCowboy.isDead) || (!negCowboy.isDead && !posCowboy.isDead)) { } else if (negCowboy.isDead && !posCowboy.isDead) { ret = 1; } else if (posCowboy.isDead && !negCowboy.isDead) { ret = -1; } return ret; }
/// <summary> /// Breeds a new cowboy with the specified parents. /// </summary> /// <param name="c1">The first parent of the new cowboy</param> /// <param name="c2">The second parent of the new cowboy</param> /// <returns>The cowboy that is the offspring of the 2 parent cowboys provided</returns> public Cowboy breedNewCowboy(Cowboy c1, Cowboy c2) { return new Cowboy(this, new decimal[] { c1.Location[0], c2.Location[1] }); }
/// <summary> /// Breeds a new cowboy selecting the parents randomly from a specified list /// </summary> /// <param name="cowboysList">The list of cowboys that are potential parents for the new cowboy</param> /// <returns>A new cowboy bred from 2 random parents in the provided list</returns> public Cowboy breedNewCowboy(Cowboy[] cowboysList) { if (Random.Next(MutationChance) == 0) { return new Cowboy(this); } else { Cowboy cb1 = cowboysList[Random.Next(cowboysList.Length)]; Cowboy cb2 = cowboysList[Random.Next(cowboysList.Length)]; return (breedNewCowboy(cb1, cb2)); } }
/// <summary> /// Initialises the solution providing it the list of cowboys and the problem to /// be associated with /// </summary> /// <param name="problem">The problem to associate this solution with</param> /// <param name="cowboys">The list of cowboys to use for this solution</param> public Solution(Problem problem, Cowboy[] cowboys) { this.problem = problem; this.cowboys = cowboys; checkCowboys(); }
/// <summary> /// Returns the better cowboy based on the visibility of the cowboy, with better /// cowboys having lower visibility. /// </summary> /// <param name="posCowboy">The cowboy that if better will get the return value 1</param> /// <param name="negCowboy">The cowboy that if better will get the return value -1</param> /// <returns>1 if posCowboy is better, -1 if negCowboy is better and 0 if they are equal</returns> public static int sortByNumSeen(Cowboy negCowboy, Cowboy posCowboy) { posCowboy.problem.compare(); if (posCowboy.NumSeen < negCowboy.NumSeen) { return 1; } else if (posCowboy.NumSeen > negCowboy.NumSeen) { return -1; } else //if posCowboy.NumSeen == negCowboy.NumSeen { return 0; } }
/// <summary> /// Sorts the cowboys by location, with top left being placed first /// </summary> /// <param name="negCowboy">The cowboy which will elicit a -1 response if it is found to be more NW than the other</param> /// <param name="posCowboy">The cowboy which will elicit a 0 response if it is found to be more NW than the other</param> /// <returns>-1 or 1 depending on which cowboy is the furthest NW or 0 if they are equal</returns> public static int sortByLocation(Cowboy negCowboy, Cowboy posCowboy) { negCowboy.problem.compare(); decimal[] negLoc = negCowboy.Location; decimal[] posLoc = posCowboy.Location; if (negLoc[1] < posLoc[1]) { return -1; } else if (negLoc[1] > posLoc[1]) { return 1; } else { if (negLoc[0] < posLoc[0]) { return -1; } else if (negLoc[0] > posLoc[0]) { return 1; } else { return 0; } } }