void Mutate() { var bf = new BottomLeftBestFitHeuristic(_demand, _master, null); var getParent = GetSelectParentFn(); var parent = getParent(); Random rn = new Random(); var length = parent.PatternDemands.Count; var indexToShuffle = rn.Next(0, length); var patterns = parent.Patterns; var pd = parent.PatternDemands[indexToShuffle]; var pattern = patterns[indexToShuffle]; var shuffleRan = rn.NextDouble(); if (shuffleRan <= _chanceToShuffle)//shuffle { pd.Pattern = bf.Shuffle(pd.Pattern); RepairSolution(parent); } else//rotate { shuffleRan = rn.NextDouble(); indexToShuffle = rn.Next(0, length); patterns = parent.Patterns; pd = parent.PatternDemands[indexToShuffle]; pattern = patterns[indexToShuffle]; if ((shuffleRan <= _chanceToShuffle)) { parent.PatternDemands.Shuffle(); RepairSolution(parent); } } SetFitness(); }
public void CreateInitialSolutions() { List <PatternDemand2d> allSolutions = new List <PatternDemand2d>(); var sol = new Solution(); Func <List <Rect>, List <Rect> > _sort; _sort = (t) => { return(t.OrderBy(x => x.Height * x.Width).ToList()); }; var BLBF = new BottomLeftBestFitHeuristic(_demand, _master, _sort); sol.PatternDemands = BLBF.Process(); _solutions.Add(sol); sol = new Solution(); _sort = (t) => { return(t.OrderBy(x => x.Height).ToList()); }; BLBF = new BottomLeftBestFitHeuristic(_demand, _master, _sort); sol.PatternDemands = BLBF.Process(); _solutions.Add(sol); sol = new Solution(); _sort = (t) => { return(t.OrderBy(x => x.Width).ToList()); }; BLBF = new BottomLeftBestFitHeuristic(_demand, _master, _sort); sol.PatternDemands = BLBF.Process(); _solutions.Add(sol); ////////// var bestScrap = new BestFitScrap(); sol = new Solution(); _sort = (t) => { return(t.OrderByDescending(x => x.Height * x.Width).ToList()); }; sol.PatternDemands = bestScrap.Process(_demand, _sort, _master, x => true); //testing _solutions = new List <Solution>(); _solutions.Add(sol); return; /////////// SetFitness(); while (_solutions.Count < _population) { var children = Crossover(); foreach (var solu in children) { RepairSolution(solu); _solutions.Add(solu); } SetFitness(); } var pds = _solutions[0].PatternDemands; var bf = new BottomLeftBestFitHeuristic(_demand, _master, null); var shufMe = pds.First(); var shuffled = bf.Shuffle(shufMe.Pattern); shufMe.Pattern = shuffled; _solutions.Reverse(); var shuffleSolutions = _solutions.Take(_shuffleSolutionCount); foreach (var s in shuffleSolutions) { foreach (var pd in s.PatternDemands) { pd.Pattern = bf.Shuffle(pd.Pattern); } RepairSolution(s); } SetFitness(); //patterns.Remove(shufMe); //patterns.Insert(0, shuffled); //return _solutions.OrderBy(x=>x.MasterCount).ToList(); }