Labels

Showing posts with label Computational Science. Show all posts
Showing posts with label Computational Science. Show all posts

Monday, July 16, 2018

Mixed level factorial design algorithm

Assume you have a set of factors let's say [A,B,C] and each factor may be assigned different values (let's call these values "levels"). For the purpose of testing (using general full factorial design) you want to try all combinations of all factor levels with each other knowing that each factor may have number of levels different from the other factor. The following table will be used as an example:


Theoretically, to solve this problem, nested "For" loops will be needed where number of loops equals number of factors. This may be impractical to implement specially when number of factors is not constant.

The proposed algorithm simply mimics the mechanical analog counter (or more specifically Tally counter) like the one shown in the picture below but with a slight difference in implementation. In analog counter, the rightmost digit wheel is the driving wheel and when it increases above 9 it will reset back to 0 and the next digit one the left will increment by one. The same rule applies for all other digits: when the digit value increases above 9 it resets to 0 and increment the next digit on the left side by one.  

Reference: https://www.shutterstock.com/video/clip-4902245-analog-mechanical-counter-numbers-that-turned-quickly

In mechanical analog counter, all digits have an upper value of 9 and lower value of 0. On the other side, in this algorithm, each digit will have its own upper value which equals the number of levels while the lower value will be 1.

Let's assume that factor A will be represented by the digit wheel on the leftmost side and factor C will be on the rightmost side so that factor C will be the driving wheel.

By knowing the number of levels of each factor we can calculate the total number of combinations N by multiplying all factor levels (N=3*2*4=24 in this example).

Now, we will increment factor C for N times and after each increment we will do a check for all factors (digits), if any factor exceed its upper bound, it will reset to 1 and increment the next digit on the left side by one.

The counting process of the previous example will look like the simple animation shown below:



This algorithm is very efficient and -theoretically- has no limitation on number of factors or number of levels.

The following VBA code can be used to implement the previous algorithm

Sub CreateTreatments()

Const NoOfFactors = 3

Dim MaxLevels(NoOfFactors) As Integer
Dim FactorLevels(NoOfFactors) As Integer
Dim Letters(NoOfFactors) As String


' Define factor letters
Letters(0) = " A"
Letters(1) = " B"
Letters(2) = " C"

' Define number of levels of each factor
MaxLevels(0) = 3
MaxLevels(1) = 2
MaxLevels(2) = 4

' Reset all factors to the minimum level
FactorLevels(0) = 1
FactorLevels(1) = 1
FactorLevels(2) = 1

' Calculate number of runs (combinations)
NoOfRuns = 1
For i = 0 To NoOfFactors - 1
NoOfRuns = NoOfRuns * MaxLevels(i)
Next i

For i = 1 To NoOfRuns

' Create combination text
CombinationString = ""
For j = NoOfFactors To 1 Step -1
CombinationString = Letters(j - 1) + CStr(FactorLevels(j - 1)) + CombinationString
Next j

FactorLevels(NoOfFactors - 1) = FactorLevels(NoOfFactors - 1) + 1    'Increment rightmost digit

For j = NoOfFactors To 2 Step -1    'For all digits
'If factor level is higher than number of levels, then reset it and increment the left digit
If FactorLevels(j - 1) > MaxLevels(j - 1) Then
FactorLevels(j - 1) = 1
FactorLevels(j - 2) = FactorLevels(j - 2) + 1
End If
Next j

Debug.Print CombinationString

Next i

End Sub


This code will output the following lines in debug window:

A1 B1 C1
A1 B1 C2
A1 B1 C3
A1 B1 C4
A1 B2 C1
A1 B2 C2
A1 B2 C3
A1 B2 C4
A2 B1 C1
A2 B1 C2
A2 B1 C3
A2 B1 C4
A2 B2 C1
A2 B2 C2
A2 B2 C3
A2 B2 C4
A3 B1 C1
A3 B1 C2
A3 B1 C3
A3 B1 C4
A3 B2 C1
A3 B2 C2
A3 B2 C3
A3 B2 C4


Keywords:

Mixed level factorial design algorithm

Sunday, February 12, 2017

Battleship game



Probability matrix:

Probability matrix is a two-dimensional matrix that represents the probability of each cell to be a member of any ship size. In other words, the probability of any cell is the possible number of times it can be a member of any ship. During the game the probability matrix is updating. The minimum value of probability is zero while the maximum value is 34.

The image below shows the probability matrix at the start of the game before any shooting.



As an example, the following animated gif image shows how the probability of cell A1 is calculated for the probability matrix shown above:

5-cell ship:
Number of probabilities to be placed horizontally in single row=6
Number of probabilities to be placed horizontally=6*10=60
Number of probabilities to be placed vertically=6*10=60
Total number of probabilities=120

4-cell ship:
Number of probabilities to be placed horizontally in single row=7
Number of probabilities to be placed horizontally=6*10=70
Number of probabilities to be placed vertically=6*10=70
Total number of probabilities=140


3-cell ship:
Number of probabilities to be placed horizontally in single row=8
Number of probabilities to be placed horizontally=6*10=80
Number of probabilities to be placed vertically=6*10=80
Total number of probabilities=160


2-cell ship:
Number of probabilities to be placed horizontally in single row=9
Number of probabilities to be placed horizontally=6*10=90
Number of probabilities to be placed vertically=6*10=90
Total number of probabilities=180

Number of combinations of placement of ships in Battleship game= 180*160*160*140*120=77,414,400,000



Efficiency measures:

The minimum number of shots possible to finish battleship game for the luckiest person on earth is 17 (20 if the ship's will not be declared).

The maximum number of shots possible to finish battleship game for the most stupid person on earth is 100.

Small ships consumes more shots to detect. So, the strategy aims to hit the largest ships first.

The maximum number of shots to get the first correct hit should be 20. If your technique reach the first hit after this number of iterations then it is not efficient. You can randomly select one of the predefined cells below to shorten number of iterations. The following is valid if all cells of this technique are shot:




Probability to hit ship

Size in blocks

Probability

5

100%

4

80%

3

60%

2

40%



The following cells pattern can be used to detect 4-blocks ship size with the given probabilities:




Probability to hit ship

Size in blocks

Probability

5

100%

4

100%

3

75%

2

51.11%



The following cells pattern can be used to detect 3-blocks ship size with the following probabilities:




Probability to hit ship

Size in blocks

Probability

5

100%

4

100%

3

100%

2

66.67%




The maximum number of shots for the worst-luck person to finish a game should be 75 shot (using checker board strategy of 50 shots, with no ships placed on borders, no ships adjacent to each other, ships detected 2,3,3,4,5 successively). If number of shots is more than 75, then the technique is stupid. For the versions where the player should also recognize the ship size (ship size is not declared), the number should be 79.


The following is valid if all cells in checker board  strategy are shot:

[1] The probability of first hit of 5-blocks ship is 100%
[2] The probability of first hit of 4-blocks ship is 100%
[3] The probability of first hit of 3-blocks ship is 100%
[4] The probability of first hit of 2-blocks ship is 100%




Cell status:

The possible status for any cell in this game is shown below:

Unknown: a cell that has not been shot yet

Missed: a cell that has been shot, but it is empty


Hit: a cell that has been shot and it is not empty

Sunk: a cell that is a part of sunk ship

Blocked: a cell that lies in the surrounding of sunk ship (this status is used in game version where the ship size is not declared)

Game ID:

The game identifier is a unique identifier that describes the game. It describes where the ships were positioned, their orientations, and the locations of shots. For the purpose of benchmarking of the code, I created this identifier for each game played, so I can build results based on unique games.

The following image shows the maximum possible number of shots (iterations) for each ship size once one block is hit:




If one shot is correct then, set the status to "Partial hit" select the neighboring cell with maximum probability. Shot neighboring cells until the whole ship is sunk.

If two adjacent cells are correct hit, then the orientation of ship is known. Select the neighboring cell with maximum probability on the same row or column

The ship is then sunk when one of the following takes place:

[1] All neighboring cells are missed or blocked
[2] Number of hit cells equals the maximum size of unknown ships
[3] Total probability of neighboring cell equals zero

After ship is sunk, get its size, and remove it from the unknown ship sizes.

After ship is detected (and not declared), mark the cells around as blocked.

After a ship is detected, you can define cells that will detect the maximum available ship size.

Number of vertical probabilities for a given ship size to pass through certain cell=Minimum(No. of allowed cells on top+1, No. of allowed cells on bottom+1, Ship size-No. of hit cells).

Number of horizontal probabilities for a given ship size to pass through certain cell=Minimum(No. of allowed cells on left +1, No. of allowed cells on right +1, Ship size-No. of hit cells).

The total probability for a give ship size=Vertical probabilities+Horizontal probabilities

The total probability for all unknown ship sizes is then the summation of total probabilities for all unknown ship sizes.

Where the acronym "allowed cells" are those cells having the status "Unknown" or "Hit".

Note: the previous formula is applicable where ship size is higher than or equal the number of hit cells.


The following is an animated gif image for one random game using the previously mentioned algorithm:



Key words:

Battleship board game

Battleship game best strategy

Battleship game strategies

Battleship game probability function

Battleship game probability matrix

Battleship game number of combinations

Battleship game solver

Battleship game best efficiency

Battleship game patterns

Sunday, October 16, 2016

Prioritization, prioritization matrix, and weighting

Assume that you want to buy a car and assume that there is a website that has search filters for your required specifications. Assume that you want a car with specific values for the following features:
[1] Horse power
[2] Center lock
[3] Fuel consumption
[4] Front speakers
[5] Maximum speed
[6] Driver airbag
The normal situation is that the search engine will treat all the parameters as they have the same importance (priority) to you and this may lead to results that are not logic. So, what if the search is not based on true or false, what if it is based on a total score, that meets your requirements.
How this will go?
First of all, the website will list all the features (attributes) the cars have, then you have to select the important features you are interested in to look for. The next step will be a survey to sort your priorities like the following image sequence below:


Assuming number of features is M, then the number of survey questions  N depends on the number of features M and it is the sum of the arithmetic series:

Or it can be simply calculated using this simple formula:
The survey may be a little bit long, but it really sets the right priorities in a scientific mistake-proof approach
At any time during the survey you can exit the survey and save your priorities which will be saved in what I call the "prioritization matrix" which is a square matrix that defines the relationship between each feature and looks like the following:


In the previous matrix, we count the number of "TRUE" in each row. The higher "TRUE" count, the higher priority.

Based on the previous matrix, these features are sorted in descending order based on my needs like that:
[1] Driver airbag
[2] Fuel consumption
[3] Maximum speed
[4] Horse power
[5] Center lock
[6] Front speakers

This way of prioritization can help setting weight factors for project management, risk management, time management, budget allocation, ... etc.
In this next post I will explain how set the weights and generate the score.

Friday, February 27, 2015

Upgrading excel text filter

The following is a VBA programming concept for implementing a text filter for MS Excel sheets. The text filter has the following features:

- Uses ActiveX controls
- Instantaneous search and update
- Search for cells that contain words even if they are not complete
- Search for words regardless the case of the word: upper case, lower case, or mixed case
- Search for words regardless the order of them
- Search words are assumed to be space delimited
- Extra spaces are not considered

This is a demonstration captures:


After searching for word "shady":


After searching for "Mohsen Shad":



Here is the code below (sorry for adding it as a capture because I don't know how export it with colored keywords):


Good appetite :)