Exemple #1
0
        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);
        }
Exemple #2
0
        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);
        }
Exemple #3
0
            ///<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);
            }
Exemple #4
0
        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);
        }