from explauto.experiment import Experiment

Experiment.from_settings

"""
Explanation: Running basic experiments
In this notebook, we will show you how to setup an experiment using a predifined simultated arm environment and use its default configuration. We will then run it and see how to plot the results. 
We assume that you have already installed explauto on your computer (see the installation section) and that everything works as expected. We also assume that you are familiar with the scientific grounding behind the library (see our short introduction).
Setup an experiment
Thanks to explauto, setting up an experiment is really simple. You only have to instantiate the Experiment class.
The easiest way to do that is use the from_settings factory. This method automatically creates and setups an experiment using the default settings. You only have to choose:
* the experimental Environment,
* the type of babbling (motor vs goal),
* the InterestModel,
* and the SensorimotorModel.
End of explanation
"""


from explauto.environment import environments

print 'Available environments: {}'.format(environments.keys())

"""
Explanation: You can check the documentation of the from_settings() class method for details.
Finding the available environments
As shown on the documentation, you must specify the environment and models using their name. This API has been chosen so you can have really compact code and can easily switch from one environment (or model) to another.
Yet, you need to know which environments are available (this may depend on your installation of explauto). To do that, you can directly acess the available environments dictionary from the environment package. It is constructed as follows:
python
{env_name: (env_cls, env_configs, env_testcases)}
where env_class is the class implementing the environment, env_configs are the available configurations for that environment and env_testcases is a function generating test cases for learning performance evaluation.
End of explanation
"""


env_cls, env_configs, testcases_generator = environments['simple_arm']
print 'Available configurations for the simple arm environment: {}'.format(env_configs.keys())

default_config = env_configs['default']

print 'Default configuration for the simple arm:'
for config_key, value in default_config.items():
    print '\t{}: {}'.format(config_key, value)

"""
Explanation: You can see here, that on this installation of explauto two environments are available:
* the pendulum
* the simple arm
* the npendulum (beta)
We will use the simple arm for this tutorial. It consists in the simulation of a $n$ degrees-of-freedom (DoF) arm with movements limited to a 2D plan, as detailled in this tutorial.
Each available environment comes with a set of predefined configurations. A default configuration will always be defined.
End of explanation
"""


from explauto.interest_model import interest_models
from explauto.sensorimotor_model import sensorimotor_models

print 'Available interest models: {}'.format(interest_models.keys())
print 'Available sensorimotor models: {}'.format(sensorimotor_models.keys())

"""
Explanation: Finding the available models
You can use the same trick for the interest and sensorimotor models.
End of explanation
"""


from explauto.experiment import Experiment, make_settings

s = make_settings(environment='simple_arm',
                  babbling_mode='motor', 
                  interest_model='random',
                  sensorimotor_model='nearest_neighbor')

expe = Experiment.from_settings(s)

"""
Explanation: Instantiating the experiment
Once you chose your environments and models you can directly instantiate the Experiment class using the convenience method from_settings. In this example, we chose to do motor babbling.
End of explanation
"""


expe.evaluate_at([1, 10, 20, 30, 100, 200, 300, 400], s.default_testcases)

"""
Explanation: The next step is to define at which iteration the learning should be evaluated. Note that by default your experiment will run until reaching the last evaluation.
Let's say we will evaluate at iteration steps: 1, 10, 20, 30, 100, and 200.
End of explanation
"""


expe.run()

"""
Explanation: Running the experiment
And now that the experiment is set up, you can run it! As we have defined evaluation steps, the experiment will run for 200 iterations (the last evaluation step).
Be aware that this step may actually run for a while.
End of explanation
"""


%pylab inline

ax = axes()
expe.log.plot_learning_curve(ax)

"""
Explanation: Plotting results
Now that your experiment is done, you want to plot the results.
For instance, you can plot the learning curve thanks to:
End of explanation
"""


ax = axes()
expe.log.scatter_plot(ax, [('sensori', [0, 1]), ], color='green')

"""
Explanation: You can also plot projection of the reached points of the sensorimotor space thanks to:
End of explanation
"""


# Solution
%load exercise_solutions/setting_basic_experiment__goal_babbling.py

"""
Explanation: Exercise
Run the same experiment using goal babbling instead for motor babbling.
End of explanation
"""


