/// <summary> /// Returns the egalitarian group happiness cost of the solution /// </summary> /// <param name="stableMarriage">The stable marriage</param> /// <param name="solution">The solution</param> /// <returns>The egalitarian group happiness cost</returns> protected override double EvaluateMethod(StableMarriage stableMarriage, Solution solution) { double happiness = 0; foreach (Tuple <int, int> pair in solution) { happiness += stableMarriage.Priorities[pair.Item1].FindIndex(x => x == pair.Item2) - stableMarriage.Priorities[pair.Item2].FindIndex(x => x == pair.Item1); } return(Math.Abs(happiness / stableMarriage.GroupSize)); }
/// <summary> /// Returns the amount of stable pairs in a solution /// </summary> /// <param name="stableMarriage">The stable marriage</param> /// <param name="solution">The solution</param> /// <returns>The amount of stable pairs</returns> protected override int EvaluateMethod(StableMarriage stableMarriage, Solution solution) { int correct = 0; for (int i = 0; i < stableMarriage.GroupSize; i++) { if (IsStablePair(stableMarriage.Priorities, solution, i)) { correct++; } } return(correct); }
static void Main(string[] args) { #region MarriageProblem Human[] men = new Human[] { new Human(new int[] { 2, 1, 3, 4 }), new Human(new int[] { 4, 1, 2, 3 }), new Human(new int[] { 1, 3, 2, 4 }), new Human(new int[] { 2, 3, 1, 4 }) }; Human[] women = new Human[] { new Human(new int[] { 1, 3, 2, 4 }), new Human(new int[] { 3, 4, 1, 2 }), new Human(new int[] { 4, 2, 3, 1 }), new Human(new int[] { 3, 2, 1, 4 }) }; Pair[] stablePairs = StableMarriage.Get(women, men); foreach (var p in stablePairs) { Console.WriteLine($"{p.Man} {p.Woman}"); } #endregion Console.WriteLine("\n\n\n"); #region Matchings //int[,] arr1 = new int[,] //{ // {0,0,0,1,1,0}, // {0,0,0,0,1,1}, // {0,0,0,1,1,0}, // {1,0,1,0,0,0}, // {1,1,1,0,0,0}, // {0,1,0,0,0,0} //}; //Graph g1 = new Graph(arr1); //List<Tuple<int,int>> matching1 = MaxMatching.Get(g1); //for (int i = 0; i < matching1.Count; i++) //{ // Console.Write($"{matching1[i].Item1} {matching1[i].Item2} "); //} //Console.WriteLine(); #endregion }
/// <summary> /// Creates an algorithm to solve the stable marriage problem /// </summary> /// <param name="stableMarriage">The stable marriage to be solved</param> public AlgorithmBase(StableMarriage stableMarriage) { _stableMarriage = stableMarriage; }
/// <summary> /// Creates a new genetic algorithm to solve a stable marriage problem. /// </summary> /// <param name="stableMarriage">The stable marriage to be solved</param> /// <param name="settings">The settings of the genetic algorithm</param> public GeneticAlgorithmBase(StableMarriage stableMarriage, GeneticSettings settings) : base(stableMarriage) { Settings = settings; _random = new Random(); }
/// <summary> /// Creates a new Gale-Shapley algorithm to solve a stable marriage problem /// </summary> /// <param name="stableMarriage">The stable marriage to be solved</param> public GaleShapleyAlgorithm(StableMarriage stableMarriage) : base(stableMarriage) { }
/// <summary> /// The method for evaluating the solution /// </summary> /// <param name="stableMarriage">The stable marriage</param> /// <param name="solution">The solution</param> /// <returns>The result</returns> protected abstract Score EvaluateMethod(StableMarriage stableMarriage, Solution solution);
/// <summary> /// Evaluates the solution with the set method /// </summary> /// <param name="stableMarriage">The stable marriage</param> /// <param name="solution">The solution</param> /// <returns>The result</returns> public Score Evaluate(StableMarriage stableMarriage, Solution solution) { return(EvaluateMethod(stableMarriage, solution)); }
public GeneticAlgorithm(StableMarriage stableMarriage, GeneticSettings settings) : base(stableMarriage, settings) { }