void FixErrors(Person type) { /* * Fix errors by: * 1. detect if a person appear in two triplets * 2. in the second triplet, change its index to a one not used */ SortedSet <int> indexes = new SortedSet <int>(); Dictionary <int, int> errors = new Dictionary <int, int>(); for (int i = 0; i < m_capacity; i++) { triple_t triple = m_triples[i]; if (indexes.Contains(triple.Get(type))) { Debug.Assert(!errors.ContainsKey(triple.Get(type))); errors[triple.Get(type)] = i; } else { indexes.Add(triple.Get(type)); } } foreach (KeyValuePair <int, int> pair in errors) { // find first unused index int unused = 0; while (indexes.Contains(unused)) { unused++; } indexes.Add(unused); Debug.Assert(unused < m_capacity); triple_t triple = m_triples[pair.Value]; Debug.Assert(triple.Get(type) == pair.Key); triple.Set(type, unused); } }
public void Randomize() { m_triples.Clear(); HashSet <int> menSet = new HashSet <int>(); HashSet <int> womenSet = new HashSet <int>(); HashSet <int> dogsSet = new HashSet <int>(); int imen = -1, iwomen = -1, idog = -1; for (int i = 0; i < m_capacity; i++) { // randomize triple do { imen = (int)(GA.NextRandom * m_capacity); }while (menSet.Contains(imen)); do { iwomen = (int)(GA.NextRandom * m_capacity); }while (womenSet.Contains(iwomen)); do { idog = (int)(GA.NextRandom * m_capacity); }while (dogsSet.Contains(idog)); menSet.Add(imen); womenSet.Add(iwomen); dogsSet.Add(idog); triple_t triple = new triple_t(); triple.Men = imen; triple.Women = iwomen; triple.Dog = idog; m_triples.Add(triple); } }