public PaletteGroupNode(List <ConcurrentDictionary <Int32, int> > allPals, int maxPal) : base() { MaxNumberOfColorsPerPalette = maxPal; Content = new ConcurrentDictionary <int, int>(); remainder = new List <ConcurrentDictionary <int, int> >(); FinishedPalettes = new List <ConcurrentDictionary <int, int> >(); Parent = null; ConcurrentDictionary <Int32, int> rootContent = allPals.First(); foreach (var kvp in rootContent) { Content.TryAdd(kvp.Key, kvp.Value); } allColors = new ConcurrentDictionary <int, int>(); foreach (var p in allPals) { foreach (var c in p) { if (!Content.ContainsKey(c.Key)) { allColors.TryAdd(c.Key, c.Value); } } } colorStartNumber = allColors.Count + Content.Count; paletteCost = ((allPals.Count - 1) / 2) + 1; foreach (var p in allPals) { if (p.Count > Content.Count || PaletteProcessor.CountDiffs(Content, p) != 0) { if (p.Count == MaxNumberOfColorsPerPalette) { FinishedPalettes.Add(p); } else { remainder.Add(p); } } } updateCost(); updateHeuristic(); }
private void calculatePaletteValue() { if (pval >= 0) { return; } if (Parent != null) { PaletteMixerAsterNode p = (PaletteMixerAsterNode)Parent; if (Content == Parent.Content) { Parallel.ForEach(p.FinishedPalettes, kvp => { FinishedPalettes.Enqueue(kvp); }); FinishedPalettes.Enqueue(best.Key); } else { Parallel.ForEach(p.FinishedPalettes, kvp => { FinishedPalettes.Enqueue(kvp); }); Parallel.ForEach(p.Content, kvp => { if (PaletteProcessor.CountDiffs(kvp.Key, best.Key) > 0) { Content.TryAdd(kvp.Key, kvp.Value); } }); if (best.Key.Count == MaxPalette) { FinishedPalettes.Enqueue(best.Key); } else { Content.TryAdd(best.Key, best.Value); } } } gotBest = false; pcost = FinishedPalettes.Count; pheur = Content.Count; pval = (pcost * WeightCost) + (pheur * WeightHeuristic); }
public override bool Contains(ConcurrentDictionary <int, int> cont) { return(Content.Count >= cont.Count && PaletteProcessor.CountDiffs(Content, cont) == 0); }
public override int MergeSize(ConcurrentDictionary <int, int> cont) { return(Math.Max(cont.Count, Content.Count) + PaletteProcessor.CountDiffs(cont, Content)); }
public override int Distance(ConcurrentDictionary <int, int> cont) { return(-PaletteProcessor.CountEquals(Content, cont)); }
public override int MergeSize(KeyValuePair <ConcurrentDictionary <int, int>, ConcurrentDictionary <TileKey, int> > cont) { return(PaletteProcessor.CountDiffs(Content.Key, cont.Key) + Math.Max(Content.Key.Count, cont.Key.Count)); }
public override void Expand(params object[] args) { ConcurrentDictionary <Int32, int> p; ConcurrentDictionary <TileKey, int> tl; ConcurrentQueue <PaletteMixerAsterNode> childs = new ConcurrentQueue <PaletteMixerAsterNode>(); p = best.Key; Content.TryRemove(p, out tl); if (p.Count < MaxPalette) { Parallel.ForEach(Content, kvp => { if (PaletteProcessor.CountDiffs(p, kvp.Key) + Math.Max(p.Count, kvp.Key.Count) <= MaxPalette) { PaletteMixerAsterNode newNode = new PaletteMixerAsterNode(); ConcurrentDictionary <Int32, int> paux = new ConcurrentDictionary <int, int>(); ConcurrentDictionary <TileKey, int> tlaux = new ConcurrentDictionary <TileKey, int>(); Parallel.ForEach(p, c => { paux.TryAdd(c.Key, c.Value); }); Parallel.ForEach(kvp.Key, c => { paux.TryAdd(c.Key, c.Value); }); Parallel.ForEach(tl, t => { tlaux.TryAdd(t.Key, t.Value); }); Parallel.ForEach(kvp.Value, t => { tlaux.TryAdd(t.Key, t.Value); }); newNode.MaxPalette = MaxPalette; newNode.Parent = this; newNode.best = new KeyValuePair <ConcurrentDictionary <int, int>, ConcurrentDictionary <TileKey, int> >(paux, tlaux); newNode.gotBest = true; newNode.updateCost(); newNode.updateHeuristic(); childs.Enqueue(newNode); } }); } PaletteMixerAsterNode n = new PaletteMixerAsterNode(); n.Content = Content; n.best = best; n.MaxPalette = MaxPalette; n.Parent = this; n.gotBest = true; n.updateCost(); n.updateHeuristic(); childs.Enqueue(n); foreach (var ch in childs) { Children.Add(ch); } }
public override void Expand(params object[] args) { int best = 0; int cur = 0; ConcurrentDictionary <Int32, int> curpal = null; List <ConcurrentDictionary <Int32, int> > childs = new List <ConcurrentDictionary <int, int> >(); PaletteGroupNode newNode; Children = new List <AstarNode <ConcurrentDictionary <Int32, int> > >(); PaletteGroupNode parent = (PaletteGroupNode)Parent; if (Content.Count < MaxNumberOfColorsPerPalette) { List <ConcurrentDictionary <Int32, int> > aux = new List <ConcurrentDictionary <int, int> >(); foreach (var p in remainder) { aux.Add(p); } while (aux.Count > 0) { curpal = PaletteProcessor.GetDiffs(Content, aux.First()); aux.RemoveAt(0); cur = 0; if (curpal.Count <= MaxNumberOfColorsPerPalette) { foreach (var p in remainder) { if (PaletteProcessor.CountDiffs(curpal, p) == 0) { cur++; } } if (cur > best) { best = cur; Children.Clear(); } if (cur == best) { newNode = new PaletteGroupNode(); newNode.Content = curpal; Children.Add(newNode); } } } } ConcurrentDictionary <Int32, int> newCont; PaletteGroupNode n; foreach (var node in Children) { newCont = new ConcurrentDictionary <Int32, int>(); n = (PaletteGroupNode)node; n.MaxNumberOfColorsPerPalette = MaxNumberOfColorsPerPalette; foreach (var kvp in n.Content) { newCont.TryAdd(kvp.Key, kvp.Value); } foreach (var kvp in Content) { newCont.TryAdd(kvp.Key, kvp.Value); } n.Content = newCont; n.allColors = new ConcurrentDictionary <int, int>(); foreach (var c in allColors) { if (!n.Content.ContainsKey(c.Key)) { n.allColors.TryAdd(c.Key, c.Value); } } n.FinishedPalettes = new List <ConcurrentDictionary <int, int> >(); foreach (var p in FinishedPalettes) { n.FinishedPalettes.Add(p); } n.remainder = new List <ConcurrentDictionary <int, int> >(); foreach (var p in remainder) { if (PaletteProcessor.CountDiffs(n.Content, p) != 0) { n.remainder.Add(p); } } n.Parent = this; n.updateCost(); n.updateHeuristic(); } if (remainder.Count > 0) { newNode = new PaletteGroupNode(); newNode.MaxNumberOfColorsPerPalette = MaxNumberOfColorsPerPalette; newNode.Parent = this; newNode.Content = remainder.First(); newNode.remainder = new List <ConcurrentDictionary <int, int> >(); newNode.FinishedPalettes = new List <ConcurrentDictionary <int, int> >(); foreach (var p in FinishedPalettes) { newNode.FinishedPalettes.Add(p); } newNode.FinishedPalettes.Add(Content); newNode.allColors = new ConcurrentDictionary <int, int>(); foreach (var c in allColors) { if (!newNode.Content.ContainsKey(c.Key)) { newNode.allColors.TryAdd(c.Key, c.Value); } } foreach (var p in remainder) { if (PaletteProcessor.CountDiffs(newNode.Content, p) != 0) { newNode.remainder.Add(p); } } newNode.updateCost(); newNode.updateHeuristic(); Children.Add(newNode); } if (Children.Count == 0) { FinishedPalettes.Add(Content); } }