// brute force path optimization. This should be rewritten with some nice treesearching algo.. but this is fast enough to "not matter" for now. public void Optimize() { double BeforeTotal = PathLength(); Random R = new Random(); for (int i = 0; i < 100000; i++) { int idx1 = R.Next(Shapes.Count() - 3); int idx2 = R.Next(Shapes.Count() - 3); if (idx1 != idx2) { if (idx2 < idx1) { int t = idx2; idx2 = idx1; idx1 = t; } if (idx2 > idx1) { int idx1b = idx1 + 1; int idx2b = idx2 + 1; int idx1c = idx1b + 1; int idx2c = idx2b + 1; PolyLine Aa = Shapes[idx1]; PolyLine Ab = Shapes[idx1b]; PolyLine Ac = Shapes[idx1c]; PolyLine Ba = Shapes[idx2]; PolyLine Bb = Shapes[idx2b]; PolyLine Bc = Shapes[idx2c]; double before = Helpers.DistanceSq(Aa.end(), Ab.start()) + Helpers.DistanceSq(Ab.end(), Ac.start()) + Helpers.DistanceSq(Ba.end(), Bb.start()) + Helpers.DistanceSq(Bb.end(), Bc.start()) ; double after = Helpers.DistanceSq(Ba.end(), Ab.start()) + Helpers.DistanceSq(Ab.end(), Bc.start()) + Helpers.DistanceSq(Aa.end(), Bb.start()) + Helpers.DistanceSq(Bb.end(), Ac.start()) ; if (after < before) { Shapes[idx1b] = Bb; Shapes[idx2b] = Ab; } } } } double AfterTotal = PathLength(); //Console.WriteLine("Optimized path - before: {0}, after: {1}", BeforeTotal, AfterTotal); }
public override void Step(IEnumerable <object> data) { base.Step(data); // if (Shapes.Count(a => !(a is Stroller || a is Tracker)) == 0) { FeedMap(Extensions.Random.Next(10) + 5); } // CalculateCollisions(); }