private SCPSet CreateNewVirtualSet(List <Panel> list) { SCPSet candidate = new SCPSet(); candidate.Tag = 0; list.ForEach(i => { if (candidate.Parents == null) { candidate.Parents = new List <int>(); candidate.Parents.Add(i.SetTag); candidate.Cost += i.SetCost; } else if (candidate.Parents.Contains(i.SetTag) == false) //Don`t add the same set more than twice and don`t cost the same set more than once { candidate.Parents.Add(i.SetTag); candidate.Cost += i.SetCost; } SCPAttribute attribute = new SCPAttribute(); attribute.Tag = i.AttributeTag; attribute.Cost = i.AttributeCost; candidate.Attributes.Add(attribute); }); return(candidate); }
private void DataBinding(bool initialize, int attribte, int size, System.IO.StreamReader file) { if (attribte > _problem.Matrix.Size.X) { return; } string str = ""; SCPAttribute att = null; for (int index = 0; index < size;) { int read = file.Read(); if (read == 32 || read == 10) { if (str != "") { if (initialize) { DataBinding(false, ++attribte, int.Parse(str), file); } else { if (att == null) { att = new SCPAttribute(); _problem.Source.Attributes.Add(att); att.Tag = attribte; } att.UsedIn.Add(_problem.Source.Sets[int.Parse(str) - 1]); _problem.Matrix.Write(1, att.Tag - 1, int.Parse(str) - 1); _problem.Source.Sets[int.Parse(str) - 1].Attributes.Add(att); _problem.Source.Sets[int.Parse(str) - 1].Frequency++; index++; } str = ""; } continue; } else if (read == -1) { break; //End of file } else { str += (read - 48); } } initialize = true; }
public double Execute(IProblem problem) { sp.Restart(); _problem = (SCP)problem; int coveredatt, setCount, attCount; coveredatt = 0; List <SCPSet> sets = _problem.Source.Sets; List <SCPAttribute> attributes = _problem.Source.Attributes; setCount = sets.Count; attCount = attributes.Count; double c_min = sets.Min(a => a.Cost); while (coveredatt < attCount) { double e_min = 0, e_max = 0, e_limit = 0; SCPSet k; List <SCPSet> rcl = new List <SCPSet>(); for (int i = 0; i < setCount; i++) { if (sets[i].Frequency > 0) { sets[i].Weight = sets[i].Frequency / (1 + sets[i].Cost - c_min); if (i > 0) { if (sets[i].Weight < e_min) { e_min = sets[i].Weight; } if (sets[i].Weight > e_max) { e_max = sets[i].Weight; } } else { e_min = e_max = sets[i].Weight; } } } e_limit = e_min + _alpha * (e_max - e_min); for (int i = 0; i < setCount; i++) { if (sets[i].Frequency > 0 && sets[i].Weight + _epsilon >= e_limit) { rcl.Add(sets[i]); } } if (rcl.Count == 0) { return(0); } k = rcl[new Random().Next(0, rcl.Count) % rcl.Count]; _solution.Add(k); _totalCost += k.Cost; k.Attributes.ForEach(a1 => { SCPAttribute attribute = _problem.Source.Attributes.Find(a2 => a2.Tag == a1.Tag); if (attribute != null) { if (attribute.Visit == false) { coveredatt++; attribute.UsedIn.ForEach(s1 => { s1.Frequency--; }); } attribute.Visit = true; } }); } sp.Stop(); _problem.Solution = new SCPSolution() { Sets = _solution }; return(_totalCost); }