tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
1
Task/Probabilistic-choice/E/probabilistic-choice-1.e
Normal file
1
Task/Probabilistic-choice/E/probabilistic-choice-1.e
Normal file
|
|
@ -0,0 +1 @@
|
|||
pragma.syntax("0.9")
|
||||
55
Task/Probabilistic-choice/E/probabilistic-choice-2.e
Normal file
55
Task/Probabilistic-choice/E/probabilistic-choice-2.e
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/** Makes leaves of the binary tree */
|
||||
def leaf(value) {
|
||||
return def leaf {
|
||||
to run(_) { return value }
|
||||
to __printOn(out) { out.print("=> ", value) }
|
||||
}
|
||||
}
|
||||
/** Makes branches of the binary tree */
|
||||
def split(leastRight, left, right) {
|
||||
return def tree {
|
||||
to run(specimen) {
|
||||
return if (specimen < leastRight) {
|
||||
left(specimen)
|
||||
} else {
|
||||
right(specimen)
|
||||
}
|
||||
}
|
||||
to __printOn(out) {
|
||||
out.print(" ")
|
||||
out.indent().print(left)
|
||||
out.lnPrint("< ")
|
||||
out.print(leastRight)
|
||||
out.indent().lnPrint(right)
|
||||
}
|
||||
}
|
||||
}
|
||||
def makeIntervalTree(assocs :List[Tuple[any, float64]]) {
|
||||
def size :int := assocs.size()
|
||||
if (size > 1) {
|
||||
def midpoint := size // 2
|
||||
return split(assocs[midpoint][1], makeIntervalTree(assocs.run(0, midpoint)),
|
||||
makeIntervalTree(assocs.run(midpoint)))
|
||||
} else {
|
||||
def <nowiki>[[value, _]] := assocs</nowiki>
|
||||
return leaf(value)
|
||||
}
|
||||
}
|
||||
def setupProbabilisticChoice(entropy, table :Map[any, float64]) {
|
||||
var cumulative := 0.0
|
||||
var intervalTable := []
|
||||
for value => probability in table {
|
||||
intervalTable with= [value, cumulative]
|
||||
cumulative += probability
|
||||
}
|
||||
def total := cumulative
|
||||
def selector := makeIntervalTree(intervalTable)
|
||||
return def probChoice {
|
||||
# Multiplying by the total helps correct for any error in the sum of the inputs
|
||||
to run() { return selector(entropy.nextDouble() * total) }
|
||||
to __printOn(out) {
|
||||
out.print("Probabilistic choice using tree:")
|
||||
out.indent().lnPrint(selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
22
Task/Probabilistic-choice/E/probabilistic-choice-3.e
Normal file
22
Task/Probabilistic-choice/E/probabilistic-choice-3.e
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
def rosetta := setupProbabilisticChoice(entropy, def probTable := [
|
||||
"aleph" => 1/5,
|
||||
"beth" => 1/6.0,
|
||||
"gimel" => 1/7.0,
|
||||
"daleth" => 1/8.0,
|
||||
"he" => 1/9.0,
|
||||
"waw" => 1/10.0,
|
||||
"zayin" => 1/11.0,
|
||||
"heth" => 0.063455988455988432,
|
||||
])
|
||||
|
||||
var trials := 1000000
|
||||
var timesFound := [].asMap()
|
||||
for i in 1..trials {
|
||||
if (i % 1000 == 0) { print(`${i//1000} `) }
|
||||
def value := rosetta()
|
||||
timesFound with= (value, timesFound.fetch(value, fn { 0 }) + 1)
|
||||
}
|
||||
stdout.println()
|
||||
for item in probTable.domain() {
|
||||
stdout.print(item, "\t", timesFound[item] / trials, "\t", probTable[item], "\n")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue