-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimarkov-grid-maxlikelihood-test.jl
160 lines (116 loc) · 3.46 KB
/
imarkov-grid-maxlikelihood-test.jl
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#################################
#TEST MARKOV DECISION PROCESS: GRID (GENERATIVE: MAX LIKELYHOOD)
#NOTE: probabilistic GRID problem solved with OWN solvers. Some experiments POMDPs.jl done as well.
#################################
include("imarkov-io.jl");
include("imarkov-grid-maxlikelihood.jl");
include("imarkov-grid-maxlikelihood-POMDPs.jl");
include("imarkov-solve-grid-maxlikelihood-ONENOW.jl");
include("imarkov-solve-grid-maxlikelihood-POMDP.jl");
include("imarkov-simulate-grid-maxlikelihood.jl");
using POMDPs
importall POMDPs
# using RDatasets, DataFrames
########
#CONFIG: configurable probability distribution
########
function getDiscountFactor()
return 0.95
end
function getSizeX()
return 10
end
function getSizeY()
return 10
end
function getFileName()
return "small.csv"
end
check(getFileName(), 100)
function getData()
return getDataset(getFileName())
end
dataset = getData()
########
#PROBLEM: WORLD
########
mdp = MaxlikelihoodGrid(getSizeX(), getSizeY(), getDiscountFactor(), getData())
#mdp.discount_factor = getDiscountFactor()
#mdp.size_x = getSizeX()
#mdp.size_y = getSizeY()
#mdp.data = getDataset(getFileName())
#@printf("sizeX: %d \n", mdp.size_x)
########
#POPMD.jl: REQUIREMENTS EXPERIMENT
########
# METHOD REQUIREMENTS: Returns a list of the following functions required by the solver for your problem
# mdp::MaxlikelihoodGrid(getSizeX(), getSizeY(), getDiscountFactor(), dataset)
function getImplementationRequirements(mdp::MaxlikelihoodGrid)
@requirements_info getUtilitySolver(mdp) mdp
# @requirements_info ValueIterationSolver() MaxlikelihoodGrid()
# https://github.com/JuliaPOMDP/DiscreteValueIteration.jl
# discount(::MDP)
# n_states(::MDP)
# n_actions(::MDP)
# transition(::MDP, ::State, ::Action)
# reward(::MDP, ::State, ::Action, ::State)
# state_index(::MDP, ::State)
# action_index(::MDP, ::Action)
# actions(::MDP, ::State)
# iterator(::ActionSpace)
# iterator(::StateSpace)
# iterator(::StateDistribution)
# pdf(::StateDistribution, ::State)
# states(::MDP)
# actions(::MDP)
end
# getImplementationRequirements(mdp)
########
#DATASET VS. POLICY
########
### INPUT size
# $ wc -l small.csv
# 50001 small.csv
### OUTPUT size
# Each output file should contain an action for every possible state in the problem
# small.policy => 100
# TODO: output file should be same name ".policy"
numStates = 100 # TODO
numActions = 4 # TODO
#"s","a","r","sp"
columnNames = names(dataset)
stateKey = columnNames[1]
actionKey = columnNames[2]
rewardKey = columnNames[3]
statePrimeKey = columnNames[4]
numRows = length(dataset[stateKey])
println("COLUMN NAMES ", stateKey, " ", actionKey, " ", rewardKey, " ", statePrimeKey, " x ", numRows)
########
#POLICY
########
qTable = zeros(numStates,numActions)
roTable = zeros(numStates,numActions)
visitCount = zeros(numStates,numActions)
solver = getMaxLikelihoodSolver(mdp)
qTable = solve(solver, mdp)
########
#OUTPUT
########
output_filename = "small.policy"
try
rm(output_filename)
except
println("could not delete inexistent file: " * input_filename)
end
out_file = open(output_filename, "w")
for stateId in 1:100
try
policyId = getArgmaxPolicy(qTable, stateId)
println(out_file, policyId)
@printf("%d ACTION RECOMMENDED FOR s=%d \n", policyId, stateId)
catch e
println(out_file, 3)
#@printf("NO SOUP FOR YOU! s=%d %s\n", stateId, e)
end
end
close(out_file)