public void RandomPointInRange() { var rnd = new Random(); for (int i = 0; i < 10; i++) { Point p1 = GeometricSolver.GetRandomPointInRange(400, 400, rnd); Assert.IsTrue(p1.X < 400 && p1.X > 0); Assert.IsTrue(p1.Y < 400 && p1.Y > 0); } }
/// <summary> /// Создание новой хромосомы по образцу, с случайным разположением модулей /// </summary> public Chromosome() { Age = 0; Valuation = int.MaxValue; PrintCircuitBoard = new PrintedCircuitBoard(ExamplePrintedCircuitBoard); var rnd = new Random(); foreach (var module in PrintCircuitBoard.Modules) { module.Position = GeometricSolver.GetRandomPointInRange(WorkspaceWidth, WorkspaceHeight, rnd) + LeftUpperPoint; if (!module.IsLocked()) { var angle = rnd.Next(0, 4) * Angle; RotateModule(module, angle); } } PrintCircuitBoard.LeadToCorrectForm(PrintCircuitBoard.Modules); }
/// <summary> /// Приводит плату к корректному виду без пересечений модулей /// </summary> /// <param name="modules"> Список модулей которые нужно проверить на пересечение</param> public void LeadToCorrectForm(List <Module> modules) { int swapCount = 0; var notVisited = modules.ToList(); foreach (var e in notVisited) { if (!Modules.Contains(e)) { throw new Exception("Invalid arguments for lead to correct form"); } } var rnd = new Random(); while (true) { if (notVisited.Count == 0) { break; } var curModule = notVisited[notVisited.Count - 1]; notVisited.Remove(curModule); foreach (var module in Modules) { if (curModule.Equals(module)) { continue; } if (AreModulesIntersect(curModule, module)) { swapCount++; if (module.IsLocked()) { DivideModules(module, curModule); } else { DivideModules(curModule, module); } if (!notVisited.Contains(module)) { notVisited.Add(module); } if (!GeometricSolver.AreModuleInBounds(module.LeftUpperBound + module.Position, module.RighLowerBound + module.Position, Chromosome.LeftUpperPoint, new Point(Chromosome.LeftUpperPoint.X + Chromosome.WorkspaceWidth, Chromosome.LeftUpperPoint.Y + Chromosome.WorkspaceHeight))) { module.Position = GeometricSolver.GetRandomPointInRange(Chromosome.WorkspaceWidth, Chromosome.WorkspaceHeight, rnd) + Chromosome.LeftUpperPoint; } } } if (swapCount > 500) { break; } } }