/// <summary> /// Solve the game by solving the subset that considedrs the ith element in isolation /// </summary> /// <param name="game"></param> /// <param name="i"></param> /// <returns></returns> private static Solutions SolveSubset(DownCountGame game, int i, bool allSolutions = true) { Solutions solutions = new Solutions(); int currentNumber = game.Numbers[i]; IntegerSet subset = game.Numbers.SubsetNotIncluding(i); DownCountGame gameAdd = new DownCountGame(); gameAdd.TargetNumber = game.TargetNumber - currentNumber; gameAdd.Numbers = subset; solutions.AddRange(Solve(gameAdd, allSolutions) + new Integer(currentNumber)); if (!allSolutions && solutions.Count > 0) { return(solutions); } DownCountGame gameMinus = new DownCountGame(); gameMinus.TargetNumber = game.TargetNumber + currentNumber; gameMinus.Numbers = subset; solutions.AddRange(Solve(gameMinus, allSolutions) - new Integer(currentNumber)); if (!allSolutions && solutions.Count > 0) { return(solutions); } DownCountGame gameMultiply = null; if (game.TargetNumber % currentNumber == 0) { gameMultiply = new DownCountGame(); gameMultiply.TargetNumber = game.TargetNumber / currentNumber; gameMultiply.Numbers = subset; solutions.AddRange(Solve(gameMultiply, allSolutions) * new Integer(currentNumber)); if (!allSolutions && solutions.Count > 0) { return(solutions); } } DownCountGame gameDivide = new DownCountGame(); gameDivide.TargetNumber = game.TargetNumber * currentNumber; gameDivide.Numbers = subset; solutions.AddRange(Solve(gameDivide, allSolutions) / new Integer(currentNumber)); if (!allSolutions && solutions.Count > 0) { return(solutions); } return(solutions); }
/// <summary> /// Solve the given downcount game /// </summary> /// <param name="game"></param> /// <param name="allSolutions"></param> /// <returns></returns> public static Solutions Solve(DownCountGame game, bool allSolutions = true) { Solutions solutions = new Solutions(); if (game.Numbers.Contains(game.TargetNumber)) { solutions.Add(new Integer(game.TargetNumber)); } if (!allSolutions && solutions.Count > 0) { return(solutions); } if (game.Numbers.Count == 1) { return(solutions); } for (int i = 0; i < game.Numbers.Count; ++i) { solutions.AddRange(SolveSubset(game, i, allSolutions)); if (!allSolutions && solutions.Count > 0) { return(solutions); } } return(solutions); }
/// <summary> /// aggregates the data from solving engines /// that were run in parallel partitions /// </summary> /// <param name="solvingEngine"></param> public void AggregateData(SolvingEngine solvingEngine) { if ((solvingEngine is null) || (solvingEngine.Solutions is null)) { throw new ArgumentNullException(nameof(solvingEngine)); } if (solvingEngine.Solutions.Count > 0) { lock (addLock) { Solutions.AddRange(solvingEngine.Solutions); } } else if ((Solutions.Count == 0) && solvingEngine.HasClosestMatch) // no existing or new matches { // there is a race hazard reading Solutions.Count but the down side is trivial, // even the c# concurrent collections don't guarantee counts lock (updateLock) { if (Math.Abs(solvingEngine.Difference) < Math.Abs(Difference)) // record the closest { Difference = solvingEngine.Difference; ClosestMatch = solvingEngine.ClosestMatch; } } } }
/// <summary> /// Solve the game by solving the subset that considedrs the ith element in isolation /// </summary> /// <param name="game"></param> /// <param name="i"></param> /// <returns></returns> private static Solutions SolveSubset(DownCountGame game, int i, bool allSolutions = true) { Solutions solutions = new Solutions(); int currentNumber = game.Numbers[i]; IntegerSet subset = game.Numbers.SubsetNotIncluding(i); DownCountGame gameAdd = new DownCountGame(); gameAdd.TargetNumber = game.TargetNumber - currentNumber; gameAdd.Numbers = subset; solutions.AddRange(Solve(gameAdd, allSolutions) + new Integer(currentNumber)); if (!allSolutions && solutions.Count > 0) return solutions; DownCountGame gameMinus = new DownCountGame(); gameMinus.TargetNumber = game.TargetNumber + currentNumber; gameMinus.Numbers = subset; solutions.AddRange(Solve(gameMinus, allSolutions) - new Integer(currentNumber)); if (!allSolutions && solutions.Count > 0) return solutions; DownCountGame gameMultiply = null; if (game.TargetNumber % currentNumber == 0) { gameMultiply = new DownCountGame(); gameMultiply.TargetNumber = game.TargetNumber / currentNumber; gameMultiply.Numbers = subset; solutions.AddRange(Solve(gameMultiply, allSolutions) * new Integer(currentNumber)); if (!allSolutions && solutions.Count > 0) return solutions; } DownCountGame gameDivide = new DownCountGame(); gameDivide.TargetNumber = game.TargetNumber * currentNumber; gameDivide.Numbers = subset; solutions.AddRange(Solve(gameDivide, allSolutions) / new Integer(currentNumber)); if (!allSolutions && solutions.Count > 0) return solutions; return solutions; }
/// <summary> /// Solve the given downcount game /// </summary> /// <param name="game"></param> /// <param name="allSolutions"></param> /// <returns></returns> public static Solutions Solve(DownCountGame game, bool allSolutions = true) { Solutions solutions = new Solutions(); if (game.Numbers.Contains(game.TargetNumber)) solutions.Add(new Integer(game.TargetNumber)); if (!allSolutions && solutions.Count > 0) return solutions; if (game.Numbers.Count == 1) return solutions; for (int i = 0; i < game.Numbers.Count; ++i) { solutions.AddRange(SolveSubset(game, i, allSolutions)); if (!allSolutions && solutions.Count > 0) return solutions; } return solutions; }
private void initialSolutionMethod() { SupportSolutionModel m1 = new SupportSolutionModel() { Title = "方案A", SupportSolutionOptions = new List <SupportSolutionOptionModel>() { new SupportSolutionOptionModel() { Description = "退款80%,贈送免費課程十堂" }, new SupportSolutionOptionModel() { Description = "退款60%,贈送免費課程5堂" }, new SupportSolutionOptionModel() { Description = "退款40%,贈送免費課程1堂" }, } }; SupportSolutionModel m2 = new SupportSolutionModel() { Title = "方案B", SupportSolutionOptions = new List <SupportSolutionOptionModel>() { new SupportSolutionOptionModel() { Description = "申請發票" }, new SupportSolutionOptionModel() { Description = "申請訂單明細" }, new SupportSolutionOptionModel() { Description = "申請其他服務" }, } }; SupportSolutionModel m3 = new SupportSolutionModel() { Title = "是不是想要[林厚吉]的電話", SupportSolutionOptions = new List <SupportSolutionOptionModel>() { new SupportSolutionOptionModel() { Description = "是" }, new SupportSolutionOptionModel() { Description = "否" } } }; SupportSolutionModel m4 = new SupportSolutionModel() { Title = "請提供您對於此次服務的評價,作為未來改善的依據", SupportSolutionOptions = new List <SupportSolutionOptionModel>() { new SupportSolutionOptionModel() { Description = "Hen厲害" }, new SupportSolutionOptionModel() { Description = "Hen好" }, new SupportSolutionOptionModel() { Description = "一般" }, new SupportSolutionOptionModel() { Description = "Hen不好" }, new SupportSolutionOptionModel() { Description = "糟透了" } } }; Solutions.AddRange(new List <SupportSolutionModel>() { m1, m2, m3, m4 }); }