private async void solveButton_Click(object sender, EventArgs e) { ingredientsView.Enabled = false; outputView.Enabled = false; solveButton.Enabled = false; expandCheckbox.Enabled = false; try { var grader = new SolutionGrader(); var pipe = _library.Buildings.Where((b) => b.Name == "pipe").First(); var pipeToGround = _library.Buildings.Where((b) => b.Name == "pipe-to-ground").First(); var router = new FluidRouter(pipeToGround, pipe, grader); var belt = _library.Buildings.Where((b) => b.Name == "basic-transport-belt").First(); var beltGroundNormal = _library.Buildings.Where((b) => b.Name == "basic-transport-belt-to-ground").First(); var beltGroundFast = _library.Buildings.Where((b) => b.Name == "fast-transport-belt-to-ground").First(); var beltGroundExpress = _library.Buildings.Where((b) => b.Name == "express-transport-belt-to-ground").First(); var fastInserter = _library.Buildings.Where((b) => b.Name == "fast-inserter").First(); var inserter = _library.Buildings.Where((b) => b.Name == "basic-inserter").First(); var longInserter = _library.Buildings.Where((b) => b.Name == "long-handed-inserter").First(); var splitter = _library.Buildings.Where((b) => b.Name == "basic-splitter").First(); var solid = new SolidRouter(belt, beltGroundNormal, beltGroundFast, beltGroundExpress, inserter, longInserter, fastInserter, splitter, grader); _generator = new SolutionGenerator(_recipe); _generator.SolidRouter = solid; _generator.FluidRouter = router; } catch (Exception ex) { resultView.Text = ex.Message; } ComputationLoop(); while (true) { await Task.Delay(1000); bestCostLabel.Text = "Cost: " + _generator.LowestCost.ToString(); if (_generator.BestState != null) { solutionImage.Image = _generator.BestState.Draw(); } if (_generator.Preview != null) { previewImage.Image = _generator.Preview.Draw(); } } }
public double CalculateCost(Searchspace space, SolutionGrader grader) { var cost = base.CalculateCost(space, grader); var pipeBuilding = Building; BuildingRotation[] rotations = new BuildingRotation[] { BuildingRotation.North, BuildingRotation.East, BuildingRotation.South, BuildingRotation.West }; foreach (var rotation in rotations) { var neighbors = space.CalculateCollisions(Position + rotation.ToVector()); var misMatch = neighbors.Where((b) => b.Building == pipeBuilding && b is FlowBuilding).Cast <FlowBuilding>() .Where((f) => f.Item.Item != Item.Item); cost += misMatch.Count() * grader.TouchLeakCost; } return(cost); }
public void UndergroundCollisions() { var item = new Item("test"); var lib = new Library(); lib.AddItem(item); lib.Initialize(); var flow = new UndergroundFlow(new ItemAmount(item, 1), Vector2.One, Depth.Normal, BuildingRotation.South); var grader = new SolutionGrader(); var space = new Searchspace(new Vector2(4, 4)); space = space.AddRoute(flow); Assert.AreEqual(0, flow.CalculateCost(space, grader)); var flow2 = new UndergroundFlow(new ItemAmount(item, 1), Vector2.One, Depth.Fast, BuildingRotation.South); var newSpace = space.AddRoute(flow2); Assert.AreEqual(0, flow.CalculateCost(newSpace, grader)); flow2 = new UndergroundFlow(new ItemAmount(item, 1), Vector2.One, Depth.Normal, BuildingRotation.South); newSpace = space.AddRoute(flow2); Assert.IsTrue(flow.CalculateCost(newSpace, grader) > 0); flow2 = new UndergroundFlow(new ItemAmount(item, 1), Vector2.One, Depth.Normal, BuildingRotation.North); newSpace = space.AddRoute(flow2); Assert.IsTrue(flow.CalculateCost(newSpace, grader) > 0); flow2 = new UndergroundFlow(new ItemAmount(item, 1), Vector2.One, Depth.Normal, BuildingRotation.East); newSpace = space.AddRoute(flow2); Assert.AreEqual(0, flow.CalculateCost(newSpace, grader)); var building = new PhysicalFlowBuilding(new ItemAmount(item, 1), new Building("test1"), Vector2.One, BuildingRotation.North); newSpace = space.AddRoute(building); Assert.AreEqual(0, flow.CalculateCost(newSpace, grader)); }