Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Washington's updates to calibration 0.3 and 0.4 - part 1 #200

Merged
merged 11 commits into from
Dec 16, 2017
39 changes: 35 additions & 4 deletions lua/MultipleRuns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ local function redirectPrint(f)
return log
end

local function freeModel(model)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should not be necessary.

forEachElement(model, function(member)
model[member] = nil
end)
end

-- function used in run() to test the model with all the possible combinations of parameters.
-- params: Table with all the parameters and it's ranges or values indexed by number.
-- Example: params = {{id = "x", min = 1, max = 10, elements = nil, ranged = true, step = 2},
Expand Down Expand Up @@ -396,6 +402,11 @@ local function factorialRecursive(data, params, a, variables, resultTable, addFu
testAddFunctions(resultTable, addFunctions, data, m, summaryResult)
testDir:setCurrentDir()
table.insert(resultTable.simulations, stringSimulations)
if data.free then
freeModel(m)
m = nil
collectgarbage()
end
end

if data.summary and type(data.summary) == "function" then
Expand Down Expand Up @@ -519,6 +530,11 @@ local function factorialRecursive(data, params, a, variables, resultTable, addFu
testAddFunctions(resultTable, addFunctions, data, m, summaryResult)
testDir:setCurrentDir()
table.insert(resultTable.simulations, stringSimulations)
if data.free then
freeModel(m)
m = nil
collectgarbage()
end
end

if data.summary then
Expand Down Expand Up @@ -666,6 +682,8 @@ metaTableMultipleRuns_ = {
-- @arg data.folderName Name or file path of the folder where the simulations output will be saved.
-- Whenever the Model saves one or more files along its simulation, it is necessary to use this
-- argument to guarantee that the files of each simulation will be saved in a different directory.
-- @arg data.free If true, then the memory used by Model instances will be removed after their simulations. Only the observed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after each simulation.

-- properties of the model will be stored within MultipleRuns, (Default is false).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

within MultipleRuns. Default is false.

-- @arg data.hideGraphics If true (default), then sessionInfo().graphics will disable all charts and observers during models execution.
-- @arg data.showProgress If true, a message is printed on screen to show the models executions progress on repeated strategy,
-- (Default is false).
Expand All @@ -677,13 +695,13 @@ metaTableMultipleRuns_ = {
-- @tabular strategy
-- Strategy & Description & Mandatory arguments & Optional arguments \
-- "factorial" & Simulate the Model with all combinations of the argument parameters.
-- & parameters, model & repetition, output, hideGraphics, quantity, folderName, showProgress, ... \
-- & parameters, model & repetition, output, hideGraphics, quantity, folderName, free, showProgress, ... \
-- "sample" & Run the model with a random combination of the possible parameters & parameters,
-- repetition, model & output, folderName, hideGraphics, quantity, showProgress, ... \
-- repetition, model & output, folderName, free, hideGraphics, quantity, showProgress, ... \
-- "selected" & This should test the Model with a given set of parameters values. In this case,
-- the argument parameters must be a named table, where each position is another table describing
-- the parameters to be used in such simulation. &
-- model, parameters & output, folderName, hideGraphics, repetition, showProgress, quantity, ...
-- model, parameters & output, folderName, free, hideGraphics, repetition, showProgress, quantity, ...
function MultipleRuns(data)
mandatoryTableArgument(data, "model", "Model")
mandatoryTableArgument(data, "parameters", "table")
Expand All @@ -695,6 +713,7 @@ function MultipleRuns(data)
defaultTableValue(data, "hideGraphics", true)
defaultTableValue(data, "showProgress", true)
optionalTableArgument(data, "summary", "function")
defaultTableValue(data, "free", false)

if data.strategy == nil then
local choiceStrg = false
Expand Down Expand Up @@ -742,7 +761,7 @@ function MultipleRuns(data)
local checkingArgument = {}
checkingArgument[idx] = idx
verifyUnnecessaryArguments(checkingArgument, {
"model", "strategy", "parameters", "repetition", "folderName", "hideGraphics", "showProgress", "repeat", "quantity", "outputVariables"})
"model", "strategy", "parameters", "repetition", "folderName", "hideGraphics", "showProgress", "repeat", "quantity", "outputVariables", "free"})
end
end)

Expand Down Expand Up @@ -921,6 +940,12 @@ function MultipleRuns(data)

table.insert(resultTable[idx2], att2)
end)

if data.free then
freeModel(m)
m = nil
collectgarbage()
end
end

if data.summary then
Expand Down Expand Up @@ -1029,6 +1054,12 @@ function MultipleRuns(data)

table.insert(resultTable[idx2], att2)
end)

if data.free then
freeModel(m)
m = nil
collectgarbage()
end
end

if data.summary then
Expand Down
97 changes: 97 additions & 0 deletions tests/core/basic/MultipleRuns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,46 @@ local MyModel6 = Model{
end
}

local MyModel7 = Model{
x = 1,
y = 1,
finalTime = 1,
init = function(self)
self.timer = Timer{
Event{action = function()
self.iniMem = collectgarbage("count") / 1024 -- in megabytes
self.z = {}
for i = 1, 100000 do
self.z[i] = i
end

self.finMem = collectgarbage("count") / 1024
end}
}
end
}

local MyModel8 = Model{
p = {
x = Choice{-1, 0, 1},
y = Choice{min= - 1, max = 1, step = 1},
},
finalTime = 1,
init = function(self)
self.timer = Timer{
Event{action = function()
self.iniMem = collectgarbage("count") / 1024 -- in megabytes
self.z = {}
for i = 1, 100000 do
self.z[i] = i
end

self.finMem = collectgarbage("count") / 1024
end}
}
end
}

local function fileExists(name)
local sep = sessionInfo().separator
return File(currentDir().."results"..sep..name..sep.."data.csv"):exists()
Expand Down Expand Up @@ -659,5 +699,62 @@ return{
unitTest:assert(fileExists("1_execution_3"))
unitTest:assert(fileExists("2_execution_3"))
unitTest:assert(Directory(currentDir().."results"):delete())

local free1 = MultipleRuns{
model = MyModel7,
showProgress = false,
free = true,
parameters = {
x = Choice{-1, 0, 1},
y = Choice{-1, 0, 1}
}
}

unitTest:assertEquals(free1.output.iniMem[3], free1.output.iniMem[6], 1)
unitTest:assertEquals(free1.output.finMem[3], free1.output.finMem[6], 1)

local free2 = MultipleRuns{
model = MyModel7,
showProgress = false,
free = true,
strategy = "sample",
parameters = {
x = Choice{-1, 0, 1},
y = Choice{-1, 0, 1}
},
quantity = 9
}

unitTest:assertEquals(free2.output.iniMem[3], free2.output.iniMem[6], 1)
unitTest:assertEquals(free2.output.finMem[3], free2.output.finMem[6], 1)

local free3 = MultipleRuns{
model = MyModel7,
showProgress = false,
free = true,
strategy = "selected",
parameters = {
scenario1 = {x = -1, y = -1},
scenario2 = {x = 0, y = 0},
scenario3 = {x = 1, y = 1}
},
repetition = 3
}

unitTest:assertEquals(free3.output.iniMem[3], free3.output.iniMem[6], 1)
unitTest:assertEquals(free3.output.finMem[3], free3.output.finMem[6], 1)

local free4 = MultipleRuns{
model = MyModel8,
showProgress = false,
free = true,
parameters = {p = {
x = Choice{-1, 0, 1},
y = Choice{min= - 1, max = 1, step = 1}
}},
}

unitTest:assertEquals(free4.output.iniMem[3], free4.output.iniMem[6], 1)
unitTest:assertEquals(free4.output.finMem[3], free4.output.finMem[6], 1)
end
}