mirror of
https://github.com/lxsang/antd-lua-plugin
synced 2025-07-13 13:44:24 +02:00
mimgrating from another repo
This commit is contained in:
86
lib/ann/examples/rpg/test.lua
Executable file
86
lib/ann/examples/rpg/test.lua
Executable file
@ -0,0 +1,86 @@
|
||||
#! /usr/bin/env lua
|
||||
|
||||
require("lfann")
|
||||
|
||||
local classes = {
|
||||
"Fighter", "Paladin", "Druid", "Cleric", "Monk",
|
||||
"Thief", "Sorceress", "Mage", "Bard"
|
||||
}
|
||||
|
||||
-- Load the network from a file
|
||||
local net = fann.Net.create_from_file("rpg.net")
|
||||
math.randomseed(os.time())
|
||||
|
||||
while true do
|
||||
print("Enter the character attributes or: -1 to exit, -2 to generate:")
|
||||
|
||||
local input = {}
|
||||
local aux = io.read("*n")
|
||||
|
||||
if not aux or aux == -1 then
|
||||
-- Quit
|
||||
break
|
||||
elseif aux == -2 then
|
||||
-- Random
|
||||
|
||||
for i = 1, 6 do
|
||||
local d = {}
|
||||
for i = 1, 4 do table.insert(d, math.random(1, 6)) end
|
||||
table.sort(d)
|
||||
table.insert(input, d[2] + d[3] + d[4])
|
||||
end
|
||||
|
||||
print("Generated attributes:")
|
||||
print(table.concat(input, " "))
|
||||
else
|
||||
-- Read the attributes from stdin
|
||||
table.insert(input, aux)
|
||||
|
||||
for i = 2, 6 do
|
||||
aux = io.read("*n")
|
||||
table.insert(input, aux)
|
||||
end
|
||||
end
|
||||
|
||||
-- Get the result and calculate suggest the character class
|
||||
local output = net:run(input)
|
||||
local maxClass, maxValue = -1, -1
|
||||
|
||||
-- Get the max value
|
||||
for class, value in ipairs(output) do
|
||||
if value > maxValue then
|
||||
maxClass = class
|
||||
maxValue = value
|
||||
end
|
||||
end
|
||||
|
||||
-- Only check if there's a class with at least 0.4 fitting
|
||||
if maxValue < 0.4 then
|
||||
print("-> The attributes are too low to suggest a class")
|
||||
else
|
||||
-- Calculate the diff using the maxValue
|
||||
local diff = maxValue / 6
|
||||
diff = math.max(diff, 0.1)
|
||||
|
||||
-- Get the suggestions
|
||||
local suggestions = {}
|
||||
|
||||
for class, value in ipairs(output) do
|
||||
if value > maxValue - diff then
|
||||
table.insert(suggestions, {["class"] = class, ["value"] = value})
|
||||
end
|
||||
end
|
||||
|
||||
-- Sort the suggestions according to the fitness
|
||||
table.sort(suggestions, function(a, b) return a.value > b.value end)
|
||||
|
||||
-- Inform the user about the suggestions
|
||||
print("\nClass suggestions:")
|
||||
|
||||
for i, tbl in ipairs(suggestions) do
|
||||
print(string.format("-> %s (%d%%)", classes[tbl.class], tbl.value * 100))
|
||||
end
|
||||
end
|
||||
|
||||
print()
|
||||
end
|
53
lib/ann/examples/rpg/train.data
Normal file
53
lib/ann/examples/rpg/train.data
Normal file
@ -0,0 +1,53 @@
|
||||
26 6 9
|
||||
18 13 15 10 9 10
|
||||
1.0 0.3 0.0 0.0 0.1 0.4 0.0 0.0 0.0
|
||||
15 10 14 10 14 14
|
||||
0.6 1.0 0.6 0.5 0.4 0.0 0.4 0.0 0.3
|
||||
10 12 10 14 16 12
|
||||
0.1 0.4 1.0 0.6 0.2 0.1 0.1 0.4 0.1
|
||||
14 8 12 10 16 12
|
||||
0.4 0.8 0.6 1.0 0.3 0.0 0.1 0.0 0.1
|
||||
14 14 14 10 16 10
|
||||
0.7 0.8 0.9 0.9 1.0 0.2 0.0 0.0 0.0
|
||||
10 18 11 14 10 12
|
||||
0.2 0.1 0.0 0.0 0.1 1.0 0.1 0.3 0.2
|
||||
10 12 14 12 10 17
|
||||
0.1 0.1 0.0 0.0 0.1 0.2 0.9 0.1 0.6
|
||||
12 14 12 12 10 18
|
||||
0.1 0.1 0.0 0.0 0.1 0.2 1.0 0.1 0.9
|
||||
8 11 16 18 10 10
|
||||
0.1 0.1 0.0 0.0 0.2 0.2 0.0 1.0 0.0
|
||||
18 10 16 10 10 10
|
||||
1.0 0.1 0.0 0.0 0.2 0.0 0.0 0.0 0.0
|
||||
16 14 14 10 18 10
|
||||
0.7 0.8 0.6 1.0 0.9 0.6 0.0 0.0 0.0
|
||||
10 12 14 16 10 10
|
||||
0.1 0.1 0.0 0.0 0.1 0.2 0.0 1.0 0.0
|
||||
10 12 14 20 10 10
|
||||
0.1 0.1 0.0 0.0 0.1 0.2 0.0 1.0 0.0
|
||||
12 12 14 12 16 10
|
||||
0.2 0.5 1.0 0.7 0.2 0.2 0.0 0.1 0.0
|
||||
3 3 3 3 3 3
|
||||
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
|
||||
18 18 18 18 18 18
|
||||
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
|
||||
14 10 9 9 11 15
|
||||
0.2 0.0 0.0 0.0 0.1 0.0 0.7 0.0 0.2
|
||||
13 15 14 14 14 16
|
||||
0.2 0.4 0.3 0.4 0.2 0.5 0.7 0.3 1.0
|
||||
12 16 12 10 16 11
|
||||
0.2 0.3 0.5 0.4 0.4 0.5 0.0 0.0 0.0
|
||||
15 17 12 12 12 13
|
||||
1.0 0.3 0.2 0.1 0.3 0.5 0.1 0.2 0.1
|
||||
16 12 14 10 16 15
|
||||
0.7 1.0 0.8 0.9 0.8 0.1 0.3 0.0 0.2
|
||||
10 16 14 18 10 14
|
||||
0.0 0.0 0.0 0.0 0.1 0.8 0.2 1.0 0.3
|
||||
12 12 12 12 12 12
|
||||
0.2 0.1 0.2 0.2 0.1 0.2 0.1 0.1 0.2
|
||||
14 14 14 14 13 14
|
||||
0.4 0.5 0.5 0.5 0.4 0.4 0.1 0.2 0.6
|
||||
8 8 8 18 18 18
|
||||
0.0 0.0 0.1 0.1 0.0 0.0 1.0 1.0 1.0
|
||||
18 18 18 8 8 8
|
||||
1.0 0.2 0.0 0.0 0.0 0.0 0.0 0.0 0.0
|
24
lib/ann/examples/rpg/train.lua
Executable file
24
lib/ann/examples/rpg/train.lua
Executable file
@ -0,0 +1,24 @@
|
||||
#! /usr/bin/env lua
|
||||
|
||||
--[[
|
||||
This example classifies a RPG character in classes, according to its
|
||||
attributes (strength, dexterity, constitution, intelligence, wisdom and
|
||||
charisma).
|
||||
--]]
|
||||
|
||||
require("lfann")
|
||||
|
||||
local net = fann.Net.create_standard{6, 18, 18, 9}
|
||||
|
||||
-- Configure the activation function
|
||||
net:set_activation_function_hidden(fann.SIGMOID_SYMMETRIC)
|
||||
net:set_activation_function_output(fann.SIGMOID_SYMMETRIC)
|
||||
|
||||
-- Configure other parameters
|
||||
net:set_training_algorithm(fann.TRAIN_RPROP)
|
||||
|
||||
-- Train the net from a file
|
||||
net:train_on_file("train.data", 100000, 50, 0.001)
|
||||
|
||||
-- Save the net to a file for a latter execution
|
||||
net:save("rpg.net");
|
Reference in New Issue
Block a user