public void Random(int w, int h, int BeadTypes, Random rnd = null) { Random rand = rnd ?? new Random(Guid.NewGuid().GetHashCode()); Width = w; Height = h; Weights = new List <float>(); Beads = new List <int>(); for (int i = 0; i < BeadTypes; i++) { Weights.Add(1); } IBoardEvaluator eval = new ComboEvaluator(); do { Beads.Clear(); for (int i = 0; i < Length; i++) { Beads.Add(rand.Next(BeadTypes)); } } while (eval.EvalBoard(Clone()) > 0); }
public Route GenTask(Board board, CancellationToken ct) { Random rand = new Random(Guid.NewGuid().GetHashCode()); List <string> Directions = new List <string>(); int Score = 0; Route Result = new Route(); Board TempBoard = null; IBoardEvaluator eval = new ComboEvaluator() { PerScore = 1000 }; int PickX = board.SelectStartX; int PickY = board.SelectStartY; try { do { Interlocked.Increment(ref Attempts); Directions.Clear(); Point CurrentPoint = new Point(PickX, PickY); if (++PickX >= board.SelectEndX) { PickX = 0; if (++PickY >= board.SelectEndY) { PickY = 0; } } Result.StartX = CurrentPoint.X; Result.StartY = CurrentPoint.Y; TempBoard = board.Clone(); for (int i = 0; i < board.StepLimit; i++) { ct.ThrowIfCancellationRequested(); int DirRnd = rand.Next(AvailableDir.Length); var dir = AvailableDir[DirRnd]; var BackDir = BackwardDir[DirRnd]; if (BackDir == Directions.LastOrDefault()) { --i; continue;//don't select backward path } var NextPoint = TempBoard.MoveBeads(CurrentPoint, dir); if (CurrentPoint == NextPoint)//MoveFail { --i; continue; } CurrentPoint = NextPoint; Directions.Add(dir); Score = eval.EvalBoard(TempBoard.Clone()); if (Score >= TargetScore) { break; } } } while (Score < TargetScore); Console.WriteLine($"Finish in {Attempts.ToString()}"); Console.WriteLine($"TargetScore {TargetScore.ToString()},Final Score {Score.ToString()}"); Result.Score = Score; Result.Result = TempBoard.Beads; Result.Directions = Directions; return(Result); } catch (OperationCanceledException) { //Console.WriteLine($"Cancelled Task Id:{Thread.CurrentThread.ManagedThreadId}"); } return(null); }
public Route GenTask(Board board, List <Point> StartPoints, CancellationToken ct) { Random rand = new Random(Guid.NewGuid().GetHashCode()); List <string> Directions = new List <string>(); int CurrentScore = 0; Route Result = new Route(); Board TempBoard = null; int PickStartIdx = 0; try { do { Interlocked.Increment(ref Attempts); Directions.Clear(); Point CurrentPoint = StartPoints[PickStartIdx++]; if (PickStartIdx >= StartPoints.Count) { PickStartIdx = 0; } Result.StartX = CurrentPoint.X; Result.StartY = CurrentPoint.Y; TempBoard = board.Clone(); IBoardEvaluator eval = new EvaluatorCollection { new ComboEvaluator() { PerScore = 1000 }, new Hori2Pattern() { PerScore = 10 }, new Vert2Pattern() { PerScore = 10 }, new ArrowUPattern() { PerScore = 10 }, new ArrowDPattern() { PerScore = 10 }, new ArrowLPattern() { PerScore = 10 }, new ArrowRPattern() { PerScore = 10 }, new BowULPattern() { PerScore = 10 }, new BowURPattern() { PerScore = 10 }, new BowDLPattern() { PerScore = 10 }, new BowDRPattern() { PerScore = 10 }, new BowRDPattern() { PerScore = 10 }, new BowRUPattern() { PerScore = 10 }, new BowLDPattern() { PerScore = 10 }, new BowLUPattern() { PerScore = 10 }, //new LGapDLPattern(){PerScore=5}, //new LGapURPattern(){PerScore=5}, //new LGapDLPattern(){PerScore=5}, //new LGapDRPattern(){PerScore=5}, //new LGapRDPattern(){PerScore=5}, //new LGapRUPattern(){PerScore=5}, //new LGapLDPattern(){PerScore=5}, //new LGapLUPattern(){PerScore=5}, // new Hori4Pattern(){ PerScore=-10}, // new Hori4Pattern(){ PerScore=-10}, // new Hori6Pattern(){ PerScore=-10}, // new Vert4Pattern(){ PerScore=-10}, // new Vert5Pattern(){ PerScore=-10}, }; for (int i = 0; i < board.StepLimit; i++) { ct.ThrowIfCancellationRequested(); Dictionary <string, int> DirToScore = new Dictionary <string, int>(); foreach (var dir in AvailableDir) { var DirBoard = TempBoard.Clone(); //Try all path select max score var BackDir = BackwardDirDict[dir]; if (BackDir == Directions.LastOrDefault()) { continue;//don't select backward path } Point NextPoint = DirBoard.MoveBeads(CurrentPoint, dir); if (CurrentPoint == NextPoint) { continue;//MoveFail } int DirScore = eval.EvalBoard(DirBoard); DirToScore.Add(dir, DirScore); } //Select Max score direction int MaxScore = DirToScore.Values.Max(); var AcceptableScores = DirToScore.Where(x => x.Value >= MaxScore - AllowPathScoreDrop); var SelectScoreDirKvp = AcceptableScores.ElementAt(rand.Next(AcceptableScores.Count())); var MaxScoreDir = SelectScoreDirKvp.Key; CurrentScore = SelectScoreDirKvp.Value; Directions.Add(MaxScoreDir); CurrentPoint = TempBoard.MoveBeads(CurrentPoint, MaxScoreDir); //Console.WriteLine(TempBoard.Dump()); if (CurrentScore >= TargetScore) { break; } } } while (CurrentScore < TargetScore); var ev = new ComboEvaluator() { PerScore = 1000 }; Console.WriteLine($"Finish in {Attempts.ToString()}"); Console.WriteLine($"TargetScore {TargetScore.ToString()},Score {CurrentScore.ToString()}"); Result.Score = CurrentScore; Result.Result = TempBoard.Beads; Result.Directions = Directions; return(Result); } catch (OperationCanceledException) { //Console.WriteLine($"Cancelled Task Id:{Thread.CurrentThread.ManagedThreadId}"); } return(null); }