Naive Ship Placement
I then wanted to find a probability density function (PDF) from which to pick my positions; want to sample (take more shots) more where there are more shots. This was first approached numerically and then analytically. The numerical PDF's were calculated by creating 1 million boards and summing them, and then averaging for the number of boards run (1 million). Ships are placed anywhere on the board under the following conditions:
- The ship is continuous
- Another ship is not present
- Entire ship fits on the board
![]() |
| PDF of ships of length [2,3,3,4,5] being placed on a 10x10 board |
![]() |
| PDF of ships of length [2,3,3,4] place on a 10x10 board |
Analytically this was approached by counting the number of ways to arrange a ship of a given size on the board. All possible combinations are worked out for ships of size 2 and 3.
![]() | |
| Possible placements of ships of size two, 9 total. |
![]() |
| Possible combinations of ships of size three. 17 possible combinations. |
Code for generating Boards:
function [board] = generateBoard(obj) % Allocating space for the board board = zeros(obj.n,obj.n); i = 1; while i <= numel(obj.ships) ship = obj.ships(i); % Determining Orientation ort = (rand>0.5); % Determining Position x = randi(obj.n); y = randi(obj.n); shipEndPoints = [x x+ship-1]; % Placing Ship, if it is a valid position if canPlaceShip(obj,shipEndPoints,y,board,ort) board = placeShip(shipEndPoints,y,board,ort); i = i +1; end end end end function validPosition = canPlaceShip(obj,shipEndpoints,col,board,ort) if ort; board = board'; end % Logical Values for Board Placement if (shipEndpoints(1) >= 1 && shipEndpoints(2) <= obj.n) && (0 == sum(board(shipEndpoints,col))) validPosition = true; else validPosition = false; end end function board = placeShip(shipEndpoints,col,board,ort) if ort; board = board'; end % Spots are now filled board(shipEndpoints(1):shipEndpoints(2),col) = ones(abs(diff(shipEndpoints)+1),1); % Need to Flip Back if ort; board = board'; end end




No comments:
Post a Comment