forked from stinaq/WEASEL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowershell.ps1
More file actions
70 lines (60 loc) · 1.92 KB
/
powershell.ps1
File metadata and controls
70 lines (60 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<#
.Synopsis
Short description
.DESCRIPTION
All the calculations are done with string arrays. Only for outputing they are joined
.EXAMPLE
Example of how to use this cmdlet
.EXAMPLE
Another example of how to use this cmdlet
#>
$goal = "METHINKS IT IS LIKE A WEASEL"
$alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ".ToCharArray()
$mutationRate = 0.04d
$generations = 0
$mutationRandom = new-object Random
$numberOfChildren = 100
function New-RandomCharacter {
$alphabet[$mutationRandom.Next($alphabet.Length)]
}
function New-Sire {
(1..$goal.Length | % { New-RandomCharacter }) -join ""
}
function New-Mutation {
[CmdletBinding()]
Param ([Parameter(Mandatory=$true)]$Character)
if ($mutationRandom.NextDouble() -lt $mutationRate) { New-RandomCharacter } else { $Character }
}
function New-Child {
[CmdletBinding()]
Param([Parameter(Mandatory=$true)][string]$Parent)
#write-host "new-child parent: $parent"
($Parent[0..$Parent.Length] | % { New-Mutation -Character $_ }) -join ""
}
function New-Children {
[CmdletBinding()]
Param([Parameter(Mandatory=$true)][string]$Parent)
1..$numberOfChildren | % { New-Child -Parent $Parent }
}
function Get-Alikeness {
[CmdletBinding()]
Param([Parameter(Mandatory=$true)]$Child)
(0..($goal.Length-1) | ? { $goal[$_] -eq $Child[$_] }).Length
}
function Get-Fittest {
[CmdletBinding()]
Param([Parameter(Mandatory=$true)]$Children)
($children | Sort -Property @{Expression={ Get-Alikeness -Child $_ }} | Select -Last 1) -join ""
}
function New-EvolutionGeneration {
[CmdletBinding()]
Param([string]$Parent=$(New-Sire))
$generations++
$children = New-Children -Parent $Parent
$fittestChild = Get-Fittest -Children $children
Write "Generations: $generations`t`tWeasel: $fittestChild"
if ($fittestChild -ne $goal) {
New-EvolutionGeneration -Parent $fittestChild
}
}
New-EvolutionGeneration