public EvolutionStrategy() { this._random = new Random(); this._tpdict = null; this._chromosomes = null; this._tp_size = 2; this._iteration = 0; this._warp = null; this._borders = null; }
public void InitializePatternDictionary(List <int[, ]> input_samples, int tp_size, WarpOptions warp, BorderOptions borders) { List <int> sizes = new List <int>(); for (int i = 1; i <= tp_size; i++) { sizes.Add(i); } this._tpdict = new TPDict(input_samples, sizes, warp, borders); this._tp_size = tp_size; this._warp = warp; this._borders = borders; }
public Chromosome Mutate(List <int> tp_sizes, int mut_times, BorderOptions borders) { Chromosome clone = this.Clone(); int times = Math.Max(0, this._random.Next(mut_times)) + 1; for (int i = 0; i < times; i++) { int size = tp_sizes[this._random.Next(tp_sizes.Count)]; int x = clone._random.Next(clone._width - size + 1); int y = clone._random.Next(clone._height - size + 1); PatternList patterns = clone._tpdict.GetTPArray(size); PatternList border_patterns = new PatternList(); if (borders != null) { if (borders["top"] && y == 0) { border_patterns.AddRange(clone._tpdict.GetTPBorderArray(size, "top")); } if (borders["bot"] && y == clone._height - size) { border_patterns.AddRange(clone._tpdict.GetTPBorderArray(size, "bot")); } if (borders["left"] && x == 0) { border_patterns.AddRange(clone._tpdict.GetTPBorderArray(size, "left")); } if (borders["right"] && x == clone._width - size) { border_patterns.AddRange(clone._tpdict.GetTPBorderArray(size, "right")); } } if (border_patterns.Length > 0) { patterns = border_patterns; } if (patterns.Length > 0) { clone.ApplyTP(patterns.Sample(clone._random), x, y); clone._calculated = false; } } return(clone); }
public void RandomInitialize(List <int> tp_sizes, BorderOptions borders) { for (int y = 0; y < this._height; y++) { for (int x = 0; x < this._width; x++) { int size = tp_sizes[this._random.Next(tp_sizes.Count)]; size = Math.Min(size, this._height - y); size = Math.Min(size, this._width - x); PatternList patterns = this._tpdict.GetTPArray(size); PatternList border_patterns = new PatternList(); if (borders != null) { if (borders["top"] && y == 0) { border_patterns.AddRange(this._tpdict.GetTPBorderArray(size, "top")); } if (borders["bot"] && y == this._height - size) { border_patterns.AddRange(this._tpdict.GetTPBorderArray(size, "bot")); } if (borders["left"] && x == 0) { border_patterns.AddRange(this._tpdict.GetTPBorderArray(size, "left")); } if (borders["right"] && x == this._width - size) { border_patterns.AddRange(this._tpdict.GetTPBorderArray(size, "rigth")); } } if (border_patterns.Length > 0) { patterns = border_patterns; } if (patterns.Length > 0) { this.ApplyTP(patterns.Sample(this._random), x, y); } } } }
public TPDict(List <int[, ]> input_samples, List <int> sizes, WarpOptions warp = null, BorderOptions borders = null) { Tuple <Dictionary <int, PatternCounts>, Dictionary <int, PatternList>, Dictionary <int, BorderPatternList> > temp = Helper.CalculateTilePatternProbabilities(input_samples, sizes, warp, borders); this._q_counts = temp.Item1; this._patterns = temp.Item2; this._border_patterns = temp.Item3; }
public static Tuple <Dictionary <int, PatternCounts>, Dictionary <int, PatternList>, Dictionary <int, BorderPatternList> > CalculateTilePatternProbabilities(List <int[, ]> maps, List <int> tp_sizes, WarpOptions warp = null, BorderOptions borders = null) { Dictionary <int, PatternCounts> p = new Dictionary <int, PatternCounts>(); Dictionary <int, PatternList> patterns = new Dictionary <int, PatternList>(); Dictionary <int, BorderPatternList> border_patterns = new Dictionary <int, BorderPatternList>(); foreach (int size in tp_sizes) { p[size] = new PatternCounts(); patterns[size] = new PatternList(); border_patterns[size] = new BorderPatternList(); for (int i = 0; i < maps.Count; i++) { int[,] map = maps[i]; int ySize = size; if (warp != null && warp["y"]) { ySize = 1; } for (int y = 0; y < map.GetLength(0) - ySize + 1; y++) { int xSize = size; if (warp != null && warp["x"]) { xSize = 1; } for (int x = 0; x < map.GetLength(1) - xSize + 1; x++) { Pattern pattern = CalculateTilePattern(map, x, y, size); p[size].Add(pattern); if (borders != null) { bool temp_border = false; if (borders["top"] && y == 0) { temp_border = true; border_patterns[size].Add("top", pattern); } if (borders["bot"] && y == map.GetLength(0) - size) { temp_border = true; border_patterns[size].Add("bot", pattern); } if (borders["left"] && x == 0) { temp_border = true; border_patterns[size].Add("left", pattern); } if (borders["right"] && x == map.GetLength(1) - size) { temp_border = true; border_patterns[size].Add("right", pattern); } if (!temp_border) { patterns[size].Add(pattern); } } else { patterns[size].Add(pattern); } } } } } return(Tuple.Create(p, patterns, border_patterns)); }
/// <summary> /// Initialize the pattern dictionary that is used during generation /// only call that function when you want to change any aspect of the patterns /// </summary> /// <param name="input_samples">the input integer matrix that the algorithm sample from</param> /// <param name="tp_size">the size of the tile patterns used in generation larger than 1</param> /// <param name="warp">an object that allow the patterns to sample by wrapping across the edges</param> /// <param name="borders">an object that allow the edges to be similar to the edges from the input_samples</param> public void InitializePatternDictionary(List <int[, ]> input_samples, int tp_size, WarpOptions warp = null, BorderOptions borders = null) { if (tp_size <= 1) { tp_size = 2; } this._es.InitializePatternDictionary(input_samples, tp_size, warp, borders); }
/// <summary> /// Run the algorithm for a fixed amount of iterations. /// This function doesn't need anything to be called before hand. /// You can call step to enhance the generation after that function is done /// </summary> /// <param name="input_samples">the input integer matrix that the algorithm sample from</param> /// <param name="tp_size">the size of the tile patterns used in generation larger than 1</param> /// <param name="width">the width of the generated map</param> /// <param name="height">the height of the generated map</param> /// <param name="iterations">the number of iterations that the algorithm should do before finishing</param> /// <param name="warp">an object that allow the patterns to sample by wrapping across the edges</param> /// <param name="borders">an object that allow the edges to be similar to the edges from the input_samples</param> /// <param name="pop_size">the number of generated map at once (having more maps in parallel) helps find good solution faster</param> /// <param name="inter_weight">the Asymmetric weight defined from Lucas and Volz work. It balances between having the input_sample have at least one of each pattern in the generated image or vice versa.</param> /// <param name="mut_times">the maximum number of modifications the algorithm is allowed to do in one step</param> /// <param name="noise">noise value to push the algorithm away from certain arrays and embrace some new noise</param> /// <returns>The best generated map so far</returns> public int[,] Generate(List <int[, ]> input_samples, int tp_size, int width, int height, int iterations = 10000, WarpOptions warp = null, BorderOptions borders = null, int pop_size = 1, double inter_weight = 0.5, int mut_times = 1, double noise = 0) { this.InitializePatternDictionary(input_samples, tp_size, warp, borders); this.InitializeGeneration(width, height, pop_size); while (this.GetIteration() < iterations) { this.Step(inter_weight, mut_times, noise); } return(this.GetMap()); }