private static async Task DoClientWork(IClusterClient client) { var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\Resources\a_example.in")); var solution = new Solution(problem.NumberOfCars); var state = new CityState(problem.Cars.ToImmutableList(), new RidesView3(problem.Rides, problem.Bonus), 0); var tree = client.GetGrain <ITreeGrain <MakeRideAction> >(Guid.NewGuid()); tree.Init(state).Wait(); tree.Build().Wait(); INodeView <MakeRideAction> node; // while ((node = tree.GetTopAction().Result) != null) // { // if (!node.Action.Car.Equals(Car.SkipRide)) // { // solution.CarActions[node.Action.Car.Id].Add(node.Action); // } // // tree.ContinueFrom(node.Id).Wait(); // tree.Build().Wait(); // } Console.WriteLine("Finished"); Console.WriteLine(solution.GetTotalScore(problem.Bonus).ToString()); Console.WriteLine(solution.ToString()); }
static void Main(string[] args) { var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\a_example.in")); var solution = CityProblemSolver.Solve(problem); Console.WriteLine($"a: {solution.GetTotalScore(problem.Bonus).ToString()}"); File.WriteAllText(@"..\..\..\Resources\a_example.out", solution.ToString()); problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\b_should_be_easy.in")); solution = CityProblemSolver.Solve(problem); Console.WriteLine($"b: {solution.GetTotalScore(problem.Bonus).ToString()}"); File.WriteAllText(@"..\..\..\Resources\b_should_be_easy.out", solution.ToString()); problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\c_no_hurry.in")); solution = CityProblemSolver.Solve(problem); Console.WriteLine($"c: {solution.GetTotalScore(problem.Bonus).ToString()}"); File.WriteAllText(@"..\..\..\Resources\c_no_hurry.out", solution.ToString()); problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\d_metropolis.in")); solution = CityProblemSolver.Solve(problem); Console.WriteLine($"d: {solution.GetTotalScore(problem.Bonus).ToString()}"); File.WriteAllText(@"..\..\..\Resources\d_metropolis.out", solution.ToString()); problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\e_high_bonus.in")); solution = CityProblemSolver.Solve(problem); Console.WriteLine($"e: {solution.GetTotalScore(problem.Bonus).ToString()}"); File.WriteAllText(@"..\..\..\Resources\e_high_bonus.out", solution.ToString()); }
public async Task ATest() { var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\a_example.in")); var solution = new Solution(problem.NumberOfCars); var state = new CityState(problem.Cars.ToImmutableList(), new RidesView3(problem.Rides, problem.Bonus), 0); var tree = GrainFactory.GetGrain <ITreeGrain <MakeRideAction> >(Guid.NewGuid()); await tree.Init(state); await tree.Build(); INodeView <MakeRideAction> node; while ((node = await tree.GetTopAction()) != null) { Trace.WriteLine(node.Action); if (!node.Action.Car.Equals(Car.SkipRide)) { solution.CarActions[node.Action.Car.Id].Add(node.Action); } await tree.ContinueFrom(node.Id); await tree.Build(); } Assert.NotNull(solution); Trace.WriteLine("Finished"); Trace.WriteLine(solution.GetTotalScore(problem.Bonus).ToString()); Trace.WriteLine(solution.ToString()); Assert.Equal(10, solution.GetTotalScore(problem.Bonus)); }
public void ZeroConsecutiveSlotAptitudeIsRespected() { const int days = 5; const int slots = 5; const int items = 5; var fromSlot1 = rnd.Next(slots); var toSlot1 = rnd.Next(slots); if (fromSlot1 > toSlot1) { var temp = fromSlot1; fromSlot1 = toSlot1; toSlot1 = temp; } var fromSlot2 = rnd.Next(slots); var toSlot2 = rnd.Next(slots); if (fromSlot2 > toSlot2) { var temp = fromSlot2; fromSlot2 = toSlot2; toSlot2 = temp; } var problem = ProblemBuilder.Configure() .WithDays(days) .WithSlots(slots) .WithItems(items) .Multiplying.AptitudeBy(0).WhenSlots().From(fromSlot1).To(toSlot1).IsFollowedBySlots().From(fromSlot2).To(toSlot2) .Build(); var solvingEnvironment = SolvingEnvironmentBuilder.Configure() .ForProblem(problem) .WithPopulationSize(100) .RenewingPopulationAfterSameFitnessEpochs(10) .StoppingComputationAfter(1).Milliseconds .Build(); var solution = solvingEnvironment.Solve(); for (int day = 0; day < problem.Days - 1; day++) { for (int slot = fromSlot1; slot <= toSlot1; slot++) { if (day < problem.Days - 2) { var chosenItem = solution.Allocations[day, slot]; if (chosenItem.HasValue) { for (int slot2 = fromSlot2; slot2 <= toSlot2; slot2++) { Assert.That(solution.Allocations[day + 1, slot2], Is.Null.Or.Not.EqualTo(chosenItem)); } } } } } }
public CityTSM() { IList <City> cities = new List <City>(); cities.Add(new City("Dresden", 51.050407, 13.737262)); cities.Add(new City("Freiburg", 47.997791, 7.842609)); cities.Add(new City("Grimma", 51.236443, 12.720231)); cities.Add(new City("Gera", 50.884842, 12.079811)); cities.Add(new City("Bremen", 53.073635, 8.806422)); cities.Add(new City("Darmstadt", 49.878708, 8.646927)); cities.Add(new City("Berlin", 52.520008, 13.404954)); cities.Add(new City("Munich", 48.137154, 11.576124)); IProblem <City> problem = new ProblemBuilder <City>() .Locations(cities) .Distances((City c1, City c2) => DistFrom(c1.lat, c1.lon, c2.lat, c2.lon)) .FixedFirstLocation(0) .BuildIntegerArray(); //ISolver<City> solver = new NearestNeighborSolver<City>(problem) ISolver <City> solver = new ParallelBranchBoundSolver <City>(problem); IPath <City> path = solver.Solve(); System.Console.Out.WriteLine(solver.ToString()); System.Console.Out.WriteLine("\tbest=" + path); }
public void UnavailableItemDoesntShift() { const int days = 5; const int slots = 5; const int items = 8; var fromSlot = rnd.Next(slots); var toSlot = rnd.Next(slots); if (fromSlot > toSlot) { var temp = fromSlot; fromSlot = toSlot; toSlot = temp; } var fromDay = rnd.Next(days); var toDay = rnd.Next(days); if (fromDay > toDay) { var temp = fromDay; fromDay = toDay; toDay = temp; } var fromItem = rnd.Next(items); var toItem = rnd.Next(items); if (fromItem > toItem) { var temp = fromItem; fromItem = toItem; toItem = temp; } var problem = ProblemBuilder.Configure() .WithDays(days) .WithSlots(slots) .WithItems(items) .Making.Items().From(fromItem).To(toItem).UnavailableForSlots().From(fromSlot).To(toSlot).InDays().From(fromDay).To(toDay) .Build(); var solvingEnvironment = SolvingEnvironmentBuilder.Configure() .ForProblem(problem) .WithPopulationSize(100) .RenewingPopulationAfterSameFitnessEpochs(10) .StoppingComputationAfter(1).Milliseconds .Build(); var solution = solvingEnvironment.Solve(); for (int day = fromDay; day <= toDay; day++) { for (int slot = fromSlot; slot <= toSlot; slot++) { Assert.That(solution.Allocations[day, slot], Is.Null.Or.Not.InRange(fromItem, toItem)); } } }
public void GetTest() { var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\b_should_be_easy.in")); var sut = new RidesView(problem.Rides); var actual = sut.GetRides(); Trace.WriteLine(sut.GetEarliestFinish()); }
public void CBuildTest() { var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\c_no_hurry.in")); var graph = Graph.Build(problem.Rides); Assert.NotNull(graph); Assert.Equal(10000, graph.Vertices.Count); Trace.WriteLine(graph.Edges.Count); }
public void CTest() { var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\c_no_hurry.in")); var solution = CityProblemSolver.Solve(problem); Assert.NotNull(solution); Trace.WriteLine(solution.GetTotalScore(problem.Bonus).ToString()); Trace.WriteLine(solution.ToString()); }
public void DTest() { var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\d_metropolis.in")); var solution = GreedyProblemSolver.Solve(problem); Assert.NotNull(solution); Trace.WriteLine(solution.GetTotalScore(problem.Bonus).ToString()); Trace.WriteLine(solution.ToString()); }
public void BTest() { var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\b_should_be_easy.in")); var solution = IndependentSetProblemSolver.Solve(problem); Assert.NotNull(solution); Trace.WriteLine(solution.GetTotalScore(problem.Bonus).ToString()); Trace.WriteLine($"Missed rides: {string.Join(" ", solution.GetMissedRides())}"); Trace.WriteLine(solution.ToString()); }
public void ATest() { var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\a_example.in")); var solution = IndependentSetProblemSolver.Solve(problem); Assert.NotNull(solution); Trace.WriteLine("Finished"); Trace.WriteLine(solution.GetTotalScore(problem.Bonus).ToString()); Trace.WriteLine(solution.ToString()); Assert.Equal(10, solution.GetTotalScore(problem.Bonus)); }
public async Task SiloSayHelloTest() { var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\a_example.in")); var state = new CityState(problem.Cars.ToImmutableList(), new RidesView3(problem.Rides, problem.Bonus), 0); var grain = GrainFactory.GetGrain <ITreeGrain <MakeRideAction> >(Guid.NewGuid()); await grain.Init(state); await grain.Build(); var bestNode = await grain.GetTopAction(); Assert.NotNull(bestNode); Trace.WriteLine(bestNode.Action); }
public void ClosedSlotsAreEmpty() { const int days = 5; const int slots = 5; const int items = 8; var fromSlot = rnd.Next(slots); var toSlot = rnd.Next(slots); if (fromSlot > toSlot) { var temp = fromSlot; fromSlot = toSlot; toSlot = temp; } var fromDay = rnd.Next(days); var toDay = rnd.Next(days); if (fromDay > toDay) { var temp = fromDay; fromDay = toDay; toDay = temp; } var problem = ProblemBuilder.Configure() .WithDays(days) .WithSlots(slots) .WithItems(items) .Closing.Slots().From(fromSlot).To(toSlot).InDays().From(fromDay).To(toDay) .Build(); var solvingEnvironment = SolvingEnvironmentBuilder.Configure() .ForProblem(problem) .WithPopulationSize(100) .RenewingPopulationAfterSameFitnessEpochs(10) .StoppingComputationAfter(1).Milliseconds .Build(); var solution = solvingEnvironment.Solve(); for (int day = fromDay; day <= toDay; day++) { for (int slot = fromSlot; slot <= toSlot; slot++) { Assert.That(solution.Allocations[day, slot], Is.Null); } } }
public void GetAvailableActionsTest() { var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\b_should_be_easy.in")); var availableRides = new HashSet <Ride>(); availableRides.Add(new Ride(0, new Point(), new Point(10, 10), 0, 110)); availableRides.Add(new Ride(1, new Point(1, 1), new Point(10, 10), 0, 110)); availableRides.Add(new Ride(2, new Point(2, 2), new Point(3, 3), 0, 110)); var sut = new CityCarState(problem, new Car(0, new Point(), 0), availableRides, 0); var actual = sut.GetAvailableActions(); foreach (var makeRideAction in actual) { Trace.WriteLine(makeRideAction); } }
public void ABuildTest() { var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\a_example.in")); var graph = Graph.Build(problem.Rides); Assert.NotNull(graph); Assert.Equal(3, graph.Vertices.Count); foreach (var vertex in graph.Vertices) { Trace.WriteLine(vertex); } foreach (var edge in graph.Edges) { Trace.WriteLine(edge); } }
public void GetAvailableActionsTest() { var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\b_should_be_easy.in")); var availableRides = new List <Ride> { new Ride(0, new Point(), new Point(10, 10), 0, 110), new Ride(1, new Point(1, 1), new Point(10, 10), 0, 110), new Ride(2, new Point(2, 2), new Point(3, 3), 0, 110) }; var cars = new List <Car> { new Car(0, new Point(), 0), new Car(1, new Point(), 0), }; var sut = new CityState(cars.ToImmutableList(), new RidesView3(availableRides, problem.Bonus), 0); var actual = sut.GetAvailableActions(); foreach (var makeRideAction in actual) { Trace.WriteLine(makeRideAction); } }
public void Run(string filename) { Dictionary <string, Pin> startPins = new Dictionary <string, Pin>(); Dictionary <string, List <Pin> > pinMap = new Dictionary <string, List <Pin> >(); FileInfo file = new FileInfo(filename); using (ExcelPackage excel = new ExcelPackage(file)) { //foreach (ExcelWorksheet worksheet in excel.Workbook.Worksheets.) ExcelWorksheet worksheet = excel.Workbook.Worksheets["Datensatz1"]; { for (int l = 2; l <= worksheet.Dimension.End.Row; l++) { double xPos = GetValue(worksheet.Cells[l, 1].Value); double yPos = GetValue(worksheet.Cells[l, 2].Value); double zPos = GetValue(worksheet.Cells[l, 3].Value); string pinType = (string)worksheet.Cells[l, 4].Value; string circuit = (string)worksheet.Cells[l, 5].Value; Pin pin = new Pin(pinType, circuit, xPos, yPos, zPos); Match match = Regex.Match(circuit, "(.*) (F\\d+)", RegexOptions.IgnoreCase); if (match.Success) { string parentCircuit = match.Groups[1].Value; List <Pin> pins = null; if (pinMap.ContainsKey(circuit)) { pins = pinMap[circuit]; } else { pins = new List <Pin>(); pinMap.Add(circuit, pins); } pins.Add(pin); } else { startPins.Add(circuit, pin); } Debug.WriteLine(circuit + " - " + xPos + ":" + yPos + ":" + zPos); } } } foreach (KeyValuePair <string, List <Pin> > entry in pinMap) { string circuit = entry.Key; List <Pin> pins = entry.Value; Pin startPin = null; Match match = Regex.Match(circuit, "(.*) (F\\d+)", RegexOptions.IgnoreCase); if (match.Success) { string key = match.Groups[1].Value; startPin = startPins[match.Groups[1].Value]; } IProblem <Pin> problem = new ProblemBuilder <Pin>() .Locations(pins) .Distances((Pin p1, Pin p2) => CalcDistance(p1, p2)) .FixedFirstLocation(startPin) .BuildIntegerArray(); ISolver <Pin> solverNN = new NearestNeighborSolver <Pin>(problem); ISolver <Pin> solverPBB = new ParallelBranchBoundSolver <Pin>(problem); Stopwatch stopWatch = new Stopwatch(); System.Console.Out.WriteLine("Circuit: " + circuit); System.Console.Out.WriteLine("\tNN = " + solverNN.Solve()); stopWatch.Start(); System.Console.Out.WriteLine("\tPBB= " + solverPBB.Solve()); stopWatch.Stop(); System.Console.Out.WriteLine("\t\t time = " + stopWatch.Elapsed); } }
private static void Main(string[] args) { log4net.Config.BasicConfigurator.Configure(); //log4net.Config.XmlConfigurator.Configure(); var problem = ProblemBuilder.Configure() .WithDays(30) .WithSlots(14) .WithItems(18) .WithMaxConsecutiveWorkingDaysEqualTo(5) .RestAfterMaxWorkingDaysReached(2) //.Making.Item(0).UnavailableForSlots().From(10).To(13).Always() //.Making.Items().From(0).To(4) // .UnavailableForSlots().From(10).To(13) // .InDays().From(3).To(6) //.AssigningAptitude(5).ToItem(0).ForSlots().From(0).To(4).Always() .AssigningLength(2).ToSlots().From(10).To(13) //.AssigningWeight(1 / 7 * 10).ToSlots().From(10).To(13) //.AssigningValue(4).ToSlots().From(0).To(3) //.AssigningValue(1).ToSlot(4) //.AssigningValue(4).ToSlots().From(5).To(8) //.AssigningValue(0.25F).ToSlot(9) //.AssigningValue(10).ToSlots().From(10).To(13) //.Multiplying.AptitudeBy(0.05F).WhenSlots().From(5).To(9).IsFollowedBySlots().From(0).To(4) //.Multiplying.AptitudeBy(1.5F).WhenSlots().From(10).To(13).IsFollowedBySlots().From(0).To(4) //.Making.AllItems().UnavailableForAllSlots().InDay(10) //.AssigningAptitude(0).ToItem(1).ForSlots().From(6).To(12).InDays().From(14).To(29) //.AssigningAptitude(0).ToItem(2).ForSlots().From(7).To(13).InDays().From(0).To(19) //.AssigningAptitude(0).ToItem(3).ForSlots().From(8).To(12).InDays().From(5).To(20) //.AssigningAptitude(0).ToItem(4).ForSlots().From(9).To(11).InDays().From(14).To(20) //.AssigningValue(2).ToSlot(2) //.AssigningValue(2).ToSlot(6) //.AssigningWeight(2.1F).ToSlot(7) //.AssigningWeight(2.5F).ToSlot(8) .Closing.Slots().From(8).To(9).InDays().From(5).To(6) .Closing.Slots().From(8).To(9).InDays().From(12).To(13) .Closing.Slots().From(8).To(9).InDays().From(19).To(20) .Closing.Slots().From(8).To(9).InDays().From(26).To(27) //.Closing.AllSlots().InDay(4) //.ConsideringThat.WhenItem(2).CoversSlot(3).AptitudeIsMultipliedBy(2).ForItem(3).CoveringSlot(5) //.ConsideringThat.WhenItem(3).CoversSlot(4).AptitudeIsMultipliedBy(3).ForItem(4).CoveringSlot(4) //.ConsideringThat.WhenItem(4).CoversSlot(5).AptitudeIsMultipliedBy(3).ForItem(5).CoveringSlot(3) //.ConsideringThat.WhenItem(5).CoversSlot(6).AptitudeIsMultipliedBy(3).ForItem(6).CoveringSlot(2) //.ConsideringThat.WhenItem(6).CoversSlot(7).AptitudeIsMultipliedBy(2).ForItem(7).CoveringSlot(1) //.ConsideringThat.WhenItem(7).CoversSlot(8).AptitudeIsMultipliedBy(2).ForItem(8).CoveringSlot(0) //.Making.Items().From(4).To(10).UnavailableForSlots().From(2).To(6).InDays().From(2).To(25) .Build(); var solvingEnvironment = SolvingEnvironmentBuilder.Configure() .ForProblem(problem) .WithPopulationSize(100) //.RenewingPopulationAfterEpochs(0) .RenewingPopulationAfterSameFitnessEpochs(10) .StoppingComputationAfter(2).Minutes //.UsingExactlyANumberOfThreadsEqualTo(4) //.UsingExactlyANumberOfThreadsEqualTo(1) .Build(); var solution = solvingEnvironment.Solve(); Console.WriteLine(solution); Console.ReadLine(); }
static void Main(string[] args) { Trace.Listeners.Add(new ConsoleTraceListener()); // First, configure and start a local silo var siloConfig = ClusterConfiguration.LocalhostPrimarySilo(); siloConfig.Globals.ResponseTimeout = TimeSpan.FromMinutes(10); var silo = new SiloHost("TestSilo", siloConfig); silo.InitializeOrleansSilo(); silo.StartOrleansSilo(); Console.WriteLine("Silo started."); // Then configure and connect a client. var clientConfig = ClientConfiguration.LocalhostSilo(); clientConfig.ResponseTimeout = TimeSpan.FromMinutes(10); var client = new ClientBuilder().UseConfiguration(clientConfig).Build(); client.Connect().Wait(); Console.WriteLine("Client connected."); // // This is the place for your test code. // var problem = ProblemBuilder.Build(File.ReadAllLines(@"..\..\..\Resources\b_should_be_easy.in")); var solution = new Solution(problem.NumberOfCars); var state = new CityState(problem.Cars.ToImmutableList(), new RidesView3(problem.Rides, problem.Bonus), 0); var tree = client.GetGrain <ITreeGrain <MakeRideAction> >(Guid.NewGuid()); tree.Init(state).Wait(); tree.Build().Wait(); var counter = 0; INodeView <MakeRideAction> node; while ((node = tree.GetTopAction().Result) != null) { //Trace.WriteLine(node.Action); if (!node.Action.Car.Equals(Car.SkipRide)) { solution.CarActions[node.Action.Car.Id].Add(node.Action); } if (counter++ % 20 == 0) { Trace.WriteLine($"Counter: {counter}"); Trace.WriteLine($"Current score: {solution.GetTotalScore(problem.Bonus).ToString()}"); } tree.ContinueFrom(node.Id).Wait(); tree.Build().Wait(); } Console.WriteLine("Finished"); Console.WriteLine(solution.GetTotalScore(problem.Bonus).ToString()); Console.WriteLine(solution.ToString()); Console.WriteLine("\nPress Enter to terminate..."); Console.ReadLine(); // Shut down client.Close(); silo.ShutdownOrleansSilo(); }