Basic Simulation
Tutorial about build simulation.
Before you begin, please finish basic concepts section.
This tutorial is going to walk through a simple simulation setup. The complete source code can be found at "all the codes" section in this tutorial, and sample programs under repo root folder.
In this example, we are going to setup a environment with one person and one door, and let the person interact with the door.
Environment setup & reset all
You are required to have a firedb server, and a postgres server to run. You can either request us to set up one for you, or setup for yourself. Follow the following steps:
- Create a new firebase account
- Create a new project
- Setup Admin SDK, download json file to the root folder and name it as "fire.secret", and record API key
- Create a local Postgresql db, record username and password
Once you setup two DBs, you can setup in your environment variables of both:
DB_URL
By sqlalchemy url format. Example: 'postgresql://scott:tiger@localhost/mydatabase'
FIRE_API
Your firebase api key
If you are having trouble doing so, contact us and we will set it up for you.
It maybe more convenient in this version, to reset all databases before you begin. We provides a reset function for you to use:
from algotron import reset
reset()
Setup actions
First, in order to interact with the object, we have to first setup some actions. We will start by setup the action "open" and "close" for the door.
from algotron.core import Action
Action(name='open', formula='1', physics='opened')
Action(name='close', formula='0', physics='opened')
The algotron core provides you data management tool for setup actions. Simply include from core, and init an action class, the tool will take care of the rest for you.
Let us look into this action a little bit. The action contains
- A unique name.
- A formula indicates what the state of the object should be after performing this action.
- A physical state that it represents.
In this case we created two actions, open and close, to impact physical state "opened" of any object, and when the action "open" is performed, the physical state "opened" will be set to 1, and close will set it to 0.
Action scope
The action you just setup, will be visible across all simulation, include yours and others, just like any physics that you modified or added. This is a temporary solution for running on individual database, and will be changed in the future releases.
In the future, the actions, like the physics, will only allow to be changed on admin level users.
Simulation setup
We now need to setup a simulation, as a container for your environment. We can login as a user, and build a simulation relative to the user.
from algotron.core import User, Simulation
User.login('aaron')
myhome = Simulation('myhome', caching=True)
We login as aaron, and build a simulation called 'myhome' that belongs to the user. The caching option is used to build local caching, and speed up the access speed of the states in simulation.
When to use caching
Caching option can only be used when you are the only server that have a human to change the states in the simulation, therefore it is by default disabled. However, In this release, we recommend to use caching system, since no-caching is slow and not fully tested.
In the future release, we will add support for consistent caching if there are more than one server running.
Add object
Next, we are going to add a chair into the environment.
door1 = myhome.add_object('door1')
door1.set_state('opened', '0')
We create a object in simulation called door1, and add a physical state of "opened" in door1. The door initial open state is 0, which is closed.
Add sensor
Next in order to read the chair pressure state, we are going to add a sensor into simulation.
sensor1 = myhome.add_sensor('door1_sensor', 'door1', rate=0.9, states=['opened'])
This added a door sensor called 'door1_sensor', that watching physical state "opened" of door1, that have a success rate of 90%. This means that at 90% of times, the sensor will report the correct value of the door physical state.
Add human
Next, we are going to add a human into simulation, so he can interact with it. The human simulation is provided by Hugnman class in algotron.
from algotron.hugnman import Hugnman
jackman = Hugnman('hugn jackman', myhome)
We created a human called "hugn jackman", and we can set whatever characteristic (human potential) he have by use "set" function.
Now the human is ready to interact with the environment. Now let us tell him to open the door.
jackman.do('open door1')
This well tell jackman to use action "open" to "door1" in simulation. this will change the physical state of door1, and sensor1 if the sensor successfully detected (with 90% of success chance).
We can check on the door1 state and sensor state by "get_state" function:
door1.get_state('opened')
>>> 1
sensor1.get_state('opened')
>>> 1
(OR if not success)
>>> 0
All the code
You have just finished basic simulation tutorial.
If you followed all the code, here is what your codebase should looks like:
from algotron import reset
reset()
from algotron.core import User, Simulation, Action
from algotron.hugnman import Hugnman
print '----Setup----'
# actions
Action(name='open', formula='1', physics='opened')
Action(name='close', formula='0', physics='opened')
# simulatin setup
User.login('aaron')
myhome = Simulation('myhome', caching=True)
# init object
door1 = myhome.add_object('door1')
door1.set_state('opened', '0')
# init sensor
sensor1 = myhome.add_sensor('door1_sensor', 'door1', rate=0.9, states=['opened'])
print '___Setup END___'
# init human
jackman = Hugnman('hugn jackman', myhome)
print '---- door1 test ----'
print 'door1: {}'.format(sensor1.get_state('opened'))
print 'door1: {}'.format(sensor1.get_state('opened'))
Updated over 8 years ago