Low-carbon growth in Brazil?

An Introduction
to TerraME
Pedro Ribeiro de Andrade
São José dos Campos, 2011
www.terrame.org
TerraME: Ambiente Computacional
Multi-paradigma para o Desenvolvimento de
Modelos Integrados Natureza-Sociedade
Cell Spaces
Hipótese: Nenhuma abordagem sozinha é suficiente para representar a
complexidade das interações sociedade-natureza
Fonte: (Carneiro, 2006)
Desenvolvimento do TerraME
 CCST-INPE
 Pedro Andrade, Gilberto Camara, Raian Maretto
 TerraLab/UFOP
 Tiago Carneiro
 DPI/OBT-INPE
 Miguel Monteiro, Time TerraLib
TerraME
TerraME INTERPRETER
• model syntax semantic checking
• model execution
TerraView
• data acquisition
• data visualization
• data management
• data analysis
LUA interpreter
TerraME framework
data
model
model
TerraME/LUA interface
MODEL DATA
Model
source code
TerraLib
database
data
Eclipse & LUA plugin
• model description
• model highlight syntax
Software: Open source GIS
Visualization (TerraView)
Modelling (TerraME)
Spatio-temporal
Database (TerraLib)
Statistics (R interface)
Data Mining(GeoDMA)
TerraME: Components
1. Get first pair
2. Execute the ACTION
3. Timer =EVENT
return value
1.
1:32:00
Mens. 1
2.
1:32:10
Mens. 3
3.
1:38:07
Mens. 2
4.
1:42:00
...
Mens.4
true
4. timeToHappen += period
Temporal structure
Spatial structure
latency
> 6 years
Deforesting
Newly implanted
Year of
creation
Slowing down
Iddle
Deforestation =
100%
Rules of behaviour
Spatial relations
Source: [Carneiro, 2006]
TerraME extensions to Lua
 Lua classes using the constructor mechanism: Cell,
CellularSpace, Neighborhood, Timer, Event, Legend
 A CellularSpace is a multivalued set of Cells. It consists
of a geographical area of interest, divided into regular
or irregular objects.
 Each Cell has a set of attributes.
 CellularSpaces can be stored and retrieved from
TerraLib databases if the modeler specify where the
data is.
Example 1: Random cellular space
Cellular Space in memory and forEachCell
DEAD = 0
ALIVE = 1
cs = CellularSpace {
xdim = 30,
ydim = 20
}
function random (cs)
forEachCell (cs, function (cell)
v = math.random() -- a value between 0 and 1
if v > 0.5 then
cell.state = ALIVE
else
cell.state = DEAD
end
end)
end
random(cs)
Legend and Observer
lifeLeg = Legend{
type = TYPES.NUMBER,
groupingMode = GROUPING.UNIQUEVALUE,
slices = 2,
maximum = ALIVE,
minimum = DEAD,
colorBar1 = {
{BLACK, DEAD},
{WHITE, ALIVE}
},
}
cs:createObserver(OBSERVERS.MAP, {"state"}, {lifeLeg})
cs:notifyObservers()
Example 2: Game of life
Synchronizing a CellularSpace
 TerraME keeps two copies of a cellular space in memory: one
stores the past values of the cells, and another stores the
current (present) values of the cells.
 The model equations must read the past copy and write the
values to the present copy of the cellular space.
 At the correct moment, it will be necessary to synchronize the
past copy with the current values of the cellular space.
Game of life source code
DEAD = 0
ALIVE = 1
TURNS = 400
function random (cs)
forEachCell (cs, function (cell)
v = math.random() -- a value between 0 and 1
if v > 0.5 then
cell.state = ALIVE
else
cell.state = DEAD
end
end)
end
Cellular Space and neighborhood
cs = CellularSpace {
xdim = 50
}
random(cs)
createMooreNeighborhood(cs, "1", false)
function countAlive(cell)
count = 0
forEachNeighbor(cell, function(cell, neigh)
if neigh.past.state == ALIVE then
count = count + 1
end
end)
return count
end
Updating the cellular space
function updateSpace(mycs)
forEachCell(mycs, function(cell)
n = countAlive(cell)
-- cells with one or no neighbors die (loneliness).
if
n < 2 then cell.state = DEAD
-- cells with four or more neighbors die (overpopulation).
elseif n > 3 then cell.state = DEAD
-- cells with two neighbors survive.
elseif n == 2 then cell.state = cell.past.state
-- cells with three neighbors become populated.
elseif n == 3 then cell.state = ALIVE
end
end)
end
Running and synchronizing
lifeLeg = Legend{
type = TYPES.NUMBER,
groupingMode = GROUPING.UNIQUEVALUE,
slices = 2,
maximum = ALIVE,
minimum = DEAD,
colorBar1 = {
{BLACK, ALIVE},
{WHITE, DEAD}
}
}
cs:createObserver(OBSERVERS.MAP, {"state"}, {lifeLeg})
cs:notifyObservers()
for i = 1,TURNS do
cs:synchronize()
updateSpace(cs)
cs:notifyObservers()
end
Example 3: Runoff
rain
rain
Itacolomi do Itambé
Peak
rain
Lobo’s Range
CellularSpace from database and neighborhood
-- input and output data paths
TERRAME_PATH = "e:\\Programas\\TerraME\\"
INPUT_PATH = TERRAME_PATH.."Database\\"
-- retrieve the cell space from the database
csQ = CellularSpace{
database = INPUT_PATH.."cabecaDeBoi.mdb",
theme = "cells",
select = { "height", "soilWater" }
}
csQ:load()
function filter(cell, neigh)
if cell.height >= neigh.height then
return true
end
return false
end
create3x3Neighborhood(csQ, filter)
Observers using two attributes
heightLeg = Legend{
type = TYPES.NUMBER, groupingMode = GROUPING.EQUALSTEPS,
slices = 50, maximum = 255, minimum = 0,
colorBar1 = {
{BLACK, 0},
{WHITE, 1}
}
}
soilWaterLeg = Legend{
type = TYPES.NUMBER, groupingMode = GROUPING.EQUALSTEPS,
slices = 100, maximum = 200, minimum = 0,
colorBar1 = {
{WHITE, 0},
{BLUE, 200}
}
}
csQ:createObserver(OBSERVERS.MAP, {"soilWater", "height"},
{soilWaterLeg, heightLeg})
Rain and runoff
RAIN = 4 -- rain/t
MIN_HEIGHT = 200
function rain(cs)
forEachCell(cs, function(cell)
if ANYWHERE or (cell.height > MIN_HEIGHT) then
cell.soilWater = cell.soilWater + RAIN
end
end)
end
function runoff(cs)
cs:synchronize("soilWater")
forEachCell(cs, function(cell) cell.soilWater = 0 end)
forEachCell(cs, function(cell)
countNeigh = cell:getNeighborhood():size()
if countNeigh > 0 then
soilWater = cell.past.soilWater / countNeigh
forEachNeighbor(cell, function(cell, neigh)
neigh.soilWater = neigh.soilWater + soilWater
end)
else
cell.soilWater = cell.soilWater + cell.past.soilWater
end
end)
end
Timer
RAINING_TIME = 5
FINAL_TIME = 50
t = Timer{
Event{message = function(event)
rain(csQ)
if event:getTime() > RAINING_TIME then
return false
end
end},
Event{message = function(event)
runoff(csQ)
end},
Event{priority = 5, message = function(event)
csQ:notifyObservers()
end}
}
t:execute(FINAL_TIME)
Different spatial resolutions
1985
Small farms environments:
500 m resolution
Categorical variable:
deforested or forest
One neighborhood relation:
•connection through roads
Large farm environments:
2500 m resolution
1997
Continuous variable:
% deforested
Two alternative neighborhood
relations:
•connection through roads
• farm limits proximity
1997
Generalized Proximity Matrices (GPM)
Models of Computation
(von Neumann, 1966)
(Wooldbridge, 1995)
(Minsky, 1967)
(Pedrosa et al, 2003)
(Aguiar et al, 2004)
(Straatman et al, 2001)
(Rosenschein and Kaelbling, 1995)
Agent based models
Cellular automata models
State machines
Settlement/
invaded land
Sustainability path
(alternative uses, technology)
Diversify use
money surplus
Subsistence
agriculture
Create pasture/
Deforest
Sustainability path
(technology)
Manage cattle
bad land
management
Move towards
the frontier
Abandon/Sell
the property
Buy new
land
Speculator/
large/small
GPM as a graph
From
a
Cell
Agent
Agent
To
Cell
a
b
c
b
c
Agent-based modelling relations
forEachRelative
forEachNeighbor
forEachCell
Agent
Cell
forEachAgent
forEachAgent
Society
forEachCell
CellularSpace
Group
Trajectory
DBMS
Nested environments
Prodes/INPE 2000-2001
Nested environments
Escala 1
pai
up-scaling
down-scaling
Escala 2
filho
An Introduction
to TerraME
Pedro Ribeiro de Andrade
São José dos Campos, 2011
www.terrame.org