public Dictionary <int, InstructionsHelper> GenerateData(CargoShipCollection ships, ref ContainersCollection containers) { this._ships = ships; this._containers = containers; Dictionary <int, InstructionsHelper> Solution = new Dictionary <int, InstructionsHelper>(); InitialFill(); log.Debug("Initial fill"); for (var i = 0; i < cycles; ++i) { EvaluateSpecimens(); Crossover(); } log.Debug("Mutation and crossover done"); Specimen bestSpec = _specimens[0]; for (var i = 0; i < _ships.Count; ++i) { Solution.Add(i, bestSpec._shipCargo[i]); } log.Debug("Solution created"); return(Solution); }
public MainWindow() { #if !MINIMUM_MODE InitializeComponent(); DataContext = this; #endif _cargoShips = new CargoShipCollection(shipDataGrid); SeriesCollection = new SeriesCollection(); _containers = new ContainersCollection(); Tools.ContainerGenerator.Generate(); _containers.LoadCsv("DataInputGroupPT1440.csv"); _availableContainers = _containers.GetAvailable(_turn); for (int i = 0; i < 5; i++) { _cargoShips.Add(32, 30); } _cargoShips.DataGenStrategy = new IterativeStrategy(); #if MINIMUM_MODE Hide(); _ss = new SummaryScreen(); _ss.Show(); _cargoShips.DataGenStrategy = new GenAlgorithm(); // Make sure program closes after SS is closed _ss.SyncronizationClosedEvent += delegate { Close(); }; GenerateLoadingInstructions(); #else UpdatePie(); #endif }
private static InstructionsHelper FillShip(ContainersCollection containers, Ship s) { var helper = new InstructionsHelper(s); for (var y = 0; y < s.Depth; y++) { for (var x = 0; x < s.Width; x++) { if (helper.IsOccupied(x, y)) { continue; } for (var i = 0; i < containers.Count; i++) { var c = containers[i]; if (!helper.CanOccupy(c, x, y)) { continue; } helper.Occupy(c, x, y); containers.Remove(c); } } } return(helper); }
private void UpdateContainerCollection() { ContainersCollection reference = _containers.GetAvailable(_turn); foreach (var container in reference) { if (!_availableContainers.Contains(container)) { _containers.Remove(container); } } }
public ContainersCollection GetAvailable(int turn) { var temp = new ContainersCollection(); foreach (var container in this) { if (container.TurnCreated <= turn) { temp.Add(container); } } return(temp); }
///<summary> /// Allocates not yet allocated containers randomly throught all ships after crossover ///</summary> ///<param name="containers">Requires list of all containers, doesn't duplicate containers in hold</param> ///<returns> Returns false if ran out of free space </returns> public bool Repair(ContainersCollection containers) /* TODO, optional: check all ships for space availiable and randomize from them */ { int shipAmount = _shipCargo.Count; foreach (var c in containers) { var contFound = false; Container container = c; foreach (var ship in _shipCargo) // checking if cointainer is present on any ship { if (!ship.IsContPresent(container.ID)) { continue; } contFound = true; break; } if (!contFound) // if not present put it in the next empty space (or random place) on a random (next) ship { int x, y, index = _rng.Next(shipAmount); /* TODO, optional: check all ships for space availiable and randomize only from them */ InstructionsHelper shipSel = _shipCargo[index]; Coords coords = shipSel.NextOccupyableCoords(container); if (coords.X < 0 || coords.Y < 0) // alongside the coords used to check if theres even space available on the ship selected { return(false); } do /* TODO: change this super inefficent way of selecting a free space for a container */ { x = _rng.Next(shipSel.GetWidth()); y = _rng.Next(shipSel.GetDepth()); }while (!shipSel.CanOccupy(c, x, y)); shipSel.Occupy(c, x, y); _shipCargo[index] = shipSel; } } return(true); }
private void btn_ship_Click(object sender, RoutedEventArgs e) { //Make sure that only one summary window exists _ss?.Close(); ((Button)sender).IsEnabled = false; new Task(GenerateLoadingInstructions).Start(); lbl_status.Content = "Instructions generation in process"; _ss = new SummaryScreen(); _ss.Show(); UpdateContainerCollection(); _turn++; _availableContainers = _containers.GetAvailable(_turn); //Tools.ContainerGenerator.Generate(); //_containers.LoadCsv("containers.csv", _turn); }
public async Task <Dictionary <int, InstructionsHelper> > LoadContainers(ContainersCollection containers) { if (DataGenStrategy is null) { throw new NullReferenceException("Loading strategy not chosen"); } Logger.Debug("Loading containers collectionwise"); Dictionary <int, InstructionsHelper> res = DataGenStrategy.GenerateData(this, ref containers); foreach (var i in res.Keys) { await Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => { this[i].ContainersHistory = res[i].Instructions.Values.ToList(); this[i].ShipHistory[0].Values.Add(res[i].Instructions.Count); })); } Logger.Trace("Updated ships data"); return(res); }
public Dictionary <int, InstructionsHelper> GenerateData(CargoShipCollection ships, ref ContainersCollection containers) { Logger.Trace("Starting data generation"); List <Container> sorted = containers.OrderBy(s => s.TurnCreated).ThenByDescending(s => s.Size).ToList(); containers.RecreateFromList(sorted); Dictionary <int, InstructionsHelper> res = new Dictionary <int, InstructionsHelper>(); foreach (Ship s in ships) { InstructionsHelper helper = FillShip(containers, s); res.Add(s.ID, helper); } Logger.Trace("Finished data generation"); return(res); }
public Dictionary <int, InstructionsHelper> GenerateData(CargoShipCollection ships, ref ContainersCollection containers) { Logger.Trace("Starting data generation"); Random r = new Random(); Dictionary <int, InstructionsHelper> res = new Dictionary <int, InstructionsHelper>(); foreach (Ship s in ships) { var helper = new InstructionsHelper(s); for (int i = 0; i < 20; i++) { int id = r.Next(containers.Count - 1); int x = r.Next(s.Width - 1); int y = r.Next(s.Depth - 1); if (!helper.CanOccupy(containers[id], x, y)) { continue; } helper.Occupy(containers[id], x, y); containers.RemoveAt(id); } res.Add(s.ID, helper); } Logger.Trace("Finished data generation"); return(res); }