コード例 #1
0
        public RandomStartGeneticAlgorithm(ProblemInstance problemInstance, int phenotypeCount, int iterationCount, 
		                                   int secondCount)
        {
            this.randomizationLevel = 10;
            this.problemInstance = problemInstance;
            this.phenotypeCount = phenotypeCount;
            this.iterationCount = iterationCount;
            this.secondCount = secondCount;
            random = new Random();
        }
コード例 #2
0
 public ProblemController(ProblemInstance instance)
 {
     this.Instance = instance;
     this.ArchivedCarData = new List<Car>();
     random = new Random(ProblemInstance.Seed);
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: Elthiryel/SAO-project
 static void Main(string[] args)
 {
     // TESTING PARSER
     ProblemInstance pi = new ProblemInstance();
     InputParser.FillRoadsAndCrossroads(pi, "test.txt");
     InputParser.FillRoutes(pi, "routes.xml");
     /*
     foreach (Road r in pi.Roads)
     {
         Console.Write("ROAD " + r.Id + ": " + r.Length);
         if (r.First != null)
         {
             Console.Write("; first: " + r.First.X + ", " + r.First.Y);
         }
         if (r.Second != null)
         {
             Console.Write("; second: " + r.Second.X + ", " + r.Second.Y);
         }
         Console.Write( "; lanes: " + r.IncreasingLaneCount + ", " + r.DecreasingLaneCount);
         Console.Write( "; orientation: " + r.Orientation);
         Console.WriteLine();
     }
     foreach (Crossroad c in pi.Crossroads)
     {
         Console.Write("CROSSROAD " + c.Id + ": " + c.X + ", " + c.Y);
         if (c.North != null)
         {
             Console.Write("; north: " + c.North.Id);
         }
         if (c.South != null)
         {
             Console.Write("; south: " + c.South.Id);
         }
         if (c.West != null)
         {
             Console.Write("; west: " + c.West.Id);
         }
         if (c.East != null)
         {
             Console.Write("; east: " + c.East.Id);
         }
         Console.WriteLine();
     }*/
     /*ProblemController controller = new ProblemController(pi);
     Dictionary<int,TrafficLights> lightsConfig = new Dictionary<int, TrafficLights>();
     foreach (var crossroad in pi.Crossroads)
     {
         TrafficLights lights = new TrafficLights(10,30,6);
         lightsConfig.Add(crossroad.Id,lights);
     }
     controller.SetTrafficLightsConfiguration(lightsConfig);
     controller.Start(10000);
     var result = controller.ComputeResult();
     Console.WriteLine("Average route time in seconds: "+result);
     Console.WriteLine(String.Format("Total number of cars: {0}",controller.ArchivedCarData.Count));
     Dictionary<Route, int> carCount;
     var dict = controller.ComputeEachRoute(out carCount);
     foreach (var route in dict.Keys)
     {
         Console.WriteLine("Average route time in seconds: " + dict[route] + " for Route number "+ route.Id +
                           " (" + carCount[route] + " cars)");
     }
     Console.ReadKey(); */
     //var algorithm = new RandomStartGeneticAlgorithm(pi, 30, 30, 10000);
     var algorithm = new RandomStartAdaptiveGeneticAlgorithm(pi, 10, 50, 10000);
     algorithm.Run();
     var result = algorithm.GetResult();
     Console.WriteLine("END, result: " + result);
 }
コード例 #4
0
        public RandomStartAdaptiveGeneticAlgorithm(ProblemInstance problemInstance, int phenotypeCount, int iterationCount, 
		                                           int secondCount)
            : base(problemInstance, phenotypeCount, iterationCount, secondCount)
        {
            this.stuck = 0;
        }
コード例 #5
0
ファイル: InputParser.cs プロジェクト: Elthiryel/SAO-project
        public static void FillRoadsAndCrossroads(ProblemInstance problemInstance, String inputFileName)
        {
            try
            {
                var reader = File.OpenText(inputFileName);

                var ySize = Convert.ToInt32(reader.ReadLine());
                var xSize = Convert.ToInt32(reader.ReadLine());
                var tileLength = Convert.ToInt32(reader.ReadLine());

                char[,] tiles = new char[ySize, xSize];
                var crossroadsDict = new Dictionary<Coordinates, Crossroad>();
                var roadsList = new List<Road>();

                for (var i = 0; i < ySize; ++i)
                {
                    var line = reader.ReadLine();
                    for (var j = 0; j < xSize; ++j)
                    {
                        var tile = line[j];
                        tiles[i, j] = tile;
                        if (tile == 'X')
                        {
                            crossroadsDict.Add(new Coordinates(i, j), new Crossroad(i * tileLength, j * tileLength));
                        }
                    }
                }

                var coordinates = crossroadsDict.Keys;

                foreach (Coordinates c in coordinates)
                {
                    Crossroad crossroad = crossroadsDict[c];

                    if (c.Y < ySize - 1)
                    {
                        int lanes = 1;
                        int iterY = c.Y + 1;
                        while (true)
                        {
                            if (iterY >= ySize)
                            {
                                var road = new Road((iterY - c.Y - 1) * tileLength, crossroad, null, lanes, Orientation.NorthSouth);
                                crossroad.South = road;
                                roadsList.Add(road);
                                break;
                            }
                            char tile = tiles[iterY, c.X];
                            if (tile == '-')
                            {
                                ++iterY;
                            }
                            else if (tile == '=')
                            {
                                ++iterY;
                                lanes = 2;
                            }
                            else if (tile == 'X')
                            {
                                var endCrossroad = crossroadsDict[new Coordinates(iterY, c.X)];
                                var road = new Road((iterY - c.Y) * tileLength, crossroad, endCrossroad, lanes, Orientation.NorthSouth);
                                crossroad.South = road;
                                endCrossroad.North = road;
                                roadsList.Add(road);
                                break;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (c.X < xSize - 1)
                    {
                        int lanes = 1;
                        int iterX = c.X + 1;
                        while (true)
                        {
                            if (iterX >= xSize)
                            {
                                var road = new Road((iterX - c.X - 1) * tileLength, crossroad, null, lanes, Orientation.EastWest);
                                crossroad.East = road;
                                roadsList.Add(road);
                                break;
                            }
                            char tile = tiles[c.Y, iterX];
                            if (tile == '-')
                            {
                                ++iterX;
                            }
                            else if (tile == '=')
                            {
                                ++iterX;
                                lanes = 2;
                            }
                            else if (tile == 'X')
                            {
                                var endCrossroad = crossroadsDict[new Coordinates(c.Y, iterX)];
                                var road = new Road((iterX - c.X) * tileLength, crossroad, endCrossroad, lanes, Orientation.EastWest);
                                crossroad.East = road;
                                endCrossroad.West = road;
                                roadsList.Add(road);
                                break;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }

                for (int i = 0; i < ySize; ++i)
                {
                    int lanes = 1;
                    int iterX = 0;
                    while (true) {
                        char tile = tiles[i, iterX];
                        if (tile == '-')
                        {
                            ++iterX;
                        }
                        else if (tile == '=')
                        {
                            ++iterX;
                            lanes = 2;
                        }
                        else if (tile == 'X')
                        {
                            if (iterX == 0)
                            {
                                break;
                            }
                            var endCrossroad = crossroadsDict[new Coordinates(i, iterX)];
                            var road = new Road(iterX * tileLength, null, endCrossroad, lanes, Orientation.EastWest);
                            endCrossroad.West = road;
                            roadsList.Add(road);
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                for (int i = 0; i < xSize; ++i)
                {
                    int lanes = 1;
                    int iterY = 0;
                    while (true)
                    {
                        char tile = tiles[iterY, i];
                        if (tile == '-')
                        {
                            ++iterY;
                        }
                        else if (tile == '=')
                        {
                            ++iterY;
                            lanes = 2;
                        }
                        else if (tile == 'X')
                        {
                            if (iterY == 0)
                            {
                                break;
                            }
                            var endCrossroad = crossroadsDict[new Coordinates(iterY, i)];
                            var road = new Road(iterY * tileLength, null, endCrossroad, lanes, Orientation.NorthSouth);
                            endCrossroad.North = road;
                            roadsList.Add(road);
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                problemInstance.Crossroads = new List<Crossroad>(crossroadsDict.Values);
                problemInstance.Roads = roadsList;
                problemInstance.Routes = new List<Route>();
            }

            catch (Exception e)
            {
                Console.Out.Write(e.StackTrace);
            }
        }
コード例 #6
0
ファイル: InputParser.cs プロジェクト: Elthiryel/SAO-project
        public static void FillRoutes(ProblemInstance problemInstance, String inputFileName)
        {
            var serializer = new XmlSerializer(typeof(RoutesList));

            using (var reader = new StreamReader(inputFileName))
            {
                var routesSerialized = (RoutesList)serializer.Deserialize(reader);
                foreach (var route in routesSerialized.Routes)
                {
                    List<Road> roads = new List<Road>();
                    var currentDirection = Direction.NotSet;
                    Crossroad currentCrossroad = null;
                    foreach (var crossroads in route.Crossroads)
                    {
                        if (crossroads.Direction != Direction.NotSet)
                        {
                            currentDirection = crossroads.Direction;
                        }
                        else
                        {
                            var nextCrossroad = problemInstance.Crossroads[crossroads.Crossroad - 1];
                            if (currentCrossroad != null)
                            {
                                roads.AddRange(from road in currentCrossroad.Roads
                                    from _road in nextCrossroad.Roads
                                    where road.Id == _road.Id
                                    select road);
                            }
                            else
                            {
                                AddRoad(roads, currentDirection, nextCrossroad);
                                currentDirection = Direction.NotSet;
                            }
                            currentCrossroad = problemInstance.Crossroads[crossroads.Crossroad-1];
                        }
                    }
                    if (currentDirection != Direction.NotSet)
                    {
                        AddRoad(roads, currentDirection, currentCrossroad);
                    }
                    problemInstance.Routes.Add(new Route(roads, route.Rate, route.Id));
                }
            }
        }