From 9c4f153a2814bd53db5ae54508fab6885796d35f Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Tue, 9 Nov 2010 16:49:47 +0100
Subject: [PATCH 01/83] invisible
---
examples/optimization/benchmarkplots.py | 0
examples/optimization/multiobjective/nsga2.py | 0
examples/optimization/optimizerinterface.py | 0
examples/optimization/optimizers_for_rl.py | 0
examples/rl/environments/capturegame/evolvingplayer.py | 0
examples/rl/environments/capturegame/minitournament.py | 0
examples/rl/environments/capturegame/pente.py | 0
examples/rl/environments/cartpole/cart_all.py | 0
examples/rl/environments/cartpole/cart_cma.py | 0
examples/rl/environments/cartpole/cart_enac.py | 0
examples/rl/environments/cartpole/cart_fem.py | 0
examples/rl/environments/cartpole/cart_nes.py | 0
examples/rl/environments/cartpole/cart_pgpe.py | 0
examples/rl/environments/cartpole/cart_reinf.py | 0
examples/rl/environments/cartpole/play_cartpole.py | 0
examples/rl/environments/flexcube/flexcube_pgpe.py | 0
examples/rl/environments/flexcube/flexcube_spsa.py | 0
examples/rl/environments/maze/td.py | 0
examples/rl/environments/ode/acrobot_fd.py | 0
examples/rl/environments/ode/acrobot_pgpe.py | 0
examples/rl/environments/ode/ccrl_glass_pgpe.py | 0
examples/rl/environments/ode/ccrl_plate_pgpe.py | 0
examples/rl/environments/ode/johnnie_pgpe.py | 0
examples/rl/environments/ode/johnnie_reinforce.py | 0
examples/rl/environments/shipsteer/shipbench_pgpe.py | 0
examples/rl/environments/shipsteer/shipbench_sde.py | 0
examples/rl/valuebased/nfq.py | 0
examples/rl/valuebased/td.py | 0
examples/supervised/backprop/backpropanbncn.py | 0
examples/supervised/backprop/backpropxor.py | 0
examples/supervised/backprop/datasets/anbncn.py | 0
examples/supervised/backprop/datasets/parity.py | 0
examples/supervised/backprop/datasets/xor.py | 0
examples/supervised/backprop/parityrnn.py | 0
examples/supervised/evolino/superimposed_sine.py | 0
examples/supervised/neuralnets+svm/datasets/datagenerator.py | 0
examples/supervised/neuralnets+svm/example_fnn.py | 0
examples/supervised/neuralnets+svm/example_rnn.py | 0
examples/supervised/neuralnets+svm/example_svm.py | 0
examples/unsupervised/gp.py | 0
examples/unsupervised/kohonen.py | 0
examples/unsupervised/lsh.py | 0
examples/unsupervised/rbm.py | 0
pybrain/tests/optimizationtest.py | 0
pybrain/tests/runtests.py | 0
45 files changed, 0 insertions(+), 0 deletions(-)
mode change 100755 => 100644 examples/optimization/benchmarkplots.py
mode change 100755 => 100644 examples/optimization/multiobjective/nsga2.py
mode change 100755 => 100644 examples/optimization/optimizerinterface.py
mode change 100755 => 100644 examples/optimization/optimizers_for_rl.py
mode change 100755 => 100644 examples/rl/environments/capturegame/evolvingplayer.py
mode change 100755 => 100644 examples/rl/environments/capturegame/minitournament.py
mode change 100755 => 100644 examples/rl/environments/capturegame/pente.py
mode change 100755 => 100644 examples/rl/environments/cartpole/cart_all.py
mode change 100755 => 100644 examples/rl/environments/cartpole/cart_cma.py
mode change 100755 => 100644 examples/rl/environments/cartpole/cart_enac.py
mode change 100755 => 100644 examples/rl/environments/cartpole/cart_fem.py
mode change 100755 => 100644 examples/rl/environments/cartpole/cart_nes.py
mode change 100755 => 100644 examples/rl/environments/cartpole/cart_pgpe.py
mode change 100755 => 100644 examples/rl/environments/cartpole/cart_reinf.py
mode change 100755 => 100644 examples/rl/environments/cartpole/play_cartpole.py
mode change 100755 => 100644 examples/rl/environments/flexcube/flexcube_pgpe.py
mode change 100755 => 100644 examples/rl/environments/flexcube/flexcube_spsa.py
mode change 100755 => 100644 examples/rl/environments/maze/td.py
mode change 100755 => 100644 examples/rl/environments/ode/acrobot_fd.py
mode change 100755 => 100644 examples/rl/environments/ode/acrobot_pgpe.py
mode change 100755 => 100644 examples/rl/environments/ode/ccrl_glass_pgpe.py
mode change 100755 => 100644 examples/rl/environments/ode/ccrl_plate_pgpe.py
mode change 100755 => 100644 examples/rl/environments/ode/johnnie_pgpe.py
mode change 100755 => 100644 examples/rl/environments/ode/johnnie_reinforce.py
mode change 100755 => 100644 examples/rl/environments/shipsteer/shipbench_pgpe.py
mode change 100755 => 100644 examples/rl/environments/shipsteer/shipbench_sde.py
mode change 100755 => 100644 examples/rl/valuebased/nfq.py
mode change 100755 => 100644 examples/rl/valuebased/td.py
mode change 100755 => 100644 examples/supervised/backprop/backpropanbncn.py
mode change 100755 => 100644 examples/supervised/backprop/backpropxor.py
mode change 100755 => 100644 examples/supervised/backprop/datasets/anbncn.py
mode change 100755 => 100644 examples/supervised/backprop/datasets/parity.py
mode change 100755 => 100644 examples/supervised/backprop/datasets/xor.py
mode change 100755 => 100644 examples/supervised/backprop/parityrnn.py
mode change 100755 => 100644 examples/supervised/evolino/superimposed_sine.py
mode change 100755 => 100644 examples/supervised/neuralnets+svm/datasets/datagenerator.py
mode change 100755 => 100644 examples/supervised/neuralnets+svm/example_fnn.py
mode change 100755 => 100644 examples/supervised/neuralnets+svm/example_rnn.py
mode change 100755 => 100644 examples/supervised/neuralnets+svm/example_svm.py
mode change 100755 => 100644 examples/unsupervised/gp.py
mode change 100755 => 100644 examples/unsupervised/kohonen.py
mode change 100755 => 100644 examples/unsupervised/lsh.py
mode change 100755 => 100644 examples/unsupervised/rbm.py
mode change 100755 => 100644 pybrain/tests/optimizationtest.py
mode change 100755 => 100644 pybrain/tests/runtests.py
diff --git a/examples/optimization/benchmarkplots.py b/examples/optimization/benchmarkplots.py
old mode 100755
new mode 100644
diff --git a/examples/optimization/multiobjective/nsga2.py b/examples/optimization/multiobjective/nsga2.py
old mode 100755
new mode 100644
diff --git a/examples/optimization/optimizerinterface.py b/examples/optimization/optimizerinterface.py
old mode 100755
new mode 100644
diff --git a/examples/optimization/optimizers_for_rl.py b/examples/optimization/optimizers_for_rl.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/capturegame/evolvingplayer.py b/examples/rl/environments/capturegame/evolvingplayer.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/capturegame/minitournament.py b/examples/rl/environments/capturegame/minitournament.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/capturegame/pente.py b/examples/rl/environments/capturegame/pente.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/cartpole/cart_all.py b/examples/rl/environments/cartpole/cart_all.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/cartpole/cart_cma.py b/examples/rl/environments/cartpole/cart_cma.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/cartpole/cart_enac.py b/examples/rl/environments/cartpole/cart_enac.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/cartpole/cart_fem.py b/examples/rl/environments/cartpole/cart_fem.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/cartpole/cart_nes.py b/examples/rl/environments/cartpole/cart_nes.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/cartpole/cart_pgpe.py b/examples/rl/environments/cartpole/cart_pgpe.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/cartpole/cart_reinf.py b/examples/rl/environments/cartpole/cart_reinf.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/cartpole/play_cartpole.py b/examples/rl/environments/cartpole/play_cartpole.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/flexcube/flexcube_pgpe.py b/examples/rl/environments/flexcube/flexcube_pgpe.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/flexcube/flexcube_spsa.py b/examples/rl/environments/flexcube/flexcube_spsa.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/maze/td.py b/examples/rl/environments/maze/td.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/ode/acrobot_fd.py b/examples/rl/environments/ode/acrobot_fd.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/ode/acrobot_pgpe.py b/examples/rl/environments/ode/acrobot_pgpe.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/ode/ccrl_glass_pgpe.py b/examples/rl/environments/ode/ccrl_glass_pgpe.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/ode/ccrl_plate_pgpe.py b/examples/rl/environments/ode/ccrl_plate_pgpe.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/ode/johnnie_pgpe.py b/examples/rl/environments/ode/johnnie_pgpe.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/ode/johnnie_reinforce.py b/examples/rl/environments/ode/johnnie_reinforce.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/shipsteer/shipbench_pgpe.py b/examples/rl/environments/shipsteer/shipbench_pgpe.py
old mode 100755
new mode 100644
diff --git a/examples/rl/environments/shipsteer/shipbench_sde.py b/examples/rl/environments/shipsteer/shipbench_sde.py
old mode 100755
new mode 100644
diff --git a/examples/rl/valuebased/nfq.py b/examples/rl/valuebased/nfq.py
old mode 100755
new mode 100644
diff --git a/examples/rl/valuebased/td.py b/examples/rl/valuebased/td.py
old mode 100755
new mode 100644
diff --git a/examples/supervised/backprop/backpropanbncn.py b/examples/supervised/backprop/backpropanbncn.py
old mode 100755
new mode 100644
diff --git a/examples/supervised/backprop/backpropxor.py b/examples/supervised/backprop/backpropxor.py
old mode 100755
new mode 100644
diff --git a/examples/supervised/backprop/datasets/anbncn.py b/examples/supervised/backprop/datasets/anbncn.py
old mode 100755
new mode 100644
diff --git a/examples/supervised/backprop/datasets/parity.py b/examples/supervised/backprop/datasets/parity.py
old mode 100755
new mode 100644
diff --git a/examples/supervised/backprop/datasets/xor.py b/examples/supervised/backprop/datasets/xor.py
old mode 100755
new mode 100644
diff --git a/examples/supervised/backprop/parityrnn.py b/examples/supervised/backprop/parityrnn.py
old mode 100755
new mode 100644
diff --git a/examples/supervised/evolino/superimposed_sine.py b/examples/supervised/evolino/superimposed_sine.py
old mode 100755
new mode 100644
diff --git a/examples/supervised/neuralnets+svm/datasets/datagenerator.py b/examples/supervised/neuralnets+svm/datasets/datagenerator.py
old mode 100755
new mode 100644
diff --git a/examples/supervised/neuralnets+svm/example_fnn.py b/examples/supervised/neuralnets+svm/example_fnn.py
old mode 100755
new mode 100644
diff --git a/examples/supervised/neuralnets+svm/example_rnn.py b/examples/supervised/neuralnets+svm/example_rnn.py
old mode 100755
new mode 100644
diff --git a/examples/supervised/neuralnets+svm/example_svm.py b/examples/supervised/neuralnets+svm/example_svm.py
old mode 100755
new mode 100644
diff --git a/examples/unsupervised/gp.py b/examples/unsupervised/gp.py
old mode 100755
new mode 100644
diff --git a/examples/unsupervised/kohonen.py b/examples/unsupervised/kohonen.py
old mode 100755
new mode 100644
diff --git a/examples/unsupervised/lsh.py b/examples/unsupervised/lsh.py
old mode 100755
new mode 100644
diff --git a/examples/unsupervised/rbm.py b/examples/unsupervised/rbm.py
old mode 100755
new mode 100644
diff --git a/pybrain/tests/optimizationtest.py b/pybrain/tests/optimizationtest.py
old mode 100755
new mode 100644
diff --git a/pybrain/tests/runtests.py b/pybrain/tests/runtests.py
old mode 100755
new mode 100644
From 03a8bb88be78d8d88333a81d95dfa37554a1a4a1 Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Tue, 24 May 2011 21:58:32 +0200
Subject: [PATCH 02/83] bugfix
---
pybrain/optimization/distributionbased/snes.py | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/pybrain/optimization/distributionbased/snes.py b/pybrain/optimization/distributionbased/snes.py
index 5f6121df5..c167179b1 100644
--- a/pybrain/optimization/distributionbased/snes.py
+++ b/pybrain/optimization/distributionbased/snes.py
@@ -56,8 +56,11 @@ def _additionalInit(self):
@property
def _population(self):
- return [self._allEvaluated[i] for i in self._pointers]
-
+ if self._wasUnwrapped:
+ return [self._allEvaluated[i].params for i in self._pointers]
+ else:
+ return [self._allEvaluated[i] for i in self._pointers]
+
@property
def _currentEvaluations(self):
fits = [self._allEvaluations[i] for i in self._pointers]
From 0b4539002b0c4cfbb6493e2d232f6f40e0162a55 Mon Sep 17 00:00:00 2001
From: schaul
Date: Wed, 25 May 2011 14:37:48 +0200
Subject: [PATCH 03/83] bugfix
---
pybrain/rl/agents/linearfa.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pybrain/rl/agents/linearfa.py b/pybrain/rl/agents/linearfa.py
index 87e1632de..3805424c6 100644
--- a/pybrain/rl/agents/linearfa.py
+++ b/pybrain/rl/agents/linearfa.py
@@ -68,7 +68,7 @@ def newEpisode(self):
else:
self._temperature *= self.temperature_decay
self._expl_proportion *= self.exploration_decay
- self.learner._decayLearningRate()
+ self.learner.newEpisode()
def learn(self):
From 092f59d6b00e9bd21bfaee888ea41413763bd0c0 Mon Sep 17 00:00:00 2001
From: Trevor Bekolay
Date: Thu, 26 May 2011 14:14:56 -0400
Subject: [PATCH 04/83] Reorganized the tests package into subpackages for
better organization and easier (shorter) file names. Any new directories
added in unittests will be automatically run by runtests.py.
---
pybrain/tests/runtests.py | 101 ++++++++++--------
pybrain/tests/unittests/auxiliary/__init__.py | 0
.../test_pca.py} | 0
pybrain/tests/unittests/datasets/__init__.py | 0
.../{ => datasets}/test_datasets_datasets.py | 0
.../tests/unittests/optimization/__init__.py | 0
.../optimization/populationbased/__init__.py | 0
.../populationbased/test_pso_ring.py} | 0
pybrain/tests/unittests/rl/__init__.py | 0
.../unittests/rl/environments/__init__.py | 0
.../environments/twoplayergames/__init__.py | 0
.../twoplayergames}/test_capture_game.py | 0
.../twoplayergames}/test_pente.py | 0
pybrain/tests/unittests/structure/__init__.py | 0
.../structure/connections/__init__.py | 0
.../connections}/test_shared_connections.py | 0
.../connections}/test_sliced_connections.py | 0
.../test_subsampling_connection.py | 0
.../unittests/structure/modules/__init__.py | 0
.../modules}/test_peephole_lstm.py | 0
.../modules}/test_peephole_mdlstm.py | 0
.../modules/test_samplelayer.py} | 0
.../modules}/test_simple_lstm_network.py | 0
.../modules}/test_simple_mdlstm.py | 0
.../unittests/structure/networks/__init__.py | 0
.../{ => structure/networks}/_test_mdrnn.py | 0
.../{ => structure/networks}/_test_rbm.py | 0
.../structure/networks/custom/__init__.py | 0
.../custom}/test_capturegame_network.py | 0
.../custom}/test_convolutional_nets.py | 0
.../networks}/test_bidirectional_network.py | 0
.../networks}/test_borderswipingnetwork.py | 0
.../networks}/test_cyclic_network.py | 0
.../networks}/test_nested_ffn_and_rnn.py | 0
.../networks}/test_nested_network.py | 0
.../networks}/test_network_decomposition.py | 0
.../test_network_forward_backward.py | 0
.../networks}/test_network_sort.py | 0
.../networks}/test_no_gravity_network.py | 0
.../networks}/test_recurrent_network.py | 0
.../networks}/test_swiping_network.py | 0
.../tests/unittests/supervised/__init__.py | 0
.../unittests/supervised/knn/__init__.py | 0
.../unittests/supervised/knn/lsh/__init__.py | 0
.../knn/lsh/test_minhash.py} | 0
.../knn/lsh/test_nearoptimal.py} | 0
.../unittests/supervised/trainers/__init__.py | 0
.../{ => supervised/trainers}/test_evolino.py | 0
.../trainers/test_rprop.py} | 0
pybrain/tests/unittests/tools/__init__.py | 0
.../test_ibp_leftordered.py} | 0
.../test_rlgluebridge.py} | 0
.../tests/unittests/unsupervised/__init__.py | 0
53 files changed, 59 insertions(+), 42 deletions(-)
create mode 100644 pybrain/tests/unittests/auxiliary/__init__.py
rename pybrain/tests/unittests/{test_auxiliary_pca.py => auxiliary/test_pca.py} (100%)
create mode 100644 pybrain/tests/unittests/datasets/__init__.py
rename pybrain/tests/unittests/{ => datasets}/test_datasets_datasets.py (100%)
create mode 100644 pybrain/tests/unittests/optimization/__init__.py
create mode 100644 pybrain/tests/unittests/optimization/populationbased/__init__.py
rename pybrain/tests/unittests/{test_rl_learner_blackbox_pso.py => optimization/populationbased/test_pso_ring.py} (100%)
create mode 100644 pybrain/tests/unittests/rl/__init__.py
create mode 100644 pybrain/tests/unittests/rl/environments/__init__.py
create mode 100644 pybrain/tests/unittests/rl/environments/twoplayergames/__init__.py
rename pybrain/tests/unittests/{ => rl/environments/twoplayergames}/test_capture_game.py (100%)
rename pybrain/tests/unittests/{ => rl/environments/twoplayergames}/test_pente.py (100%)
create mode 100644 pybrain/tests/unittests/structure/__init__.py
create mode 100644 pybrain/tests/unittests/structure/connections/__init__.py
rename pybrain/tests/unittests/{ => structure/connections}/test_shared_connections.py (100%)
rename pybrain/tests/unittests/{ => structure/connections}/test_sliced_connections.py (100%)
rename pybrain/tests/unittests/{ => structure/connections}/test_subsampling_connection.py (100%)
create mode 100644 pybrain/tests/unittests/structure/modules/__init__.py
rename pybrain/tests/unittests/{ => structure/modules}/test_peephole_lstm.py (100%)
rename pybrain/tests/unittests/{ => structure/modules}/test_peephole_mdlstm.py (100%)
rename pybrain/tests/unittests/{test_structure_modules_samplelayer.py => structure/modules/test_samplelayer.py} (100%)
rename pybrain/tests/unittests/{ => structure/modules}/test_simple_lstm_network.py (100%)
rename pybrain/tests/unittests/{ => structure/modules}/test_simple_mdlstm.py (100%)
create mode 100644 pybrain/tests/unittests/structure/networks/__init__.py
rename pybrain/tests/unittests/{ => structure/networks}/_test_mdrnn.py (100%)
rename pybrain/tests/unittests/{ => structure/networks}/_test_rbm.py (100%)
create mode 100644 pybrain/tests/unittests/structure/networks/custom/__init__.py
rename pybrain/tests/unittests/{ => structure/networks/custom}/test_capturegame_network.py (100%)
rename pybrain/tests/unittests/{ => structure/networks/custom}/test_convolutional_nets.py (100%)
rename pybrain/tests/unittests/{ => structure/networks}/test_bidirectional_network.py (100%)
rename pybrain/tests/unittests/{ => structure/networks}/test_borderswipingnetwork.py (100%)
rename pybrain/tests/unittests/{ => structure/networks}/test_cyclic_network.py (100%)
rename pybrain/tests/unittests/{ => structure/networks}/test_nested_ffn_and_rnn.py (100%)
rename pybrain/tests/unittests/{ => structure/networks}/test_nested_network.py (100%)
rename pybrain/tests/unittests/{ => structure/networks}/test_network_decomposition.py (100%)
rename pybrain/tests/unittests/{ => structure/networks}/test_network_forward_backward.py (100%)
rename pybrain/tests/unittests/{ => structure/networks}/test_network_sort.py (100%)
rename pybrain/tests/unittests/{ => structure/networks}/test_no_gravity_network.py (100%)
rename pybrain/tests/unittests/{ => structure/networks}/test_recurrent_network.py (100%)
rename pybrain/tests/unittests/{ => structure/networks}/test_swiping_network.py (100%)
create mode 100644 pybrain/tests/unittests/supervised/__init__.py
create mode 100644 pybrain/tests/unittests/supervised/knn/__init__.py
create mode 100644 pybrain/tests/unittests/supervised/knn/lsh/__init__.py
rename pybrain/tests/unittests/{test_supervised_knn_lsh_minhash.py => supervised/knn/lsh/test_minhash.py} (100%)
rename pybrain/tests/unittests/{test_supervised_knn_lsh_nearoptimal.py => supervised/knn/lsh/test_nearoptimal.py} (100%)
create mode 100644 pybrain/tests/unittests/supervised/trainers/__init__.py
rename pybrain/tests/unittests/{ => supervised/trainers}/test_evolino.py (100%)
rename pybrain/tests/unittests/{test_backprop_rprop.py => supervised/trainers/test_rprop.py} (100%)
create mode 100644 pybrain/tests/unittests/tools/__init__.py
rename pybrain/tests/unittests/{test_lof.py => tools/test_ibp_leftordered.py} (100%)
rename pybrain/tests/unittests/{test_tools_rlgluebridge.py => tools/test_rlgluebridge.py} (100%)
create mode 100644 pybrain/tests/unittests/unsupervised/__init__.py
diff --git a/pybrain/tests/runtests.py b/pybrain/tests/runtests.py
index 8b35e9f03..3918b7ea1 100644
--- a/pybrain/tests/runtests.py
+++ b/pybrain/tests/runtests.py
@@ -11,6 +11,7 @@
import logging
import os
import sys
+from copy import copy
from unittest import TestLoader, TestSuite, TextTestRunner
@@ -50,56 +51,73 @@ def missingDependencies(target_module):
dependencies = getattr(target_module, '_dependencies', [])
return [i for i in dependencies if not testImport(i)]
+def getSubDirectories(testdir):
+ """Recursively builds a list of all subdirectories in the test suite."""
+ subdirs = [os.path.join(testdir,d) for d in
+ filter(os.path.isdir,[os.path.join(testdir,dd) for dd in os.listdir(testdir)])]
+
+ for d in copy(subdirs):
+ subdirs.extend(getSubDirectories(os.path.join(testdir,d)))
+
+ return subdirs
def make_test_suite():
"""Load unittests placed in pybrain/tests/unittests, then return a
TestSuite object of those."""
- # [...]/pyrain/pybrain [cut] /tests/runtests.py
+ # [...]/pybrain/pybrain [cut] /tests/runtests.py
path = os.path.abspath(__file__).rsplit(os.sep+'tests', 1)[0]
sys.path.append(path.rstrip('pybrain'))
- testdir = os.path.join(path, 'tests', 'unittests')
- # All unittest modules have to start with 'test_' and have to be, of
- # course, python files
- module_names = [f[:-3] for f in os.listdir(testdir)
- if f.startswith('test_') and f.endswith('.py')]
-
- if not module_names:
- logging.info('No tests found in %s' % testdir)
- sys.exit()
-
- # "Magically" import the tests package and its test-modules that we've
- # found
- test_package_path = 'pybrain.tests.unittests'
- test_package = __import__(test_package_path, fromlist=module_names)
-
- # Put the test modules in a list that can be passed to the testsuite
- modules = (getattr(test_package, n) for n in module_names)
- modules = [(m, missingDependencies(m)) for m in modules]
- untests = [(m, md) for m, md in modules if md]
- modules = [m for m, md in modules if not md]
-
- # Print out modules that are missing dependencies
- for module, miss_dep in untests: # Mr Dep is not around, though
- logging.warning('Module %s is missing dependencies: %s' % (
- module.__name__, ', '.join(miss_dep)))
-
- # Print out a list of tests that are found
- for m in modules:
- logging.info('Tests found: %s' % m.__name__)
-
- # Build up the testsuite
- suite = TestSuite([TestLoader().loadTestsFromModule(m) for m in modules])
-
- # Add doctests from the unittest modules to the suite
+ top_testdir = os.path.join(path, 'tests', 'unittests')
+ testdirs = getSubDirectories(top_testdir)
+
+ # Initialize the testsuite to add to
+ suite = TestSuite()
optionflags = doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE
- for mod in modules:
- try:
- suite.addTest(doctest.DocTestSuite(mod, optionflags=optionflags))
- except ValueError:
- # No tests found.
- pass
+
+ for testdir in testdirs:
+ # All unittest modules have to start with 'test_' and have to be, of
+ # course, python files
+ module_names = [f[:-3] for f in os.listdir(testdir)
+ if f.startswith('test_') and f.endswith('.py')]
+
+ if not module_names:
+ logging.info('No tests found in %s' % testdir)
+ continue
+
+ # "Magically" import the tests package and its test-modules that we've
+ # found
+ test_package_path = 'pybrain.tests.unittests'
+ sub_path = os.path.relpath(testdir, top_testdir).split(os.sep)
+ test_package_path = '.'.join([test_package_path]+sub_path)
+ test_package = __import__(test_package_path, fromlist=module_names)
+
+ # Put the test modules in a list that can be passed to the testsuite
+ modules = (getattr(test_package, n) for n in module_names)
+ modules = [(m, missingDependencies(m)) for m in modules]
+ untests = [(m, md) for m, md in modules if md]
+ modules = [m for m, md in modules if not md]
+
+ # Print out modules that are missing dependencies
+ for module, miss_dep in untests: # Mr Dep is not around, though
+ logging.warning('Module %s is missing dependencies: %s' % (
+ module.__name__, ', '.join(miss_dep)))
+
+ # Print out a list of tests that are found
+ for m in modules:
+ logging.info('Tests found: %s' % m.__name__)
+
+ # Build up the testsuite
+ suite.addTests([TestLoader().loadTestsFromModule(m) for m in modules])
+
+ # Add doctests from the unittest modules to the suite
+ for mod in modules:
+ try:
+ suite.addTest(doctest.DocTestSuite(mod, optionflags=optionflags))
+ except ValueError:
+ # No tests found.
+ pass
return suite
@@ -108,4 +126,3 @@ def make_test_suite():
setUpLogging()
runner = TextTestRunner()
runner.run(make_test_suite())
-
diff --git a/pybrain/tests/unittests/auxiliary/__init__.py b/pybrain/tests/unittests/auxiliary/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/test_auxiliary_pca.py b/pybrain/tests/unittests/auxiliary/test_pca.py
similarity index 100%
rename from pybrain/tests/unittests/test_auxiliary_pca.py
rename to pybrain/tests/unittests/auxiliary/test_pca.py
diff --git a/pybrain/tests/unittests/datasets/__init__.py b/pybrain/tests/unittests/datasets/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/test_datasets_datasets.py b/pybrain/tests/unittests/datasets/test_datasets_datasets.py
similarity index 100%
rename from pybrain/tests/unittests/test_datasets_datasets.py
rename to pybrain/tests/unittests/datasets/test_datasets_datasets.py
diff --git a/pybrain/tests/unittests/optimization/__init__.py b/pybrain/tests/unittests/optimization/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/optimization/populationbased/__init__.py b/pybrain/tests/unittests/optimization/populationbased/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/test_rl_learner_blackbox_pso.py b/pybrain/tests/unittests/optimization/populationbased/test_pso_ring.py
similarity index 100%
rename from pybrain/tests/unittests/test_rl_learner_blackbox_pso.py
rename to pybrain/tests/unittests/optimization/populationbased/test_pso_ring.py
diff --git a/pybrain/tests/unittests/rl/__init__.py b/pybrain/tests/unittests/rl/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/rl/environments/__init__.py b/pybrain/tests/unittests/rl/environments/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/rl/environments/twoplayergames/__init__.py b/pybrain/tests/unittests/rl/environments/twoplayergames/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/test_capture_game.py b/pybrain/tests/unittests/rl/environments/twoplayergames/test_capture_game.py
similarity index 100%
rename from pybrain/tests/unittests/test_capture_game.py
rename to pybrain/tests/unittests/rl/environments/twoplayergames/test_capture_game.py
diff --git a/pybrain/tests/unittests/test_pente.py b/pybrain/tests/unittests/rl/environments/twoplayergames/test_pente.py
similarity index 100%
rename from pybrain/tests/unittests/test_pente.py
rename to pybrain/tests/unittests/rl/environments/twoplayergames/test_pente.py
diff --git a/pybrain/tests/unittests/structure/__init__.py b/pybrain/tests/unittests/structure/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/structure/connections/__init__.py b/pybrain/tests/unittests/structure/connections/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/test_shared_connections.py b/pybrain/tests/unittests/structure/connections/test_shared_connections.py
similarity index 100%
rename from pybrain/tests/unittests/test_shared_connections.py
rename to pybrain/tests/unittests/structure/connections/test_shared_connections.py
diff --git a/pybrain/tests/unittests/test_sliced_connections.py b/pybrain/tests/unittests/structure/connections/test_sliced_connections.py
similarity index 100%
rename from pybrain/tests/unittests/test_sliced_connections.py
rename to pybrain/tests/unittests/structure/connections/test_sliced_connections.py
diff --git a/pybrain/tests/unittests/test_subsampling_connection.py b/pybrain/tests/unittests/structure/connections/test_subsampling_connection.py
similarity index 100%
rename from pybrain/tests/unittests/test_subsampling_connection.py
rename to pybrain/tests/unittests/structure/connections/test_subsampling_connection.py
diff --git a/pybrain/tests/unittests/structure/modules/__init__.py b/pybrain/tests/unittests/structure/modules/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/test_peephole_lstm.py b/pybrain/tests/unittests/structure/modules/test_peephole_lstm.py
similarity index 100%
rename from pybrain/tests/unittests/test_peephole_lstm.py
rename to pybrain/tests/unittests/structure/modules/test_peephole_lstm.py
diff --git a/pybrain/tests/unittests/test_peephole_mdlstm.py b/pybrain/tests/unittests/structure/modules/test_peephole_mdlstm.py
similarity index 100%
rename from pybrain/tests/unittests/test_peephole_mdlstm.py
rename to pybrain/tests/unittests/structure/modules/test_peephole_mdlstm.py
diff --git a/pybrain/tests/unittests/test_structure_modules_samplelayer.py b/pybrain/tests/unittests/structure/modules/test_samplelayer.py
similarity index 100%
rename from pybrain/tests/unittests/test_structure_modules_samplelayer.py
rename to pybrain/tests/unittests/structure/modules/test_samplelayer.py
diff --git a/pybrain/tests/unittests/test_simple_lstm_network.py b/pybrain/tests/unittests/structure/modules/test_simple_lstm_network.py
similarity index 100%
rename from pybrain/tests/unittests/test_simple_lstm_network.py
rename to pybrain/tests/unittests/structure/modules/test_simple_lstm_network.py
diff --git a/pybrain/tests/unittests/test_simple_mdlstm.py b/pybrain/tests/unittests/structure/modules/test_simple_mdlstm.py
similarity index 100%
rename from pybrain/tests/unittests/test_simple_mdlstm.py
rename to pybrain/tests/unittests/structure/modules/test_simple_mdlstm.py
diff --git a/pybrain/tests/unittests/structure/networks/__init__.py b/pybrain/tests/unittests/structure/networks/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/_test_mdrnn.py b/pybrain/tests/unittests/structure/networks/_test_mdrnn.py
similarity index 100%
rename from pybrain/tests/unittests/_test_mdrnn.py
rename to pybrain/tests/unittests/structure/networks/_test_mdrnn.py
diff --git a/pybrain/tests/unittests/_test_rbm.py b/pybrain/tests/unittests/structure/networks/_test_rbm.py
similarity index 100%
rename from pybrain/tests/unittests/_test_rbm.py
rename to pybrain/tests/unittests/structure/networks/_test_rbm.py
diff --git a/pybrain/tests/unittests/structure/networks/custom/__init__.py b/pybrain/tests/unittests/structure/networks/custom/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/test_capturegame_network.py b/pybrain/tests/unittests/structure/networks/custom/test_capturegame_network.py
similarity index 100%
rename from pybrain/tests/unittests/test_capturegame_network.py
rename to pybrain/tests/unittests/structure/networks/custom/test_capturegame_network.py
diff --git a/pybrain/tests/unittests/test_convolutional_nets.py b/pybrain/tests/unittests/structure/networks/custom/test_convolutional_nets.py
similarity index 100%
rename from pybrain/tests/unittests/test_convolutional_nets.py
rename to pybrain/tests/unittests/structure/networks/custom/test_convolutional_nets.py
diff --git a/pybrain/tests/unittests/test_bidirectional_network.py b/pybrain/tests/unittests/structure/networks/test_bidirectional_network.py
similarity index 100%
rename from pybrain/tests/unittests/test_bidirectional_network.py
rename to pybrain/tests/unittests/structure/networks/test_bidirectional_network.py
diff --git a/pybrain/tests/unittests/test_borderswipingnetwork.py b/pybrain/tests/unittests/structure/networks/test_borderswipingnetwork.py
similarity index 100%
rename from pybrain/tests/unittests/test_borderswipingnetwork.py
rename to pybrain/tests/unittests/structure/networks/test_borderswipingnetwork.py
diff --git a/pybrain/tests/unittests/test_cyclic_network.py b/pybrain/tests/unittests/structure/networks/test_cyclic_network.py
similarity index 100%
rename from pybrain/tests/unittests/test_cyclic_network.py
rename to pybrain/tests/unittests/structure/networks/test_cyclic_network.py
diff --git a/pybrain/tests/unittests/test_nested_ffn_and_rnn.py b/pybrain/tests/unittests/structure/networks/test_nested_ffn_and_rnn.py
similarity index 100%
rename from pybrain/tests/unittests/test_nested_ffn_and_rnn.py
rename to pybrain/tests/unittests/structure/networks/test_nested_ffn_and_rnn.py
diff --git a/pybrain/tests/unittests/test_nested_network.py b/pybrain/tests/unittests/structure/networks/test_nested_network.py
similarity index 100%
rename from pybrain/tests/unittests/test_nested_network.py
rename to pybrain/tests/unittests/structure/networks/test_nested_network.py
diff --git a/pybrain/tests/unittests/test_network_decomposition.py b/pybrain/tests/unittests/structure/networks/test_network_decomposition.py
similarity index 100%
rename from pybrain/tests/unittests/test_network_decomposition.py
rename to pybrain/tests/unittests/structure/networks/test_network_decomposition.py
diff --git a/pybrain/tests/unittests/test_network_forward_backward.py b/pybrain/tests/unittests/structure/networks/test_network_forward_backward.py
similarity index 100%
rename from pybrain/tests/unittests/test_network_forward_backward.py
rename to pybrain/tests/unittests/structure/networks/test_network_forward_backward.py
diff --git a/pybrain/tests/unittests/test_network_sort.py b/pybrain/tests/unittests/structure/networks/test_network_sort.py
similarity index 100%
rename from pybrain/tests/unittests/test_network_sort.py
rename to pybrain/tests/unittests/structure/networks/test_network_sort.py
diff --git a/pybrain/tests/unittests/test_no_gravity_network.py b/pybrain/tests/unittests/structure/networks/test_no_gravity_network.py
similarity index 100%
rename from pybrain/tests/unittests/test_no_gravity_network.py
rename to pybrain/tests/unittests/structure/networks/test_no_gravity_network.py
diff --git a/pybrain/tests/unittests/test_recurrent_network.py b/pybrain/tests/unittests/structure/networks/test_recurrent_network.py
similarity index 100%
rename from pybrain/tests/unittests/test_recurrent_network.py
rename to pybrain/tests/unittests/structure/networks/test_recurrent_network.py
diff --git a/pybrain/tests/unittests/test_swiping_network.py b/pybrain/tests/unittests/structure/networks/test_swiping_network.py
similarity index 100%
rename from pybrain/tests/unittests/test_swiping_network.py
rename to pybrain/tests/unittests/structure/networks/test_swiping_network.py
diff --git a/pybrain/tests/unittests/supervised/__init__.py b/pybrain/tests/unittests/supervised/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/supervised/knn/__init__.py b/pybrain/tests/unittests/supervised/knn/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/supervised/knn/lsh/__init__.py b/pybrain/tests/unittests/supervised/knn/lsh/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/test_supervised_knn_lsh_minhash.py b/pybrain/tests/unittests/supervised/knn/lsh/test_minhash.py
similarity index 100%
rename from pybrain/tests/unittests/test_supervised_knn_lsh_minhash.py
rename to pybrain/tests/unittests/supervised/knn/lsh/test_minhash.py
diff --git a/pybrain/tests/unittests/test_supervised_knn_lsh_nearoptimal.py b/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
similarity index 100%
rename from pybrain/tests/unittests/test_supervised_knn_lsh_nearoptimal.py
rename to pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
diff --git a/pybrain/tests/unittests/supervised/trainers/__init__.py b/pybrain/tests/unittests/supervised/trainers/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/test_evolino.py b/pybrain/tests/unittests/supervised/trainers/test_evolino.py
similarity index 100%
rename from pybrain/tests/unittests/test_evolino.py
rename to pybrain/tests/unittests/supervised/trainers/test_evolino.py
diff --git a/pybrain/tests/unittests/test_backprop_rprop.py b/pybrain/tests/unittests/supervised/trainers/test_rprop.py
similarity index 100%
rename from pybrain/tests/unittests/test_backprop_rprop.py
rename to pybrain/tests/unittests/supervised/trainers/test_rprop.py
diff --git a/pybrain/tests/unittests/tools/__init__.py b/pybrain/tests/unittests/tools/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/pybrain/tests/unittests/test_lof.py b/pybrain/tests/unittests/tools/test_ibp_leftordered.py
similarity index 100%
rename from pybrain/tests/unittests/test_lof.py
rename to pybrain/tests/unittests/tools/test_ibp_leftordered.py
diff --git a/pybrain/tests/unittests/test_tools_rlgluebridge.py b/pybrain/tests/unittests/tools/test_rlgluebridge.py
similarity index 100%
rename from pybrain/tests/unittests/test_tools_rlgluebridge.py
rename to pybrain/tests/unittests/tools/test_rlgluebridge.py
diff --git a/pybrain/tests/unittests/unsupervised/__init__.py b/pybrain/tests/unittests/unsupervised/__init__.py
new file mode 100644
index 000000000..e69de29bb
From 76e3a0fd6ac8cefb50efdcc64bbcad9808506efd Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Tue, 15 Nov 2011 18:53:58 -0500
Subject: [PATCH 05/83] Bugfix for _resetBuffers()
---
pybrain/structure/modules/module.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/pybrain/structure/modules/module.py b/pybrain/structure/modules/module.py
index e93010700..2b0ed8fec 100644
--- a/pybrain/structure/modules/module.py
+++ b/pybrain/structure/modules/module.py
@@ -54,6 +54,8 @@ def _resetBuffers(self, length=1):
"""Reset buffers to a length (in time dimension) of 1."""
for buffername, dim in self.bufferlist:
setattr(self, buffername, zeros((length, dim)))
+ if length==1:
+ self.offset = 0
def _growBuffers(self):
"""Double the size of the modules buffers in its first dimension and
@@ -92,7 +94,7 @@ def shift(self, items):
if items == 0:
return
self.offset += items
- for buffername, l in self.bufferlist:
+ for buffername, _ in self.bufferlist:
buf = getattr(self, buffername)
assert abs(items) <= len(buf), "Cannot shift further than length of buffer."
fill = zeros((abs(items), len(buf[0])))
From 87c7ac3152dcfe5de1920f3c433351becb40eaa9 Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Tue, 29 Nov 2011 15:17:09 -0500
Subject: [PATCH 06/83] 0.3.1
---
setup.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/setup.py b/setup.py
index 25eb12005..206ddfa25 100644
--- a/setup.py
+++ b/setup.py
@@ -10,7 +10,7 @@
setup(
name="PyBrain",
- version="0.3",
+ version="0.3.1",
description="PyBrain is the Swiss army knife for neural networking.",
license="BSD",
keywords="Neural Networks Machine Learning",
From cee417a499aeede2956c53fffa23817e4f7105e6 Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Thu, 16 Feb 2012 18:35:09 -0500
Subject: [PATCH 07/83] Rank-1 NES algorithm
---
.../optimization/distributionbased/rank1.py | 186 ++++++++++++++++++
pybrain/tools/neuralnets.py | 3 +-
2 files changed, 188 insertions(+), 1 deletion(-)
create mode 100644 pybrain/optimization/distributionbased/rank1.py
diff --git a/pybrain/optimization/distributionbased/rank1.py b/pybrain/optimization/distributionbased/rank1.py
new file mode 100644
index 000000000..2fbea8de8
--- /dev/null
+++ b/pybrain/optimization/distributionbased/rank1.py
@@ -0,0 +1,186 @@
+__author__ = 'Tom Schaul, Tobias Glasmachers'
+
+
+from scipy import dot, array, randn, exp, floor, log, sqrt, ones, multiply, log2
+
+from pybrain.tools.rankingfunctions import HansenRanking
+from pybrain.optimization.distributionbased.distributionbased import DistributionBasedOptimizer
+
+
+
+class Rank1NES(DistributionBasedOptimizer):
+ """ Natural Evolution Strategies with rank-1 covariance matrices.
+
+ See http://arxiv.org/abs/1106.1998 for a description. """
+
+ # parameters, which can be set but have a good (adapted) default value
+ centerLearningRate = 1.0
+ scaleLearningRate = None
+ covLearningRate = None
+ batchSize = None
+ uniformBaseline = True
+ shapingFunction = HansenRanking()
+
+ # fixed settings
+ mustMaximize = True
+ storeAllEvaluations = True
+ storeAllEvaluated = True
+ storeAllDistributions = True
+ storeAllRates = True
+ verboseGaps = 1
+ initVariance = 1.
+ varianceCutoff = 1e-20
+
+
+ def _additionalInit(self):
+ # heuristic settings
+ if self.covLearningRate is None:
+ self.covLearningRate = self._initLearningRate()
+ if self.scaleLearningRate is None:
+ self.scaleLearningRate = self.covLearningRate
+ if self.batchSize is None:
+ self.batchSize = self._initBatchSize()
+
+ # other initializations
+ self._center = self._initEvaluable.copy()
+ self._logDetA = log(self.initVariance) / 2
+ self._principalVector = randn(self.numParameters)
+ self._principalVector /= sqrt(dot(self._principalVector, self._principalVector))
+ self._allDistributions = [(self._center.copy(), self._principalVector.copy(), self._logDetA)]
+ self.covLearningRate = 0.1
+ self.batchSize = int(max(5,max(4*log2(self.numParameters),0.2*self.numParameters)))
+ self.uniformBaseline = False
+ self.scaleLearningRate = 0.1
+
+ def _stoppingCriterion(self):
+ if DistributionBasedOptimizer._stoppingCriterion(self):
+ return True
+ elif self._getMaxVariance < self.varianceCutoff:
+ return True
+ else:
+ return False
+
+ @property
+ def _getMaxVariance(self):
+ return exp(self._logDetA * 2 / self.numParameters)
+
+ def _initLearningRate(self):
+ return 0.6 * (3 + log(self.numParameters)) / self.numParameters / sqrt(self.numParameters)
+
+ def _initBatchSize(self):
+ return 4 + int(floor(3 * log(self.numParameters)))
+
+ @property
+ def _population(self):
+ return [self._allEvaluated[i] for i in self._pointers]
+
+ @property
+ def _currentEvaluations(self):
+ fits = [self._allEvaluations[i] for i in self._pointers]
+ if self._wasOpposed:
+ fits = map(lambda x:-x, fits)
+ return fits
+
+ def _produceSample(self):
+ return randn(self.numParameters + 1)
+
+ def _produceSamples(self):
+ """ Append batch size new samples and evaluate them. """
+ tmp = [self._sample2base(self._produceSample()) for _ in range(self.batchSize)]
+ map(self._oneEvaluation, tmp)
+ self._pointers = list(range(len(self._allEvaluated) - self.batchSize, len(self._allEvaluated)))
+
+ def _notify(self):
+ """ Provide some feedback during the run. """
+ if self.verbose:
+ if self.numEvaluations % self.verboseGaps == 0:
+ print 'Step:', self.numLearningSteps, 'best:', self.bestEvaluation,
+ print 'logVar', round(self._logDetA, 3),
+ print 'log|vector|', round(log(dot(self._principalVector, self._principalVector))/2, 3)
+
+ if self.listener is not None:
+ self.listener(self.bestEvaluable, self.bestEvaluation)
+
+ def _learnStep(self):
+ # concatenations of y vector and z value
+ samples = [self._produceSample() for _ in range(self.batchSize)]
+
+ u = self._principalVector
+ a = self._logDetA
+
+ # unnamed in paper (y+zu), or x/exp(lambda)
+ W = [s[:-1] + u * s[-1] for s in samples]
+ points = [self._center+exp(a) *w for w in W]
+
+ map(self._oneEvaluation, points)
+
+ self._pointers = list(range(len(self._allEvaluated) - self.batchSize, len(self._allEvaluated)))
+
+ utilities = self.shapingFunction(self._currentEvaluations)
+ utilities /= sum(utilities) # make the utilities sum to 1
+ if self.uniformBaseline:
+ utilities -= 1. / self.batchSize
+
+ W = [w for i,w in enumerate(W) if utilities[i] != 0]
+ utilities = [uw for uw in utilities if uw != 0]
+
+ dim = self.numParameters
+
+ r = sqrt(dot(u, u))
+ v = u / r
+ c = log(r)
+
+ #inner products, but not scaled with exp(lambda)
+ wws = array([dot(w, w) for w in W])
+ wvs = array([dot(v, w) for w in W])
+ wv2s = array([wv ** 2 for wv in wvs])
+
+ dCenter = exp(self._logDetA) * dot(utilities, W)
+ self._center += self.centerLearningRate * dCenter
+
+ kp = ((r ** 2 - dim + 2) * wv2s - (r ** 2 + 1) * wws) / (2 * r * (dim - 1.))
+
+ # natural gradient on lambda, equation (5)
+ da = 1. / (2 * (dim - 1)) * dot((wws - dim) - (wv2s - 1), utilities)
+
+ # natural gradient on u, equation (6)
+ du = dot(kp, utilities) * v + dot(multiply(wvs / r, utilities), W)
+
+ # equation (7)
+ dc = dot(du, v) / r
+
+ # equation (8)
+ dv = du / r - dc * v
+
+ epsilon = min(self.covLearningRate, 2 * sqrt(r ** 2 / dot(du, du)))
+ if dc > 0:
+ # additive update
+ self._principalVector += epsilon * du
+ else:
+ # multiplicative update
+ # prevents instability
+ c += epsilon * dc
+ v += epsilon * dv
+ v /= sqrt(dot(v, v))
+ r = exp(c)
+ self._principalVector = r * v
+
+ self._lastLogDetA = self._logDetA
+ self._logDetA += self.scaleLearningRate * da
+
+ if self.storeAllDistributions:
+ self._allDistributions.append((self._center.copy(), self._principalVector.copy(), self._logDetA))
+
+
+def test():
+ """ Rank-1 NEX easily solves high-dimensional Rosenbrock functions. """
+ from pybrain.rl.environments.functions.unimodal import RosenbrockFunction
+ dim = 40
+ f = RosenbrockFunction(dim)
+ x0 = -ones(dim)
+ l = Rank1NES(f, x0, verbose=True, verboseGaps=500)
+ l.learn()
+
+
+if __name__ == '__main__':
+ test()
\ No newline at end of file
diff --git a/pybrain/tools/neuralnets.py b/pybrain/tools/neuralnets.py
index be2de6734..29ff86351 100644
--- a/pybrain/tools/neuralnets.py
+++ b/pybrain/tools/neuralnets.py
@@ -208,7 +208,8 @@ def setupRNN(self, trainer=BackpropTrainer, hidden=None, **trnargs):
self.hidden = hidden
self._convertAllDataToOneOfMany()
- RNN = buildNetwork(self.DS.indim, self.hidden, self.DS.outdim, hiddenclass=LSTMLayer, outclass=SoftmaxLayer)
+ RNN = buildNetwork(self.DS.indim, self.hidden, self.DS.outdim, hiddenclass=LSTMLayer,
+ recurrent=True, outclass=SoftmaxLayer)
logging.info("Constructing classification RNN with following config:")
logging.info(str(RNN) + "\n Hidden units:\n " + str(self.hidden))
logging.info("Trainer received the following special arguments:")
From 8fe989d1cd0db53d285230120a16ff0a197c2790 Mon Sep 17 00:00:00 2001
From: unknown
Date: Mon, 5 Mar 2012 18:27:00 +0100
Subject: [PATCH 08/83] Rank-1 NES algorithm
Signed-off-by: unknown
---
.../multiobjective/constnsga2jpq.py | 68 ++++++
.../optimization/multiobjective/nsga2jpq.py | 58 ++++++
.../optimization/multiobjective/readme.txt | 47 +++++
.../jpq2layersReader.py | 70 +++++++
.../jpq2layersWriter.py | 130 ++++++++++++
.../Test Network Reader&Writer/myparam2.txt | 16 ++
.../Test Network Reader&Writer/readme.txt | 24 +++
.../Test Network Reader&Writer/trndata | 80 ++++++++
.../optimization/distributionbased/rank1.py | 186 +++++++++++++++++
pybrain/optimization/optimizer.py | 47 ++++-
pybrain/optimization/populationbased/ga.py | 127 +++++++++++-
.../multiobjective/__init__.py | 5 +-
.../multiobjective/constnsga2.py | 89 ++++++++
.../populationbased/multiobjective/nsga2.py | 17 +-
.../environments/functions/multiobjective.py | 194 +++++++++++++++++-
.../environments/functions/transformations.py | 8 +-
pybrain/tools/customxml/handling.py | 8 +-
pybrain/tools/neuralnets.py | 3 +-
pybrain/tools/nondominated.py | 114 +++++++++-
19 files changed, 1268 insertions(+), 23 deletions(-)
create mode 100644 examples/optimization/multiobjective/constnsga2jpq.py
create mode 100644 examples/optimization/multiobjective/nsga2jpq.py
create mode 100644 examples/optimization/multiobjective/readme.txt
create mode 100644 examples/supervised/Test Network Reader&Writer/jpq2layersReader.py
create mode 100644 examples/supervised/Test Network Reader&Writer/jpq2layersWriter.py
create mode 100644 examples/supervised/Test Network Reader&Writer/myparam2.txt
create mode 100644 examples/supervised/Test Network Reader&Writer/readme.txt
create mode 100644 examples/supervised/Test Network Reader&Writer/trndata
create mode 100644 pybrain/optimization/distributionbased/rank1.py
create mode 100644 pybrain/optimization/populationbased/multiobjective/constnsga2.py
diff --git a/examples/optimization/multiobjective/constnsga2jpq.py b/examples/optimization/multiobjective/constnsga2jpq.py
new file mode 100644
index 000000000..bfdbd6dbd
--- /dev/null
+++ b/examples/optimization/multiobjective/constnsga2jpq.py
@@ -0,0 +1,68 @@
+#!/usr/bin/env python
+""" An illustration of using the NSGA-II multi-objective optimization algorithm
+on Constrained Multi-Objective Optimization benchmark function. """
+
+__author__ = 'Jean Pierre Queau, jeanpierre.queau@sbmoffshore.com'
+
+from pybrain.optimization import ConstMultiObjectiveGA
+from pybrain.rl.environments.functions.multiobjective import ConstDeb,ConstSrn, \
+ ConstOsy,ConstTnk,ConstBnh
+import pylab
+from scipy import zeros, array
+
+# The Deb function
+#f = ConstDeb()
+# The Srinivas & Deb function
+#f = ConstSrn()
+# The Osyczka & Kundu function
+#f = ConstOsy()
+# The Tanaka function
+#f = ConstTnk()
+# The Binh & Korn function
+f = ConstBnh()
+# start at the origin
+x0 = zeros(f.indim)
+
+x0 = array([min_ for min_, max_ in f.xbound])
+
+# the optimization for a maximum of 25 generations
+n = ConstMultiObjectiveGA(f, x0, storeAllEvaluations = True, populationSize = 100, eliteProportion = 1.0,
+ topProportion = 1.0, mutationProb = 1.0, mutationStdDev = 0.3, storeAllPopulations = True, allowEquality = False)
+print 'Start Learning'
+n.learn(50)
+print 'End Learning'
+# plotting the results (blue = all evaluated points, red = resulting pareto front)
+print 'Plotting the Results'
+print 'All Evaluations.... take some time'
+for x in n._allEvaluations:
+ if x[1]:
+ pylab.plot([x[0][0]], [x[0][1]], 'b.')
+ else:
+ pylab.plot([x[0][0]], [x[0][1]], 'r.')
+for x in n.bestEvaluation: pylab.plot([x[0][0]], [x[0][1]], 'go')
+pylab.show()
+print 'Pareto Front'
+for x in n.bestEvaluation: pylab.plot([x[0][0]], [x[0][1]], 'go')
+pylab.show()
+
+print '==========='
+print '= Results ='
+print '==========='
+'''
+i=0
+for gen in n._allGenerations:
+ print 'Generation: ',i
+ for j in range(len(gen[1])):
+ print gen[1].keys()[j],gen[1].values()[j]
+ i+=1
+'''
+print 'Population size ',n.populationSize
+print 'Elitism Proportion ',n.eliteProportion
+print 'Mutation Probability ',n.mutationProb
+print 'Mutation Std Deviation ',n.mutationStdDev
+print 'Objective Evaluation number ',n.numEvaluations
+print 'last generation Length of bestEvaluation ',len(n.bestEvaluation)
+print 'Best Evaluable : Best Evaluation'
+for i in range(len(n.bestEvaluation)):
+ assert len(n.bestEvaluation) == len(n.bestEvaluable)
+ print n.bestEvaluable[i],':',n.bestEvaluation[i]
\ No newline at end of file
diff --git a/examples/optimization/multiobjective/nsga2jpq.py b/examples/optimization/multiobjective/nsga2jpq.py
new file mode 100644
index 000000000..05dc55813
--- /dev/null
+++ b/examples/optimization/multiobjective/nsga2jpq.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python
+""" An illustration of using the NSGA-II multi-objective optimization algorithm
+on Unconstrained Multi-Objective Optimization benchmark function. """
+
+__author__ = 'Jean Pierre Queau, jeanpierre.queau@sbmoffshore.com'
+
+from pybrain.optimization import MultiObjectiveGA
+from pybrain.rl.environments.functions.multiobjective import Deb, Pol
+import pylab
+from scipy import zeros, array
+
+# The Deb function
+#f = Deb()
+# The Pol function
+f = Pol()
+
+# start at the origin
+x0 = zeros(f.indim)
+
+x0 = array([min_ for min_, max_ in f.xbound])
+
+# the optimization for a maximum of 25 generations
+n = MultiObjectiveGA(f, x0, storeAllEvaluations = True, populationSize = 50, eliteProportion = 1.0,
+ topProportion = 1.0, mutationProb = 0.5, mutationStdDev = 0.1, storeAllPopulations = True, allowEquality = False)
+print 'Start Learning'
+n.learn(30)
+print 'End Learning'
+
+# plotting the results (blue = all evaluated points, red = resulting pareto front)
+print 'Plotting the Results'
+print 'All Evaluations'
+for x in n._allEvaluations: pylab.plot([x[0]], [x[1]], 'b.')
+for x in n.bestEvaluation: pylab.plot([x[0]], [x[1]], 'ro')
+pylab.show()
+print 'Pareto Front'
+for x in n.bestEvaluation: pylab.plot([x[0]], [x[1]], 'ro')
+pylab.show()
+print '==========='
+print '= Results ='
+print '==========='
+'''
+i=0
+for gen in n._allGenerations:
+ print 'Generation: ',i
+ for j in range(len(gen[1])):
+ print gen[1].keys()[j],gen[1].values()[j]
+ i+=1
+'''
+print 'Population size ',n.populationSize
+print 'Elitism Proportion ',n.eliteProportion
+print 'Mutation Probability ',n.mutationProb
+print 'Mutation Std Deviation ',n.mutationStdDev
+print 'Objective Evaluation number ',n.numEvaluations
+print 'last generation Length of bestEvaluation ',len(n.bestEvaluation)
+print 'Best Evaluable : Best Evaluation'
+for i in range(len(n.bestEvaluation)):
+ assert len(n.bestEvaluation) == len(n.bestEvaluable)
+ print n.bestEvaluable[i],':',n.bestEvaluation[i]
\ No newline at end of file
diff --git a/examples/optimization/multiobjective/readme.txt b/examples/optimization/multiobjective/readme.txt
new file mode 100644
index 000000000..b1e59d8c6
--- /dev/null
+++ b/examples/optimization/multiobjective/readme.txt
@@ -0,0 +1,47 @@
+
+nsga2.py is the original example for the Kurbenchmark function
+
+nsga2jpq.py is the modified example to demonstrate the implementation
+ for Unconstrained Multi-Objective Optimization Problems and
+ has been validated against the DEB by Deb (2001) function
+ and the POL by Poloni & all (2000) function.
+ Modification have been made to the original code to implement
+ the boundaries of the parameters
+
+constnsga2jpq is new and is an example to demonstrate the implementation
+ for Constrained Multi-Objective Optimization Problems and
+ has been validated against the DEB CONSTR by Deb (2001)
+ function, the SRN by Srinivas & Deb (1994) and the
+ OSY by Osyczka and Kundu (1995).
+ New class has been defined and modification have been made
+ to the original code to implement the constrained functions.
+
+
+the modifications or new fuctions added are encapsulated in the code by
+
+""" added by JPQ """"
+
+ ....
+ ....
+
+# ---
+
+
+The following code files have been modified:
+
+pybrain/rl/environments/functions/multiobjective.py
+pybrain/rl/environments/functions/transformations.py
+pybrain/tools/nondominated.py
+pybrain/optimization/optimizer.py
+pybrain/optimization/populationbased/ga.py
+pybrain/optimization/populationbased/multiobjective/__init__.py
+pybrain/optimization/populationbased/multiobjective/nsga2.py
+pybrain/optimization/populationbased/multiobjective/constnsga2.py
+
+It is clear at the end that nsga2.py and constnsga2.py should be merged.
+In the transformation file only the oppositeFunction has been modified, may
+be the other functions should also be modified but this was not required to
+make the code running.
+
+Hope this will help to make you understanding what i have been doing.
+
diff --git a/examples/supervised/Test Network Reader&Writer/jpq2layersReader.py b/examples/supervised/Test Network Reader&Writer/jpq2layersReader.py
new file mode 100644
index 000000000..be90f0a8c
--- /dev/null
+++ b/examples/supervised/Test Network Reader&Writer/jpq2layersReader.py
@@ -0,0 +1,70 @@
+from pybrain.structure import FeedForwardNetwork
+from pybrain.tools.validation import ModuleValidator,Validator
+from pybrain.utilities import percentError
+from pybrain.tools.customxml import NetworkReader
+from pybrain.datasets import SupervisedDataSet
+import numpy
+import pylab
+import os
+
+def myplot(trns,ctrns = None,tsts = None,ctsts = None,iter = 0):
+ plotdir = os.path.join(os.getcwd(),'plot')
+ pylab.clf()
+ try:
+ assert len(tsts) > 1
+ tstsplot = True
+ except:
+ tstsplot = False
+ try:
+ assert len(ctsts) > 1
+ ctstsplot = True
+ except:
+ ctstsplot = False
+ try:
+ assert len(ctrns) > 1
+ ctrnsplot = True
+ except:
+ ctrnsplot = False
+ if tstsplot:
+ pylab.plot(tsts['input'],tsts['target'],c='b')
+ pylab.scatter(trns['input'],trns['target'],c='r')
+ if ctrnsplot:
+ pylab.scatter(trns['input'],ctrns,c='y')
+ if tstsplot and ctstsplot:
+ pylab.plot(tsts['input'], ctsts,c='g')
+
+ pylab.xlabel('x')
+ pylab.ylabel('y')
+ pylab.title('Neuron Number:'+str(nneuron))
+ pylab.grid(True)
+ plotname = os.path.join(plotdir,('jpq2layers_plot'+ str(iter)))
+ pylab.savefig(plotname)
+
+
+# set-up the neural network
+nneuron = 5
+mom = 0.98
+netname="LSL-"+str(nneuron)+"-"+str(mom)
+mv=ModuleValidator()
+v = Validator()
+
+
+#create the test DataSet
+x = numpy.arange(0.0, 1.0+0.01, 0.01)
+s = 0.5+0.4*numpy.sin(2*numpy.pi*x)
+tsts = SupervisedDataSet(1,1)
+tsts.setField('input',x.reshape(len(x),1))
+tsts.setField('target',s.reshape(len(s),1))
+#read the train DataSet from file
+trndata = SupervisedDataSet.loadFromFile(os.path.join(os.getcwd(),'trndata'))
+
+myneuralnet = os.path.join(os.getcwd(),'myneuralnet.xml')
+if os.path.isfile(myneuralnet):
+ n = NetworkReader.readFrom(myneuralnet,name=netname)
+ #calculate the test DataSet based on the trained Neural Network
+ ctsts = mv.calculateModuleOutput(n,tsts)
+ tserr = v.MSE(ctsts,tsts['target'])
+ print 'MSE error on TSTS:',tserr
+ myplot(trndata,tsts = tsts,ctsts = ctsts)
+
+ pylab.show()
diff --git a/examples/supervised/Test Network Reader&Writer/jpq2layersWriter.py b/examples/supervised/Test Network Reader&Writer/jpq2layersWriter.py
new file mode 100644
index 000000000..15344f2d5
--- /dev/null
+++ b/examples/supervised/Test Network Reader&Writer/jpq2layersWriter.py
@@ -0,0 +1,130 @@
+from pybrain.structure import FeedForwardNetwork
+from pybrain.structure import LinearLayer, SigmoidLayer
+from pybrain.structure import BiasUnit,TanhLayer
+from pybrain.structure import FullConnection
+from pybrain.datasets import SupervisedDataSet
+from pybrain.supervised.trainers import BackpropTrainer, RPropMinusTrainer
+from pybrain.tools.validation import ModuleValidator,Validator
+from pybrain.utilities import percentError
+from pybrain.tools.customxml import NetworkWriter
+import numpy
+import pylab
+import os
+
+def myplot(trns,ctrns,tsts = None,ctsts = None,iter = 0):
+ plotdir = os.path.join(os.getcwd(),'plot')
+ pylab.clf()
+ try:
+ assert len(tsts) > 1
+ tstsplot = True
+ except:
+ tstsplot = False
+ try:
+ assert len(ctsts) > 1
+ ctstsplot = True
+ except:
+ ctstsplot = False
+ if tstsplot:
+ pylab.plot(tsts['input'],tsts['target'],c='b')
+ pylab.scatter(trns['input'],trns['target'],c='r')
+ pylab.scatter(trns['input'],ctrns,c='y')
+ if tstsplot and ctstsplot:
+ pylab.plot(tsts['input'], ctsts,c='g')
+
+ pylab.xlabel('x')
+ pylab.ylabel('y')
+ pylab.title('Neuron Number:'+str(nneuron))
+ pylab.grid(True)
+ plotname = os.path.join(plotdir,('jpq2layers_plot'+ str(iter)))
+ pylab.savefig(plotname)
+
+
+# set-up the neural network
+nneuron = 5
+mom = 0.98
+netname="LSL-"+str(nneuron)+"-"+str(mom)
+mv=ModuleValidator()
+v = Validator()
+n=FeedForwardNetwork(name=netname)
+inLayer = LinearLayer(1,name='in')
+hiddenLayer = SigmoidLayer(nneuron,name='hidden0')
+outLayer = LinearLayer(1,name='out')
+biasinUnit = BiasUnit(name="bhidden0")
+biasoutUnit = BiasUnit(name="bout")
+n.addInputModule(inLayer)
+n.addModule(hiddenLayer)
+n.addModule(biasinUnit)
+n.addModule(biasoutUnit)
+n.addOutputModule(outLayer)
+in_to_hidden = FullConnection(inLayer,hiddenLayer)
+bias_to_hidden = FullConnection(biasinUnit,hiddenLayer)
+bias_to_out = FullConnection(biasoutUnit,outLayer)
+hidden_to_out = FullConnection(hiddenLayer,outLayer)
+n.addConnection(in_to_hidden)
+n.addConnection(bias_to_hidden)
+n.addConnection(bias_to_out)
+n.addConnection(hidden_to_out)
+
+n.sortModules()
+n.reset()
+
+#read the initail weight values from myparam2.txt
+filetoopen = os.path.join(os.getcwd(),'myparam2.txt')
+if os.path.isfile(filetoopen):
+ myfile = open('myparam2.txt','r')
+ c=[]
+ for line in myfile:
+ c.append(float(line))
+ n._setParameters(c)
+else:
+ myfile = open('myparam2.txt','w')
+ for i in n.params:
+ myfile.write(str(i)+'\n')
+myfile.close()
+
+#activate the neural networks
+act = SupervisedDataSet(1,1)
+act.addSample((0.2,),(0.880422606518061,))
+n.activateOnDataset(act)
+#create the test DataSet
+x = numpy.arange(0.0, 1.0+0.01, 0.01)
+s = 0.5+0.4*numpy.sin(2*numpy.pi*x)
+tsts = SupervisedDataSet(1,1)
+tsts.setField('input',x.reshape(len(x),1))
+tsts.setField('target',s.reshape(len(s),1))
+
+#read the train DataSet from file
+trndata = SupervisedDataSet.loadFromFile(os.path.join(os.getcwd(),'trndata'))
+
+#create the trainer
+
+t = BackpropTrainer(n, learningrate = 0.01 ,
+ momentum = mom)
+#train the neural network from the train DataSet
+
+cterrori=1.0
+print "trainer momentum:"+str(mom)
+for iter in range(25):
+ t.trainOnDataset(trndata, 1000)
+ ctrndata = mv.calculateModuleOutput(n,trndata)
+ cterr = v.MSE(ctrndata,trndata['target'])
+ relerr = abs(cterr-cterrori)
+ cterrori = cterr
+ print 'iteration:',iter+1,'MSE error:',cterr
+ myplot(trndata,ctrndata,iter=iter+1)
+ if cterr < 1.e-5 or relerr < 1.e-7:
+ break
+#write the network using xml file
+myneuralnet = os.path.join(os.getcwd(),'myneuralnet.xml')
+if os.path.isfile(myneuralnet):
+ NetworkWriter.appendToFile(n,myneuralnet)
+else:
+ NetworkWriter.writeToFile(n,myneuralnet)
+
+#calculate the test DataSet based on the trained Neural Network
+ctsts = mv.calculateModuleOutput(n,tsts)
+tserr = v.MSE(ctsts,tsts['target'])
+print 'MSE error on TSTS:',tserr
+myplot(trndata,ctrndata,tsts,ctsts)
+
+pylab.show()
diff --git a/examples/supervised/Test Network Reader&Writer/myparam2.txt b/examples/supervised/Test Network Reader&Writer/myparam2.txt
new file mode 100644
index 000000000..ebbb542cd
--- /dev/null
+++ b/examples/supervised/Test Network Reader&Writer/myparam2.txt
@@ -0,0 +1,16 @@
+-0.876387117735
+0.0228437748357
+0.0936056466341
+-0.90331132597
+-0.117868958091
+0.755572921676
+-0.48378563418
+-0.635899582263
+1.43586286085
+-0.791688941299
+1.16894753711
+1.47155607167
+0.301809828737
+0.814171589059
+1.02308464693
+-0.746364019039
diff --git a/examples/supervised/Test Network Reader&Writer/readme.txt b/examples/supervised/Test Network Reader&Writer/readme.txt
new file mode 100644
index 000000000..02259257b
--- /dev/null
+++ b/examples/supervised/Test Network Reader&Writer/readme.txt
@@ -0,0 +1,24 @@
+Simple neural network to test the NetworkWriter and NetworkReader modules
+
+The Neural network consists of 5 Sigmoid neurons
+the function to train the network against is
+
+y = 0.5 + 0.4*sin(2.0*pi*x)
+
+for x in the range [0,1]
+
+The file param2.txt if it exists in the folder contains the initial values of
+the network.params
+
+the train data set is read from trndata file
+
+
+First execute jpq2layersWriter.py to write the neural network after convergence
+ -The trainer is looping till convergence with a maximum of 25 iterations and
+ at each iteration a plot is save in the plot directory
+ -The train network is written on a xml file: myneuralnet.xml
+ -the test data set is calculated based on the trained Neural Network
+ -the train data set and test data set are then plot with the indice 0
+
+Second execute jpq2layerReader.py
+
diff --git a/examples/supervised/Test Network Reader&Writer/trndata b/examples/supervised/Test Network Reader&Writer/trndata
new file mode 100644
index 000000000..5b7030048
--- /dev/null
+++ b/examples/supervised/Test Network Reader&Writer/trndata
@@ -0,0 +1,80 @@
+cpybrain.datasets.supervised
+SupervisedDataSet
+p0
+(I1
+I1
+tp1
+Rp2
+(dp3
+S'link'
+p4
+(lp5
+S'input'
+p6
+aS'target'
+p7
+asS'data'
+p8
+(dp9
+g6
+cnumpy.core.multiarray
+_reconstruct
+p10
+(cnumpy
+ndarray
+p11
+(I0
+tp12
+S'b'
+p13
+tp14
+Rp15
+(I1
+(I62
+I1
+tp16
+cnumpy
+dtype
+p17
+(S'f8'
+p18
+I0
+I1
+tp19
+Rp20
+(I3
+S'<'
+p21
+NNNI-1
+I-1
+I0
+tp22
+bI00
+S'{\x14\xaeG\xe1z\x84?\x9a\x99\x99\x99\x99\x99\xb9?\xb8\x1e\x85\xebQ\xb8\xbe?{\x14\xaeG\xe1z\xc4?\x9a\x99\x99\x99\x99\x99\xc9?\xe1z\x14\xaeG\xe1\xca?\xb8\x1e\x85\xebQ\xb8\xce?\x00\x00\x00\x00\x00\x00\xd0?\xa4p=\n\xd7\xa3\xd0?H\xe1z\x14\xaeG\xd1?\xecQ\xb8\x1e\x85\xeb\xd1?\xaeG\xe1z\x14\xae\xd7?\xcd\xcc\xcc\xcc\xcc\xcc\xdc?\x9a\x99\x99\x99\x99\x99\xe1?\x8f\xc2\xf5(\\\x8f\xe2?)\\\x8f\xc2\xf5(\xe4?\x1f\x85\xebQ\xb8\x1e\xe5?\xc3\xf5(\\\x8f\xc2\xe5?\x15\xaeG\xe1z\x14\xe6?gfffff\xe6?\xb8\x1e\x85\xebQ\xb8\xe6?R\xb8\x1e\x85\xebQ\xe8?\xf6(\\\x8f\xc2\xf5\xe8?>\n\xd7\xa3p=\xea?\x90\xc2\xf5(\\\x8f\xea?\x85\xebQ\xb8\x1e\x85\xeb?\xd7\xa3p=\n\xd7\xeb?\xcd\xcc\xcc\xcc\xcc\xcc\xec?\xc3\xf5(\\\x8f\xc2\xed?\xb8\x1e\x85\xebQ\xb8\xee?\xaeG\xe1z\x14\xae\xef?\x9a\x99\x99\x99\x99\x99\xb9?\xb8\x1e\x85\xebQ\xb8\xbe?{\x14\xaeG\xe1z\xc4?\x9a\x99\x99\x99\x99\x99\xc9?\xe1z\x14\xaeG\xe1\xca?\xb8\x1e\x85\xebQ\xb8\xce?\x00\x00\x00\x00\x00\x00\xd0?\xa4p=\n\xd7\xa3\xd0?H\xe1z\x14\xaeG\xd1?\xecQ\xb8\x1e\x85\xeb\xd1?\xaeG\xe1z\x14\xae\xd7?\xcd\xcc\xcc\xcc\xcc\xcc\xdc?\x9a\x99\x99\x99\x99\x99\xe1?\x8f\xc2\xf5(\\\x8f\xe2?)\\\x8f\xc2\xf5(\xe4?\x1f\x85\xebQ\xb8\x1e\xe5?\xc3\xf5(\\\x8f\xc2\xe5?\x15\xaeG\xe1z\x14\xe6?gfffff\xe6?\xb8\x1e\x85\xebQ\xb8\xe6?R\xb8\x1e\x85\xebQ\xe8?\xf6(\\\x8f\xc2\xf5\xe8?>\n\xd7\xa3p=\xea?\x90\xc2\xf5(\\\x8f\xea?\x85\xebQ\xb8\x1e\x85\xeb?\xd7\xa3p=\n\xd7\xeb?\xcd\xcc\xcc\xcc\xcc\xcc\xec?\xc3\xf5(\\\x8f\xc2\xed?\xb8\x1e\x85\xebQ\xb8\xee?{\x14\xaeG\xe1z\x84?\x9a\x99\x99\x99\x99\x99\xb9?'
+p23
+tp24
+bsg7
+g10
+(g11
+(I0
+tp25
+g13
+tp26
+Rp27
+(I1
+(I62
+I1
+tp28
+g20
+I00
+S',\xd4d\x81\xc0\xcd\xe0?\x8c\x8a\xc8\x01\x0e\x86\xe7?;s\xa0\xbb\x1f\xc3\xe8?\xd0E[\x99\xb1\xce\xea?\xcc\xee\xb4\x07l,\xec?\xc0r\x06r\xdae\xec?\x00\xe8\xf4\x7fU\xc6\xec?\xcd\xcc\xcc\xcc\xcc\xcc\xec?\x00\xe8\xf4\x7fU\xc6\xec?\x9a#\xcd!\xf6\xb2\xec?\x99\x9d\xdbD\xc2\x92\xec?\x08\xb0\xba4\xafT\xe9?D\xc8<>\x96\xf4\xe3?xo\x86\x83\xd3\x16\xd8?\xae"\x80t\xc7\xaa\xd3?\xe0?\x15-C\xad\xca?\xc0\xe8\x92\x9a9\xc5\xc4?\x1cw98@\xac\xc1?\xc4\xa1\x1a\xd0;e\xc0?\x9c\x89X\xc2\x9f\x9c\xbe?\x00j\xcco,\xd1\xbc?\x04\xc0X\x00T\xcd\xb9?4\x13#\xd9\xedi\xbb? w98@\xac\xc1?@\xcc\x96\xb5\x12"\xc3?\x84\x86:\x94\xba\x8c\xc8?\xdc?\x15-C\xad\xca?\xe6\xean\xfc\xe3\xf3\xd0?+\xcc\xbd\xe2\x9c\x19\xd5?\xab1\xc7\x9a/\xa2\xd9?\xa8W6\xfd~d\xde?\x8c\x8a\xc8\x01\x0e\x86\xe7?;s\xa0\xbb\x1f\xc3\xe8?\xd0E[\x99\xb1\xce\xea?\xcc\xee\xb4\x07l,\xec?\xc0r\x06r\xdae\xec?\x00\xe8\xf4\x7fU\xc6\xec?\xcd\xcc\xcc\xcc\xcc\xcc\xec?\x00\xe8\xf4\x7fU\xc6\xec?\x9a#\xcd!\xf6\xb2\xec?\x99\x9d\xdbD\xc2\x92\xec?\x08\xb0\xba4\xafT\xe9?D\xc8<>\x96\xf4\xe3?xo\x86\x83\xd3\x16\xd8?\xae"\x80t\xc7\xaa\xd3?\xe0?\x15-C\xad\xca?\xc0\xe8\x92\x9a9\xc5\xc4?\x1cw98@\xac\xc1?\xc4\xa1\x1a\xd0;e\xc0?\x9c\x89X\xc2\x9f\x9c\xbe?\x00j\xcco,\xd1\xbc?\x04\xc0X\x00T\xcd\xb9?4\x13#\xd9\xedi\xbb? w98@\xac\xc1?@\xcc\x96\xb5\x12"\xc3?\x84\x86:\x94\xba\x8c\xc8?\xdc?\x15-C\xad\xca?\xe6\xean\xfc\xe3\xf3\xd0?+\xcc\xbd\xe2\x9c\x19\xd5?\xab1\xc7\x9a/\xa2\xd9?,\xd4d\x81\xc0\xcd\xe0?\x8c\x8a\xc8\x01\x0e\x86\xe7?'
+p29
+tp30
+bssS'endmarker'
+p31
+(dp32
+g6
+I31
+sg7
+I31
+ssb.
\ No newline at end of file
diff --git a/pybrain/optimization/distributionbased/rank1.py b/pybrain/optimization/distributionbased/rank1.py
new file mode 100644
index 000000000..2fbea8de8
--- /dev/null
+++ b/pybrain/optimization/distributionbased/rank1.py
@@ -0,0 +1,186 @@
+__author__ = 'Tom Schaul, Tobias Glasmachers'
+
+
+from scipy import dot, array, randn, exp, floor, log, sqrt, ones, multiply, log2
+
+from pybrain.tools.rankingfunctions import HansenRanking
+from pybrain.optimization.distributionbased.distributionbased import DistributionBasedOptimizer
+
+
+
+class Rank1NES(DistributionBasedOptimizer):
+ """ Natural Evolution Strategies with rank-1 covariance matrices.
+
+ See http://arxiv.org/abs/1106.1998 for a description. """
+
+ # parameters, which can be set but have a good (adapted) default value
+ centerLearningRate = 1.0
+ scaleLearningRate = None
+ covLearningRate = None
+ batchSize = None
+ uniformBaseline = True
+ shapingFunction = HansenRanking()
+
+ # fixed settings
+ mustMaximize = True
+ storeAllEvaluations = True
+ storeAllEvaluated = True
+ storeAllDistributions = True
+ storeAllRates = True
+ verboseGaps = 1
+ initVariance = 1.
+ varianceCutoff = 1e-20
+
+
+ def _additionalInit(self):
+ # heuristic settings
+ if self.covLearningRate is None:
+ self.covLearningRate = self._initLearningRate()
+ if self.scaleLearningRate is None:
+ self.scaleLearningRate = self.covLearningRate
+ if self.batchSize is None:
+ self.batchSize = self._initBatchSize()
+
+ # other initializations
+ self._center = self._initEvaluable.copy()
+ self._logDetA = log(self.initVariance) / 2
+ self._principalVector = randn(self.numParameters)
+ self._principalVector /= sqrt(dot(self._principalVector, self._principalVector))
+ self._allDistributions = [(self._center.copy(), self._principalVector.copy(), self._logDetA)]
+ self.covLearningRate = 0.1
+ self.batchSize = int(max(5,max(4*log2(self.numParameters),0.2*self.numParameters)))
+ self.uniformBaseline = False
+ self.scaleLearningRate = 0.1
+
+ def _stoppingCriterion(self):
+ if DistributionBasedOptimizer._stoppingCriterion(self):
+ return True
+ elif self._getMaxVariance < self.varianceCutoff:
+ return True
+ else:
+ return False
+
+ @property
+ def _getMaxVariance(self):
+ return exp(self._logDetA * 2 / self.numParameters)
+
+ def _initLearningRate(self):
+ return 0.6 * (3 + log(self.numParameters)) / self.numParameters / sqrt(self.numParameters)
+
+ def _initBatchSize(self):
+ return 4 + int(floor(3 * log(self.numParameters)))
+
+ @property
+ def _population(self):
+ return [self._allEvaluated[i] for i in self._pointers]
+
+ @property
+ def _currentEvaluations(self):
+ fits = [self._allEvaluations[i] for i in self._pointers]
+ if self._wasOpposed:
+ fits = map(lambda x:-x, fits)
+ return fits
+
+ def _produceSample(self):
+ return randn(self.numParameters + 1)
+
+ def _produceSamples(self):
+ """ Append batch size new samples and evaluate them. """
+ tmp = [self._sample2base(self._produceSample()) for _ in range(self.batchSize)]
+ map(self._oneEvaluation, tmp)
+ self._pointers = list(range(len(self._allEvaluated) - self.batchSize, len(self._allEvaluated)))
+
+ def _notify(self):
+ """ Provide some feedback during the run. """
+ if self.verbose:
+ if self.numEvaluations % self.verboseGaps == 0:
+ print 'Step:', self.numLearningSteps, 'best:', self.bestEvaluation,
+ print 'logVar', round(self._logDetA, 3),
+ print 'log|vector|', round(log(dot(self._principalVector, self._principalVector))/2, 3)
+
+ if self.listener is not None:
+ self.listener(self.bestEvaluable, self.bestEvaluation)
+
+ def _learnStep(self):
+ # concatenations of y vector and z value
+ samples = [self._produceSample() for _ in range(self.batchSize)]
+
+ u = self._principalVector
+ a = self._logDetA
+
+ # unnamed in paper (y+zu), or x/exp(lambda)
+ W = [s[:-1] + u * s[-1] for s in samples]
+ points = [self._center+exp(a) *w for w in W]
+
+ map(self._oneEvaluation, points)
+
+ self._pointers = list(range(len(self._allEvaluated) - self.batchSize, len(self._allEvaluated)))
+
+ utilities = self.shapingFunction(self._currentEvaluations)
+ utilities /= sum(utilities) # make the utilities sum to 1
+ if self.uniformBaseline:
+ utilities -= 1. / self.batchSize
+
+ W = [w for i,w in enumerate(W) if utilities[i] != 0]
+ utilities = [uw for uw in utilities if uw != 0]
+
+ dim = self.numParameters
+
+ r = sqrt(dot(u, u))
+ v = u / r
+ c = log(r)
+
+ #inner products, but not scaled with exp(lambda)
+ wws = array([dot(w, w) for w in W])
+ wvs = array([dot(v, w) for w in W])
+ wv2s = array([wv ** 2 for wv in wvs])
+
+ dCenter = exp(self._logDetA) * dot(utilities, W)
+ self._center += self.centerLearningRate * dCenter
+
+ kp = ((r ** 2 - dim + 2) * wv2s - (r ** 2 + 1) * wws) / (2 * r * (dim - 1.))
+
+ # natural gradient on lambda, equation (5)
+ da = 1. / (2 * (dim - 1)) * dot((wws - dim) - (wv2s - 1), utilities)
+
+ # natural gradient on u, equation (6)
+ du = dot(kp, utilities) * v + dot(multiply(wvs / r, utilities), W)
+
+ # equation (7)
+ dc = dot(du, v) / r
+
+ # equation (8)
+ dv = du / r - dc * v
+
+ epsilon = min(self.covLearningRate, 2 * sqrt(r ** 2 / dot(du, du)))
+ if dc > 0:
+ # additive update
+ self._principalVector += epsilon * du
+ else:
+ # multiplicative update
+ # prevents instability
+ c += epsilon * dc
+ v += epsilon * dv
+ v /= sqrt(dot(v, v))
+ r = exp(c)
+ self._principalVector = r * v
+
+ self._lastLogDetA = self._logDetA
+ self._logDetA += self.scaleLearningRate * da
+
+ if self.storeAllDistributions:
+ self._allDistributions.append((self._center.copy(), self._principalVector.copy(), self._logDetA))
+
+
+def test():
+ """ Rank-1 NEX easily solves high-dimensional Rosenbrock functions. """
+ from pybrain.rl.environments.functions.unimodal import RosenbrockFunction
+ dim = 40
+ f = RosenbrockFunction(dim)
+ x0 = -ones(dim)
+ l = Rank1NES(f, x0, verbose=True, verboseGaps=500)
+ l.learn()
+
+
+if __name__ == '__main__':
+ test()
\ No newline at end of file
diff --git a/pybrain/optimization/optimizer.py b/pybrain/optimization/optimizer.py
index dd7a45f18..7410a9e16 100644
--- a/pybrain/optimization/optimizer.py
+++ b/pybrain/optimization/optimizer.py
@@ -42,6 +42,13 @@ class BlackBoxOptimizer(DirectSearchLearner):
#: dimension of the search space, if applicable
numParameters = None
+ '''added by JPQ Boundaries of the search space, if applicable'''
+ xBound = None
+ feasible = None
+ constrained = None
+ violation = None
+ # ---
+
#: Store all evaluations (in the ._allEvaluations list)?
storeAllEvaluations = False
#: Store all evaluated instances (in the ._allEvaluated list)?
@@ -123,15 +130,23 @@ def setEvaluator(self, evaluator, initEvaluable = None):
elif self.numParameters is not evaluator.xdim:
raise ValueError("Parameter dimension mismatch: evaluator expects "+str(evaluator.xdim)\
+" but it was set to "+str(self.numParameters)+".")
-
+ '''added by JPQ to handle boundaries on the parameters'''
+ self.evaluator = evaluator
+ if self.xBound is None:
+ self.xBound = evaluator.xbound
+ if self.feasible is None:
+ self.feasible = evaluator.feasible
+ if self.constrained is None:
+ self.constrained = evaluator.constrained
+ if self.violation is None:
+ self.violation = evaluator.violation
+ # ---
# default: maximize
if self.minimize is None:
self.minimize = False
-
self.__evaluator = evaluator
if self._wasOpposed:
self._flipDirection()
-
#set the starting point for optimization (as provided, or randomly)
self._setInitEvaluable(initEvaluable)
self.bestEvaluation = None
@@ -211,6 +226,11 @@ def _oneEvaluation(self, evaluable):
res = self.__evaluator(evaluable.params)
else:
res = self.__evaluator(evaluable)
+ ''' added by JPQ '''
+ if self.constrained :
+ self.feasible = self.__evaluator.outfeasible
+ self.violation = self.__evaluator.outviolation
+ # ---
if isscalar(res):
# detect numerical instability
if isnan(res) or isinf(res):
@@ -235,10 +255,25 @@ def _oneEvaluation(self, evaluable):
self._allEvaluated.append(evaluable.copy())
if self.storeAllEvaluations:
if self._wasOpposed and isscalar(res):
- self._allEvaluations.append(-res)
+ ''' added by JPQ '''
+ if self.constrained :
+ self._allEvaluations.append([-res,self.feasible,self.violation])
+ # ---
+ else:
+ self._allEvaluations.append(-res)
else:
- self._allEvaluations.append(res)
- return res
+ ''' added by JPQ '''
+ if self.constrained :
+ self._allEvaluations.append([res,self.feasible,self.violation])
+ # ---
+ else:
+ self._allEvaluations.append(res)
+ ''' added by JPQ '''
+ if self.constrained :
+ return [res,self.feasible,self.violation]
+ else:
+ # ---
+ return res
def _stoppingCriterion(self):
if self.maxEvaluations is not None and self.numEvaluations+self.batchSize > self.maxEvaluations:
diff --git a/pybrain/optimization/populationbased/ga.py b/pybrain/optimization/populationbased/ga.py
index 7f47a0897..00872d353 100644
--- a/pybrain/optimization/populationbased/ga.py
+++ b/pybrain/optimization/populationbased/ga.py
@@ -1,7 +1,9 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
from scipy import randn, zeros
-from random import choice, random, gauss
+from scipy import random as rd, array
+from random import choice, random, gauss, shuffle, sample
+from numpy import ndarray
from evolution import Evolution
from pybrain.optimization.optimizer import ContinuousOptimizer
@@ -27,19 +29,39 @@ class GA(ContinuousOptimizer, Evolution):
initRangeScaling = 10.
initialPopulation = None
-
+
mustMaximize = True
+ '''added by JPQ'''
+ def initBoundaries(self):
+ assert len(self.xBound) == self.numParameters
+ self.mins = array([min_ for min_, max_ in self.xBound])
+ self.maxs = array([max_ for min_, max_ in self.xBound])
+ # ---
def initPopulation(self):
+ '''added by JPQ '''
+ if self.xBound is not None:
+ self.initBoundaries()
+ # ---
if self.initialPopulation is not None:
self.currentpop = self.initialPopulation
else:
self.currentpop = [self._initEvaluable]
for _ in range(self.populationSize-1):
- self.currentpop.append(self._initEvaluable+randn(self.numParameters)
+ '''added by JPQ '''
+ if self.xBound is None:
+ # ---
+ self.currentpop.append(self._initEvaluable+randn(self.numParameters)
*self.mutationStdDev*self.initRangeScaling)
-
- def crossOver(self, parents, nbChildren):
+ '''added by JPQ '''
+ else:
+ position = rd.random(self.numParameters)
+ position *= (self.maxs-self.mins)
+ position += self.mins
+ self.currentpop.append(position)
+ # ---
+
+ def crossOverOld(self, parents, nbChildren):
""" generate a number of children by doing 1-point cross-over """
xdim = self.numParameters
children = []
@@ -50,20 +72,103 @@ def crossOver(self, parents, nbChildren):
else:
p2 = choice(parents)
point = choice(range(xdim-1))
+ point += 1
res = zeros(xdim)
res[:point] = p1[:point]
res[point:] = p2[point:]
children.append(res)
return children
-
- def mutated(self, indiv):
+
+ def mutatedOld(self, indiv):
""" mutate some genes of the given individual """
res = indiv.copy()
for i in range(self.numParameters):
if random() < self.mutationProb:
res[i] = indiv[i] + gauss(0, self.mutationStdDev)
return res
+
+ ''' added by JPQ in replacement of crossover and mutated '''
+ def crossOver(self, parents, nbChildren):
+ """ generate a number of children by doing 1-point cross-over """
+ """ change as the return quite often the same p1 and even
+ several time p2 was return the same than p1 """
+ xdim = self.numParameters
+ shuffle(parents)
+ children = []
+ for i in range(len(parents)/2):
+ p1 = parents[i]
+ p2 = parents[i+(len(parents)/2)]
+ if xdim < 2:
+ children.append(p1)
+ children.append(p2)
+ else:
+ point = choice(range(xdim-1))
+ point += 1
+ res = zeros(xdim)
+ res[:point] = p1[:point]
+ res[point:] = p2[point:]
+ children.append(res)
+ res = zeros(xdim)
+ res[:point] = p2[:point]
+ res[point:] = p1[point:]
+ children.append(res)
+ shuffle(children)
+ if len(children) > nbChildren:
+ children = children[:nbChildren]
+ elif len(children) < nbChildren:
+ children +=sample(children,(nbChildren-len(children)))
+ return children
+
+ def childexist(self,indiv,pop):
+ if isinstance(pop,list):
+ for i in range(len(pop)):
+ if all((abs(indiv[k] - pop[i][k])/(self.maxs[k]-self.mins[k]))
+ < 1.e-7 for k in xrange(self.numParameters)):
+ return True
+ return False
+
+ def mutated(self, indiv):
+ """ mutate some genes of the given individual """
+ res = indiv.copy()
+ #to avoid having a child identical to one of the currentpopulation'''
+ for i in range(self.numParameters):
+ if random() < self.mutationProb:
+ if self.xBound is None:
+ res[i] = indiv[i] + gauss(0, self.mutationStdDev)
+ else:
+ res[i] = max(min(indiv[i] + gauss(0, self.mutationStdDev),self.maxs[i]),
+ self.mins[i])
+ return res
+
+ def old_jpq_mutated(self, indiv, pop):
+ """ mutate some genes of the given individual """
+ res = indiv.copy()
+ #to avoid having a child identical to one of the currentpopulation'''
+ in_pop = self.childexist(indiv,pop)
+ for i in range(self.numParameters):
+ if random() < self.mutationProb:
+ res[i] = max(min(indiv[i] + gauss(0, self.mutationStdDev),self.maxs[i]),
+ self.mins[i])
+
+ if random() < self.mutationProb or in_pop:
+ if self.xBound is None:
+ res[i] = indiv[i] + gauss(0, self.mutationStdDev)
+ else:
+ if in_pop:
+ cmin = abs(indiv[i] - self.mins[i])/(self.maxs[i]-self.mins[i])
+ cmax = abs(indiv[i] - self.maxs[i])/(self.maxs[i]-self.mins[i])
+ if cmin < 1.e-7 or cmax < 1.e-7:
+ res[i] = self.mins[i] + random()*random()*(self.maxs[i]-self.mins[i])
+ else:
+ res[i] = max(min(indiv[i] + gauss(0, self.mutationStdDev),self.maxs[i]),
+ self.mins[i])
+ else:
+ res[i] = max(min(indiv[i] + gauss(0, self.mutationStdDev),self.maxs[i]),
+ self.mins[i])
+ return res
+ # ---
+
@property
def selectionSize(self):
""" the number of parents selected from the current population """
@@ -97,7 +202,11 @@ def produceOffspring(self):
parents = self.select()
es = min(self.eliteSize, self.selectionSize)
self.currentpop = parents[:es]
- for child in self.crossOver(parents, self.populationSize-es):
+ '''Modified by JPQ '''
+ nbchildren = self.populationSize - es
+ if self.populationSize - es <= 0:
+ nbchildren = len(parents)
+ for child in self.crossOver(parents, nbchildren ):
self.currentpop.append(self.mutated(child))
-
+ # ---
diff --git a/pybrain/optimization/populationbased/multiobjective/__init__.py b/pybrain/optimization/populationbased/multiobjective/__init__.py
index 3f53af7d0..b6d3c8826 100644
--- a/pybrain/optimization/populationbased/multiobjective/__init__.py
+++ b/pybrain/optimization/populationbased/multiobjective/__init__.py
@@ -1 +1,4 @@
-from nsga2 import MultiObjectiveGA
\ No newline at end of file
+from nsga2 import MultiObjectiveGA
+""" added by JPQ """
+from constnsga2 import ConstMultiObjectiveGA
+# ---
\ No newline at end of file
diff --git a/pybrain/optimization/populationbased/multiobjective/constnsga2.py b/pybrain/optimization/populationbased/multiobjective/constnsga2.py
new file mode 100644
index 000000000..cab60b72a
--- /dev/null
+++ b/pybrain/optimization/populationbased/multiobjective/constnsga2.py
@@ -0,0 +1,89 @@
+__author__ = 'proposed by Jean Pierre Queau , jeanpierre.queau"sbmoffshore.com'
+
+
+from scipy import array
+
+from pybrain.optimization.optimizer import BlackBoxOptimizer
+from pybrain.optimization.populationbased.ga import GA
+from pybrain.tools.nondominated import const_non_dominated_front, const_crowding_distance, const_non_dominated_sort
+
+# TODO: not very elegant, because of the conversions between tuples and arrays all the time...
+
+
+class ConstMultiObjectiveGA(GA):
+ """ Constrained Multi-objective Genetic Algorithm: the fitness is a vector with one entry per objective.
+ By default we use NSGA-II selection. """
+
+ topProportion = 0.5
+ elitism = True
+
+ populationSize = 100
+ mutationStdDev = 1.
+
+ allowEquality = True
+
+ mustMaximize = True
+
+ def _learnStep(self):
+ """ do one generation step """
+ # evaluate fitness
+ if isinstance(self.fitnesses,dict):
+ oldfitnesses = self.fitnesses
+ self.fitnesses = dict()
+ for indiv in self.currentpop:
+ if tuple(indiv) in oldfitnesses:
+ self.fitnesses[tuple(indiv)] = oldfitnesses[tuple(indiv)]
+ else:
+ self.fitnesses[tuple(indiv)] = self._oneEvaluation(indiv)
+ del oldfitnesses
+ else:
+ self.fitnesses = dict([(tuple(indiv), self._oneEvaluation(indiv)) for indiv in self.currentpop])
+ if self.storeAllPopulations:
+ self._allGenerations.append((self.currentpop, self.fitnesses))
+
+ if self.elitism:
+ self.bestEvaluable = list(const_non_dominated_front(map(tuple, self.currentpop),
+ key=lambda x: self.fitnesses[x],
+ allowequality = self.allowEquality))
+ else:
+ self.bestEvaluable = list(const_non_dominated_front(map(tuple, self.currentpop)+self.bestEvaluable,
+ key=lambda x: self.fitnesses[x],
+ allowequality = self.allowEquality))
+ self.bestEvaluation = [self.fitnesses[indiv] for indiv in self.bestEvaluable]
+# self.eliteProportion = float(len(self.bestEvaluable))/self.populationSize
+# number_of_feasible = const_number_of_feasible_pop(map(tuple, self.currentpop),
+# key=lambda x: self.fitnesses[x],
+# allowequality = self.allowEquality)
+# self.topProportion = float(number_of_feasible)/self.populationSize
+# print 'Len bestEvaluable ',len(self.bestEvaluable)
+# for i in range(len(self.bestEvaluable)):
+# print self.bestEvaluable[i],':',self.bestEvaluation[i]
+ self.produceOffspring()
+
+ def select(self):
+ return map(array, nsga2select(map(tuple, self.currentpop), self.fitnesses,
+ self.selectionSize, self.allowEquality))
+
+
+
+def nsga2select(population, fitnesses, survivors, allowequality = True):
+ """The NSGA-II selection strategy (Deb et al., 2002).
+ The number of individuals that survive is given by the survivors parameter."""
+ fronts = const_non_dominated_sort(population,
+ key=lambda x: fitnesses[x],
+ allowequality = allowequality)
+
+ individuals = set()
+ for front in fronts:
+ remaining = survivors - len(individuals)
+ if not remaining > 0:
+ break
+ if len(front) > remaining:
+ # If the current front does not fit in the spots left, use those
+ # that have the biggest crowding distance.
+ crowd_dist = const_crowding_distance(front, fitnesses)
+ front = sorted(front, key=lambda x: crowd_dist[x], reverse=True)
+ front = set(front[:remaining])
+ individuals |= front
+
+ return list(individuals)
diff --git a/pybrain/optimization/populationbased/multiobjective/nsga2.py b/pybrain/optimization/populationbased/multiobjective/nsga2.py
index b31c4ad60..7bdbfc03c 100644
--- a/pybrain/optimization/populationbased/multiobjective/nsga2.py
+++ b/pybrain/optimization/populationbased/multiobjective/nsga2.py
@@ -26,7 +26,20 @@ class MultiObjectiveGA(GA):
def _learnStep(self):
""" do one generation step """
# evaluate fitness
- self.fitnesses = dict([(tuple(indiv), self._oneEvaluation(indiv)) for indiv in self.currentpop])
+ """ added by JPQ """
+ if isinstance(self.fitnesses,dict):
+ oldfitnesses = self.fitnesses
+ self.fitnesses = dict()
+ for indiv in self.currentpop:
+ if tuple(indiv) in oldfitnesses:
+ self.fitnesses[tuple(indiv)] = oldfitnesses[tuple(indiv)]
+ else:
+ self.fitnesses[tuple(indiv)] = self._oneEvaluation(indiv)
+ del oldfitnesses
+ else:
+ # ---
+ self.fitnesses = dict([(tuple(indiv), self._oneEvaluation(indiv)) for indiv in self.currentpop])
+
if self.storeAllPopulations:
self._allGenerations.append((self.currentpop, self.fitnesses))
@@ -39,6 +52,7 @@ def _learnStep(self):
key=lambda x: self.fitnesses[x],
allowequality = self.allowEquality))
self.bestEvaluation = [self.fitnesses[indiv] for indiv in self.bestEvaluable]
+
self.produceOffspring()
def select(self):
@@ -53,6 +67,7 @@ def nsga2select(population, fitnesses, survivors, allowequality = True):
fronts = non_dominated_sort(population,
key=lambda x: fitnesses[x],
allowequality = allowequality)
+
individuals = set()
for front in fronts:
remaining = survivors - len(individuals)
diff --git a/pybrain/rl/environments/functions/multiobjective.py b/pybrain/rl/environments/functions/multiobjective.py
index 965639e0e..7a044d78d 100644
--- a/pybrain/rl/environments/functions/multiobjective.py
+++ b/pybrain/rl/environments/functions/multiobjective.py
@@ -1,8 +1,10 @@
""" Some multi-objective benchmark functions.
Implemented according to the classical reference paper of Deb et al. (Evolutionary Computation 2002) """
-from scipy import array, exp, sqrt, sin, cos, power
+from scipy import array, exp, sqrt, sin, cos, power, pi, arctan
from pybrain.rl.environments.functions.function import FunctionEnvironment
+from pybrain.structure.parametercontainer import ParameterContainer
+from scipy import array, ndarray
__author__ = 'Tom Schaul, tom@idsia.ch'
@@ -10,10 +12,30 @@
class MultiObjectiveFunction(FunctionEnvironment):
""" A function with multiple outputs. """
ydim = 2 # by default
+
+ feasible = None
+ xbound = None
+ constrained = None
+ violation = None
+
+ @property
+ def outfeasible(self):
+ return self.feasible
+
+ @property
+ def outviolation(self):
+ return self.violation
@property
def outdim(self):
return self.ydim
+
+ def __call__(self, x):
+ if isinstance(x, ParameterContainer):
+ x = x.params
+ assert type(x) == ndarray, 'FunctionEnvironment: Input not understood: '+str(type(x))
+ res = self.f(x)
+ return res
class SchBenchmark(MultiObjectiveFunction):
""" Schaffer 1987 """
@@ -59,5 +81,175 @@ def f(self, x):
f2 = sum(power(abs(x), 0.8)+5*sin(x**3))
return -array([f1, f2])
+''' added by JPQ'''
+class Deb(MultiObjectiveFunction):
+ """ Deb 2001 """
+ xdim = 2
+ def __init__(self):
+ self.xbound = []
+ self.xbound.append((0.1,1.0))
+ self.xbound.append((0.0,5.0))
+ self.constrained = False
+
+ def f(self, x):
+ f1 = x[0]
+ f2 = (1+x[1])/x[0]
+ return -array([f1, f2])
+
+class ConstDeb(MultiObjectiveFunction):
+ """ Deb 2001 """
+ xdim = 2
+ def __init__(self):
+ self.feasible = None
+ self.xbound = []
+ self.xbound.append((0.1,1.0))
+ self.xbound.append((0.0,5.0))
+ self.constrained = True
+
+ def g(self, x):
+ g1 = x[1] + 9.0*x[0] - 6.0
+ g2 = -x[1] + 9.0*x[0] - 1.0
+ if g1 >= 0 and g2 >= 0:
+ return True,array([0.,0.])
+ return False,array([g1,g2])
+ def f(self, x):
+ self.feasible,self.violation = self.g(x)
+ '''
+ not nice, due to the fact that oppositeFunction does not used
+ the instance of this class for the evaluator but creates a new instance
+ of class MultiObjectiveFunction
+ '''
+ MultiObjectiveFunction.feasible = self.feasible
+ MultiObjectiveFunction.violation = self.violation
+ f1 = x[0]
+ f2 = (1+x[1])/x[0]
+ return -array([f1, f2])
+
+class Pol(MultiObjectiveFunction):
+ """ Poloni 1997 """
+ xdim = 2
+ _A1 = 0.5 * sin(1) - 2*cos(1) + sin(2) -1.5*cos(2)
+ _A2 = 1.5 * sin(1) - cos(1) + 2*sin(2) -0.5*cos(2)
+
+ def __init__(self):
+ self.xbound = []
+ self.xbound.append((-pi,pi))
+ self.xbound.append((-pi,pi))
+ self.constrained = False
+ def f(self, x):
+ B1 = 0.5 * sin(x[0]) - 2*cos(x[0]) + sin(x[1]) -1.5*cos(x[1])
+ B2 = 1.5 * sin(x[0]) - cos(x[0]) + 2*sin(x[1]) -0.5*cos(x[1])
+
+ f1 = 1 + (self._A1-B1)**2 + (self._A2-B2)**2
+ f2 = (x[0]+3)**2 + (x[1]+1)**2
+ return -array([f1, f2])
+
+class ConstSrn(MultiObjectiveFunction):
+ """ Srinivas and Deb 1994 """
+ xdim = 2
+ def __init__(self):
+ self.feasible = None
+ self.xbound = []
+ self.xbound.append((-20.0,20.0))
+ self.xbound.append((-20.0,20.0))
+ self.constrained = True
+
+ def g(self, x):
+ g1 = 225 - (x[1]**2 + x[0]**2)
+ g2 = 3*x[1] -x[0] - 10.0
+ if g1 >= 0 and g2 >= 0:
+ return True,array([0.,0.])
+ return False,array([g1,g2])
+ def f(self, x):
+ self.feasible,self.violation = self.g(x)
+ MultiObjectiveFunction.feasible = self.feasible
+ MultiObjectiveFunction.violation = self.violation
+ f1 = 2+(x[0]-2)**2+(x[1]-1)**2
+ f2 = 9*x[0]-(x[1]-1)**2
+ return -array([f1, f2])
+
+class ConstOsy(MultiObjectiveFunction):
+ """ Osyczka and Kundu 1995 """
+ xdim = 6
+ def __init__(self):
+ self.feasible = None
+ self.xbound = []
+ self.xbound.append((0.0,10.0))
+ self.xbound.append((0.0,10.0))
+ self.xbound.append((1.0,5.0))
+ self.xbound.append((0.0,6.0))
+ self.xbound.append((1.0,5.0))
+ self.xbound.append((0.0,10.0))
+ self.constrained = True
+
+ def g(self, x):
+ g1 = x[0] + x[1] -2.0
+ g2 = 6.0 -x[0] -x[1]
+ g3 = 2.0 -x[1] +x[0]
+ g4 = 1.0 -x[0] +3.0*x[1]
+ g5 = 4.0 -(x[2]-3)**2 -x[3]
+ g6 = (x[4]-3)**2 +x[5] -4.0
+ if g1 >= 0 and g2 >= 0 and g3 >= 0 and g4 >= 0 and g5 >= 0 and g6 >= 0:
+ return True,array([0.,0.])
+ return False,array([g1,g2,g3,g4,g5,g6])
+ def f(self, x):
+ self.feasible,self.violation = self.g(x)
+ MultiObjectiveFunction.feasible = self.feasible
+ MultiObjectiveFunction.violation = self.violation
+ f1 = -(25.0*(x[0]-2.0)**2+(x[1]-2.0)**2+(x[2]-1.0)**2+(x[3]-4.0)**2+(x[4]-1.0)**2)
+ f2 = x[0]**2+x[1]**2+x[2]**2+x[3]**2+x[4]**2+x[5]**2
+ return -array([f1, f2])
+
+class ConstTnk(MultiObjectiveFunction):
+ """ Tanaka 1995 """
+ xdim = 2
+ def __init__(self):
+ self.feasible = None
+ self.xbound = []
+ self.xbound.append((0.0,pi))
+ self.xbound.append((0.0,pi))
+ self.constrained = True
+
+ def g(self, x):
+ if x[1] == 0.0:
+ A = pi/2.0
+ else:
+ A = arctan(x[0]/x[1])
+ g1 = x[1]**2 + x[0]**2 - 1.0 -0.1*cos(16.0*A)
+ g2 = 0.5 -(x[0]-0.5)**2 -(x[1]-0.5)**2
+ if g1 >= 0 and g2 >= 0:
+ return True,array([0.,0.])
+ return False,array([g1,g2])
+ def f(self, x):
+ self.feasible,self.violation = self.g(x)
+ MultiObjectiveFunction.feasible = self.feasible
+ MultiObjectiveFunction.violation = self.violation
+ f1 = x[0]
+ f2 = x[1]
+ return -array([f1, f2])
+class ConstBnh(MultiObjectiveFunction):
+ """ Binh & Korn 1997 """
+ xdim = 2
+ def __init__(self):
+ self.feasible = None
+ self.xbound = []
+ self.xbound.append((0.0,5.0))
+ self.xbound.append((0.0,3.0))
+ self.constrained = True
+
+ def g(self, x):
+ g1 = 25 - (x[0]-5.0)**2 - x[1]**2
+ g2 = (x[0]-8.0)**2 + (x[1]+3.0)**2 - 7.7
+ if g1 >= 0 and g2 >= 0:
+ return True,array([0.,0.])
+ return False,array([g1,g2])
+ def f(self, x):
+ self.feasible,self.violation = self.g(x)
+ MultiObjectiveFunction.feasible = self.feasible
+ MultiObjectiveFunction.violation = self.violation
+ f1 = 4*x[0]**2 + 4*x[1]**2
+ f2 = (x[0]-5)**2 + (x[1]-5)**2
+ return -array([f1, f2])
+# ---
\ No newline at end of file
diff --git a/pybrain/rl/environments/functions/transformations.py b/pybrain/rl/environments/functions/transformations.py
index cd3feb472..7b40bc493 100644
--- a/pybrain/rl/environments/functions/transformations.py
+++ b/pybrain/rl/environments/functions/transformations.py
@@ -9,13 +9,19 @@
from pybrain.structure.parametercontainer import ParameterContainer
from pybrain.rl.environments.fitnessevaluator import FitnessEvaluator
from pybrain.utilities import sparse_orth, dense_orth
+from pybrain.rl.environments.functions.multiobjective import MultiObjectiveFunction
def oppositeFunction(basef):
""" the opposite of a function """
if isinstance(basef, FitnessEvaluator):
if isinstance(basef, FunctionEnvironment):
- res = FunctionEnvironment(basef.xdim, basef.xopt)
+ ''' added by JPQ '''
+ if isinstance(basef, MultiObjectiveFunction):
+ res = MultiObjectiveFunction()
+ else:
+ # ---
+ res = FunctionEnvironment(basef.xdim, basef.xopt)
else:
res = FitnessEvaluator()
res.f = lambda x:-basef.f(x)
diff --git a/pybrain/tools/customxml/handling.py b/pybrain/tools/customxml/handling.py
index a8c4af708..2260dee08 100644
--- a/pybrain/tools/customxml/handling.py
+++ b/pybrain/tools/customxml/handling.py
@@ -94,8 +94,12 @@ def findNamedNode(self, name, nameattr, root = None):
root = self.root
for n in root.childNodes:
if n.nodeName == name:
- if 'name' in n.attributes:
- if n.attributes['name'] == nameattr:
+# modif JPQ
+# if 'name' in n.attributes:
+ if n.attributes['name']:
+# modif JPQ
+# if n.attributes['name'] == nameattr:
+ if n.attributes['name'].value == nameattr:
return n
return None
diff --git a/pybrain/tools/neuralnets.py b/pybrain/tools/neuralnets.py
index be2de6734..29ff86351 100644
--- a/pybrain/tools/neuralnets.py
+++ b/pybrain/tools/neuralnets.py
@@ -208,7 +208,8 @@ def setupRNN(self, trainer=BackpropTrainer, hidden=None, **trnargs):
self.hidden = hidden
self._convertAllDataToOneOfMany()
- RNN = buildNetwork(self.DS.indim, self.hidden, self.DS.outdim, hiddenclass=LSTMLayer, outclass=SoftmaxLayer)
+ RNN = buildNetwork(self.DS.indim, self.hidden, self.DS.outdim, hiddenclass=LSTMLayer,
+ recurrent=True, outclass=SoftmaxLayer)
logging.info("Constructing classification RNN with following config:")
logging.info(str(RNN) + "\n Hidden units:\n " + str(self.hidden))
logging.info("Trainer received the following special arguments:")
diff --git a/pybrain/tools/nondominated.py b/pybrain/tools/nondominated.py
index 776003c35..9fc4c453d 100644
--- a/pybrain/tools/nondominated.py
+++ b/pybrain/tools/nondominated.py
@@ -162,7 +162,6 @@ def _non_dominated_front_merge_arr(iterable, key=lambda x: x, allowequality=True
non_dominated_front = _non_dominated_front_merge_arr
-
def non_dominated_sort(iterable, key=lambda x: x, allowequality=True):
"""Return a list that is sorted in a non-dominating fashion.
Keys have to be n-tuple."""
@@ -173,4 +172,117 @@ def non_dominated_sort(iterable, key=lambda x: x, allowequality=True):
items -= front
fronts.append(front)
return fronts
+
+''' added by JPQ for Constraine Multi-objective Optimization '''
+
+
+def _const_non_dominated_front_merge_arr(iterable, key=lambda x: x, allowequality=True):
+ items = list(iterable)
+ l = len(items)
+ if l > 100:
+ part1 = list(_const_non_dominated_front_merge_arr(items[:l / 2], key, allowequality))
+ part2 = list(_const_non_dominated_front_merge_arr(items[l / 2:], key, allowequality))
+ if len(part1) >= l / 3 or len(part2) >= l / 3:
+ return _const_non_dominated_front_arr(part1 + part2, key, allowequality)
+ else:
+ return _const_non_dominated_front_merge_arr(part1 + part2, key, allowequality)
+ else:
+ return _const_non_dominated_front_arr(items, key, allowequality)
+
+def _const_non_dominated_front_arr(iterable, key=lambda x: x, allowequality=True):
+ """Return a subset of items from iterable which are not dominated by any
+ other item in iterable.
+
+ Faster version, based on boolean matrix manipulations.
+ """
+ items = list(iterable) # pop
+
+ fits = map(key, items) # fitness
+
+ x = array([fits[i][0] for i in range(len(fits))])
+ v = array([fits[i][1] for i in range(len(fits))])
+ c = array([fits[i][2] for i in range(len(fits))])
+
+ l = len(items)
+ a = tile(x, (l, 1, 1))
+ b = a.transpose((1, 0, 2))
+ if allowequality:
+ ndom = sum(a <= b, axis=2)
+ else:
+ ndom = sum(a < b, axis=2)
+ ndom = array(ndom, dtype=bool)
+ res = set()
+ for ii in range(l):
+ res.add(ii)
+ for ij in list(res):
+ if ii == ij:
+ continue
+ if not ndom[ij, ii] and v[ij] and v[ii]:
+ res.remove(ii)
+ break
+ elif not ndom[ii, ij] and v[ij] and v[ii]:
+ res.remove(ij)
+ elif v[ij] and not v[ii]:
+ res.remove(ii)
+ break
+ elif v[ii] and not v[ij]:
+ res.remove(ij)
+ elif not v[ii] and not v[ij]:
+ cii = abs(sum(c[ii]))
+ cij = abs(sum(c[ij]))
+ if cii < cij:
+ res.remove(ij)
+ else:
+ res.remove(ii)
+ break
+
+ return set(map(lambda i: items[i], res))
+
+const_non_dominated_front = _const_non_dominated_front_merge_arr
+
+def const_non_dominated_sort(iterable, key=lambda x: x, allowequality=True):
+ """Return a list that is sorted in a non-dominating fashion.
+ Keys have to be n-tuple."""
+
+ items = set(iterable)
+
+ fronts = []
+ while items:
+ front = const_non_dominated_front(items, key, allowequality)
+ items -= front
+ fronts.append(front)
+ return fronts
+
+def const_crowding_distance(individuals, fitnesses):
+ """ Crowding distance-measure for multiple objectives. """
+ distances = collections.defaultdict(lambda: 0)
+ individuals = list(individuals)
+ # Infer the number of objectives by looking at the fitness of the first.
+ n_obj = len(fitnesses[individuals[0]][0])
+
+ for i in xrange(n_obj):
+ individuals.sort(key=lambda x: fitnesses[x][0][i])
+ # normalization between 0 and 1.
+ normalization = float(fitnesses[individuals[0]][0][i] - fitnesses[individuals[-1]][0][i])
+ # Make sure the boundary points are always selected.
+ distances[individuals[0]] = 1e100
+ distances[individuals[-1]] = 1e100
+ tripled = zip(individuals, individuals[1:-1], individuals[2:])
+ for pre, ind, post in tripled:
+ distances[ind] += (fitnesses[pre][0][i] - fitnesses[post][0][i]) / normalization
+ return distances
+
+def const_number_of_feasible_pop(iterable, key=lambda x: x, allowequality=True):
+ """Return a subset of items from iterable which are not dominated by any
+ other item in iterable.
+
+ Faster version, based on boolean matrix manipulations.
+ """
+ items = list(iterable) # pop
+
+ fits = map(key, items) # fitness
+ v = list([fits[i][1] for i in range(len(fits))])
+ n = v.count(True)
+ return n
+# ---
\ No newline at end of file
From 6a97bb24ffbd46cecf8c2d7065b4f37ba83fce77 Mon Sep 17 00:00:00 2001
From: graingert
Date: Sun, 1 Apr 2012 20:46:20 +0200
Subject: [PATCH 09/83] Add scipy dependency to setup.py
---
setup.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/setup.py b/setup.py
index 206ddfa25..4265a9f98 100644
--- a/setup.py
+++ b/setup.py
@@ -19,4 +19,5 @@
include_package_data=True,
test_suite='pybrain.tests.runtests.make_test_suite',
package_data={'pybrain': ['rl/environments/ode/models/*.xode']},
+ install_requires = ["scipy"],
)
\ No newline at end of file
From 05d72269166a4dbf389c766fc523b039f4212b09 Mon Sep 17 00:00:00 2001
From: Davoud Taghawi-Nejad
Date: Sat, 4 Aug 2012 23:39:08 +0200
Subject: [PATCH 10/83] added an explenation how the explorers can be added to
Learning Agents
---
pybrain/rl/explorers/explorer.py | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/pybrain/rl/explorers/explorer.py b/pybrain/rl/explorers/explorer.py
index 1f0f81e60..99f32d97a 100644
--- a/pybrain/rl/explorers/explorer.py
+++ b/pybrain/rl/explorers/explorer.py
@@ -8,6 +8,16 @@ class Explorer(Module):
""" An Explorer object is used in Agents, receives the current state
and action (from the controller Module) and returns an explorative
action that is executed instead the given action.
+
+ Explorer have to be added to the learner before adding the learner
+ to the LearningAgent.
+
+ For Example::
+
+ controller = ActionValueNetwork(2, 100)
+ learner = SARSA()
+ learner.explorer = NormalExplorer(1, 0.1)
+ self.learning_agent = LearningAgent(controller, learner)
"""
def activate(self, state, action):
@@ -20,4 +30,4 @@ def activate(self, state, action):
def newEpisode(self):
""" Inform the explorer about the start of a new episode. """
- pass
\ No newline at end of file
+ pass
From b8c06cf6b7033339f046ca19cc372722bd518a67 Mon Sep 17 00:00:00 2001
From: Davoud Taghawi-Nejad
Date: Sun, 5 Aug 2012 09:03:45 +0300
Subject: [PATCH 11/83] Update pybrain/rl/explorers/explorer.py
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Added table of explorers and explanation how to add explorers to a LearningAgent
---
pybrain/rl/explorers/explorer.py | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/pybrain/rl/explorers/explorer.py b/pybrain/rl/explorers/explorer.py
index 99f32d97a..b7a78c71a 100644
--- a/pybrain/rl/explorers/explorer.py
+++ b/pybrain/rl/explorers/explorer.py
@@ -8,8 +8,20 @@ class Explorer(Module):
""" An Explorer object is used in Agents, receives the current state
and action (from the controller Module) and returns an explorative
action that is executed instead the given action.
-
- Explorer have to be added to the learner before adding the learner
+
+ Continous explorer will produce continous action states, discrete
+ once discrete actions accordingly.
+
+ Explorer action episodic?
+ =============================== ========= =========
+ NormalExplorer continous no
+ StateDependentExplorer continous yes
+ BoltzmannExplorer discrete no
+ EpsilonGreedyExplorer discrete no
+ DiscreteStateDependentExplorer discrete yes
+
+
+ Explorer has to be added to the learner before adding the learner
to the LearningAgent.
For Example::
From 2af8e59d47c80d3accc030a5542d9e74d9f3b5a5 Mon Sep 17 00:00:00 2001
From: Steffen Kampmann
Date: Fri, 10 Aug 2012 09:44:00 +0200
Subject: [PATCH 12/83] fix
tests/unittests/supervised/knn/lsh/test_nearoptimal.py float equality
---
.../unittests/supervised/knn/lsh/test_nearoptimal.py | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py b/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
index a667e3f81..23e0e3aea 100644
--- a/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
+++ b/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
@@ -20,10 +20,10 @@
>>> m = nearoptimal.MultiDimHash(dim=dim, omega=omega, prob=0.8)
- >>> m.radius
- 1.189207115002721
- >>> m.radiusSquared
- 1.4142135623730949
+ >>> '%.14f' % m.radius
+ '1.18920711500272'
+ >>> '%.14f' % m.radiusSquared
+ '1.41421356237309'
This gives us hypecube sidelength of
From 3867a98310b7bca61216b37fd58a186e7c92c090 Mon Sep 17 00:00:00 2001
From: Steffen Kampmann
Date: Fri, 10 Aug 2012 10:06:43 +0200
Subject: [PATCH 13/83] fix tests/unittests/datasets/test_datasets_datasets.py
addField
---
.../datasets/test_datasets_datasets.py | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/pybrain/tests/unittests/datasets/test_datasets_datasets.py b/pybrain/tests/unittests/datasets/test_datasets_datasets.py
index ba061f04f..5a32e2e17 100644
--- a/pybrain/tests/unittests/datasets/test_datasets_datasets.py
+++ b/pybrain/tests/unittests/datasets/test_datasets_datasets.py
@@ -1,14 +1,25 @@
# -*- coding: utf-8 -*-
"""
-
>>> from scipy import array
>>> from pybrain import datasets
>>> from copy import deepcopy
>>> d = datasets.dataset.DataSet()
>>> d.addField('input', 2)
- >>> d.data['input']
- array([], shape=(0, 2), dtype=...)
+ >>> type(d.data['input'])
+
+
+ >>> len(d.data['input'])
+ 0
+
+ >>> x, y = d.data['input'].shape
+ >>> str(x)
+ 0
+ >>> str(y)
+ 2
+
+ #>>> d.data['input'].dtype
+ #...
Build up a DataSet for testing:
From 67b715eab9f7889be790130820b9f0c1a00fb187 Mon Sep 17 00:00:00 2001
From: Steffen Kampmann
Date: Thu, 23 Aug 2012 11:16:23 +0200
Subject: [PATCH 14/83] add BackpropTrainer parameter for trainingset and
validationset
---
pybrain/supervised/trainers/backprop.py | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/pybrain/supervised/trainers/backprop.py b/pybrain/supervised/trainers/backprop.py
index b4acd05c5..74ce3a6f4 100644
--- a/pybrain/supervised/trainers/backprop.py
+++ b/pybrain/supervised/trainers/backprop.py
@@ -184,7 +184,8 @@ def testOnClassData(self, dataset=None, verbose=False,
return out
def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
- continueEpochs=10, validationProportion=0.25):
+ continueEpochs=10, validationProportion=0.25,
+ trainingset=None, validationset=None):
"""Train the module on the dataset until it converges.
Return the module with the parameters that gave the minimal validation
@@ -193,19 +194,24 @@ def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
If no dataset is given, the dataset passed during Trainer
initialization is used. validationProportion is the ratio of the dataset
that is used for the validation dataset.
+
+ If the training and validation data is already set, the splitPropotion is ignored
If maxEpochs is given, at most that many epochs
are trained. Each time validation error hits a minimum, try for
continueEpochs epochs to find a better one."""
epochs = 0
- if dataset == None:
+ if dataset is None:
dataset = self.ds
- if verbose == None:
+ if verbose is None:
verbose = self.verbose
- # Split the dataset randomly: validationProportion of the samples for
- # validation.
- trainingData, validationData = (
- dataset.splitWithProportion(1 - validationProportion))
+ if trainingset is None and validationset is None:
+ # Split the dataset randomly: validationProportion of the samples for
+ # validation.
+ trainingData, validationData = (
+ dataset.splitWithProportion(1 - validationProportion))
+ else:
+ trainingData, validationData = trainingset, validationset
if not (len(trainingData) > 0 and len(validationData)):
raise ValueError("Provided dataset too small to be split into training " +
"and validation sets with proportion " + str(validationProportion))
From cdc5df145107d764c66c689bb46872dc40fc2527 Mon Sep 17 00:00:00 2001
From: Steffen Kampmann
Date: Thu, 4 Oct 2012 12:41:12 +0200
Subject: [PATCH 15/83] change local vars trainingError and validationError to
attributes
---
pybrain/supervised/trainers/backprop.py | 26 ++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)
diff --git a/pybrain/supervised/trainers/backprop.py b/pybrain/supervised/trainers/backprop.py
index 74ce3a6f4..0678d8321 100644
--- a/pybrain/supervised/trainers/backprop.py
+++ b/pybrain/supervised/trainers/backprop.py
@@ -218,14 +218,14 @@ def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
self.ds = trainingData
bestweights = self.module.params.copy()
bestverr = self.testOnData(validationData)
- trainingErrors = []
- validationErrors = [bestverr]
+ self.trainingErrors = []
+ self.validationErrors = [bestverr]
while True:
- trainingErrors.append(self.train())
- validationErrors.append(self.testOnData(validationData))
- if epochs == 0 or validationErrors[-1] < bestverr:
+ self.trainingErrors.append(self.train())
+ self.validationErrors.append(self.testOnData(validationData))
+ if epochs == 0 or self.validationErrors[-1] < bestverr:
# one update is always done
- bestverr = validationErrors[-1]
+ bestverr = self.validationErrors[-1]
bestweights = self.module.params.copy()
if maxEpochs != None and epochs >= maxEpochs:
@@ -233,17 +233,17 @@ def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
break
epochs += 1
- if len(validationErrors) >= continueEpochs * 2:
+ if len(self.validationErrors) >= continueEpochs * 2:
# have the validation errors started going up again?
# compare the average of the last few to the previous few
- old = validationErrors[-continueEpochs * 2:-continueEpochs]
- new = validationErrors[-continueEpochs:]
+ old = self.validationErrors[-continueEpochs * 2:-continueEpochs]
+ new = self.validationErrors[-continueEpochs:]
if min(new) > max(old):
self.module.params[:] = bestweights
break
- trainingErrors.append(self.testOnData(trainingData))
+ self.trainingErrors.append(self.testOnData(trainingData))
self.ds = dataset
if verbose:
- print 'train-errors:', fListToString(trainingErrors, 6)
- print 'valid-errors:', fListToString(validationErrors, 6)
- return trainingErrors, validationErrors
+ print 'train-errors:', fListToString(self.trainingErrors, 6)
+ print 'valid-errors:', fListToString(self.validationErrors, 6)
+ return self.trainingErrors, self.validationErrors
From 2bfd4821fa07aa4f0bead572661d21d08564f628 Mon Sep 17 00:00:00 2001
From: Steffen Kampmann
Date: Wed, 14 Nov 2012 12:47:36 +0100
Subject: [PATCH 16/83] add raised exception on NaN in backprop.py
---
pybrain/supervised/trainers/backprop.py | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/pybrain/supervised/trainers/backprop.py b/pybrain/supervised/trainers/backprop.py
index 0678d8321..9cb4e296f 100644
--- a/pybrain/supervised/trainers/backprop.py
+++ b/pybrain/supervised/trainers/backprop.py
@@ -2,7 +2,7 @@
from scipy import dot, argmax
from random import shuffle
-
+from math import isnan
from trainer import Trainer
from pybrain.utilities import fListToString
from pybrain.auxiliary import GradientDescent
@@ -221,8 +221,12 @@ def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
self.trainingErrors = []
self.validationErrors = [bestverr]
while True:
- self.trainingErrors.append(self.train())
- self.validationErrors.append(self.testOnData(validationData))
+ trainingError = self.train()
+ validationError = self.testOnData(validationData)
+ if isnan(trainingError) or isnan(validationError):
+ raise Exception("Training produced NaN results")
+ self.trainingErrors.append(trainingError)
+ self.validationErrors.append(validationError)
if epochs == 0 or self.validationErrors[-1] < bestverr:
# one update is always done
bestverr = self.validationErrors[-1]
From 066d5528e5a5012644ab7e3ae38cc865181621b8 Mon Sep 17 00:00:00 2001
From: Steffen Kampmann
Date: Wed, 14 Nov 2012 13:39:04 +0100
Subject: [PATCH 17/83] minimal refactoring in backprop.py
---
pybrain/supervised/trainers/backprop.py | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/pybrain/supervised/trainers/backprop.py b/pybrain/supervised/trainers/backprop.py
index 9cb4e296f..ee6a97b7e 100644
--- a/pybrain/supervised/trainers/backprop.py
+++ b/pybrain/supervised/trainers/backprop.py
@@ -185,7 +185,7 @@ def testOnClassData(self, dataset=None, verbose=False,
def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
continueEpochs=10, validationProportion=0.25,
- trainingset=None, validationset=None):
+ trainingData=None, validationData=None):
"""Train the module on the dataset until it converges.
Return the module with the parameters that gave the minimal validation
@@ -205,13 +205,11 @@ def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
dataset = self.ds
if verbose is None:
verbose = self.verbose
- if trainingset is None and validationset is None:
+ if trainingData is None or validationData is None:
# Split the dataset randomly: validationProportion of the samples for
# validation.
trainingData, validationData = (
dataset.splitWithProportion(1 - validationProportion))
- else:
- trainingData, validationData = trainingset, validationset
if not (len(trainingData) > 0 and len(validationData)):
raise ValueError("Provided dataset too small to be split into training " +
"and validation sets with proportion " + str(validationProportion))
From 9a97511e00a44893ab8c5b3a5acf7ab50800d16c Mon Sep 17 00:00:00 2001
From: Steffen Kampmann
Date: Wed, 14 Nov 2012 14:21:07 +0100
Subject: [PATCH 18/83] created unittests/supervised/trainers/test_backprop.py
for bug demonstration
---
.../supervised/trainers/test_backprop.py | 53 +++++++++++++++++++
1 file changed, 53 insertions(+)
create mode 100644 pybrain/tests/unittests/supervised/trainers/test_backprop.py
diff --git a/pybrain/tests/unittests/supervised/trainers/test_backprop.py b/pybrain/tests/unittests/supervised/trainers/test_backprop.py
new file mode 100644
index 000000000..76a57c03d
--- /dev/null
+++ b/pybrain/tests/unittests/supervised/trainers/test_backprop.py
@@ -0,0 +1,53 @@
+"""
+ >>> from pybrain.datasets.supervised import SupervisedDataSet
+ >>> from pybrain.supervised.trainers import BackpropTrainer
+ >>> from pybrain import FeedForwardNetwork
+ >>> from pybrain.structure import LinearLayer, SigmoidLayer, FullConnection
+ >>> from random import randrange
+ >>> dataset = SupervisedDataSet(6, 2)
+ >>> for i in range(1000):
+ ... state = [randrange(0, 15),
+ ... randrange(-70, 50),
+ ... randrange(-70, 50),
+ ... randrange(-70, 50),
+ ... randrange(-70, 50),
+ ... float(randrange(1, 5))/20.]
+ ... action = [float(randrange(-1, 1))/10.0,
+ ... randrange(0, 1)]
+ ... dataset.addSample(state, action)
+ >>>
+ >>> net = FeedForwardNetwork()
+ >>>
+ >>> net.addInputModule(SigmoidLayer(6, name='in'))
+ >>> net.addModule(LinearLayer(40, name='hidden_0'))
+ >>> net.addModule(LinearLayer(16, name='hidden_1'))
+ >>> net.addOutputModule(LinearLayer(2, name='out'))
+ >>>
+ >>> net.addConnection(FullConnection(net['in'], net['hidden_0']))
+ >>> net.addConnection(FullConnection(net['hidden_0'], net['hidden_1']))
+ >>> net.addConnection(FullConnection(net['hidden_1'], net['out']))
+ >>>
+ >>> net.sortModules()
+ >>>
+ >>> trainer = BackpropTrainer(net,
+ ... dataset=dataset,
+ ... learningrate=0.01,
+ ... lrdecay=1,
+ ... momentum=0.5,
+ ... verbose=False,
+ ... weightdecay=0,
+ ... batchlearning=False)
+ >>>
+ >>> trainingErrors, validationErrors = trainer.trainUntilConvergence(
+ ... dataset=dataset,
+ ... maxEpochs=100)
+"""
+
+
+
+__author__ = 'Steffen Kampmann, steffen.kampmann@gmail.com'
+
+from pybrain.tests import runModuleTestSuite
+
+if __name__ == "__main__":
+ runModuleTestSuite(__import__('__main__'))
From 5f15fd5b41dfee243ae136e0701fe1059adf54dd Mon Sep 17 00:00:00 2001
From: Steffen Kampmann
Date: Thu, 15 Nov 2012 15:35:12 +0100
Subject: [PATCH 19/83] fix test_backprop.py
---
.../tests/unittests/supervised/trainers/test_backprop.py | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/pybrain/tests/unittests/supervised/trainers/test_backprop.py b/pybrain/tests/unittests/supervised/trainers/test_backprop.py
index 76a57c03d..b2fe8c18f 100644
--- a/pybrain/tests/unittests/supervised/trainers/test_backprop.py
+++ b/pybrain/tests/unittests/supervised/trainers/test_backprop.py
@@ -18,9 +18,9 @@
>>>
>>> net = FeedForwardNetwork()
>>>
- >>> net.addInputModule(SigmoidLayer(6, name='in'))
- >>> net.addModule(LinearLayer(40, name='hidden_0'))
- >>> net.addModule(LinearLayer(16, name='hidden_1'))
+ >>> net.addInputModule(LinearLayer(6, name='in'))
+ >>> net.addModule(SigmoidLayer(40, name='hidden_0'))
+ >>> net.addModule(SigmoidLayer(16, name='hidden_1'))
>>> net.addOutputModule(LinearLayer(2, name='out'))
>>>
>>> net.addConnection(FullConnection(net['in'], net['hidden_0']))
@@ -40,7 +40,7 @@
>>>
>>> trainingErrors, validationErrors = trainer.trainUntilConvergence(
... dataset=dataset,
- ... maxEpochs=100)
+ ... maxEpochs=10)
"""
From dbc1d3cd68dcd2cc4c859882d52acc6a00c15ddb Mon Sep 17 00:00:00 2001
From: Steffen Kampmann
Date: Thu, 20 Dec 2012 12:36:06 +0100
Subject: [PATCH 20/83] add threshold for convergence check in backprop.py fix
variable name in dataset.py
---
pybrain/datasets/dataset.py | 2 +-
pybrain/supervised/trainers/backprop.py | 12 +++++++++---
2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/pybrain/datasets/dataset.py b/pybrain/datasets/dataset.py
index fd36860e2..081a3d504 100644
--- a/pybrain/datasets/dataset.py
+++ b/pybrain/datasets/dataset.py
@@ -363,6 +363,6 @@ def replaceNansByMeans(self):
for d in self.data.itervalues():
means = scipy.nansum(d[:self.getLength()], axis=0) / self.getLength()
for i in xrange(self.getLength()):
- for j in xrange(ds.dim):
+ for j in xrange(d.dim):
if not scipy.isfinite(d[i, j]):
d[i, j] = means[j]
diff --git a/pybrain/supervised/trainers/backprop.py b/pybrain/supervised/trainers/backprop.py
index ee6a97b7e..0d3f4f6f2 100644
--- a/pybrain/supervised/trainers/backprop.py
+++ b/pybrain/supervised/trainers/backprop.py
@@ -185,7 +185,8 @@ def testOnClassData(self, dataset=None, verbose=False,
def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
continueEpochs=10, validationProportion=0.25,
- trainingData=None, validationData=None):
+ trainingData=None, validationData=None,
+ convergence_threshold=10):
"""Train the module on the dataset until it converges.
Return the module with the parameters that gave the minimal validation
@@ -216,6 +217,7 @@ def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
self.ds = trainingData
bestweights = self.module.params.copy()
bestverr = self.testOnData(validationData)
+ bestepoch = 0
self.trainingErrors = []
self.validationErrors = [bestverr]
while True:
@@ -229,6 +231,7 @@ def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
# one update is always done
bestverr = self.validationErrors[-1]
bestweights = self.module.params.copy()
+ bestepoch = epochs
if maxEpochs != None and epochs >= maxEpochs:
self.module.params[:] = bestweights
@@ -243,9 +246,12 @@ def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
if min(new) > max(old):
self.module.params[:] = bestweights
break
- self.trainingErrors.append(self.testOnData(trainingData))
+ elif reduce(lambda x, y: x + (y - round(new[-1], convergence_threshold)), [round(y, convergence_threshold) for y in new]) == 0:
+ self.module.params[:] = bestweights
+ break
+ #self.trainingErrors.append(self.testOnData(trainingData))
self.ds = dataset
if verbose:
print 'train-errors:', fListToString(self.trainingErrors, 6)
print 'valid-errors:', fListToString(self.validationErrors, 6)
- return self.trainingErrors, self.validationErrors
+ return self.trainingErrors[:bestepoch], self.validationErrors[:1 + bestepoch]
From 0d651ab912fbfbb1b6c80daf7d3c6b0f571e68a7 Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Fri, 1 Feb 2013 19:55:24 -0500
Subject: [PATCH 21/83] minor
---
.pydevproject | 2 +-
pybrain/optimization/distributionbased/__init__.py | 1 +
pybrain/optimization/optimizer.py | 3 +++
pybrain/tools/functions.py | 2 +-
4 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/.pydevproject b/.pydevproject
index 6c9f8f4ba..9b056b0fb 100644
--- a/.pydevproject
+++ b/.pydevproject
@@ -5,6 +5,6 @@
python 2.6
Default
-/pybrain
+/PyBrain-git-master
diff --git a/pybrain/optimization/distributionbased/__init__.py b/pybrain/optimization/distributionbased/__init__.py
index ac5f31c9d..9acd01f85 100644
--- a/pybrain/optimization/distributionbased/__init__.py
+++ b/pybrain/optimization/distributionbased/__init__.py
@@ -3,3 +3,4 @@
from nes import ExactNES, OriginalNES
from ves import VanillaGradientEvolutionStrategies
from xnes import XNES
+from snes import SNES
diff --git a/pybrain/optimization/optimizer.py b/pybrain/optimization/optimizer.py
index dd7a45f18..d3da5387f 100644
--- a/pybrain/optimization/optimizer.py
+++ b/pybrain/optimization/optimizer.py
@@ -187,6 +187,9 @@ def learn(self, additionalLearningSteps = None):
except DivergenceError:
logging.warning("Algorithm diverged. Stopped after "+str(self.numLearningSteps)+" learning steps.")
break
+ except ValueError:
+ logging.warning("Something numerical went wrong. Stopped after "+str(self.numLearningSteps)+" learning steps.")
+ break
return self._bestFound()
def _learnStep(self):
diff --git a/pybrain/tools/functions.py b/pybrain/tools/functions.py
index fc36dfbcf..b87887f82 100644
--- a/pybrain/tools/functions.py
+++ b/pybrain/tools/functions.py
@@ -47,7 +47,7 @@ def f(val):
def safeExp(x):
- """ Bounded range for the exponential function (won't rpoduce inf or NaN). """
+ """ Bounded range for the exponential function (won't produce inf or NaN). """
return exp(clip(x, -500, 500))
From afdeb497489f40f0609fb381c44ab418311c0286 Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Fri, 1 Feb 2013 21:50:31 -0500
Subject: [PATCH 22/83] Model-based RL, policy iteration, LSTD and LSPI, all
for when a transition probability model is given.
---
pybrain/rl/learners/__init__.py | 1 +
pybrain/rl/learners/modelbased/__init__.py | 2 +
.../rl/learners/modelbased/leastsquares.py | 127 +++++++++++++++
.../rl/learners/modelbased/policyiteration.py | 144 ++++++++++++++++++
pybrain/utilities.py | 33 +++-
5 files changed, 306 insertions(+), 1 deletion(-)
create mode 100644 pybrain/rl/learners/modelbased/__init__.py
create mode 100644 pybrain/rl/learners/modelbased/leastsquares.py
create mode 100644 pybrain/rl/learners/modelbased/policyiteration.py
diff --git a/pybrain/rl/learners/__init__.py b/pybrain/rl/learners/__init__.py
index 997edc0b1..4c4f3b0cc 100644
--- a/pybrain/rl/learners/__init__.py
+++ b/pybrain/rl/learners/__init__.py
@@ -1,2 +1,3 @@
from directsearch.__init__ import *
from valuebased.__init__ import *
+from modelbased.__init__ import *
diff --git a/pybrain/rl/learners/modelbased/__init__.py b/pybrain/rl/learners/modelbased/__init__.py
new file mode 100644
index 000000000..94b67c6a0
--- /dev/null
+++ b/pybrain/rl/learners/modelbased/__init__.py
@@ -0,0 +1,2 @@
+from policyiteration import trueValues, trueQValues, policyIteration
+from leastsquares import LSTD_values, LSTD_Qvalues, LSPI_policy, LSTD_PI_policy
\ No newline at end of file
diff --git a/pybrain/rl/learners/modelbased/leastsquares.py b/pybrain/rl/learners/modelbased/leastsquares.py
new file mode 100644
index 000000000..1715d57c7
--- /dev/null
+++ b/pybrain/rl/learners/modelbased/leastsquares.py
@@ -0,0 +1,127 @@
+__author__ = 'Tom Schaul, tom@idsia.ch'
+
+"""
+Doing RL when an environment model (transition matrices and rewards) are available,
+and the states are observed by a feature vector for each state:
+
+ - a feature map (fMap) is a 2D array of features, one row per state.
+
+(otherwise same representation as for in policyiteration.py)
+
+Here we provide some algorithms when the values are estimated linearly from the features.
+
+"""
+
+# TODO: optimize LSTDQ by vectorization
+
+import random
+from scipy import ravel, zeros, outer, dot, tile, transpose, tensordot
+from scipy.linalg import lstsq
+from numpy.matlib import repmat
+
+
+from policyiteration import randomPolicy, greedyQPolicy, collapsedTransitions, policyIteration
+
+
+def trueFeatureStats(T, R, fMap, discountFactor, stateProp=1, MAT_LIMIT=1e8):
+ """ Gather the statistics needed for LSTD,
+ assuming infinite data (true probabilities).
+ Option: if stateProp is < 1, then only a proportion of all
+ states will be seen as starting state for transitions """
+ dim = len(fMap)
+ numStates = len(T)
+ statMatrix = zeros((dim, dim))
+ statResidual = zeros(dim)
+ ss = range(numStates)
+ repVersion = False
+
+ if stateProp < 1:
+ ss = random.sample(ss, int(numStates * stateProp))
+ elif dim * numStates**2 < MAT_LIMIT:
+ repVersion = True
+
+ # two variants, depending on how large we can afford our matrices to become.
+ if repVersion:
+ tmp1 = tile(fMap, (numStates,1,1))
+ tmp2 = transpose(tmp1, (2,1,0))
+ tmp3 = tmp2 - discountFactor * tmp1
+ tmp4 = tile(T, (dim,1,1))
+ tmp4 *= transpose(tmp1, (1,2,0))
+ statMatrix = tensordot(tmp3, tmp4, axes=[[0,2], [1,2]]).T
+ statResidual = dot(R, dot(fMap, T).T)
+ else:
+ for sto in ss:
+ tmp = fMap - discountFactor * repmat(fMap[:, sto], numStates, 1).T
+ tmp2 = fMap * repmat(T[:, sto], dim, 1)
+ statMatrix += dot(tmp2, tmp.T)
+ statResidual += R[sto] * dot(fMap, T[:, sto])
+ return statMatrix, statResidual
+
+
+def LSTD_values(T, R, fMap, discountFactor, **kwargs):
+ """ Least-squares temporal difference algorithm. """
+ statMatrix, statResidual = trueFeatureStats(T, R, fMap, discountFactor,**kwargs)
+ weights = lstsq(statMatrix, statResidual)[0]
+ return dot(weights, fMap)
+
+
+def LSTD_Qvalues(Ts, policy, R, fMap, discountFactor):
+ """ LSTDQ is like LSTD, but with features replicated
+ once for each possible action.
+
+ Returns Q-values in a 2D array. """
+ numA = len(Ts)
+ dim = len(Ts[0])
+ numF = len(fMap)
+ fMapRep = zeros((numF * numA, dim * numA))
+ for a in range(numA):
+ fMapRep[numF * a:numF * (a + 1), dim * a:dim * (a + 1)] = fMap
+
+ statMatrix = zeros((numF * numA, numF * numA))
+ statResidual = zeros(numF * numA)
+ for sto in range(dim):
+ r = R[sto]
+ fto = zeros(numF * numA)
+ for nextA in range(numA):
+ fto += fMapRep[:, sto + nextA * dim] * policy[sto][nextA]
+ for sfrom in range(dim):
+ for a in range(numA):
+ ffrom = fMapRep[:, sfrom + a * dim]
+ prob = Ts[a][sfrom, sto]
+ statMatrix += outer(ffrom, ffrom - discountFactor * fto) * prob
+ statResidual += ffrom * r * prob
+
+ Qs = zeros((dim, numA))
+ w = lstsq(statMatrix, statResidual)[0]
+ for a in range(numA):
+ Qs[:,a] = dot(w[numF*a:numF*(a+1)], fMap)
+ return Qs
+
+
+def LSPI_policy(fMap, Ts, R, discountFactor, initpolicy=None, maxIters=20):
+ """ LSPI is like policy iteration, but Q-values are estimated based
+ on the feature map.
+ Returns the best policy found. """
+ if initpolicy is None:
+ policy, _ = randomPolicy(Ts)
+ else:
+ policy = initpolicy
+
+ while maxIters > 0:
+ Qs = LSTD_Qvalues(Ts, policy, R, fMap, discountFactor)
+ newpolicy = greedyQPolicy(Qs)
+ if sum(ravel(abs(newpolicy - policy))) < 1e-3:
+ return policy, collapsedTransitions(Ts, policy)
+ policy = newpolicy
+ maxIters -= 1
+ return policy, collapsedTransitions(Ts, policy)
+
+
+def LSTD_PI_policy(fMap, Ts, R, discountFactor, initpolicy=None, maxIters=20):
+ """ Alternative version of LSPI using value functions
+ instead of state-action values as intermediate.
+ """
+ def veval(T):
+ return LSTD_values(T, R, fMap, discountFactor)
+ return policyIteration(Ts, R, discountFactor, VEvaluator=veval,
+ initpolicy=initpolicy, maxIters=maxIters)
diff --git a/pybrain/rl/learners/modelbased/policyiteration.py b/pybrain/rl/learners/modelbased/policyiteration.py
new file mode 100644
index 000000000..e04a66817
--- /dev/null
+++ b/pybrain/rl/learners/modelbased/policyiteration.py
@@ -0,0 +1,144 @@
+__author__ = 'Tom Schaul, tom@idsia.ch'
+
+"""
+Doing RL when an environment model (transition matrices and rewards) are available.
+
+
+Representation:
+ - a policy is a 2D-array of probabilities,
+ one row per state (summing to 1), one column per action.
+
+ - a transition matrix (T) maps from originating states to destination states
+ (probabilities in each row sum to 1).
+
+ - a reward vector (R) maps each state to the reward value obtained when entering (or staying in) a state.
+
+ - a feature map (fMap) is a 2D array of features, one row per state.
+
+ - a task model is defined by a list of transition matrices (Ts), one per action, a
+ reward vector R, a discountFactor
+
+Note: a task model combined with a policy is again a transition matrix ("collapsed" dynamics).
+
+ - a value function (V) is a vector of expected discounted rewards (one per state).
+
+ - a set of state-action values (Qs) is a 2D array, one row per action.
+
+"""
+
+# TODO: we may use an alternative, more efficient representation if all actions are deterministic
+# TODO: it may be worth considering a sparse representation of T matrices.
+# TODO: optimize some of this code with vectorization
+
+
+from scipy import dot, zeros, zeros_like, ones, mean, array, ravel, rand
+from numpy.matlib import repmat
+
+from pybrain.utilities import all_argmax
+
+
+def trueValues(T, R, discountFactor):
+ """ Compute the true discounted value function for each state,
+ given a policy (encoded as collapsed transition matrix). """
+ assert discountFactor < 1
+ distr = T.copy()
+ res = dot(T, R)
+ for i in range(1, int(10 / (1. - discountFactor))):
+ distr = dot(distr, T)
+ res += (discountFactor ** i) * dot(distr, R)
+ return res
+
+
+def trueQValues(Ts, R, discountFactor, policy):
+ """ The true Q-values, given a model and a policy. """
+ T = collapsedTransitions(Ts, policy)
+ V = trueValues(T, R, discountFactor)
+ Vnext = V*discountFactor+R
+ numA = len(Ts)
+ dim = len(R)
+ Qs = zeros((dim, numA))
+ for si in range(dim):
+ for a in range(numA):
+ Qs[si, a] = dot(Ts[a][si], Vnext)
+ return Qs
+
+
+def collapsedTransitions(Ts, policy):
+ """ Collapses a list of transition matrices (one per action) and a list
+ of action probability vectors into a single transition matrix."""
+ res = zeros_like(Ts[0])
+ dim = len(Ts[0])
+ for ai, ap in enumerate(policy.T):
+ res += Ts[ai] * repmat(ap, dim, 1).T
+ return res
+
+
+def greedyPolicy(Ts, R, discountFactor, V):
+ """ Find the greedy policy, (soft tie-breaking)
+ given a value function and full transition model. """
+ dim = len(V)
+ numA = len(Ts)
+ Vnext = V*discountFactor+R
+ policy = zeros((dim, numA))
+ for si in range(dim):
+ actions = all_argmax([dot(T[si, :], Vnext) for T in Ts])
+ for a in actions:
+ policy[si, a] = 1. / len(actions)
+ return policy, collapsedTransitions(Ts, policy)
+
+
+def greedyQPolicy(Qs):
+ """ Find the greedy deterministic policy,
+ given the Q-values. """
+ dim = len(Qs)
+ numA = len(Qs[0])
+ policy = zeros((dim, numA))
+ for si in range(dim):
+ actions = all_argmax(Qs[si])
+ for a in actions:
+ policy[si, a] = 1. / len(actions)
+ return policy
+
+
+def randomPolicy(Ts):
+ """ Each action is equally likely. """
+ numA = len(Ts)
+ dim = len(Ts[0])
+ return ones((dim, numA)) / float(numA), mean(array(Ts), axis=0)
+
+
+def randomDeterministic(Ts):
+ """ Pick a random deterministic action for each state. """
+ numA = len(Ts)
+ dim = len(Ts[0])
+ choices = (rand(dim) * numA).astype(int)
+ policy = zeros((dim, numA))
+ for si, a in choices:
+ policy[si, a] = 1
+ return policy, collapsedTransitions(Ts, policy)
+
+
+def policyIteration(Ts, R, discountFactor, VEvaluator=None, initpolicy=None, maxIters=20):
+ """ Given transition matrices (one per action),
+ produce the optimal policy, using the policy iteration algorithm.
+
+ A custom function that maps policies to value functions can be provided. """
+ if initpolicy is None:
+ policy, T = randomPolicy(Ts)
+ else:
+ policy = initpolicy
+ T = collapsedTransitions(Ts, policy)
+
+ if VEvaluator is None:
+ VEvaluator = lambda T: trueValues(T, R, discountFactor)
+
+ while maxIters > 0:
+ V = VEvaluator(T)
+ newpolicy, T = greedyPolicy(Ts, R, discountFactor, V)
+ # if the probabilities are not changing more than by 0.001, we're done.
+ if sum(ravel(abs(newpolicy - policy))) < 1e-3:
+ return policy, T
+ policy = newpolicy
+ maxIters -= 1
+ return policy, T
+
diff --git a/pybrain/utilities.py b/pybrain/utilities.py
index 847e39b61..aa34ed2a7 100644
--- a/pybrain/utilities.py
+++ b/pybrain/utilities.py
@@ -684,6 +684,10 @@ def r_argmax(v):
maxbidders = [i for (i, b) in enumerate(v) if b==maxbid]
return choice(maxbidders)
+def all_argmax(x):
+ """ Return the indices of all values that are equal to the maximum: no breaking ties. """
+ m = max(x)
+ return [i for i, v in enumerate(x) if v == m]
def dense_orth(dim):
""" Constructs a dense orthogonal matrix. """
@@ -718,4 +722,31 @@ def sparse_orth(d):
Qi[i,(i+1)] = sin(theta)
Qi[(i+1),(i+1)] = -cos(theta)
Q = Q*Qi;
- return Q
\ No newline at end of file
+ return Q
+
+def xhash(arr):
+ """ Hashing function for arrays. Use with care. """
+ import hashlib
+ return hashlib.sha1(arr).hexdigest()
+
+def binArr2int(arr):
+ """ Convert a binary array into its (long) integer representation. """
+ from numpy import packbits
+ tmp2 = packbits(arr.astype(int))
+ return sum(val * 256 ** i for i, val in enumerate(tmp2[::-1]))
+
+def uniqueArrays(vs):
+ """ create a set of arrays """
+ resdic = {}
+ for v in vs:
+ resdic[xhash(v)] = v
+ return resdic.values()
+
+
+def seedit(seed=0):
+ """ Fixed seed makes for repeatability, but there may be two different
+ random number generators involved. """
+ import random
+ import numpy
+ random.seed(seed)
+ numpy.random.seed(seed)
From bf6a5ca81dd6a945766d9bb08e2ba9a99b1fc1ce Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Sat, 2 Feb 2013 13:32:57 -0500
Subject: [PATCH 23/83] minor
---
.gitignore | 3 ++-
.pydevproject | 20 ++++++++++----------
acknowledgements.txt | 6 +++---
pybrain/rl/environments/episodic.py | 4 ++--
4 files changed, 17 insertions(+), 16 deletions(-)
diff --git a/.gitignore b/.gitignore
index e97855d52..121814af5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,4 +10,5 @@ dist
docs/sphinx/.build
.DS_Store
/pybrain/rl/environments/cartpole/fast_version/cartpolewrap.cpp
-/pybrain/rl/environments/cartpole/fast_version/cartpolewrap.pyd
\ No newline at end of file
+/pybrain/rl/environments/cartpole/fast_version/cartpolewrap.pyd
+/.pydevproject
diff --git a/.pydevproject b/.pydevproject
index 9b056b0fb..a5385fa0c 100644
--- a/.pydevproject
+++ b/.pydevproject
@@ -1,10 +1,10 @@
-
-
-
-
-python 2.6
-Default
-
-/PyBrain-git-master
-
-
+
+
+
+
+python 2.6
+Default
+
+/pybrain
+
+
diff --git a/acknowledgements.txt b/acknowledgements.txt
index 11d985316..9882fbb8c 100644
--- a/acknowledgements.txt
+++ b/acknowledgements.txt
@@ -2,6 +2,6 @@ PyBrain was developed originally as a joint project between
PhD students at IDSIA and at the CogBotLab at the TU Munich.
Part of the development was supported by the Swiss National Science Foundation
-grants 200021-111968/1 and 200021-113364/1, as well as through the
-Excellence Cluster Cognition for Technical Systems (CoTeSys)
-
+grants 200020-122124, 200021-111968/1 and 200021-113364/1, through the
+German Excellence Cluster Cognition for Technical Systems (CoTeSys), as well as
+by the National Research Fund Luxembourg through AFR grant number 2915104.
\ No newline at end of file
diff --git a/pybrain/rl/environments/episodic.py b/pybrain/rl/environments/episodic.py
index 435a7d6b1..d8e9997c9 100644
--- a/pybrain/rl/environments/episodic.py
+++ b/pybrain/rl/environments/episodic.py
@@ -39,9 +39,9 @@ def isFinished(self):
def performAction(self, action):
""" Execute one action. """
Task.performAction(self, action)
- self.addReward()
self.samples += 1
-
+ self.addReward()
+
def addReward(self):
""" A filtered mapping towards performAction of the underlying environment. """
# by default, the cumulative reward is just the sum over the episode
From a56fafca7c87ceb41d97b5ad009f7e75a39ba95c Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Sat, 2 Feb 2013 13:46:37 -0500
Subject: [PATCH 24/83] fixed a couple of tests that were not passing for the
wrong reasons
---
pybrain/tests/unittests/datasets/test_datasets_datasets.py | 2 +-
.../tests/unittests/supervised/knn/lsh/test_nearoptimal.py | 7 +++----
2 files changed, 4 insertions(+), 5 deletions(-)
diff --git a/pybrain/tests/unittests/datasets/test_datasets_datasets.py b/pybrain/tests/unittests/datasets/test_datasets_datasets.py
index ba061f04f..80fa6fc0a 100644
--- a/pybrain/tests/unittests/datasets/test_datasets_datasets.py
+++ b/pybrain/tests/unittests/datasets/test_datasets_datasets.py
@@ -8,7 +8,7 @@
>>> d = datasets.dataset.DataSet()
>>> d.addField('input', 2)
>>> d.data['input']
- array([], shape=(0, 2), dtype=...)
+ array([], shape=(0..., 2...), dtype=...)
Build up a DataSet for testing:
diff --git a/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py b/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
index a667e3f81..8f556a385 100644
--- a/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
+++ b/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
@@ -8,8 +8,6 @@
Internal Tests:
====================
- >>> from pybrain.supervised.knn.lsh import nearoptimal
-
Let's make a hypercube for 2 dimensions.
>>> dim = 2
@@ -21,9 +19,9 @@
>>> m = nearoptimal.MultiDimHash(dim=dim, omega=omega, prob=0.8)
>>> m.radius
- 1.189207115002721
+ 1.189207115002...
>>> m.radiusSquared
- 1.4142135623730949
+ 1.414213562373...
This gives us hypecube sidelength of
@@ -118,6 +116,7 @@
"""
from pybrain.tests import runModuleTestSuite
+from pybrain.supervised.knn.lsh import nearoptimal
if __name__ == "__main__":
runModuleTestSuite(__import__('__main__'))
From 84223361a6efdb3d34344f93c0daffc48e98f74f Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Thu, 28 Feb 2013 22:20:04 -0500
Subject: [PATCH 25/83] too precise
---
pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py b/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
index a667e3f81..824aac442 100644
--- a/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
+++ b/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
@@ -23,7 +23,7 @@
>>> m.radius
1.189207115002721
>>> m.radiusSquared
- 1.4142135623730949
+ 1.41421356237309...
This gives us hypecube sidelength of
From a3bfe0182c260cccf4703525b1e7ce29ac3d64c0 Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Mon, 4 Mar 2013 23:54:40 -0500
Subject: [PATCH 26/83] renamed directory
---
.pydevproject | 20 +++++++++----------
.../jpq2layersReader.py | 0
.../jpq2layersWriter.py | 0
.../myparam2.txt | 0
.../readme.txt | 0
.../trndata | 0
6 files changed, 10 insertions(+), 10 deletions(-)
rename examples/supervised/{Test Network Reader&Writer => test_network_read_write}/jpq2layersReader.py (100%)
rename examples/supervised/{Test Network Reader&Writer => test_network_read_write}/jpq2layersWriter.py (100%)
rename examples/supervised/{Test Network Reader&Writer => test_network_read_write}/myparam2.txt (100%)
rename examples/supervised/{Test Network Reader&Writer => test_network_read_write}/readme.txt (100%)
rename examples/supervised/{Test Network Reader&Writer => test_network_read_write}/trndata (100%)
diff --git a/.pydevproject b/.pydevproject
index a5385fa0c..9b056b0fb 100644
--- a/.pydevproject
+++ b/.pydevproject
@@ -1,10 +1,10 @@
-
-
-
-
-python 2.6
-Default
-
-/pybrain
-
-
+
+
+
+
+python 2.6
+Default
+
+/PyBrain-git-master
+
+
diff --git a/examples/supervised/Test Network Reader&Writer/jpq2layersReader.py b/examples/supervised/test_network_read_write/jpq2layersReader.py
similarity index 100%
rename from examples/supervised/Test Network Reader&Writer/jpq2layersReader.py
rename to examples/supervised/test_network_read_write/jpq2layersReader.py
diff --git a/examples/supervised/Test Network Reader&Writer/jpq2layersWriter.py b/examples/supervised/test_network_read_write/jpq2layersWriter.py
similarity index 100%
rename from examples/supervised/Test Network Reader&Writer/jpq2layersWriter.py
rename to examples/supervised/test_network_read_write/jpq2layersWriter.py
diff --git a/examples/supervised/Test Network Reader&Writer/myparam2.txt b/examples/supervised/test_network_read_write/myparam2.txt
similarity index 100%
rename from examples/supervised/Test Network Reader&Writer/myparam2.txt
rename to examples/supervised/test_network_read_write/myparam2.txt
diff --git a/examples/supervised/Test Network Reader&Writer/readme.txt b/examples/supervised/test_network_read_write/readme.txt
similarity index 100%
rename from examples/supervised/Test Network Reader&Writer/readme.txt
rename to examples/supervised/test_network_read_write/readme.txt
diff --git a/examples/supervised/Test Network Reader&Writer/trndata b/examples/supervised/test_network_read_write/trndata
similarity index 100%
rename from examples/supervised/Test Network Reader&Writer/trndata
rename to examples/supervised/test_network_read_write/trndata
From de5bfc8df12a41a80de1e5f656a099458506a711 Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Wed, 13 Mar 2013 13:18:34 -0400
Subject: [PATCH 27/83] flooding without specifying domain
---
pybrain/utilities.py | 19 ++++++++++++-------
1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/pybrain/utilities.py b/pybrain/utilities.py
index aa34ed2a7..788cbda86 100644
--- a/pybrain/utilities.py
+++ b/pybrain/utilities.py
@@ -456,22 +456,27 @@ def flood(stepFunction, fullSet, initSet, relevant=None):
:key relevant: (optional) list of relevant elements: stop once all relevant elements are found.
"""
- full = set(fullSet)
- flooded = full.intersection(set(initSet))
-
- if relevant is None:
- relevant = full.copy()
+ if fullSet is None:
+ flooded = set(initSet)
else:
+ full = set(fullSet)
+ flooded = full.intersection(set(initSet))
+ if relevant is None:
+ relevant = full.copy()
+ if relevant:
relevant = set(relevant)
change = flooded.copy()
while len(change)>0:
new = set()
for m in change:
- new.update(full.intersection(stepFunction(m)))
+ if fullSet is None:
+ new.update(stepFunction(m))
+ else:
+ new.update(full.intersection(stepFunction(m)))
change = new.difference(flooded)
flooded.update(change)
- if relevant.issubset(flooded):
+ if relevant is not None and relevant.issubset(flooded):
break
return list(flooded)
From b5843c0626b5b65bc73e09e358adb040dc33f3ad Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Wed, 20 Mar 2013 21:20:29 -0400
Subject: [PATCH 28/83] off by one in incremental learning step assignment
---
.pydevproject | 20 ++++++++++----------
pybrain/optimization/optimizer.py | 2 +-
2 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/.pydevproject b/.pydevproject
index a5385fa0c..9b056b0fb 100644
--- a/.pydevproject
+++ b/.pydevproject
@@ -1,10 +1,10 @@
-
-
-
-
-python 2.6
-Default
-
-/pybrain
-
-
+
+
+
+
+python 2.6
+Default
+
+/PyBrain-git-master
+
+
diff --git a/pybrain/optimization/optimizer.py b/pybrain/optimization/optimizer.py
index 56f6a0531..56804bead 100644
--- a/pybrain/optimization/optimizer.py
+++ b/pybrain/optimization/optimizer.py
@@ -193,7 +193,7 @@ def learn(self, additionalLearningSteps = None):
""" The main loop that does the learning. """
assert self.__evaluator is not None, "No evaluator has been set. Learning cannot start."
if additionalLearningSteps is not None:
- self.maxLearningSteps = self.numLearningSteps + additionalLearningSteps
+ self.maxLearningSteps = self.numLearningSteps + additionalLearningSteps - 1
while not self._stoppingCriterion():
try:
self._learnStep()
From 9070967f5dd78fdd3a2713be5e31cd362fd517a6 Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Wed, 20 Mar 2013 21:55:10 -0400
Subject: [PATCH 29/83] fix: reset transient module state upon a new episode.
---
pybrain/rl/agents/learning.py | 3 +++
1 file changed, 3 insertions(+)
diff --git a/pybrain/rl/agents/learning.py b/pybrain/rl/agents/learning.py
index 182166ae7..06c1e0baf 100644
--- a/pybrain/rl/agents/learning.py
+++ b/pybrain/rl/agents/learning.py
@@ -57,6 +57,9 @@ def getAction(self):
def newEpisode(self):
""" Indicate the beginning of a new episode in the training cycle. """
+ # reset the module when a new episode starts.
+ self.module.reset()
+
if self.logging:
self.history.newSequence()
From 9648aaa5d8239fb17daca7c04fa1120d0b6f06e0 Mon Sep 17 00:00:00 2001
From: Ludwig Schmidt-Hackenberg
Date: Thu, 11 Apr 2013 17:30:07 +0300
Subject: [PATCH 30/83] Added var DS to def of evalRnnOnSeqDataset
---
examples/supervised/backprop/parityrnn.py | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/examples/supervised/backprop/parityrnn.py b/examples/supervised/backprop/parityrnn.py
index ab1c48f0f..278e9f28b 100644
--- a/examples/supervised/backprop/parityrnn.py
+++ b/examples/supervised/backprop/parityrnn.py
@@ -27,7 +27,7 @@ def buildParityNet():
return net
-def evalRnnOnSeqDataset(net, verbose = False, silent = False):
+def evalRnnOnSeqDataset(net, DS, verbose = False, silent = False):
""" evaluate the network on all the sequences of a dataset. """
r = 0.
samples = 0.
@@ -49,10 +49,10 @@ def evalRnnOnSeqDataset(net, verbose = False, silent = False):
if __name__ == "__main__":
N = buildParityNet()
DS = ParityDataSet()
- evalRnnOnSeqDataset(N, verbose = True)
+ evalRnnOnSeqDataset(N, DS, verbose = True)
print '(preset weights)'
N.randomize()
- evalRnnOnSeqDataset(N)
+ evalRnnOnSeqDataset(N, DS)
print '(random weights)'
@@ -60,5 +60,5 @@ def evalRnnOnSeqDataset(net, verbose = False, silent = False):
N.reset()
bp = BackpropTrainer(N, DS, verbose = True)
bp.trainEpochs(5000)
- evalRnnOnSeqDataset(N)
+ evalRnnOnSeqDataset(N, DS)
print '(backprop-trained weights)'
From acf1dfd8f2c4361ed7988b7bbb005ecc854711a9 Mon Sep 17 00:00:00 2001
From: CJ Carey
Date: Wed, 24 Apr 2013 12:09:21 -0300
Subject: [PATCH 31/83] small typo fix
---
docs/html/_sources/advanced/ode.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/docs/html/_sources/advanced/ode.txt b/docs/html/_sources/advanced/ode.txt
index 7248b4ca2..2d52c9406 100644
--- a/docs/html/_sources/advanced/ode.txt
+++ b/docs/html/_sources/advanced/ode.txt
@@ -21,7 +21,7 @@ You also need to exchange the following two ``.py`` files with custom versions:
::
cd pybrain/pybrain/rl/environments/ode/xode_changes/
- sudo cp * /usr/lib/python2.6/dist-packages/xode/ (or there ever your dist-packages are)
+ sudo cp * /usr/lib/python2.6/dist-packages/xode/ (or whereever your dist-packages are)
You can test if all your settings are ok by starting following example:
::
From ed51b72221822a3c08ef4dab514bd5b1b276b180 Mon Sep 17 00:00:00 2001
From: CJ Carey
Date: Thu, 25 Apr 2013 14:02:46 -0300
Subject: [PATCH 32/83] oops, typo in my typo fix
---
docs/html/_sources/advanced/ode.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/docs/html/_sources/advanced/ode.txt b/docs/html/_sources/advanced/ode.txt
index 2d52c9406..2be993cf2 100644
--- a/docs/html/_sources/advanced/ode.txt
+++ b/docs/html/_sources/advanced/ode.txt
@@ -21,7 +21,7 @@ You also need to exchange the following two ``.py`` files with custom versions:
::
cd pybrain/pybrain/rl/environments/ode/xode_changes/
- sudo cp * /usr/lib/python2.6/dist-packages/xode/ (or whereever your dist-packages are)
+ sudo cp * /usr/lib/python2.6/dist-packages/xode/ (or wherever your dist-packages are)
You can test if all your settings are ok by starting following example:
::
From 434083ead5339712cdd8b2b35be025838ebbc64e Mon Sep 17 00:00:00 2001
From: Sun Junyi
Date: Tue, 7 May 2013 15:01:34 +0800
Subject: [PATCH 33/83] Update mnist.py
fix the file reading bug on Windows
---
pybrain/tools/datasets/mnist.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/pybrain/tools/datasets/mnist.py b/pybrain/tools/datasets/mnist.py
index 8d7e58611..06c7bf52d 100644
--- a/pybrain/tools/datasets/mnist.py
+++ b/pybrain/tools/datasets/mnist.py
@@ -16,7 +16,7 @@ def labels(filename):
def images(filename):
- fp = file(filename)
+ fp = file(filename,'rb')
chunk = fp.read(16)
magicnumber, length, numrows, numcols = struct.unpack('>iiii', chunk)
assert magicnumber in (2049, 2051), ("Not an MNIST file: %i" % magicnumber)
@@ -53,4 +53,4 @@ def makeMnistDataSets(path):
for image, label in itertools.izip(train_images, train_labels):
train.addSample(image, label)
- return train, test
\ No newline at end of file
+ return train, test
From 0a05f4cae2aa3be6fe29ea5de25f6142aea0ed08 Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Thu, 16 May 2013 22:40:57 -0400
Subject: [PATCH 34/83] new module: layer of rectified linear units
---
pybrain/structure/connections/linear.py | 2 +-
pybrain/structure/connections/shared.py | 1 -
pybrain/structure/modules/__init__.py | 1 +
pybrain/structure/modules/relulayer.py | 12 ++++++++++++
4 files changed, 14 insertions(+), 2 deletions(-)
create mode 100644 pybrain/structure/modules/relulayer.py
diff --git a/pybrain/structure/connections/linear.py b/pybrain/structure/connections/linear.py
index 277989407..46f85595a 100644
--- a/pybrain/structure/connections/linear.py
+++ b/pybrain/structure/connections/linear.py
@@ -22,5 +22,5 @@ def _forwardImplementation(self, inbuf, outbuf):
outbuf += inbuf * self.params
def _backwardImplementation(self, outerr, inerr, inbuf):
- #CHECKME: not setting derivatives
+ #CHECKME: not setting derivatives -- this means the multiplicative weight is never updated!
inerr += outerr * self.params
diff --git a/pybrain/structure/connections/shared.py b/pybrain/structure/connections/shared.py
index de7b3248a..07939641c 100644
--- a/pybrain/structure/connections/shared.py
+++ b/pybrain/structure/connections/shared.py
@@ -1,4 +1,3 @@
-from pybrain.structure.modules.linearlayer import LinearLayer
__author__ = 'Tom Schaul, tom@idsia.ch'
from pybrain.structure.parametercontainer import ParameterContainer
diff --git a/pybrain/structure/modules/__init__.py b/pybrain/structure/modules/__init__.py
index 42af4f555..67663ff58 100644
--- a/pybrain/structure/modules/__init__.py
+++ b/pybrain/structure/modules/__init__.py
@@ -12,3 +12,4 @@
from kohonen import KohonenMap
from table import Table
from module import Module
+from relulayer import ReluLayer
diff --git a/pybrain/structure/modules/relulayer.py b/pybrain/structure/modules/relulayer.py
new file mode 100644
index 000000000..53a3376e0
--- /dev/null
+++ b/pybrain/structure/modules/relulayer.py
@@ -0,0 +1,12 @@
+__author__ = 'Tom Schaul, tom@idsia.ch'
+
+from neuronlayer import NeuronLayer
+
+class ReluLayer(NeuronLayer):
+ """ Layer of rectified linear units (relu). """
+
+ def _forwardImplementation(self, inbuf, outbuf):
+ outbuf[:] = inbuf * (inbuf > 0)
+
+ def _backwardImplementation(self, outerr, inerr, outbuf, inbuf):
+ inerr[:] = outerr * (inbuf > 0)
\ No newline at end of file
From e248d97e088ee278dd8d562bd31f0561db9f04cf Mon Sep 17 00:00:00 2001
From: Andrew Chase
Date: Thu, 30 May 2013 02:23:36 -0600
Subject: [PATCH 35/83] Fixed an issue where a local variable overwrote a
matplotlib one.
---
pybrain/tools/plotting/multiline.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/pybrain/tools/plotting/multiline.py b/pybrain/tools/plotting/multiline.py
index 879daede7..3333952ff 100644
--- a/pybrain/tools/plotting/multiline.py
+++ b/pybrain/tools/plotting/multiline.py
@@ -180,7 +180,7 @@ def update(self):
self.replot = False
- def show(self, xLabel='', yLabel='', title='', popup=False, imgfile=None):
+ def show(self, xLabel='', yLabel='', Title='', popup=False, imgfile=None):
""" Plots the data internally and saves an image of it to the plotting directory.
:key title: The title of the plot.
@@ -192,7 +192,7 @@ def show(self, xLabel='', yLabel='', title='', popup=False, imgfile=None):
plot(self.indexList[i], self.dataList[i])
xlabel(xLabel)
ylabel(yLabel)
- title(title)
+ title(Title)
if imgfile == None:
imgfile = imp.find_module('pybrain')[1] + "/tools/plotting/plot.png"
savefig(imgfile)
From 6cf467a6f1ac28039b4b262bd0e0ea788fef246d Mon Sep 17 00:00:00 2001
From: Johannes Bjerva
Date: Sun, 9 Jun 2013 10:07:51 +0200
Subject: [PATCH 36/83] Checking 'index in *list*' takes forever. Changed to
set for O(1) access times.
---
pybrain/datasets/supervised.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pybrain/datasets/supervised.py b/pybrain/datasets/supervised.py
index d08634870..03eb7e7a2 100644
--- a/pybrain/datasets/supervised.py
+++ b/pybrain/datasets/supervised.py
@@ -102,7 +102,7 @@ def evaluateModuleMSE(self, module, averageOver = 1, **args):
def splitWithProportion(self, proportion = 0.5):
"""Produce two new datasets, the first one containing the fraction given
by `proportion` of the samples."""
- leftIndices = sample(range(len(self)), int(len(self)*proportion))
+ leftIndices = set(sample(range(len(self)), int(len(self)*proportion)))
leftDs = self.copy()
leftDs.clear()
rightDs = leftDs.copy()
From c54661f13857d5bcb0095ba2fb12f5a403a4a70f Mon Sep 17 00:00:00 2001
From: Tom Schaul
Date: Mon, 22 Jul 2013 19:40:21 -0400
Subject: [PATCH 37/83] Added weighted version of Mann-Whitney U-test
---
.../environments/functions/multiobjective.py | 5 +--
pybrain/tools/nondominated.py | 2 +-
pybrain/utilities.py | 32 +++++++++++++++++++
3 files changed, 36 insertions(+), 3 deletions(-)
diff --git a/pybrain/rl/environments/functions/multiobjective.py b/pybrain/rl/environments/functions/multiobjective.py
index 7a044d78d..c5006aeec 100644
--- a/pybrain/rl/environments/functions/multiobjective.py
+++ b/pybrain/rl/environments/functions/multiobjective.py
@@ -1,10 +1,10 @@
""" Some multi-objective benchmark functions.
Implemented according to the classical reference paper of Deb et al. (Evolutionary Computation 2002) """
-from scipy import array, exp, sqrt, sin, cos, power, pi, arctan
+from scipy import array, exp, sqrt, sin, cos, power, pi, arctan, ndarray
from pybrain.rl.environments.functions.function import FunctionEnvironment
from pybrain.structure.parametercontainer import ParameterContainer
-from scipy import array, ndarray
+
__author__ = 'Tom Schaul, tom@idsia.ch'
@@ -229,6 +229,7 @@ def f(self, x):
f1 = x[0]
f2 = x[1]
return -array([f1, f2])
+
class ConstBnh(MultiObjectiveFunction):
""" Binh & Korn 1997 """
xdim = 2
diff --git a/pybrain/tools/nondominated.py b/pybrain/tools/nondominated.py
index 9fc4c453d..bda2054f5 100644
--- a/pybrain/tools/nondominated.py
+++ b/pybrain/tools/nondominated.py
@@ -173,7 +173,7 @@ def non_dominated_sort(iterable, key=lambda x: x, allowequality=True):
fronts.append(front)
return fronts
-''' added by JPQ for Constraine Multi-objective Optimization '''
+''' added by JPQ for Constrained Multi-objective Optimization '''
def _const_non_dominated_front_merge_arr(iterable, key=lambda x: x, allowequality=True):
diff --git a/pybrain/utilities.py b/pybrain/utilities.py
index 788cbda86..19d74aa01 100644
--- a/pybrain/utilities.py
+++ b/pybrain/utilities.py
@@ -755,3 +755,35 @@ def seedit(seed=0):
import numpy
random.seed(seed)
numpy.random.seed(seed)
+
+
+
+def weightedUtest(g1, w1, g2, w2):
+ """ Determines the confidence level of the assertion:
+ 'The values of g2 are higher than those of g1'.
+ (adapted from the scipy.stats version)
+
+ Twist: here the elements of each group have associated weights,
+ corresponding to how often they are present (i.e. two identical entries with
+ weight w are equivalent to one entry with weight 2w).
+ Reference: "Studies in Continuous Black-box Optimization", Schaul, 2011 [appendix B].
+
+ TODO: make more efficient for large sets.
+ """
+ from scipy.stats.distributions import norm
+ import numpy
+ n1 = sum(w1)
+ n2 = sum(w2)
+ u1 = 0.
+ for x1, wx1 in zip(g1, w1):
+ for x2, wx2 in zip(g2, w2):
+ if x1 == x2:
+ u1 += 0.5 * wx1 * wx2
+ elif x1 > x2:
+ u1 += wx1 * wx2
+ mu = n1*n2/2.
+ sigu = numpy.sqrt(n1*n2*(n1+n2+1)/12.)
+ z = (u1 - mu) / sigu
+ conf = norm.cdf(z)
+ return conf
+
From b4368fe6452ed3265de721dd6b3fc209895e74b8 Mon Sep 17 00:00:00 2001
From: Ian Danforth
Date: Thu, 12 Sep 2013 17:05:46 -0700
Subject: [PATCH 38/83] Quick answer needs another step to actually work
---
docs/html/_sources/index.txt | 1 +
1 file changed, 1 insertion(+)
diff --git a/docs/html/_sources/index.txt b/docs/html/_sources/index.txt
index d6c60b7a6..a0e6d141d 100644
--- a/docs/html/_sources/index.txt
+++ b/docs/html/_sources/index.txt
@@ -34,6 +34,7 @@ Quick answer:
::
$ git clone git://github.com/pybrain/pybrain.git
+ $ cd pybrain
$ python setup.py install
Long answer:
From a701f557a7613c3b26fd568f8ddb4c97fc2ba699 Mon Sep 17 00:00:00 2001
From: Patrick
Date: Wed, 18 Sep 2013 17:55:25 -0500
Subject: [PATCH 39/83] ExcelTools
---
ExcelTools.py | 39 +++++++++++++++++++++++++++++++++++++++
1 file changed, 39 insertions(+)
create mode 100644 ExcelTools.py
diff --git a/ExcelTools.py b/ExcelTools.py
new file mode 100644
index 000000000..88c18d8fa
--- /dev/null
+++ b/ExcelTools.py
@@ -0,0 +1,39 @@
+from pybrain.datasets.supervised import SupervisedDataSet
+from pybrain.optimization.randomsearch import RandomSearch
+from xlrd import open_workbook
+
+def readFromExcel(inCols,targetCols, numRows, fileName, offset=0, sheet=0, dataSet=None, conversionFun=None):
+ """Populates a given dataset or creates a new SupervisedDataSet from an exccel file.
+
+ inCols = array of colum numbers containing the input data colums, colums are indexed from 0
+ targetCols = array of colum numbers containing the target data colums, colums are indexed from 0
+ numRows = the number of rows ofs data
+ fileName= the name of the excel file
+ offset = the row the vaild data starts at
+ sheet = the sheet of the workbook the data is on, indexed from 0 as it is in xlrd
+ dataSet = the dataset to be populated, a SupervisedDataSet if created if it is None
+ conversionFun = used to preprocess data.
+ """
+ book = open_workbook(fileName)
+ sheet=book.sheet_by_index(sheet)
+
+ if dataSet is None:
+ dataSet=SupervisedDataSet(len(inCols),len(targetCols))
+ for r in range(offset,(offset+numRows)):
+ input=[]
+ target=[]
+ for inC in inCols:
+ input.append(sheet.cell_value(r,inC))
+
+ for tC in targetCols:
+ target.append(sheet.cell_value(r,tC))
+ try:
+ if conversionFun:
+ input=[conversionFun(i) for i in input]
+ target=[conversionFun(t) for t in target]
+ print input,target
+
+ dataSet.addSample(input, target)
+ except Exception:
+ print 'rejected row {}'.format(r)
+ return dataSet
From 2ffc359b0f2a6cfe4521aa7661a3ec270c5cf1f5 Mon Sep 17 00:00:00 2001
From: Patrick
Date: Fri, 20 Sep 2013 18:44:35 -0500
Subject: [PATCH 40/83] moved
---
ExcelTools.py | 39 ---------------------------------------
1 file changed, 39 deletions(-)
delete mode 100644 ExcelTools.py
diff --git a/ExcelTools.py b/ExcelTools.py
deleted file mode 100644
index 88c18d8fa..000000000
--- a/ExcelTools.py
+++ /dev/null
@@ -1,39 +0,0 @@
-from pybrain.datasets.supervised import SupervisedDataSet
-from pybrain.optimization.randomsearch import RandomSearch
-from xlrd import open_workbook
-
-def readFromExcel(inCols,targetCols, numRows, fileName, offset=0, sheet=0, dataSet=None, conversionFun=None):
- """Populates a given dataset or creates a new SupervisedDataSet from an exccel file.
-
- inCols = array of colum numbers containing the input data colums, colums are indexed from 0
- targetCols = array of colum numbers containing the target data colums, colums are indexed from 0
- numRows = the number of rows ofs data
- fileName= the name of the excel file
- offset = the row the vaild data starts at
- sheet = the sheet of the workbook the data is on, indexed from 0 as it is in xlrd
- dataSet = the dataset to be populated, a SupervisedDataSet if created if it is None
- conversionFun = used to preprocess data.
- """
- book = open_workbook(fileName)
- sheet=book.sheet_by_index(sheet)
-
- if dataSet is None:
- dataSet=SupervisedDataSet(len(inCols),len(targetCols))
- for r in range(offset,(offset+numRows)):
- input=[]
- target=[]
- for inC in inCols:
- input.append(sheet.cell_value(r,inC))
-
- for tC in targetCols:
- target.append(sheet.cell_value(r,tC))
- try:
- if conversionFun:
- input=[conversionFun(i) for i in input]
- target=[conversionFun(t) for t in target]
- print input,target
-
- dataSet.addSample(input, target)
- except Exception:
- print 'rejected row {}'.format(r)
- return dataSet
From 455449054a2deda261f42145f65dc55c5c469290 Mon Sep 17 00:00:00 2001
From: Patrick
Date: Fri, 20 Sep 2013 19:06:03 -0500
Subject: [PATCH 41/83] Moved ExcelTools.py
---
pybrain/tools/ExcelTools.py | 42 +++++++++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
create mode 100644 pybrain/tools/ExcelTools.py
diff --git a/pybrain/tools/ExcelTools.py b/pybrain/tools/ExcelTools.py
new file mode 100644
index 000000000..613fe7328
--- /dev/null
+++ b/pybrain/tools/ExcelTools.py
@@ -0,0 +1,42 @@
+__author__ = 'Patrick Hunter, pk_hunter@sbcglobal.net'
+from pybrain.datasets.supervised import SupervisedDataSet
+from pybrain.optimization.randomsearch import RandomSearch
+from xlrd import open_workbook
+
+def readFromExcel(inCols,targetCols, numRows, fileName, offset=0, sheet=0, dataSet=None, conversionFun=None):
+ """Populates a given dataset or creates a new SupervisedDataSet from an exccel file.
+
+ Arguments:
+ return value: A tuple containg the dataset followed by an array containg the row numbers of every row that included unparsable/missing/invaild data.
+ No data from these rows is added to the dataSet.
+ arg: inCols: array of colum numbers containing the input data colums, colums are indexed from 0
+ targetCols: array of colum numbers containing the target data colums, colums are indexed from 0
+ numRows: the number of rows ofs data
+ fileName: the name of the excel file
+ offset: the row the vaild data starts at
+ sheet: the sheet of the workbook the data is on, indexed from 0 as it is in xlrd
+ dataSet: the dataset to be populated, a SupervisedDataSet if created if it is None
+ conversionFun: used to preprocess data
+ """
+ book = open_workbook(fileName)
+ sheet=book.sheet_by_index(sheet)
+ rejectedRows=[]
+ if dataSet is None:
+ dataSet=SupervisedDataSet(len(inCols),len(targetCols))
+ for r in range(offset,(offset+numRows)):
+ input=[]
+ target=[]
+ for inC in inCols:
+ input.append(sheet.cell_value(r,inC))
+
+ for tC in targetCols:
+ target.append(sheet.cell_value(r,tC))
+ try:
+ if conversionFun:
+ input=[conversionFun(i) for i in input]
+ target=[conversionFun(t) for t in target]
+
+ dataSet.addSample(input, target)
+ except Exception:
+ rejectedRows.append(r)
+ return dataSet, rejectedRows
From 3a7bf880a8a5e669fa7deeeb6cf6abb82e19d6a9 Mon Sep 17 00:00:00 2001
From: PatrickHunter
Date: Fri, 20 Sep 2013 23:37:06 -0500
Subject: [PATCH 42/83] Rename ExcelTools.py to exceltools.py
---
pybrain/tools/{ExcelTools.py => exceltools.py} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename pybrain/tools/{ExcelTools.py => exceltools.py} (100%)
diff --git a/pybrain/tools/ExcelTools.py b/pybrain/tools/exceltools.py
similarity index 100%
rename from pybrain/tools/ExcelTools.py
rename to pybrain/tools/exceltools.py
From 696733adb2289830293cdf87f5d930aab852cb91 Mon Sep 17 00:00:00 2001
From: Patrick
Date: Mon, 14 Oct 2013 22:29:41 -0500
Subject: [PATCH 43/83] Fixed FunctionEnviorment to be compatible with
BlackBoxOptimizer again.
As of commit 0d651ab912fbfbb1b6c80daf7d3c6b0f571e68a7 BlackBoxOptimizer expects evaluators to have the properties xbound, feasible, constrained, and violation. The functions in the function folder, which are designed as evaluators, are now intiated with these properties as keyword arguments.
---
pybrain/rl/environments/functions/function.py | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/pybrain/rl/environments/functions/function.py b/pybrain/rl/environments/functions/function.py
index ac00050ae..f8cee04f0 100644
--- a/pybrain/rl/environments/functions/function.py
+++ b/pybrain/rl/environments/functions/function.py
@@ -26,7 +26,11 @@ class FunctionEnvironment(Environment, FitnessEvaluator):
# does the function already include a penalization term, to keep search near the origin?
penalized = False
- def __init__(self, xdim = None, xopt = None, **args):
+ def __init__(self, xdim = None, xopt = None, xbound=5, feasible=True, constrained=False, violation=False, **args):
+ self.feasible=feasible
+ self.constrained=constrained
+ self.violation=violation
+ self.xbound=xbound
if xdim is None:
xdim = self.xdim
if xdim is None:
From 368898b74c1c50ef86642c5ccf549ab08b99c5c3 Mon Sep 17 00:00:00 2001
From: PatrickHunter
Date: Tue, 15 Oct 2013 21:48:04 -0500
Subject: [PATCH 44/83] Delete exceltools.py
---
pybrain/tools/exceltools.py | 42 -------------------------------------
1 file changed, 42 deletions(-)
delete mode 100644 pybrain/tools/exceltools.py
diff --git a/pybrain/tools/exceltools.py b/pybrain/tools/exceltools.py
deleted file mode 100644
index 613fe7328..000000000
--- a/pybrain/tools/exceltools.py
+++ /dev/null
@@ -1,42 +0,0 @@
-__author__ = 'Patrick Hunter, pk_hunter@sbcglobal.net'
-from pybrain.datasets.supervised import SupervisedDataSet
-from pybrain.optimization.randomsearch import RandomSearch
-from xlrd import open_workbook
-
-def readFromExcel(inCols,targetCols, numRows, fileName, offset=0, sheet=0, dataSet=None, conversionFun=None):
- """Populates a given dataset or creates a new SupervisedDataSet from an exccel file.
-
- Arguments:
- return value: A tuple containg the dataset followed by an array containg the row numbers of every row that included unparsable/missing/invaild data.
- No data from these rows is added to the dataSet.
- arg: inCols: array of colum numbers containing the input data colums, colums are indexed from 0
- targetCols: array of colum numbers containing the target data colums, colums are indexed from 0
- numRows: the number of rows ofs data
- fileName: the name of the excel file
- offset: the row the vaild data starts at
- sheet: the sheet of the workbook the data is on, indexed from 0 as it is in xlrd
- dataSet: the dataset to be populated, a SupervisedDataSet if created if it is None
- conversionFun: used to preprocess data
- """
- book = open_workbook(fileName)
- sheet=book.sheet_by_index(sheet)
- rejectedRows=[]
- if dataSet is None:
- dataSet=SupervisedDataSet(len(inCols),len(targetCols))
- for r in range(offset,(offset+numRows)):
- input=[]
- target=[]
- for inC in inCols:
- input.append(sheet.cell_value(r,inC))
-
- for tC in targetCols:
- target.append(sheet.cell_value(r,tC))
- try:
- if conversionFun:
- input=[conversionFun(i) for i in input]
- target=[conversionFun(t) for t in target]
-
- dataSet.addSample(input, target)
- except Exception:
- rejectedRows.append(r)
- return dataSet, rejectedRows
From ff77dad53bec2ae2315b23f208d1b90cfa20e002 Mon Sep 17 00:00:00 2001
From: Christopher Dembia
Date: Tue, 12 Nov 2013 07:22:29 -0800
Subject: [PATCH 45/83] Fix Task.getObservation docstring.
This method referred to getSample when it should refer to
(Environment's) getSensors.
---
pybrain/rl/environments/task.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pybrain/rl/environments/task.py b/pybrain/rl/environments/task.py
index 773406a35..e771044ab 100644
--- a/pybrain/rl/environments/task.py
+++ b/pybrain/rl/environments/task.py
@@ -34,7 +34,7 @@ def performAction(self, action):
self.env.performAction(action)
def getObservation(self):
- """ A filtered mapping to getSample of the underlying environment. """
+ """ A filtered mapping to getSensors of the underlying environment. """
sensors = self.env.getSensors()
if self.sensor_limits:
sensors = self.normalize(sensors)
From 6e877d4b7312443deb31314f8d802e8e5c5592ee Mon Sep 17 00:00:00 2001
From: Graham Percival
Date: Wed, 13 Nov 2013 12:13:26 +0800
Subject: [PATCH 46/83] doc: warn about unimplemented args in RL explorer
---
pybrain/rl/explorers/discrete/discretesde.py | 3 +++
1 file changed, 3 insertions(+)
diff --git a/pybrain/rl/explorers/discrete/discretesde.py b/pybrain/rl/explorers/discrete/discretesde.py
index c06600815..c400360b9 100644
--- a/pybrain/rl/explorers/discrete/discretesde.py
+++ b/pybrain/rl/explorers/discrete/discretesde.py
@@ -16,6 +16,9 @@ class DiscreteStateDependentExplorer(DiscreteExplorer):
"""
def __init__(self, epsilon = 0.2, decay = 0.9998):
+ """ TODO: the epsilon and decay parameters are currently
+ not implemented.
+ """
DiscreteExplorer.__init__(self)
self.state = None
From 0f73cca094f11567e8fe12f48c91a5521c8b465c Mon Sep 17 00:00:00 2001
From: Christopher Dembia
Date: Sat, 16 Nov 2013 17:36:41 -0800
Subject: [PATCH 47/83] Pause pyplot in nfq.py so plot is actuall updated.
On my machine, in the nfq.py example, with render = False, the pyplot
plot never appeared. To fix this, I put in a very short pyplot pause.
Now, the plot is updated in every iteration of the while-loop.
---
examples/rl/valuebased/nfq.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/examples/rl/valuebased/nfq.py b/examples/rl/valuebased/nfq.py
index cdb2b697c..e593adfd2 100644
--- a/examples/rl/valuebased/nfq.py
+++ b/examples/rl/valuebased/nfq.py
@@ -37,6 +37,7 @@ def plotPerformance(values, fig):
plt.clf()
plt.plot(values, 'o-')
plt.gcf().canvas.draw()
+ plt.pause(0.001)
performance = []
From 71f1d1696a27ba20236b719f587919f2aca7c0b6 Mon Sep 17 00:00:00 2001
From: Christopher Dembia
Date: Sat, 16 Nov 2013 17:53:34 -0800
Subject: [PATCH 48/83] Add a comment to nfq.py to describe my recent addition.
---
examples/rl/valuebased/nfq.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/examples/rl/valuebased/nfq.py b/examples/rl/valuebased/nfq.py
index e593adfd2..5cdf042c9 100644
--- a/examples/rl/valuebased/nfq.py
+++ b/examples/rl/valuebased/nfq.py
@@ -37,6 +37,7 @@ def plotPerformance(values, fig):
plt.clf()
plt.plot(values, 'o-')
plt.gcf().canvas.draw()
+ # Without the next line, the pyplot plot won't actually show up.
plt.pause(0.001)
performance = []
From 5270f90f6e8ef15e2a418f4da40f540bbe244a73 Mon Sep 17 00:00:00 2001
From: Graham Percival
Date: Fri, 13 Dec 2013 12:32:16 +0800
Subject: [PATCH 49/83] Q, SARSA: ignore 1st triple in episodic training
When training multiple episodes (with learnOnDataset and
batchMode=True), the final iteration of episode n-1 should not
affect the first iteration of episode n.
---
pybrain/rl/learners/valuebased/q.py | 6 ++++++
pybrain/rl/learners/valuebased/sarsa.py | 5 +++++
2 files changed, 11 insertions(+)
diff --git a/pybrain/rl/learners/valuebased/q.py b/pybrain/rl/learners/valuebased/q.py
index 626a866eb..9c7fc3f55 100644
--- a/pybrain/rl/learners/valuebased/q.py
+++ b/pybrain/rl/learners/valuebased/q.py
@@ -35,6 +35,12 @@ def learn(self):
samples = [[self.dataset.getSample()]]
for seq in samples:
+ # information from the previous episode (sequence)
+ # should not influence the training on this episode
+ self.laststate = None
+ self.lastaction = None
+ self.lastreward = None
+
for state, action, reward in seq:
state = int(state)
diff --git a/pybrain/rl/learners/valuebased/sarsa.py b/pybrain/rl/learners/valuebased/sarsa.py
index 4221a5e4c..226a40e2f 100644
--- a/pybrain/rl/learners/valuebased/sarsa.py
+++ b/pybrain/rl/learners/valuebased/sarsa.py
@@ -31,6 +31,11 @@ def learn(self):
samples = [[self.dataset.getSample()]]
for seq in samples:
+ # information from the previous episode (sequence)
+ # should not influence the training on this episode
+ self.laststate = None
+ self.lastaction = None
+ self.lastreward = None
for state, action, reward in seq:
state = int(state)
From b759edc4fc5903a94746d8595cb8b0ebb1883875 Mon Sep 17 00:00:00 2001
From: Christopher Dembia
Date: Wed, 18 Dec 2013 23:18:31 -0800
Subject: [PATCH 50/83] Add Randlov bicycle Rl example.
---
examples/rl/environments/linear_fa/bicycle.py | 429 ++++++++++++++++++
1 file changed, 429 insertions(+)
create mode 100644 examples/rl/environments/linear_fa/bicycle.py
diff --git a/examples/rl/environments/linear_fa/bicycle.py b/examples/rl/environments/linear_fa/bicycle.py
new file mode 100644
index 000000000..5fe26660e
--- /dev/null
+++ b/examples/rl/environments/linear_fa/bicycle.py
@@ -0,0 +1,429 @@
+"""An attempt to implement Randlov and Alstrom (1998). They successfully
+use reinforcement learning to balance a bicycle, and to control it to drive
+to a specified goal location. Their work has been used since then by a few
+researchers as a benchmark problem.
+
+We only implement the balance task. This implementation differs at least
+slightly, since Randlov and Alstrom did not mention anything about how they
+annealed/decayed their learning rate, etc. As a result of differences, the
+results do not match those obtained by Randlov and Alstrom.
+
+"""
+
+__author__ = 'Chris Dembia, Bruce Cam, Johnny Israeli'
+
+from scipy import asarray
+from numpy import sin, cos, tan, sqrt, arcsin, arctan, sign, clip, argwhere
+from matplotlib import pyplot as plt
+
+import pybrain.rl.environments
+from pybrain.rl.environments.environment import Environment
+from pybrain.rl.learners.valuebased.linearfa import SARSALambda_LinFA
+from pybrain.rl.agents.linearfa import LinearFA_Agent
+from pybrain.rl.experiments import EpisodicExperiment
+from pybrain.utilities import one_to_n
+
+class BicycleEnvironment(Environment):
+ """Randlov and Alstrom's bicycle model. This code matches nearly exactly
+ some c code we found online for simulating Randlov and Alstrom's
+ bicycle. The bicycle travels at a fixed speed.
+
+ """
+
+ # For superclass.
+ indim = 2
+ outdim = 10
+
+ # Environment parameters.
+ time_step = 0.01
+
+ # Goal position and radius
+ # Lagouakis (2002) uses angle to goal, not heading, as a state
+ max_distance = 1000.
+
+ # Acceleration on Earth's surface due to gravity (m/s^2):
+ g = 9.82
+
+ # See the paper for a description of these quantities:
+ # Distances (in meters):
+ c = 0.66
+ dCM = 0.30
+ h = 0.94
+ L = 1.11
+ r = 0.34
+ # Masses (in kilograms):
+ Mc = 15.0
+ Md = 1.7
+ Mp = 60.0
+ # Velocity of a bicycle (in meters per second), equal to 10 km/h:
+ v = 10.0 * 1000.0 / 3600.0
+
+ # Derived constants.
+ M = Mc + Mp # See Randlov's code.
+ Idc = Md * r**2
+ Idv = 1.5 * Md * r**2
+ Idl = 0.5 * Md * r**2
+ Itot = 13.0 / 3.0 * Mc * h**2 + Mp * (h + dCM)**2
+ sigmad = v / r
+
+ def __init__(self):
+ Environment.__init__(self)
+ self.reset()
+ self.actions = [0.0, 0.0]
+ self._save_wheel_contact_trajectories = False
+
+ def performAction(self, actions):
+ self.actions = actions
+ self.step()
+
+ def saveWheelContactTrajectories(self, opt):
+ self._save_wheel_contact_trajectories = opt
+
+ def step(self):
+ # Unpack the state and actions.
+ # -----------------------------
+ # Want to ignore the previous value of omegadd; it could only cause a
+ # bug if we assign to it.
+
+ (theta, thetad, omega, omegad, _,
+ xf, yf, xb, yb, psi) = self.sensors
+ (T, d) = self.actions
+
+ # For recordkeeping.
+ # ------------------
+ if self._save_wheel_contact_trajectories:
+ self.xfhist.append(xf)
+ self.yfhist.append(yf)
+ self.xbhist.append(xb)
+ self.ybhist.append(yb)
+
+ # Intermediate time-dependent quantities.
+ # ---------------------------------------
+ # Avoid divide-by-zero, just as Randlov did.
+ if theta == 0:
+ rf = 1e8
+ rb = 1e8
+ rCM = 1e8
+ else:
+ rf = self.L / np.abs(sin(theta))
+ rb = self.L / np.abs(tan(theta))
+ rCM = sqrt((self.L - self.c)**2 + self.L**2 / tan(theta)**2)
+
+ phi = omega + np.arctan(d / self.h)
+
+ # Equations of motion.
+ # --------------------
+ # Second derivative of angular acceleration:
+ omegadd = 1 / self.Itot * (self.M * self.h * self.g * sin(phi)
+ - cos(phi) * (self.Idc * self.sigmad * thetad
+ + sign(theta) * self.v**2 * (
+ self.Md * self.r * (1.0 / rf + 1.0 / rb)
+ + self.M * self.h / rCM)))
+ thetadd = (T - self.Idv * self.sigmad * omegad) / self.Idl
+
+ # Integrate equations of motion using Euler's method.
+ # ---------------------------------------------------
+ # yt+1 = yt + yd * dt.
+ # Must update omega based on PREVIOUS value of omegad.
+ omegad += omegadd * self.time_step
+ omega += omegad * self.time_step
+ thetad += thetadd * self.time_step
+ theta += thetad * self.time_step
+
+ # Handlebars can't be turned more than 80 degrees.
+ theta = np.clip(theta, -1.3963, 1.3963)
+
+ # Wheel ('tyre') contact positions.
+ # ---------------------------------
+
+ # Front wheel contact position.
+ front_temp = self.v * self.time_step / (2 * rf)
+ # See Randlov's code.
+ if front_temp > 1:
+ front_temp = sign(psi + theta) * 0.5 * np.pi
+ else:
+ front_temp = sign(psi + theta) * arcsin(front_temp)
+ xf += self.v * self.time_step * -sin(psi + theta + front_temp)
+ yf += self.v * self.time_step * cos(psi + theta + front_temp)
+
+ # Rear wheel.
+ back_temp = self.v * self.time_step / (2 * rb)
+ # See Randlov's code.
+ if back_temp > 1:
+ back_temp = np.sign(psi) * 0.5 * np.pi
+ else:
+ back_temp = np.sign(psi) * np.arcsin(back_temp)
+ xb += self.v * self.time_step * -sin(psi + back_temp)
+ yb += self.v * self.time_step * cos(psi + back_temp)
+
+ # Preventing numerical drift.
+ # ---------------------------
+ # Copying what Randlov did.
+ current_wheelbase = sqrt((xf - xb)**2 + (yf - yb)**2)
+ if np.abs(current_wheelbase - self.L) > 0.01:
+ relative_error = self.L / current_wheelbase - 1.0
+ xb += (xb - xf) * relative_error
+ yb += (yb - yf) * relative_error
+
+ # Update heading, psi.
+ # --------------------
+ delta_y = yf - yb
+ if (xf == xb) and delta_y < 0.0:
+ psi = np.pi
+ else:
+ if delta_y > 0.0:
+ psi = arctan((xb - xf) / delta_y)
+ else:
+ psi = sign(xb - xf) * 0.5 * np.pi - arctan(delta_y / (xb - xf))
+
+ self.sensors = np.array([theta, thetad, omega, omegad, omegadd,
+ xf, yf, xb, yb, psi])
+
+ def reset(self):
+ theta = 0
+ thetad = 0
+ omega = 0
+ omegad = 0
+ omegadd = 0
+ xf = 0
+ yf = self.L
+ xb = 0
+ yb = 0
+
+ psi = np.arctan((xb - xf) / (yf - yb))
+ self.sensors = np.array([theta, thetad, omega, omegad, omegadd,
+ xf, yf, xb, yb, psi])
+
+ self.xfhist = []
+ self.yfhist = []
+ self.xbhist = []
+ self.ybhist = []
+
+ def getSteer(self):
+ return self.sensors[0]
+ def getTilt(self):
+ return self.sensors[2]
+ def get_xfhist(self):
+ return self.xfhist
+ def get_yfhist(self):
+ return self.yfhist
+ def get_xbhist(self):
+ return self.xbhist
+ def get_ybhist(self):
+ return self.ybhist
+ def getSensors(self):
+ return self.sensors
+
+class BalanceTask(pybrain.rl.environments.EpisodicTask):
+ """The rider is to simply balance the bicycle while moving with the
+ speed perscribed in the environment. This class uses a continuous 5
+ dimensional state space, and a discrete state space.
+
+ This class is heavily guided by
+ pybrain.rl.environments.cartpole.balancetask.BalanceTask.
+
+ """
+ max_tilt = np.pi / 6.
+ nactions = 9
+
+ def __init__(self, max_time=1000.0):
+ super(BalanceTask, self).__init__(BicycleEnvironment())
+ self.max_time = max_time
+ # Keep track of time in case we want to end episodes based on number of
+ # time steps.
+ self.t = 0
+
+ @property
+ def indim(self):
+ return 1
+
+ @property
+ def outdim(self):
+ return 5
+
+ def reset(self):
+ super(BalanceTask, self).reset()
+ self.t = 0
+
+ def performAction(self, action):
+ """Incoming action is an int between 0 and 8. The action we provide to
+ the environment consists of a torque T in {-2 N, 0, 2 N}, and a
+ displacement d in {-.02 m, 0, 0.02 m}.
+
+ """
+ self.t += 1
+ assert round(action[0]) == action[0]
+
+ # -1 for action in {0, 1, 2}, 0 for action in {3, 4, 5}, 1 for
+ # action in {6, 7, 8}
+ torque_selector = np.floor(action[0] / 3.0) - 1.0
+ T = 2 * torque_selector
+ # Random number in [-1, 1]:
+ p = 2.0 * np.random.rand() - 1.0
+ # -1 for action in {0, 3, 6}, 0 for action in {1, 4, 7}, 1 for
+ # action in {2, 5, 8}
+ disp_selector = action[0] % 3 - 1.0
+ d = 0.02 * disp_selector + 0.02 * p
+ super(BalanceTask, self).performAction([T, d])
+
+ def getObservation(self):
+ (theta, thetad, omega, omegad, omegadd,
+ xf, yf, xb, yb, psi) = self.env.getSensors()
+ return self.env.getSensors()[0:5]
+
+ def isFinished(self):
+ # Criterion for ending an episode. From Randlov's paper:
+ # "When the agent can balance for 1000 seconds, the task is considered
+ # learned."
+ if np.abs(self.env.getTilt()) > self.max_tilt:
+ return True
+ elapsed_time = self.env.time_step * self.t
+ if elapsed_time > self.max_time:
+ return True
+ return False
+
+ def getReward(self):
+ # -1 reward for falling over; no reward otherwise.
+ if np.abs(self.env.getTilt()) > self.max_tilt:
+ return -1.0
+ return 0.0
+
+class LinearFATileCoding3456BalanceTask(BalanceTask):
+ """An attempt to exactly implement Randlov's function approximation. He
+ discretized (tiled) the state space into 3456 bins. We use the same action
+ space as in the superclass.
+
+ """
+ # From Randlov, 1998:
+ theta_bounds = np.array(
+ [-0.5 * np.pi, -1.0, -0.2, 0, 0.2, 1.0, 0.5 * np.pi])
+ thetad_bounds = np.array(
+ [-np.inf, -2.0, 0, 2.0, np.inf])
+ omega_bounds = np.array(
+ [-BalanceTask.max_tilt, -0.15, -0.06, 0, 0.06, 0.15,
+ BalanceTask.max_tilt])
+ omegad_bounds = np.array(
+ [-np.inf, -0.5, -0.25, 0, 0.25, 0.5, np.inf])
+ omegadd_bounds = np.array(
+ [-np.inf, -2.0, 0, 2.0, np.inf])
+ # http://stackoverflow.com/questions/3257619/numpy-interconversion-between-multidimensional-and-linear-indexing
+ nbins_across_dims = [
+ len(theta_bounds) - 1,
+ len(thetad_bounds) - 1,
+ len(omega_bounds) - 1,
+ len(omegad_bounds) - 1,
+ len(omegadd_bounds) - 1]
+ # This array, when dotted with the 5-dim state vector, gives a 'linear'
+ # index between 0 and 3455.
+ magic_array = np.cumprod([1] + nbins_across_dims)[:-1]
+
+ @property
+ def outdim(self):
+ # Used when constructing LinearFALearner's.
+ return 3456
+
+ def getBin(self, theta, thetad, omega, omegad, omegadd):
+ bin_indices = [
+ np.digitize([theta], self.theta_bounds)[0] - 1,
+ np.digitize([thetad], self.thetad_bounds)[0] - 1,
+ np.digitize([omega], self.omega_bounds)[0] - 1,
+ np.digitize([omegad], self.omegad_bounds)[0] - 1,
+ np.digitize([omegadd], self.omegadd_bounds)[0] - 1,
+ ]
+ return np.dot(self.magic_array, bin_indices)
+
+ def getBinIndices(self, linear_index):
+ """Given a linear index (integer between 0 and outdim), returns the bin
+ indices for each of the state dimensions.
+
+ """
+ return linear_index / self.magic_array % self.nbins_across_dims
+
+ def getObservation(self):
+ (theta, thetad, omega, omegad, omegadd,
+ xf, yf, xb, yb, psi) = self.env.getSensors()
+ state = one_to_n(self.getBin(theta, thetad, omega, omegad, omegadd),
+ self.outdim)
+ return state
+
+class SARSALambda_LinFA_ReplacingTraces(SARSALambda_LinFA):
+ """Randlov used replacing traces, but this doesn't exist in PyBrain's
+ SARSALambda.
+
+ """
+ def _updateEtraces(self, state, action, responsibility=1.):
+ self._etraces *= self.rewardDiscount * self._lambda * responsibility
+ # This assumes that state is an identity vector (like, from one_to_n).
+ self._etraces[action] = clip(self._etraces[action] + state, -np.inf, 1.)
+ # Set the trace for all other actions in this state to 0:
+ action_bit = one_to_n(action, self.num_actions)
+
+ for argstate in argwhere(state == 1) :
+ self._etraces[argwhere(action_bit != 1), argstate] = 0.
+
+
+task = LinearFATileCoding3456BalanceTask()
+env = task.env
+
+# The learning is very sensitive to the learning rate decay.
+learner = SARSALambda_LinFA_ReplacingTraces(task.nactions, task.outdim,
+ learningRateDecay=2000)
+learner._lambda = 0.95
+
+task.discount = learner.rewardDiscount
+
+agent = LinearFA_Agent(learner)
+agent.logging = False
+
+exp = EpisodicExperiment(task, agent)
+
+performance_agent = LinearFA_Agent(learner)
+performance_agent.logging = False
+performance_agent.greedy = True
+performance_agent.learning = False
+
+env.saveWheelContactTrajectories(True)
+plt.ion()
+plt.figure(figsize=(8, 4))
+
+ax1 = plt.subplot(1, 2, 1)
+ax2 = plt.subplot(1, 2, 2)
+
+def update_wheel_trajectories():
+ front_lines = ax2.plot(env.get_xfhist(), env.get_yfhist(), 'r')
+ back_lines = ax2.plot(env.get_xbhist(), env.get_ybhist(), 'b')
+ plt.axis('equal')
+
+perform_cumrewards = []
+for irehearsal in range(7000):
+
+ # Learn.
+ # ------
+ r = exp.doEpisodes(1)
+ # Discounted reward.
+ cumreward = exp.task.getTotalReward()
+ #print 'cumreward: %.4f; nsteps: %i; learningRate: %.4f' % (
+ # cumreward, len(r[0]), exp.agent.learner.learningRate)
+
+ if irehearsal % 50 == 0:
+ # Perform (no learning).
+ # ----------------------
+ # Swap out the agent.
+ exp.agent = performance_agent
+
+ # Perform.
+ r = exp.doEpisodes(1)
+ perform_cumreward = task.getTotalReward()
+ perform_cumrewards.append(perform_cumreward)
+ print 'PERFORMANCE: cumreward:', perform_cumreward, 'nsteps:', len(r[0])
+
+ # Swap back the learning agent.
+ performance_agent.reset()
+ exp.agent = agent
+
+ ax1.cla()
+ ax1.plot(perform_cumrewards, '.--')
+ # Wheel trajectories.
+ update_wheel_trajectories()
+
+ plt.pause(0.001)
From 5f96198f10f3b302c6d0fb26713d21b93965d2b8 Mon Sep 17 00:00:00 2001
From: Daniel Andres
Date: Thu, 16 Jan 2014 13:58:03 +0100
Subject: [PATCH 51/83] Fix: Networkwriter saves now recurrent connections of
arac networks
---
pybrain/tools/customxml/networkwriter.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/pybrain/tools/customxml/networkwriter.py b/pybrain/tools/customxml/networkwriter.py
index 7d65a55e7..37b42871d 100644
--- a/pybrain/tools/customxml/networkwriter.py
+++ b/pybrain/tools/customxml/networkwriter.py
@@ -62,7 +62,7 @@ def writeNetwork(self, net, netroot):
for m in net.modulesSorted:
for c in net.connections[m]:
self.writeConnection(conns, c, False)
- if isinstance(net, RecurrentNetwork):
+ if hasattr(net, "recurrentConns"):
for c in net.recurrentConns:
self.writeConnection(conns, c, True)
@@ -108,4 +108,4 @@ def writeArgs(self, node, argdict):
def writeParams(self, node, params):
# TODO: might be insufficient precision
pnode = self.newChild(node, 'Parameters')
- self.addTextNode(pnode, str(list(params)))
\ No newline at end of file
+ self.addTextNode(pnode, str(list(params)))
From 06ed34f76ff5a2458f3135fa53067a2fdf82b0e9 Mon Sep 17 00:00:00 2001
From: Daniel Andres
Date: Tue, 21 Jan 2014 20:33:18 +0100
Subject: [PATCH 52/83] added peephole support for buildNetwork shortcut for
LSTMLayer
---
pybrain/tools/shortcuts.py | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/pybrain/tools/shortcuts.py b/pybrain/tools/shortcuts.py
index 5eea43a79..fcc4bc9c9 100644
--- a/pybrain/tools/shortcuts.py
+++ b/pybrain/tools/shortcuts.py
@@ -80,7 +80,10 @@ def buildNetwork(*layers, **options):
# arbitrary number of hidden layers of type 'hiddenclass'
for i, num in enumerate(layers[1:-1]):
layername = 'hidden%i' % i
- n.addModule(opt['hiddenclass'](num, name=layername))
+ if issubclass(opt['hiddenclass'], LSTMLayer):
+ n.addModule(opt['hiddenclass'](num, peepholes=opt['peepholes'], name=layername))
+ else:
+ n.addModule(opt['hiddenclass'](num, name=layername))
if opt['bias']:
# also connect all the layers with the bias
n.addConnection(FullConnection(n['bias'], n[layername]))
From dd66c7bccb4df0e0ae1432b97f80c7f23e812bb4 Mon Sep 17 00:00:00 2001
From: Werner Beroux
Date: Thu, 20 Mar 2014 18:24:35 +0100
Subject: [PATCH 53/83] Port most of the code to Python3 compatible.
---
pybrain/__init__.py | 2 +-
pybrain/auxiliary/__init__.py | 6 +-
pybrain/auxiliary/gaussprocess.py | 6 +-
pybrain/datasets/__init__.py | 12 ++--
pybrain/datasets/classification.py | 30 ++++----
pybrain/datasets/dataset.py | 2 +-
pybrain/datasets/importance.py | 10 +--
pybrain/datasets/reinforcement.py | 4 +-
pybrain/datasets/sequential.py | 2 +-
pybrain/datasets/supervised.py | 8 +--
pybrain/optimization/__init__.py | 14 ++--
.../optimization/distributionbased/cmaes.py | 4 +-
pybrain/optimization/distributionbased/fem.py | 14 ++--
.../optimization/distributionbased/rank1.py | 6 +-
.../optimization/distributionbased/snes.py | 2 +-
pybrain/optimization/distributionbased/ves.py | 6 +-
.../optimization/distributionbased/xnes.py | 4 +-
pybrain/optimization/memetic/memetic.py | 2 +-
pybrain/optimization/optimizer.py | 2 +-
.../optimization/populationbased/__init__.py | 10 +--
.../coevolution/coevolution.py | 10 +--
.../coevolution/competitivecoevolution.py | 10 +--
.../coevolution/multipopulationcoevolution.py | 2 +-
pybrain/optimization/populationbased/ga.py | 2 +-
.../multiobjective/__init__.py | 4 +-
.../multiobjective/constnsga2.py | 4 +-
pybrain/optimization/populationbased/pso.py | 2 +-
pybrain/rl/agents/__init__.py | 4 +-
pybrain/rl/agents/linearfa.py | 2 +-
pybrain/rl/environments/__init__.py | 6 +-
pybrain/rl/environments/cartpole/__init__.py | 12 ++--
.../cartpole/fast_version/__init__.py | 2 +-
.../cartpole/fast_version/cartpoleenv.py | 12 ++--
pybrain/rl/environments/classic/acrobot.py | 2 +-
.../rl/environments/classic/mountaincar.py | 6 +-
pybrain/rl/environments/classic/xor.py | 2 +-
pybrain/rl/environments/flexcube/__init__.py | 4 +-
pybrain/rl/environments/flexcube/viewer.py | 2 +-
pybrain/rl/environments/functions/__init__.py | 10 +--
.../rl/environments/functions/lennardjones.py | 2 +-
.../rl/environments/functions/multimodal.py | 6 +-
.../environments/functions/transformations.py | 2 +-
.../rl/environments/functions/unbounded.py | 2 +-
pybrain/rl/environments/functions/unimodal.py | 2 +-
pybrain/rl/environments/mazes/__init__.py | 6 +-
.../rl/environments/mazes/tasks/__init__.py | 16 ++---
pybrain/rl/environments/ode/__init__.py | 8 +--
pybrain/rl/environments/ode/environment.py | 50 ++++++-------
.../rl/environments/ode/instances/__init__.py | 7 +-
pybrain/rl/environments/ode/instances/ccrl.py | 10 +--
pybrain/rl/environments/ode/tasks/__init__.py | 6 +-
pybrain/rl/environments/ode/tasks/ccrl.py | 2 +-
.../rl/environments/ode/tools/configgrab.py | 2 +-
pybrain/rl/environments/ode/tools/xmltools.py | 6 +-
.../rl/environments/ode/tools/xodetools.py | 71 ++++++++++---------
pybrain/rl/environments/ode/viewer.py | 4 +-
pybrain/rl/environments/shipsteer/__init__.py | 4 +-
pybrain/rl/environments/shipsteer/viewer.py | 2 +-
pybrain/rl/environments/simple/__init__.py | 4 +-
pybrain/rl/environments/simple/tasks.py | 2 +-
.../simplerace/simplecontroller.py | 2 +-
.../environments/simplerace/simpleracetcp.py | 16 ++---
.../environments/twoplayergames/__init__.py | 6 +-
.../capturegameplayers/__init__.py | 10 +--
.../capturegameplayers/clientwrapper.py | 14 ++--
.../capturegameplayers/moduledecision.py | 10 +--
.../twoplayergames/gomokuplayers/__init__.py | 6 +-
.../gomokuplayers/moduledecision.py | 10 +--
.../twoplayergames/tasks/__init__.py | 12 ++--
.../twoplayergames/tasks/handicaptask.py | 8 +--
.../tasks/relativegomokutask.py | 14 ++--
.../twoplayergames/tasks/relativetask.py | 14 ++--
pybrain/rl/experiments/__init__.py | 6 +-
pybrain/rl/experiments/continuous.py | 2 +-
pybrain/rl/explorers/__init__.py | 4 +-
pybrain/rl/explorers/continuous/__init__.py | 2 +-
pybrain/rl/explorers/discrete/__init__.py | 6 +-
pybrain/rl/learners/__init__.py | 6 +-
pybrain/rl/learners/directsearch/__init__.py | 6 +-
pybrain/rl/learners/directsearch/enac.py | 2 +-
pybrain/rl/learners/directsearch/gpomdp.py | 2 +-
pybrain/rl/learners/directsearch/rwr.py | 14 ++--
pybrain/rl/learners/modelbased/__init__.py | 4 +-
.../rl/learners/modelbased/leastsquares.py | 2 +-
pybrain/rl/learners/valuebased/__init__.py | 10 +--
pybrain/rl/learners/valuebased/linearfa.py | 22 +++---
pybrain/structure/__init__.py | 8 +--
pybrain/structure/connections/__init__.py | 10 +--
pybrain/structure/connections/full.py | 2 +-
pybrain/structure/connections/identity.py | 2 +-
pybrain/structure/connections/shared.py | 6 +-
pybrain/structure/connections/subsampling.py | 2 +-
pybrain/structure/evolvables/maskedmodule.py | 2 +-
.../structure/evolvables/maskedparameters.py | 2 +-
pybrain/structure/evolvables/topology.py | 2 +-
pybrain/structure/modulemesh.py | 2 +-
pybrain/structure/modules/__init__.py | 30 ++++----
pybrain/structure/modules/biasunit.py | 4 +-
pybrain/structure/modules/gaussianlayer.py | 2 +-
pybrain/structure/modules/kohonen.py | 2 +-
pybrain/structure/modules/linearlayer.py | 2 +-
pybrain/structure/modules/lstm.py | 4 +-
pybrain/structure/modules/mdlstm.py | 4 +-
pybrain/structure/modules/neuronlayer.py | 2 +-
pybrain/structure/modules/relulayer.py | 2 +-
pybrain/structure/modules/sigmoidlayer.py | 2 +-
pybrain/structure/modules/softmax.py | 2 +-
pybrain/structure/modules/softsign.py | 2 +-
.../structure/modules/statedependentlayer.py | 2 +-
pybrain/structure/modules/tanhlayer.py | 2 +-
pybrain/structure/networks/__init__.py | 14 ++--
pybrain/structure/networks/borderswiping.py | 2 +-
pybrain/structure/networks/convolutional.py | 10 +--
pybrain/structure/networks/network.py | 6 +-
pybrain/structure/networks/swiping.py | 8 +--
pybrain/supervised/__init__.py | 2 +-
pybrain/supervised/evolino/filter.py | 8 +--
pybrain/supervised/evolino/gfilter.py | 4 +-
pybrain/supervised/evolino/individual.py | 2 +-
pybrain/supervised/evolino/networkwrapper.py | 14 ++--
pybrain/supervised/evolino/population.py | 6 +-
pybrain/supervised/knn/lsh/minhash.py | 6 +-
pybrain/supervised/trainers/__init__.py | 6 +-
pybrain/supervised/trainers/backprop.py | 20 +++---
pybrain/supervised/trainers/evolino.py | 8 +--
pybrain/supervised/trainers/rprop.py | 6 +-
pybrain/supervised/trainers/svmtrainer.py | 2 +-
pybrain/tests/helpers.py | 30 ++++----
pybrain/tests/optimizationtest.py | 20 +++---
pybrain/tests/runtests.py | 4 +-
.../unittests/_test_equivalence_to_ctypes.py | 20 +++---
.../datasets/test_datasets_datasets.py | 18 ++---
.../twoplayergames/test_capture_game.py | 4 +-
.../environments/twoplayergames/test_pente.py | 8 +--
.../structure/modules/test_peephole_lstm.py | 2 +-
.../modules/test_simple_lstm_network.py | 2 +-
.../structure/modules/test_simple_mdlstm.py | 2 +-
.../custom/test_capturegame_network.py | 6 +-
.../custom/test_convolutional_nets.py | 4 +-
.../networks/test_network_decomposition.py | 2 +-
.../structure/networks/test_network_sort.py | 2 +-
.../networks/test_recurrent_network.py | 2 +-
.../unittests/test_utilities_dictionaries.py | 4 +-
.../unittests/tools/test_rlgluebridge.py | 12 ++--
pybrain/tools/aptativeresampling.py | 6 +-
pybrain/tools/customxml/networkreader.py | 4 +-
pybrain/tools/datasettools.py | 20 +++---
pybrain/tools/example_tools.py | 23 +++---
pybrain/tools/filehandling.py | 8 +--
pybrain/tools/functions.py | 2 +-
pybrain/tools/gridsearch.py | 8 +--
pybrain/tools/kwargsprocessor.py | 10 +--
pybrain/tools/mixtures/mogpuremax.py | 6 +-
pybrain/tools/networking/udpconnection.py | 12 ++--
pybrain/tools/neuralnets.py | 2 +-
pybrain/tools/plotting/fitnessprogression.py | 4 +-
pybrain/tools/rlgluebridge.py | 4 +-
pybrain/tools/shortcuts.py | 2 +-
pybrain/tools/svmdata.py | 4 +-
pybrain/tools/validation.py | 16 ++---
pybrain/unsupervised/trainers/rbm.py | 50 ++++++-------
pybrain/utilities.py | 13 ++--
162 files changed, 615 insertions(+), 613 deletions(-)
diff --git a/pybrain/__init__.py b/pybrain/__init__.py
index 54c911de7..44bda4bb8 100644
--- a/pybrain/__init__.py
+++ b/pybrain/__init__.py
@@ -1 +1 @@
-from structure.__init__ import *
+from pybrain.structure.__init__ import *
diff --git a/pybrain/auxiliary/__init__.py b/pybrain/auxiliary/__init__.py
index 15c15e34f..12fb4de4c 100644
--- a/pybrain/auxiliary/__init__.py
+++ b/pybrain/auxiliary/__init__.py
@@ -1,3 +1,3 @@
-from gradientdescent import GradientDescent
-from gaussprocess import GaussianProcess
-from importancemixing import importanceMixing
\ No newline at end of file
+from pybrain.auxiliary.gradientdescent import GradientDescent
+from pybrain.auxiliary.gaussprocess import GaussianProcess
+from pybrain.auxiliary.importancemixing import importanceMixing
\ No newline at end of file
diff --git a/pybrain/auxiliary/gaussprocess.py b/pybrain/auxiliary/gaussprocess.py
index cc4026bdb..59830d59c 100644
--- a/pybrain/auxiliary/gaussprocess.py
+++ b/pybrain/auxiliary/gaussprocess.py
@@ -81,7 +81,7 @@ def trainOnDataset(self, dataset):
self.trainx = dataset.getField('input')
self.trainy = ravel(dataset.getField('target'))
self.noise = array([0.001] * len(self.trainx))
- # print self.trainx, self.trainy
+ # print(self.trainx, self.trainy)
self.calculated = False
def addDataset(self, dataset):
@@ -195,7 +195,7 @@ def plotCurves(self, showSamples=False, force2D=True):
m = floor(sqrt(len(self.pred_mean)))
pcolor(self.pred_mean.reshape(m, m)[::-1, :])
- else: print "plotting only supported for indim=1 or indim=2."
+ else: print("plotting only supported for indim=1 or indim=2.")
if __name__ == '__main__':
@@ -224,7 +224,7 @@ def plotCurves(self, showSamples=False, force2D=True):
# you can also test the gp on single points, but this deletes the
# original testing grid. it can be restored with a call to _buildGrid()
- print gp.testOnArray(array([[0.4]]))
+ print(gp.testOnArray(array([[0.4]])))
# --- example on how to use the GP in 2 dimensions
diff --git a/pybrain/datasets/__init__.py b/pybrain/datasets/__init__.py
index f92bb9642..df1719716 100644
--- a/pybrain/datasets/__init__.py
+++ b/pybrain/datasets/__init__.py
@@ -1,7 +1,7 @@
# $Id$
-from sequential import SequentialDataSet
-from supervised import SupervisedDataSet
-from unsupervised import UnsupervisedDataSet
-from importance import ImportanceDataSet
-from reinforcement import ReinforcementDataSet
-from classification import ClassificationDataSet, SequenceClassificationDataSet
\ No newline at end of file
+from pybrain.datasets.sequential import SequentialDataSet
+from pybrain.datasets.supervised import SupervisedDataSet
+from pybrain.datasets.unsupervised import UnsupervisedDataSet
+from pybrain.datasets.importance import ImportanceDataSet
+from pybrain.datasets.reinforcement import ReinforcementDataSet
+from pybrain.datasets.classification import ClassificationDataSet, SequenceClassificationDataSet
\ No newline at end of file
diff --git a/pybrain/datasets/classification.py b/pybrain/datasets/classification.py
index 2bfbbc7aa..0b29b240e 100644
--- a/pybrain/datasets/classification.py
+++ b/pybrain/datasets/classification.py
@@ -81,13 +81,13 @@ def load_libsvm(cls, f):
def __add__(self, other):
"""Adds the patterns of two datasets, if dimensions and type match."""
if type(self) != type(other):
- raise TypeError, 'DataSets to be added must agree in type'
+ raise TypeError('DataSets to be added must agree in type')
elif self.indim != other.indim:
- raise TypeError, 'DataSets to be added must agree in input dimensions'
+ raise TypeError('DataSets to be added must agree in input dimensions')
elif self.outdim != 1 or other.outdim != 1:
- raise TypeError, 'Cannot add DataSets in 1-of-k representation'
+ raise TypeError('Cannot add DataSets in 1-of-k representation')
elif self.nClasses != other.nClasses:
- raise IndexError, 'Number of classes does not agree'
+ raise IndexError('Number of classes does not agree')
else:
result = self.copy()
for pat in other:
@@ -100,7 +100,7 @@ def assignClasses(self):
"""
if len(self['class']) < len(self['target']):
if self.outdim > 1:
- raise IndexError, 'Classes and 1-of-k representation out of sync!'
+ raise IndexError('Classes and 1-of-k representation out of sync!')
else:
self.setField('class', self.getField('target').astype(int))
@@ -123,7 +123,7 @@ def getClass(self, idx):
try:
return self.class_labels[idx]
except IndexError:
- print "error: classes not defined yet!"
+ print("error: classes not defined yet!")
def _convertToOneOfMany(self, bounds=(0, 1)):
"""Converts the target classes to a 1-of-k representation, retaining the
@@ -270,7 +270,7 @@ def getSequenceClass(self, index=None):
return self['class'][self['sequence_index'].astype(int)[index + 1, 0] - 1, 0]
elif index == lastSeq:
return self['class'][len(self) - 1, 0]
- raise IndexError, "Sequence index out of range!"
+ raise IndexError("Sequence index out of range!")
def removeSequence(self, index):
"""Remove sequence (including class field) from the dataset."""
@@ -331,7 +331,7 @@ def save_netcdf(self, flo, **kwargs):
seqLengths[:] = [self.getSequenceLength(i) for i in range(self.getNumSequences())]
# Close file
- print "wrote netCDF file " + filename
+ print("wrote netCDF file " + filename)
d.close()
@@ -345,15 +345,15 @@ def save_netcdf(self, flo, **kwargs):
dataset.appendLinked([ 0.20, 0.90 ] , [2])
dataset.calculateStatistics()
- print "class histogram:", dataset.classHist
- print "# of classes:", dataset.nClasses
- print "class 1 is: ", dataset.getClass(1)
- print "targets: ", dataset.getField('target')
+ print("class histogram:", dataset.classHist)
+ print("# of classes:", dataset.nClasses)
+ print("class 1 is: ", dataset.getClass(1))
+ print("targets: ", dataset.getField('target'))
dataset._convertToOneOfMany(bounds=[0, 1])
- print "converted targets: "
- print dataset.getField('target')
+ print("converted targets: ")
+ print(dataset.getField('target'))
dataset._convertToClassNb()
- print "reconverted to original:", dataset.getField('target')
+ print("reconverted to original:", dataset.getField('target'))
diff --git a/pybrain/datasets/dataset.py b/pybrain/datasets/dataset.py
index 081a3d504..9dd23d959 100644
--- a/pybrain/datasets/dataset.py
+++ b/pybrain/datasets/dataset.py
@@ -67,7 +67,7 @@ def setVectorFormat(self, vf):
'1d': self._convertArray1d,
'2d': self._convertArray2d,
'list': self._convertList,
- 'none': lambda(x):x
+ 'none': lambda x:x
}
try:
self._convert = switch[vf]
diff --git a/pybrain/datasets/importance.py b/pybrain/datasets/importance.py
index fc8db8cca..d6acee37e 100644
--- a/pybrain/datasets/importance.py
+++ b/pybrain/datasets/importance.py
@@ -2,7 +2,7 @@
from scipy import ones, dot
-from sequential import SequentialDataSet
+from pybrain.datasets.sequential import SequentialDataSet
from pybrain.utilities import fListToString
@@ -40,9 +40,9 @@ def _evaluateSequence(self, f, seq, verbose = False):
totalError += e
ponderation += sum(importance)
if verbose:
- print 'out: ', fListToString(list(res))
- print 'correct: ', fListToString(target)
- print 'importance:', fListToString(importance)
- print 'error: % .8f' % e
+ print( 'out: ', fListToString(list(res)))
+ print( 'correct: ', fListToString(target))
+ print( 'importance:', fListToString(importance))
+ print( 'error: % .8f' % e)
return totalError, ponderation
diff --git a/pybrain/datasets/reinforcement.py b/pybrain/datasets/reinforcement.py
index 385cfe632..bd6b20080 100644
--- a/pybrain/datasets/reinforcement.py
+++ b/pybrain/datasets/reinforcement.py
@@ -1,7 +1,7 @@
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
-from sequential import SequentialDataSet
-from dataset import DataSet
+from pybrain.datasets.sequential import SequentialDataSet
+from pybrain.datasets.dataset import DataSet
from scipy import zeros
diff --git a/pybrain/datasets/sequential.py b/pybrain/datasets/sequential.py
index a4b189c96..a24c6b580 100644
--- a/pybrain/datasets/sequential.py
+++ b/pybrain/datasets/sequential.py
@@ -4,7 +4,7 @@
from scipy import ravel, r_
from random import sample
-from supervised import SupervisedDataSet
+from pybrain.datasets.supervised import SupervisedDataSet
class EmptySequenceError(Exception): pass
diff --git a/pybrain/datasets/supervised.py b/pybrain/datasets/supervised.py
index 03eb7e7a2..6dc3b9ca7 100644
--- a/pybrain/datasets/supervised.py
+++ b/pybrain/datasets/supervised.py
@@ -3,7 +3,7 @@
from random import sample
from scipy import isscalar
-from dataset import DataSet
+from pybrain.datasets.dataset import DataSet
from pybrain.utilities import fListToString
@@ -85,9 +85,9 @@ def _evaluateSequence(self, f, seq, verbose = False):
totalError += e
ponderation += len(target)
if verbose:
- print 'out: ', fListToString( list( res ) )
- print 'correct:', fListToString( target )
- print 'error: % .8f' % e
+ print( 'out: ', fListToString( list( res ) ))
+ print( 'correct:', fListToString( target ))
+ print( 'error: % .8f' % e)
return totalError, ponderation
def evaluateModuleMSE(self, module, averageOver = 1, **args):
diff --git a/pybrain/optimization/__init__.py b/pybrain/optimization/__init__.py
index 28076b563..56d57f3b8 100644
--- a/pybrain/optimization/__init__.py
+++ b/pybrain/optimization/__init__.py
@@ -1,7 +1,7 @@
-from hillclimber import HillClimber, StochasticHillClimber
-from randomsearch import RandomSearch, WeightGuessing, WeightMaskGuessing
-from neldermead import NelderMead
-from populationbased.__init__ import *
-from finitedifference.__init__ import *
-from distributionbased.__init__ import *
-from memetic.__init__ import *
\ No newline at end of file
+from pybrain.optimization.hillclimber import HillClimber, StochasticHillClimber
+from pybrain.optimization.randomsearch import RandomSearch, WeightGuessing, WeightMaskGuessing
+from pybrain.optimization.neldermead import NelderMead
+from pybrain.optimization.populationbased.__init__ import *
+from pybrain.optimization.finitedifference.__init__ import *
+from pybrain.optimization.distributionbased.__init__ import *
+from pybrain.optimization.memetic.__init__ import *
\ No newline at end of file
diff --git a/pybrain/optimization/distributionbased/cmaes.py b/pybrain/optimization/distributionbased/cmaes.py
index f85e18dd6..299b34f81 100644
--- a/pybrain/optimization/distributionbased/cmaes.py
+++ b/pybrain/optimization/distributionbased/cmaes.py
@@ -107,13 +107,13 @@ def _learnStep(self):
if arfitness[0] == arfitness[-1] or (abs(arfitness[0] - arfitness[-1]) /
(abs(arfitness[0]) + abs(arfitness[-1]))) <= self.stopPrecision:
if self.verbose:
- print "Converged."
+ print("Converged.")
self.maxLearningSteps = self.numLearningSteps
# or diverged, unfortunately
if min(Ev) > 1e5:
if self.verbose:
- print "Diverged."
+ print("Diverged.")
self.maxLearningSteps = self.numLearningSteps
@property
diff --git a/pybrain/optimization/distributionbased/fem.py b/pybrain/optimization/distributionbased/fem.py
index aad5375ef..69990c37a 100644
--- a/pybrain/optimization/distributionbased/fem.py
+++ b/pybrain/optimization/distributionbased/fem.py
@@ -129,7 +129,7 @@ def _computeDensities(self, sample):
elif pdf < 1e-40:
pdf = 1e-40
if isnan(pdf):
- print 'NaN!'
+ print('NaN!')
pdf = 0.
densities[c] = self.alphas[c] * pdf
densities /= sum(densities)
@@ -189,7 +189,7 @@ def _updateAlphas(self, updateSize):
if self.alphas[c] < self.minimumCenterWeight:
# center-splitting
if self.verbose:
- print 'Split!'
+ print('Split!')
bestCenter = argmax(self.alphas)
totalWeight = self.alphas[c] + self.alphas[bestCenter]
self.alphas[c] = totalWeight / 2
@@ -219,8 +219,8 @@ def _updateShaping(self):
self.shapingFunction.setParameter(possible[argmax(matchValues)])
if len(self.allsamples) % 100 == 0:
- print possible[argmax(matchValues)]
- print fListToString(matchValues, 3)
+ print(possible[argmax(matchValues)])
+ print(fListToString(matchValues, 3))
def _learnStep(self):
k = len(self.allsamples) % self.windowSize
@@ -230,7 +230,7 @@ def _learnStep(self):
if len(self.allsamples) < self.windowSize:
return
if self.verbose and len(self.allsamples) % 100 == 0:
- print len(self.allsamples), min(self.fitnesses), max(self.fitnesses)#, self.alphas
+ print(len(self.allsamples), min(self.fitnesses), max(self.fitnesses)#, self.alphas)
updateSize = self._computeUpdateSize(self._computeDensities(sample), k)
self.allUpdateSizes.append(deepcopy(updateSize))
@@ -261,8 +261,8 @@ def _learnStep(self):
index = int(self.windowSize * 0.8)
if sortedLast[index] >= sortedSecond[index]:
self.sigmas = [1.2 * sigma for sigma in self.sigmas]
- #print "+"
+ #print("+")
else:
self.sigmas = [0.5 * sigma for sigma in self.sigmas]
- #print "-"
+ #print("-")
diff --git a/pybrain/optimization/distributionbased/rank1.py b/pybrain/optimization/distributionbased/rank1.py
index 2fbea8de8..ba1ef31e8 100644
--- a/pybrain/optimization/distributionbased/rank1.py
+++ b/pybrain/optimization/distributionbased/rank1.py
@@ -94,9 +94,9 @@ def _notify(self):
""" Provide some feedback during the run. """
if self.verbose:
if self.numEvaluations % self.verboseGaps == 0:
- print 'Step:', self.numLearningSteps, 'best:', self.bestEvaluation,
- print 'logVar', round(self._logDetA, 3),
- print 'log|vector|', round(log(dot(self._principalVector, self._principalVector))/2, 3)
+ print('Step:', self.numLearningSteps, 'best:', self.bestEvaluation,
+ 'logVar', round(self._logDetA, 3),
+ 'log|vector|', round(log(dot(self._principalVector, self._principalVector))/2, 3))
if self.listener is not None:
self.listener(self.bestEvaluable, self.bestEvaluation)
diff --git a/pybrain/optimization/distributionbased/snes.py b/pybrain/optimization/distributionbased/snes.py
index c167179b1..04501c4b9 100644
--- a/pybrain/optimization/distributionbased/snes.py
+++ b/pybrain/optimization/distributionbased/snes.py
@@ -112,5 +112,5 @@ def _learnStep(self):
if __name__ == "__main__":
from pybrain.rl.environments.functions.unimodal import ElliFunction
- print SNES(ElliFunction(100), ones(100), verbose=True).learn()
+ print(SNES(ElliFunction(100), ones(100), verbose=True).learn())
\ No newline at end of file
diff --git a/pybrain/optimization/distributionbased/ves.py b/pybrain/optimization/distributionbased/ves.py
index cf605a32d..0112eeb0c 100644
--- a/pybrain/optimization/distributionbased/ves.py
+++ b/pybrain/optimization/distributionbased/ves.py
@@ -158,15 +158,15 @@ def _batchLearn(self):
self.sigma = dot(self.factorSigma.T, self.factorSigma)
except ValueError:
- print 'Numerical Instability. Stopping.'
+ print('Numerical Instability. Stopping.')
self.maxLearningSteps = self.numLearningSteps
if self._hasConverged():
- print 'Premature convergence. Stopping.'
+ print('Premature convergence. Stopping.')
self.maxLearningSteps = self.numLearningSteps
if self.verbose:
- print 'Evals:', self.numEvaluations,
+ print('Evals:', self.numEvaluations,)
self.allCenters.append(self.x.copy())
self.allFactorSigmas.append(self.factorSigma.copy())
diff --git a/pybrain/optimization/distributionbased/xnes.py b/pybrain/optimization/distributionbased/xnes.py
index 830575ff3..9583b97cd 100644
--- a/pybrain/optimization/distributionbased/xnes.py
+++ b/pybrain/optimization/distributionbased/xnes.py
@@ -140,5 +140,5 @@ def _produceSamples(self):
dim = 10
f = RosenbrockFunction(dim)
l = XNES(f, -ones(dim))
- print l.learn()
- print 'Evaluations needed:', len(l._allEvaluations)
+ print(l.learn())
+ print('Evaluations needed:', len(l._allEvaluations))
diff --git a/pybrain/optimization/memetic/memetic.py b/pybrain/optimization/memetic/memetic.py
index 57608cf4d..4517e660e 100644
--- a/pybrain/optimization/memetic/memetic.py
+++ b/pybrain/optimization/memetic/memetic.py
@@ -53,7 +53,7 @@ def _learnStep(self):
def _notify(self):
HillClimber._notify(self)
if self.verbose:
- print ' Bits on in best mask:', sum(self.bestEvaluable.mask)
+ print(' Bits on in best mask:', sum(self.bestEvaluable.mask))
@property
def batchSize(self):
diff --git a/pybrain/optimization/optimizer.py b/pybrain/optimization/optimizer.py
index 56804bead..6204db06b 100644
--- a/pybrain/optimization/optimizer.py
+++ b/pybrain/optimization/optimizer.py
@@ -292,7 +292,7 @@ def _stoppingCriterion(self):
def _notify(self):
""" Provide some feedback during the run. """
if self.verbose:
- print 'Step:', self.numLearningSteps, 'best:', self.bestEvaluation
+ print('Step:', self.numLearningSteps, 'best:', self.bestEvaluation)
if self.listener is not None:
self.listener(self.bestEvaluable, self.bestEvaluation)
diff --git a/pybrain/optimization/populationbased/__init__.py b/pybrain/optimization/populationbased/__init__.py
index 336571111..d24c25b77 100644
--- a/pybrain/optimization/populationbased/__init__.py
+++ b/pybrain/optimization/populationbased/__init__.py
@@ -1,5 +1,5 @@
-from es import ES
-from ga import GA
-from pso import ParticleSwarmOptimizer
-from multiobjective.__init__ import *
-#from coevolution.__init__ import *
\ No newline at end of file
+from pybrain.optimization.populationbased.es import ES
+from pybrain.optimization.populationbased.ga import GA
+from pybrain.optimization.populationbased.pso import ParticleSwarmOptimizer
+from pybrain.optimization.populationbased.multiobjective.__init__ import *
+#from pybrain.optimization.populationbased.coevolution.__init__ import *
\ No newline at end of file
diff --git a/pybrain/optimization/populationbased/coevolution/coevolution.py b/pybrain/optimization/populationbased/coevolution/coevolution.py
index 67675cf7d..e9942cbf1 100644
--- a/pybrain/optimization/populationbased/coevolution/coevolution.py
+++ b/pybrain/optimization/populationbased/coevolution/coevolution.py
@@ -90,10 +90,10 @@ def _oneGeneration(self):
self.hallOfFitnesses.append(bestFits)
if self.verbose:
- print 'Generation', self.generation
- print ' relat. fits:', fListToString(sorted(fitnesses), 4)
+ print('Generation', self.generation)
+ print(' relat. fits:', fListToString(sorted(fitnesses), 4))
if len(best.params) < 20:
- print ' best params:', fListToString(best.params, 4)
+ print(' best params:', fListToString(best.params, 4))
self.pop = self._selectAndReproduce(self.pop, fitnesses)
@@ -287,5 +287,5 @@ def _stepsPerGeneration(self):
x.allOpponents[3] = [2, 4]
x.allOpponents[4] = [3]
x.allOpponents[5] = [2]
- print x._sharedSampling(4, [1, 2, 3, 4, 5], [1, 2, 3, 4, 6, 7, 8, 9])
- print 'should be', [4, 1, 2, 5]
+ print(x._sharedSampling(4, [1, 2, 3, 4, 5], [1, 2, 3, 4, 6, 7, 8, 9]))
+ print('should be', [4, 1, 2, 5])
diff --git a/pybrain/optimization/populationbased/coevolution/competitivecoevolution.py b/pybrain/optimization/populationbased/coevolution/competitivecoevolution.py
index fa01745ca..042ece9ee 100644
--- a/pybrain/optimization/populationbased/coevolution/competitivecoevolution.py
+++ b/pybrain/optimization/populationbased/coevolution/competitivecoevolution.py
@@ -1,6 +1,6 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from coevolution import Coevolution
+from pybrain.optimization.coevolution.coevolution import Coevolution
class CompetitiveCoevolution(Coevolution):
@@ -118,8 +118,8 @@ def b(x, y):
b(4, 5)
b(4, 7)
b(8, 4)
- print C.pop
- print C.parasitePop
- print ' ', fListToString(C._competitiveSharedFitness(C.pop, C.parasitePop), 2)
- print 'should be:', fListToString([0.83, 0.00, 1.33, 0.83], 2)
+ print(C.pop)
+ print(C.parasitePop)
+ print(' ', fListToString(C._competitiveSharedFitness(C.pop, C.parasitePop), 2))
+ print('should be:', fListToString([0.83, 0.00, 1.33, 0.83], 2))
diff --git a/pybrain/optimization/populationbased/coevolution/multipopulationcoevolution.py b/pybrain/optimization/populationbased/coevolution/multipopulationcoevolution.py
index 8a263e396..b7d853fdc 100644
--- a/pybrain/optimization/populationbased/coevolution/multipopulationcoevolution.py
+++ b/pybrain/optimization/populationbased/coevolution/multipopulationcoevolution.py
@@ -2,7 +2,7 @@
from random import choice
-from coevolution import Coevolution
+from pybrain.optimization.coevolution.coevolution import Coevolution
class MultiPopulationCoevolution(Coevolution):
diff --git a/pybrain/optimization/populationbased/ga.py b/pybrain/optimization/populationbased/ga.py
index 00872d353..833f4cc92 100644
--- a/pybrain/optimization/populationbased/ga.py
+++ b/pybrain/optimization/populationbased/ga.py
@@ -5,7 +5,7 @@
from random import choice, random, gauss, shuffle, sample
from numpy import ndarray
-from evolution import Evolution
+from pybrain.optimization.populationbased.evolution import Evolution
from pybrain.optimization.optimizer import ContinuousOptimizer
diff --git a/pybrain/optimization/populationbased/multiobjective/__init__.py b/pybrain/optimization/populationbased/multiobjective/__init__.py
index b6d3c8826..c814a7bcd 100644
--- a/pybrain/optimization/populationbased/multiobjective/__init__.py
+++ b/pybrain/optimization/populationbased/multiobjective/__init__.py
@@ -1,4 +1,4 @@
-from nsga2 import MultiObjectiveGA
+from pybrain.optimization.populationbased.multiobjective.nsga2 import MultiObjectiveGA
""" added by JPQ """
-from constnsga2 import ConstMultiObjectiveGA
+from pybrain.optimization.populationbased.multiobjective.constnsga2 import ConstMultiObjectiveGA
# ---
\ No newline at end of file
diff --git a/pybrain/optimization/populationbased/multiobjective/constnsga2.py b/pybrain/optimization/populationbased/multiobjective/constnsga2.py
index cab60b72a..5f99536f7 100644
--- a/pybrain/optimization/populationbased/multiobjective/constnsga2.py
+++ b/pybrain/optimization/populationbased/multiobjective/constnsga2.py
@@ -55,9 +55,9 @@ def _learnStep(self):
# key=lambda x: self.fitnesses[x],
# allowequality = self.allowEquality)
# self.topProportion = float(number_of_feasible)/self.populationSize
-# print 'Len bestEvaluable ',len(self.bestEvaluable)
+# print('Len bestEvaluable ',len(self.bestEvaluable))
# for i in range(len(self.bestEvaluable)):
-# print self.bestEvaluable[i],':',self.bestEvaluation[i]
+# print(self.bestEvaluable[i],':',self.bestEvaluation[i])
self.produceOffspring()
def select(self):
diff --git a/pybrain/optimization/populationbased/pso.py b/pybrain/optimization/populationbased/pso.py
index b16d5f63f..8bf5e0915 100644
--- a/pybrain/optimization/populationbased/pso.py
+++ b/pybrain/optimization/populationbased/pso.py
@@ -65,7 +65,7 @@ def _additionalInit(self):
maxs = scipy.array([max_ for min_, max_ in self.boundaries])
self.particles = []
- for _ in xrange(self.size):
+ for _ in range(self.size):
startingPosition = scipy.random.random(self.dim)
startingPosition *= (maxs - mins)
startingPosition += mins
diff --git a/pybrain/rl/agents/__init__.py b/pybrain/rl/agents/__init__.py
index 7c2e0f407..9d8269587 100644
--- a/pybrain/rl/agents/__init__.py
+++ b/pybrain/rl/agents/__init__.py
@@ -1,2 +1,2 @@
-from learning import LearningAgent
-from optimization import OptimizationAgent
\ No newline at end of file
+from pybrain.rl.agents.learning import LearningAgent
+from pybrain.rl.agents.optimization import OptimizationAgent
\ No newline at end of file
diff --git a/pybrain/rl/agents/linearfa.py b/pybrain/rl/agents/linearfa.py
index 3805424c6..8f149bcaf 100644
--- a/pybrain/rl/agents/linearfa.py
+++ b/pybrain/rl/agents/linearfa.py
@@ -75,7 +75,7 @@ def learn(self):
if not self.learning:
return
if not self.learner.batchMode:
- print 'Learning is done online, and already finished.'
+ print('Learning is done online, and already finished.')
return
for seq in self.history:
for obs, action, reward in seq:
diff --git a/pybrain/rl/environments/__init__.py b/pybrain/rl/environments/__init__.py
index 3be469cca..c923ff3ee 100644
--- a/pybrain/rl/environments/__init__.py
+++ b/pybrain/rl/environments/__init__.py
@@ -1,3 +1,3 @@
-from environment import Environment
-from task import Task
-from episodic import EpisodicTask
\ No newline at end of file
+from pybrain.rl.environments.environment import Environment
+from pybrain.rl.environments.task import Task
+from pybrain.rl.environments.episodic import EpisodicTask
\ No newline at end of file
diff --git a/pybrain/rl/environments/cartpole/__init__.py b/pybrain/rl/environments/cartpole/__init__.py
index fb02601ce..2549343ad 100644
--- a/pybrain/rl/environments/cartpole/__init__.py
+++ b/pybrain/rl/environments/cartpole/__init__.py
@@ -4,9 +4,9 @@
except ImportError:
raise ImportError('This environment needs the matplotlib library installed.')
-from cartpole import CartPoleEnvironment, CartPoleLinEnvironment
-from renderer import CartPoleRenderer
-from balancetask import BalanceTask, EasyBalanceTask, DiscreteBalanceTask, DiscreteNoHelpTask, JustBalanceTask, LinearizedBalanceTask, DiscretePOMDPTask
-from doublepole import DoublePoleEnvironment
-from nonmarkovpole import NonMarkovPoleEnvironment
-from nonmarkovdoublepole import NonMarkovDoublePoleEnvironment
\ No newline at end of file
+from pybrain.rl.environments.cartpole.cartpole import CartPoleEnvironment, CartPoleLinEnvironment
+from pybrain.rl.environments.cartpole.renderer import CartPoleRenderer
+from pybrain.rl.environments.cartpole.balancetask import BalanceTask, EasyBalanceTask, DiscreteBalanceTask, DiscreteNoHelpTask, JustBalanceTask, LinearizedBalanceTask, DiscretePOMDPTask
+from pybrain.rl.environments.cartpole.doublepole import DoublePoleEnvironment
+from pybrain.rl.environments.cartpole.nonmarkovpole import NonMarkovPoleEnvironment
+from pybrain.rl.environments.cartpole.nonmarkovdoublepole import NonMarkovDoublePoleEnvironment
\ No newline at end of file
diff --git a/pybrain/rl/environments/cartpole/fast_version/__init__.py b/pybrain/rl/environments/cartpole/fast_version/__init__.py
index cb18c0079..67477200e 100644
--- a/pybrain/rl/environments/cartpole/fast_version/__init__.py
+++ b/pybrain/rl/environments/cartpole/fast_version/__init__.py
@@ -3,4 +3,4 @@
except ImportError:
raise ImportError('This task needs to be compiled. Please use the script: cartpolecompile.py')
-from cartpoleenv import FastCartPoleTask
\ No newline at end of file
+from pybrain.rl.environments.cartpole.fast_version.cartpoleenv import FastCartPoleTask
\ No newline at end of file
diff --git a/pybrain/rl/environments/cartpole/fast_version/cartpoleenv.py b/pybrain/rl/environments/cartpole/fast_version/cartpoleenv.py
index a7a1fd69a..7cc334253 100644
--- a/pybrain/rl/environments/cartpole/fast_version/cartpoleenv.py
+++ b/pybrain/rl/environments/cartpole/fast_version/cartpoleenv.py
@@ -64,7 +64,7 @@ def __str__(self):
def reset(self):
if self.verbose:
- print '** reset **'
+ print('** reset **')
self.cumreward = 0
impl.res()
@@ -81,18 +81,18 @@ def outdim(self):
def getReward(self):
r = 1. + impl.getR()
if self.verbose:
- print ' +r', r,
+ print(' +r', r,)
return r
def isFinished(self):
if self.verbose:
- print ' -finished?', impl.isFinished()
+ print(' -finished?', impl.isFinished())
return impl.isFinished()
def getObservation(self):
obs = array(impl.getObs())
if self.verbose:
- print 'obs', obs
+ print('obs', obs)
obs.resize(self.outdim)
if self.extraObservations:
cartpos = obs[-1]
@@ -114,12 +114,12 @@ def getObservation(self):
obs[-self.extraRandoms:] = randn(self.extraRandoms)
if self.verbose:
- print 'obs', obs
+ print('obs', obs)
return obs
def performAction(self, action):
if self.verbose:
- print 'act', action
+ print('act', action)
impl.performAction(action[0])
self.addReward()
diff --git a/pybrain/rl/environments/classic/acrobot.py b/pybrain/rl/environments/classic/acrobot.py
index 65a641795..ae987f9c4 100644
--- a/pybrain/rl/environments/classic/acrobot.py
+++ b/pybrain/rl/environments/classic/acrobot.py
@@ -100,7 +100,7 @@ def GetReward(self, x):
theta2 = x[1]
y_acrobot[1] = y_acrobot[0] - cos(theta1)
y_acrobot[2] = y_acrobot[1] - cos(theta2)
- #print y_acrobot
+ #print(y_acrobot)
#goal
goal = y_acrobot[0] + self.target
if self.easy_rewards:
diff --git a/pybrain/rl/environments/classic/mountaincar.py b/pybrain/rl/environments/classic/mountaincar.py
index 3d125cf36..cc29c1d20 100644
--- a/pybrain/rl/environments/classic/mountaincar.py
+++ b/pybrain/rl/environments/classic/mountaincar.py
@@ -44,7 +44,7 @@ def reset(self):
self.state = self.GetInitialState()
def getObservation(self):
- #print array([self.state[0], self.state[1] * 100, 1])
+ #print(array([self.state[0], self.state[1] * 100, 1]))
return array([self.state[0], self.state[1] * 100, 1])
def performAction(self, action):
@@ -103,8 +103,8 @@ def DoAction(self, a, s):
# acti: is the force to be applied to the car
# x: is the vector containning the position and speed of the car
# xp: is the vector containing the new position and velocity of the car
- #print 'action',a
- #print 'state',s
+ #print('action',a)
+ #print('state',s)
force = self.action_list[a]
self.steps = self.steps + 1
diff --git a/pybrain/rl/environments/classic/xor.py b/pybrain/rl/environments/classic/xor.py
index 752c9420a..1911cb7b8 100644
--- a/pybrain/rl/environments/classic/xor.py
+++ b/pybrain/rl/environments/classic/xor.py
@@ -36,7 +36,7 @@ def performAction(self, action):
self.r = 1
else:
self.r = -1
- #print self.obs, action, self.r
+ #print(self.obs, action, self.r )
self.addReward()
def getReward(self):
diff --git a/pybrain/rl/environments/flexcube/__init__.py b/pybrain/rl/environments/flexcube/__init__.py
index 204b1c3f1..ce24244c1 100644
--- a/pybrain/rl/environments/flexcube/__init__.py
+++ b/pybrain/rl/environments/flexcube/__init__.py
@@ -1,2 +1,2 @@
-from environment import FlexCubeEnvironment
-from tasks import *
+from pybrain.rl.environments.flexcube.environment import FlexCubeEnvironment
+from pybrain.rl.environments.flexcube.tasks import *
diff --git a/pybrain/rl/environments/flexcube/viewer.py b/pybrain/rl/environments/flexcube/viewer.py
index a8ed065e2..fd2a2c843 100644
--- a/pybrain/rl/environments/flexcube/viewer.py
+++ b/pybrain/rl/environments/flexcube/viewer.py
@@ -73,7 +73,7 @@ def saveTo(self, filename, format="JPEG"):
image = Image.fromstring("RGB", (width, height), data)
image = image.transpose(Image.FLIP_TOP_BOTTOM)
image.save(filename, format)
- print 'Saved image to ', filename
+ print('Saved image to ', filename)
return image
# the render method containing the Glut mainloop
diff --git a/pybrain/rl/environments/functions/__init__.py b/pybrain/rl/environments/functions/__init__.py
index dd047cd0e..609c7ec32 100644
--- a/pybrain/rl/environments/functions/__init__.py
+++ b/pybrain/rl/environments/functions/__init__.py
@@ -1,7 +1,7 @@
-from function import FunctionEnvironment
-from unimodal import SchwefelFunction, SphereFunction, TabletFunction, DiffPowFunction, \
+from pybrain.rl.environments.functions.function import FunctionEnvironment
+from pybrain.rl.environments.functions.unimodal import SchwefelFunction, SphereFunction, TabletFunction, DiffPowFunction, \
CigarFunction, ElliFunction, RosenbrockFunction
-from multimodal import RastriginFunction, AckleyFunction, GriewankFunction, Schwefel_2_13Function, \
+from pybrain.rl.environments.functions.multimodal import RastriginFunction, AckleyFunction, GriewankFunction, Schwefel_2_13Function, \
WeierstrassFunction, FunnelFunction
-from unbounded import ParabRFunction, SharpRFunction, LinearFunction
-from transformations import oppositeFunction, RotateFunction, TranslateFunction
\ No newline at end of file
+from pybrain.rl.environments.functions.unbounded import ParabRFunction, SharpRFunction, LinearFunction
+from pybrain.rl.environments.functions.transformations import oppositeFunction, RotateFunction, TranslateFunction
\ No newline at end of file
diff --git a/pybrain/rl/environments/functions/lennardjones.py b/pybrain/rl/environments/functions/lennardjones.py
index 23a8fb6a4..dcc356ee1 100644
--- a/pybrain/rl/environments/functions/lennardjones.py
+++ b/pybrain/rl/environments/functions/lennardjones.py
@@ -1,7 +1,7 @@
__author__ = 'Tom Schaul, tom@idsia.ch, and Daan Wierstra'
-from multimodal import MultiModalFunction
+from pybrain.rl.environments.functions.multimodal import MultiModalFunction
from scipy import sqrt, tile, swapaxes, ravel, eye, randn
import scipy
diff --git a/pybrain/rl/environments/functions/multimodal.py b/pybrain/rl/environments/functions/multimodal.py
index 84bd97962..7256bc1e3 100644
--- a/pybrain/rl/environments/functions/multimodal.py
+++ b/pybrain/rl/environments/functions/multimodal.py
@@ -7,8 +7,8 @@
from math import pi
from random import shuffle
-from function import FunctionEnvironment
-from transformations import penalize, generateDiags
+from pybrain.rl.environments.functions.function import FunctionEnvironment
+from pybrain.rl.environments.functions.transformations import penalize, generateDiags
class MultiModalFunction(FunctionEnvironment):
@@ -146,7 +146,7 @@ class GallagherGauss101MeFunction(MultiModalFunction):
def __init__(self, *args, **kwargs):
MultiModalFunction.__init__(self, *args, **kwargs)
- print self.numPeaks, self.xdim
+ print(self.numPeaks, self.xdim)
self._opts = [(rand(self.xdim) - 0.5) * 8]
self._opts.extend([(rand(self.xdim) - 0.5) * 9.8 for _ in range(self.numPeaks-1)])
alphas = [power(self.maxCond, 2 * i / float(self.numPeaks - 2)) for i in range(self.numPeaks - 1)]
diff --git a/pybrain/rl/environments/functions/transformations.py b/pybrain/rl/environments/functions/transformations.py
index 7b40bc493..d34f00f66 100644
--- a/pybrain/rl/environments/functions/transformations.py
+++ b/pybrain/rl/environments/functions/transformations.py
@@ -5,7 +5,7 @@
from scipy.linalg import orth, norm, inv
from random import shuffle, random, gauss
-from function import FunctionEnvironment
+from pybrain.rl.environments.functions.function import FunctionEnvironment
from pybrain.structure.parametercontainer import ParameterContainer
from pybrain.rl.environments.fitnessevaluator import FitnessEvaluator
from pybrain.utilities import sparse_orth, dense_orth
diff --git a/pybrain/rl/environments/functions/unbounded.py b/pybrain/rl/environments/functions/unbounded.py
index d32c4a2e5..a3554f106 100644
--- a/pybrain/rl/environments/functions/unbounded.py
+++ b/pybrain/rl/environments/functions/unbounded.py
@@ -4,7 +4,7 @@
from math import sqrt
-from function import FunctionEnvironment
+from pybrain.rl.environments.functions.function import FunctionEnvironment
class UnboundedFunctionEnvironment(FunctionEnvironment):
diff --git a/pybrain/rl/environments/functions/unimodal.py b/pybrain/rl/environments/functions/unimodal.py
index 7fecca80a..f5e822b1d 100644
--- a/pybrain/rl/environments/functions/unimodal.py
+++ b/pybrain/rl/environments/functions/unimodal.py
@@ -5,7 +5,7 @@
from scipy import ones, sqrt, dot, sign, randn, power, rand, floor, array
from scipy.linalg import norm, orth
-from function import FunctionEnvironment
+from pybrain.rl.environments.functions.function import FunctionEnvironment
class SphereFunction(FunctionEnvironment):
diff --git a/pybrain/rl/environments/mazes/__init__.py b/pybrain/rl/environments/mazes/__init__.py
index 5742579b0..59ed9b753 100644
--- a/pybrain/rl/environments/mazes/__init__.py
+++ b/pybrain/rl/environments/mazes/__init__.py
@@ -1,3 +1,3 @@
-from maze import Maze
-from polarmaze import PolarMaze
-from tasks.__init__ import *
\ No newline at end of file
+from pybrain.rl.environments.mazes.maze import Maze
+from pybrain.rl.environments.mazes.polarmaze import PolarMaze
+from pybrain.rl.environments.mazes.tasks.__init__ import *
\ No newline at end of file
diff --git a/pybrain/rl/environments/mazes/tasks/__init__.py b/pybrain/rl/environments/mazes/tasks/__init__.py
index e17f9ebd1..1de2e4ac5 100644
--- a/pybrain/rl/environments/mazes/tasks/__init__.py
+++ b/pybrain/rl/environments/mazes/tasks/__init__.py
@@ -1,8 +1,8 @@
-from tiger import TigerTask
-from maze import TrivialMaze, MazeTask
-from cheesemaze import CheeseMaze
-from tmaze import TMaze
-from maze4x3 import FourByThreeMaze
-from maze89state import EightyNineStateMaze
-from shuttle import ShuttleDocking
-from mdp import MDPMazeTask
+from pybrain.rl.environments.mazes.tiger import TigerTask
+from pybrain.rl.environments.mazes.maze import TrivialMaze, MazeTask
+from pybrain.rl.environments.mazes.cheesemaze import CheeseMaze
+from pybrain.rl.environments.mazes.tmaze import TMaze
+from pybrain.rl.environments.mazes.maze4x3 import FourByThreeMaze
+from pybrain.rl.environments.mazes.maze89state import EightyNineStateMaze
+from pybrain.rl.environments.mazes.shuttle import ShuttleDocking
+from pybrain.rl.environments.mazes.mdp import MDPMazeTask
diff --git a/pybrain/rl/environments/ode/__init__.py b/pybrain/rl/environments/ode/__init__.py
index 6753a52ba..7c4a0ed80 100644
--- a/pybrain/rl/environments/ode/__init__.py
+++ b/pybrain/rl/environments/ode/__init__.py
@@ -3,7 +3,7 @@
except ImportError:
raise ImportError('This environment requires the py-ode package to be installed on your system.')
-from environment import ODEEnvironment
-from sensors import *
-from actuators import *
-from instances import *
\ No newline at end of file
+from pybrain.rl.environments.ode.environment import ODEEnvironment
+from pybrain.rl.environments.ode.sensors import *
+from pybrain.rl.environments.ode.actuators import *
+from pybrain.rl.environments.ode.instances import *
\ No newline at end of file
diff --git a/pybrain/rl/environments/ode/environment.py b/pybrain/rl/environments/ode/environment.py
index 92963ad82..7bdbc518f 100644
--- a/pybrain/rl/environments/ode/environment.py
+++ b/pybrain/rl/environments/ode/environment.py
@@ -28,7 +28,7 @@ class ODEEnvironment(Environment):
def __init__(self, render=True, realtime=True, ip="127.0.0.1", port="21590", buf='16384'):
""" initializes the virtual world, variables, the frame rate and the callback functions."""
- print "ODEEnvironment -- based on Open Dynamics Engine."
+ print("ODEEnvironment -- based on Open Dynamics Engine.")
# initialize base class
self.render = render
@@ -167,7 +167,7 @@ def centerOn(self, name):
self.getRenderer().setCenterObj(self.root.namedChild(name).getODEObject())
except KeyError:
# name not found, unset centerObj
- print "Warning: Cannot center on " + name
+ print("Warning: Cannot center on " + name)
self.centerObj = None
def loadXODE(self, filename, reload=False):
@@ -182,7 +182,7 @@ def loadXODE(self, filename, reload=False):
world = filter(lambda x: isinstance(x, xode.parser.World), self.root.getChildren())[0]
except IndexError:
# malicious format, no world tag found
- print "no tag found in " + filename + ". quitting."
+ print("no tag found in " + filename + ". quitting.")
sys.exit()
self.world = world.getODEObject()
self._setWorldParameters()
@@ -191,7 +191,7 @@ def loadXODE(self, filename, reload=False):
space = filter(lambda x: isinstance(x, xode.parser.Space), world.getChildren())[0]
except IndexError:
# malicious format, no space tag found
- print "no tag found in " + filename + ". quitting."
+ print("no tag found in " + filename + ". quitting.")
sys.exit()
self.space = space.getODEObject()
@@ -200,12 +200,12 @@ def loadXODE(self, filename, reload=False):
self._parseBodies(self.root)
if self.verbosity > 0:
- print "-------[body/mass list]-----"
+ print("-------[body/mass list]-----")
for (body, _) in self.body_geom:
try:
- print body.name, body.getMass()
+ print(body.name, body.getMass())
except AttributeError:
- print ""
+ print("")
# now parse the additional parameters at the end of the xode file
self.loadConfig(filename, reload)
@@ -220,9 +220,9 @@ def loadConfig(self, filename, reload=False):
for passpairstring in self.config.getValue("passpairs")[:]:
self.passpairs.append(eval(passpairstring))
if self.verbosity > 0:
- print "-------[pass tuples]--------"
- print self.passpairs
- print "----------------------------"
+ print("-------[pass tuples]--------")
+ print(self.passpairs)
+ print("----------------------------")
#
# set focus of camera to the first object specified in the section, if any
@@ -238,7 +238,7 @@ def loadConfig(self, filename, reload=False):
# find first object with that name
obj = self.root.namedChild(jointName).getODEObject()
except IndexError:
- print "ERROR: Could not affix object '" + jointName + "' to environment!"
+ print("ERROR: Could not affix object '" + jointName + "' to environment!")
sys.exit(1)
if isinstance(obj, ode.Joint):
# if it is a joint, use this joint to fix to environment
@@ -273,7 +273,7 @@ def loadConfig(self, filename, reload=False):
try:
self.addSensor(eval('sensors.' + s))
except AttributeError:
- print dir(sensors)
+ print(dir(sensors))
warnings.warn("Sensor name with name " + s + " not found. skipped.")
else:
for s in self.sensors:
@@ -371,7 +371,7 @@ def indim(self):
return num
def getActionLength(self):
- print "getActionLength() is deprecated. use property 'indim' instead."
+ print("getActionLength() is deprecated. use property 'indim' instead.")
return self.indim
@property
@@ -524,7 +524,7 @@ def step(self):
def _printfunc (self):
pass
- # print self.root.namedChild('palm').getODEObject().getPosition()
+ # print(self.root.namedChild('palm').getODEObject().getPosition())
def specialkeyfunc(self, c, x, y):
"""Derived classes can implement extra functionality here"""
@@ -534,17 +534,17 @@ def specialkeyfunc(self, c, x, y):
#--- helper functions ---#
def _print_help(self):
""" prints out the keyboard shortcuts. """
- print "v -> toggle view with mouse on/off"
- print "s -> toggle screen capture on/off"
- print "d -> drop an object"
- print "f -> lift all objects"
- print "m -> toggle mouse view (press button to zoom)"
- print "r -> random torque at all joints"
- print "a/z -> negative/positive torque to all joints"
- print "g -> print current state"
- print "n -> reset environment"
+ print("v -> toggle view with mouse on/off")
+ print("s -> toggle screen capture on/off")
+ print("d -> drop an object")
+ print("f -> lift all objects")
+ print("m -> toggle mouse view (press button to zoom)")
+ print("r -> random torque at all joints")
+ print("a/z -> negative/positive torque to all joints")
+ print("g -> print current state")
+ print("n -> reset environment")
self.specialfunctionDoc()
- print "x,q -> exit program"
+ print("x,q -> exit program")
def specialfunctionDoc(self):
"""Derived classes can implement extra functionality here"""
@@ -560,7 +560,7 @@ def specialfunctionDoc(self):
Parameters: modelname = base name of the xode file to use (default: johnnie)
"""
- print "ODEEnvironment -- test program"
+ print("ODEEnvironment -- test program")
if len(sys.argv) > 1:
modelName = sys.argv[1]
else:
diff --git a/pybrain/rl/environments/ode/instances/__init__.py b/pybrain/rl/environments/ode/instances/__init__.py
index d8e74df0b..0691b5649 100644
--- a/pybrain/rl/environments/ode/instances/__init__.py
+++ b/pybrain/rl/environments/ode/instances/__init__.py
@@ -1,4 +1,3 @@
-from johnnie import JohnnieEnvironment
-from ccrl import CCRLEnvironment
-from acrobot import AcrobotEnvironment
-
+from pybrain.rl.environments.ode.instances.johnnie import JohnnieEnvironment
+from pybrain.rl.environments.ode.instances.ccrl import CCRLEnvironment
+from pybrain.rl.environments.ode.instances.acrobot import AcrobotEnvironment
diff --git a/pybrain/rl/environments/ode/instances/ccrl.py b/pybrain/rl/environments/ode/instances/ccrl.py
index 3ff4937a2..828742b23 100644
--- a/pybrain/rl/environments/ode/instances/ccrl.py
+++ b/pybrain/rl/environments/ode/instances/ccrl.py
@@ -95,7 +95,7 @@ def loadXODE(self, filename, reload=False):
world = filter(lambda x: isinstance(x, xode.parser.World), self.root.getChildren())[0]
except IndexError:
# malicious format, no world tag found
- print "no tag found in " + filename + ". quitting."
+ print("no tag found in " + filename + ". quitting.")
sys.exit()
self.world = world.getODEObject()
self._setWorldParameters()
@@ -104,7 +104,7 @@ def loadXODE(self, filename, reload=False):
space = filter(lambda x: isinstance(x, xode.parser.Space), world.getChildren())[0]
except IndexError:
# malicious format, no space tag found
- print "no tag found in " + filename + ". quitting."
+ print("no tag found in " + filename + ". quitting.")
sys.exit()
self.space = space.getODEObject()
@@ -119,12 +119,12 @@ def loadXODE(self, filename, reload=False):
body.setPosition(body.getPosition() + self.pert)
if self.verbosity > 0:
- print "-------[body/mass list]-----"
+ print("-------[body/mass list]-----")
for (body, _) in self.body_geom:
try:
- print body.name, body.getMass()
+ print(body.name, body.getMass())
except AttributeError:
- print ""
+ print("")
# now parse the additional parameters at the end of the xode file
self.loadConfig(filename, reload)
diff --git a/pybrain/rl/environments/ode/tasks/__init__.py b/pybrain/rl/environments/ode/tasks/__init__.py
index dca6fd97d..8e9ade95a 100644
--- a/pybrain/rl/environments/ode/tasks/__init__.py
+++ b/pybrain/rl/environments/ode/tasks/__init__.py
@@ -1,3 +1,3 @@
-from johnnie import *
-from ccrl import *
-from acrobot import *
+from pybrain.rl.environments.ode.tasks.johnnie import *
+from pybrain.rl.environments.ode.tasks.ccrl import *
+from pybrain.rl.environments.ode.tasks.acrobot import *
diff --git a/pybrain/rl/environments/ode/tasks/ccrl.py b/pybrain/rl/environments/ode/tasks/ccrl.py
index e8163ea9d..d13451be1 100644
--- a/pybrain/rl/environments/ode/tasks/ccrl.py
+++ b/pybrain/rl/environments/ode/tasks/ccrl.py
@@ -129,7 +129,7 @@ def getReward(self):
self.dist[8] = 0.0
dis = 100.0/((self.dist[:3] ** 2).sum()+0.1)
nig = 10.0/((self.dist[3:] ** 2).sum()+0.1)
- if self.env.stepCounter == self.epiLen: print "Grip:", grip, "Dis:", dis, "Nig:", nig, "Table:", self.tableFlag
+ if self.env.stepCounter == self.epiLen: print("Grip:", grip, "Dis:", dis, "Nig:", nig, "Table:", self.tableFlag)
return (10 + grip + nig + dis + self.tableFlag) / float(self.epiLen) #-dis
#else:
# return (25.0 - dis) / float(self.epiLen) + (grip / nig - float(self.env.tableSum)) * 0.1 #+self.grepRew (10.0-dis)/float(self.epiLen)+
diff --git a/pybrain/rl/environments/ode/tools/configgrab.py b/pybrain/rl/environments/ode/tools/configgrab.py
index 19a9d7b5e..7d1f50989 100644
--- a/pybrain/rl/environments/ode/tools/configgrab.py
+++ b/pybrain/rl/environments/ode/tools/configgrab.py
@@ -31,6 +31,6 @@ def getValue(self, name):
if line == self.delim[0] + name + self.delim[1] + "\n": flag = 1
else: flag = 0
file.close()
- #if len(output)==0: print "Attention: Config for ", name, "not found"
+ #if len(output)==0: print("Attention: Config for ", name, "not found")
return output
diff --git a/pybrain/rl/environments/ode/tools/xmltools.py b/pybrain/rl/environments/ode/tools/xmltools.py
index 5b4377210..b481a69cc 100644
--- a/pybrain/rl/environments/ode/tools/xmltools.py
+++ b/pybrain/rl/environments/ode/tools/xmltools.py
@@ -163,21 +163,21 @@ def write(self, file, depth=0):
"""parse XML structure recursively and append to the output fileID,
increasing the offset (tabs) while descending into the tree"""
if not self.tag.has_key('myName'):
- print "Error parsing XML structure: Tag name missing!"
+ print("Error parsing XML structure: Tag name missing!")
sys.exit(1)
# find number of attributes (disregarding special keys)
nAttr = self.nbAttributes()
endmark = '/>'
if self.hasSubtag():
endmark = '>'
- # print start tag, with attributes if present
+ # print(start tag, with attributes if present)
if nAttr > 0:
file.write(self._tab * depth + "<" + self.tag['myName'] + " " + \
' '.join([name + '="' + str(val) + '"' for name, val in self.tag.iteritems() \
if name != 'myName' and name != 'Icontain']) + endmark + '\n')
else:
file.write(self._tab * depth + "<" + self.tag['myName'] + ">\n")
- # print enclosed tags, if any
+ # print(enclosed tags, if any)
if self.hasSubtag():
for subtag in self.tag['Icontain']:
subtag.write(file, depth=depth + 1)
diff --git a/pybrain/rl/environments/ode/tools/xodetools.py b/pybrain/rl/environments/ode/tools/xodetools.py
index 9f482b1c5..bec109f1a 100644
--- a/pybrain/rl/environments/ode/tools/xodetools.py
+++ b/pybrain/rl/environments/ode/tools/xodetools.py
@@ -45,7 +45,7 @@ def _mass2dens(self, shape, size, mass):
elif shape == 'sphere':
return mass / (4.18879020 * pow(size[0], 3))
else:
- print "Unknown shape: " + shape + " not implemented!"
+ print("Unknown shape: " + shape + " not implemented!")
sys.exit(1)
def _dens2mass(self, shape, size, dens):
@@ -60,7 +60,7 @@ def _dens2mass(self, shape, size, dens):
elif shape == 'sphere':
return dens * (4.18879020 * pow(size[0], 3))
else:
- print "Unknown shape: " + shape + " not implemented!"
+ print("Unknown shape: " + shape + " not implemented!")
sys.exit(1)
@@ -88,7 +88,7 @@ def insertBody(self, bname, shape, size, density, pos=[0, 0, 0], passSet=None, e
elif shape == 'sphere':
dims = {'radius':size[0]}
else:
- print "Unknown shape: " + shape + " not implemented!"
+ print("Unknown shape: " + shape + " not implemented!")
sys.exit(1)
if mass is not None:
density = self._mass2dens(shape, size, mass)
@@ -140,7 +140,7 @@ def insertJoint(self, body1, body2, type, axis=None, anchor=(0, 0, 0), rel=False
self.insert('anchor', {'x':anchor[0], 'y':anchor[1], 'z':anchor[2], 'absolute':abs})
self.up()
else:
- print "Sorry, joint type " + type + " not yet implemented!"
+ print("Sorry, joint type " + type + " not yet implemented!")
sys.exit()
self.up(2)
return name
@@ -214,10 +214,10 @@ def merge(self, xodefile, joinLevel='space'):
is the one given."""
self.top()
if not self.downTo(joinLevel):
- print "Error: Cannot merge " + self.name + " at level " + joinLevel
+ print("Error: Cannot merge " + self.name + " at level " + joinLevel)
xodefile.top()
if not xodefile.downTo(joinLevel):
- print "Error: Cannot merge " + xodefile.name + " at level " + joinLevel
+ print("Error: Cannot merge " + xodefile.name + " at level " + joinLevel)
self.insertMulti(xodefile.getCurrentSubtags())
self._pass.update(xodefile.getPassList())
@@ -287,7 +287,7 @@ def writeXODE(self, filename=None):
f.write('\n')
self.writeCustomParameters(f)
f.close()
- print "Wrote " + filename + '.xode'
+ print("Wrote " + filename + '.xode')
class XODEfinger(XODEfile):
@@ -641,7 +641,7 @@ def __init__(self, name, **kwargs):
self.insertJoint('plate', 'leg3', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-5.5, 0.0, -10.5))
self.insertBody('leg4', 'box', [0.5, 8.0, 0.5], 30, pos=[-19.5, -4.0, -10.5], passSet=['table'], mass=0.3, color=(0.6, 0.8, 0.8, 0.8))
self.insertJoint('plate', 'leg4', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-19.5, 0.0, -10.5))
-
+
class XODELSRGlas(XODELSRTable): #XODESLR
def __init__(self, name, **kwargs):
XODELSRTable.__init__(self, name, **kwargs)
@@ -651,41 +651,42 @@ def __init__(self, name, **kwargs):
self.insertBody('objectP02', 'box', [0.45, 0.02, 0.45], 30, pos=[-6.5, 2.01, -11.0], passSet=['object'], mass=0.01)
self.insertJoint('objectP00', 'objectP01', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-6.5, 1.01, -11.0))
self.insertJoint('objectP00', 'objectP02', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-6.5, 2.01, -11.0))
-
+
class XODELSRPlate(XODELSRTable): #XODESLR
def __init__(self, name, **kwargs):
XODELSRTable.__init__(self, name, **kwargs)
- # create plate
- # plate ground
- bX = 1.0 #width of plate floor
- bY = 0.05 #height of plate floor
- bZ = 1.0 #depth of plate floor
- #plate sides
- sX = 0.5 #width of plate side
- sY = bY #height of plate side
- sZ = 1.0 #depth of plate side
- #position of plate
- pX = -6.5
- pY = 1.02
- pZ = -11.0
- #stuff
- m = 0.05 #mass per part
- c = (0.6, 0.6, 0.8, 0.95) #color of object
- dif = sX / (2.0 * sqrt(5)) #
-
+ # create plate
+ # plate ground
+ bX = 1.0 #width of plate floor
+ bY = 0.05 #height of plate floor
+ bZ = 1.0 #depth of plate floor
+ #plate sides
+ sX = 0.5 #width of plate side
+ sY = bY #height of plate side
+ sZ = 1.0 #depth of plate side
+ #position of plate
+ pX = -6.5
+ pY = 1.02
+ pZ = -11.0
+ #stuff
+ m = 0.05 #mass per part
+ c = (0.6, 0.6, 0.8, 0.95) #color of object
+ dif = sX / (2.0 * sqrt(5)) #
+
self.insertBody('objectP00', 'box', [bX, bY, bZ], 30, pos=[pX, pY, pZ], passSet=['object'], mass=m, color=c)
- self.insertBody('objectP01', 'box', [sX, sY, sZ], 30, pos=[pX - bX * 0.5 - 2.0 * dif, pY + dif, pZ], passSet=['object'], mass=m, euler=[0, 0, 22.5], color=c)
+ self.insertBody('objectP01', 'box', [sX, sY, sZ], 30, pos=[pX - bX * 0.5 - 2.0 * dif, pY + dif, pZ], passSet=['object'], mass=m, euler=[0, 0, 22.5], color=c)
self.insertJoint('objectP00', 'objectP01', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(pX - bX * 0.5, pY, pZ))
-
- self.insertBody('objectP02', 'box', [sX, sY, sZ], 30, pos=[pX + bX * 0.5 + 2.0 * dif, pY + dif, pZ], passSet=['object'], mass=m, euler=[0, 0, -22.5], color=c)
+
+ self.insertBody('objectP02', 'box', [sX, sY, sZ], 30, pos=[pX + bX * 0.5 + 2.0 * dif, pY + dif, pZ], passSet=['object'], mass=m, euler=[0, 0, -22.5], color=c)
self.insertJoint('objectP00', 'objectP02', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(pX + bX * 0.5, pY, pZ))
-
- self.insertBody('objectP03', 'box', [sX, sY, sZ], 30, pos=[pX, pY + dif, pZ + bZ * 0.5 + 2.0 * dif], passSet=['object'], mass=m, euler=[0, 90, -22.5], color=c)
+
+ self.insertBody('objectP03', 'box', [sX, sY, sZ], 30, pos=[pX, pY + dif, pZ + bZ * 0.5 + 2.0 * dif], passSet=['object'], mass=m, euler=[0, 90, -22.5], color=c)
self.insertJoint('objectP00', 'objectP03', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(pX, pY, pZ + bZ * 0.5))
-
- self.insertBody('objectP04', 'box', [sX, sY, sZ], 30, pos=[pX, pY + dif, pZ - bZ * 0.5 - 2.0 * dif], passSet=['object'], mass=m, euler=[0, 90, 22.5], color=c)
+
+ self.insertBody('objectP04', 'box', [sX, sY, sZ], 30, pos=[pX, pY + dif, pZ - bZ * 0.5 - 2.0 * dif], passSet=['object'], mass=m, euler=[0, 90, 22.5], color=c)
self.insertJoint('objectP00', 'objectP04', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(pX, pY, pZ - bZ * 0.5))
-
if __name__ == '__main__' :
+
+if __name__ == '__main__' :
table = XODELSRPlate('../models/ccrlPlate')
diff --git a/pybrain/rl/environments/ode/viewer.py b/pybrain/rl/environments/ode/viewer.py
index 0d2d0a563..4a958f782 100644
--- a/pybrain/rl/environments/ode/viewer.py
+++ b/pybrain/rl/environments/ode/viewer.py
@@ -302,7 +302,7 @@ def _keyfunc (self, c, x, y):
""" keyboard call-back function. """
if c == 's':
self.setCaptureScreen(not self.getCaptureScreen())
- print "Screen Capture: " + (self.getCaptureScreen() and "on" or "off")
+ print("Screen Capture: " + (self.getCaptureScreen() and "on" or "off"))
if c in ['x', 'q']:
sys.exit()
if c == 'v':
@@ -360,7 +360,7 @@ def _screenshot(self, path_prefix='.', format='PNG'):
image = Image.fromstring("RGB", (self.width, self.height), data)
image = image.transpose(Image.FLIP_TOP_BOTTOM)
image.save(path, format)
- print 'Image saved to %s' % (os.path.basename(path))
+ print('Image saved to %s' % (os.path.basename(path)))
else:
self.counter += 1
diff --git a/pybrain/rl/environments/shipsteer/__init__.py b/pybrain/rl/environments/shipsteer/__init__.py
index 5e837633b..d3ee06276 100644
--- a/pybrain/rl/environments/shipsteer/__init__.py
+++ b/pybrain/rl/environments/shipsteer/__init__.py
@@ -1,2 +1,2 @@
-from shipsteer import ShipSteeringEnvironment
-from northwardtask import GoNorthwardTask
\ No newline at end of file
+from pybrain.rl.environments.shipsteer.shipsteer import ShipSteeringEnvironment
+from pybrain.rl.environments.shipsteer.northwardtask import GoNorthwardTask
\ No newline at end of file
diff --git a/pybrain/rl/environments/shipsteer/viewer.py b/pybrain/rl/environments/shipsteer/viewer.py
index 9b33510d8..070315269 100644
--- a/pybrain/rl/environments/shipsteer/viewer.py
+++ b/pybrain/rl/environments/shipsteer/viewer.py
@@ -73,7 +73,7 @@ def saveTo(self, filename, format="JPEG"):
image = Image.fromstring("RGB", (width, height), data)
image = image.transpose(Image.FLIP_TOP_BOTTOM)
image.save(filename, format)
- print 'Saved image to ', filename
+ print('Saved image to ', filename)
return image
# the render method containing the Glut mainloop
diff --git a/pybrain/rl/environments/simple/__init__.py b/pybrain/rl/environments/simple/__init__.py
index fc9c23f4b..b4d9bcf84 100644
--- a/pybrain/rl/environments/simple/__init__.py
+++ b/pybrain/rl/environments/simple/__init__.py
@@ -1,2 +1,2 @@
-from environment import SimpleEnvironment
-from tasks import MinimizeTask
\ No newline at end of file
+from pybrain.rl.environments.simple.environment import SimpleEnvironment
+from pybrain.rl.environments.simple.tasks import MinimizeTask
\ No newline at end of file
diff --git a/pybrain/rl/environments/simple/tasks.py b/pybrain/rl/environments/simple/tasks.py
index 8eb12edda..741988057 100644
--- a/pybrain/rl/environments/simple/tasks.py
+++ b/pybrain/rl/environments/simple/tasks.py
@@ -33,7 +33,7 @@ def performAction(self, action):
def getReward(self):
# sleep(0.01)
- # print self.state, self.action
+ # print(self.state, self.action)
reward = self.env.f([s + 0.1 * a for s, a in zip(self.state, self.action)])
return - sum(reward)
diff --git a/pybrain/rl/environments/simplerace/simplecontroller.py b/pybrain/rl/environments/simplerace/simplecontroller.py
index 99b40cd5e..8289d6703 100644
--- a/pybrain/rl/environments/simplerace/simplecontroller.py
+++ b/pybrain/rl/environments/simplerace/simplecontroller.py
@@ -26,6 +26,6 @@ def getAction(self):
steering = -1
else:
steering = 1
- print "speed", self.speed, "angle", self.angleToCurrentWP, "driving", driving, "steering", steering
+ print("speed", self.speed, "angle", self.angleToCurrentWP, "driving", driving, "steering", steering)
return array([driving, steering])
diff --git a/pybrain/rl/environments/simplerace/simpleracetcp.py b/pybrain/rl/environments/simplerace/simpleracetcp.py
index 731da95c0..615b495a3 100644
--- a/pybrain/rl/environments/simplerace/simpleracetcp.py
+++ b/pybrain/rl/environments/simplerace/simpleracetcp.py
@@ -20,7 +20,7 @@ def __init__(self, host="127.0.0.1", port=6524):
self.theSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.theSocket.connect((host, port))
self.step = 0
- print "Connected to a simplerace server"
+ print("Connected to a simplerace server")
self.reset()
self.serverIsReady = False
@@ -52,20 +52,20 @@ def performAction(self, action):
else:
command = 4
if self.waitOne:
- print 'Waiting one step'
+ print('Waiting one step')
self.waitOne = False
elif self.serverIsReady:
self.theSocket.send (str(command) + "\n")
else:
- print "not sending"
+ print("not sending")
# get and process the answer
data = ""
while len (data) < 2:
data = self.theSocket.recv(1000)
- #print "received", data
+ #print("received", data)
inputs = string.split(str(data), " ")
if (inputs[0][:5] == "reset"):
- print "Should we reset the scores here?"
+ print("Should we reset the scores here?")
self.reset ()
self.serverIsReady = True
self.waitOne = True
@@ -79,7 +79,7 @@ def performAction(self, action):
if (currentWp[0] != self.lastStepCurrentWp[0]):
# check that we don't have a server side change of episode
if (currentWp[0] != self.lastStepNextWp[0]):
- print "%.3f %.3f %.3f %.3f " % (currentWp[0], currentWp[1], self.lastStepNextWp[0], self.lastStepNextWp[1])
+ print("%.3f %.3f %.3f %.3f " % (currentWp[0], currentWp[1], self.lastStepNextWp[0], self.lastStepNextWp[1]))
raise Exception("Unexpected episode change")
else:
# all is fine, increase score. but for who?
@@ -93,9 +93,9 @@ def performAction(self, action):
self.lastStepCurrentWp = currentWp
self.step += 1
elif (len (inputs[0]) < 2):
- print "impossible!"
+ print("impossible!")
else:
- print "incomprehensible and thus roundly ignored", data
+ print("incomprehensible and thus roundly ignored", data)
def reset(self):
self.step = 0
diff --git a/pybrain/rl/environments/twoplayergames/__init__.py b/pybrain/rl/environments/twoplayergames/__init__.py
index 3bead57da..44a2c0157 100644
--- a/pybrain/rl/environments/twoplayergames/__init__.py
+++ b/pybrain/rl/environments/twoplayergames/__init__.py
@@ -1,3 +1,3 @@
-from capturegame import CaptureGame
-from gomoku import GomokuGame
-from tasks.__init__ import *
\ No newline at end of file
+from pybrain.rl.environments.twoplayergames.capturegame import CaptureGame
+from pybrain.rl.environments.twoplayergames.gomoku import GomokuGame
+from pybrain.rl.environments.twoplayergames.tasks.__init__ import *
\ No newline at end of file
diff --git a/pybrain/rl/environments/twoplayergames/capturegameplayers/__init__.py b/pybrain/rl/environments/twoplayergames/capturegameplayers/__init__.py
index 39d765d9d..50291fde1 100644
--- a/pybrain/rl/environments/twoplayergames/capturegameplayers/__init__.py
+++ b/pybrain/rl/environments/twoplayergames/capturegameplayers/__init__.py
@@ -1,5 +1,5 @@
-from randomplayer import RandomCapturePlayer
-from killing import KillingPlayer
-from nonsuicide import NonSuicidePlayer
-from moduledecision import ModuleDecidingPlayer
-from clientwrapper import ClientCapturePlayer
\ No newline at end of file
+from pybrain.rl.environments.twoplayergames.capturegameplayers.randomplayer import RandomCapturePlayer
+from pybrain.rl.environments.twoplayergames.capturegameplayers.killing import KillingPlayer
+from pybrain.rl.environments.twoplayergames.capturegameplayers.nonsuicide import NonSuicidePlayer
+from pybrain.rl.environments.twoplayergames.capturegameplayers.moduledecision import ModuleDecidingPlayer
+from pybrain.rl.environments.twoplayergames.capturegameplayers.clientwrapper import ClientCapturePlayer
\ No newline at end of file
diff --git a/pybrain/rl/environments/twoplayergames/capturegameplayers/clientwrapper.py b/pybrain/rl/environments/twoplayergames/capturegameplayers/clientwrapper.py
index 3ace1b3e3..c3801800d 100644
--- a/pybrain/rl/environments/twoplayergames/capturegameplayers/clientwrapper.py
+++ b/pybrain/rl/environments/twoplayergames/capturegameplayers/clientwrapper.py
@@ -24,14 +24,14 @@ def __init__(self, game, color=CaptureGame.BLACK, player='AtariGreedy', **args):
self.theSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.theSocket.connect((host, port))
if self.verbose:
- print "Connected to server"
+ print("Connected to server")
except:
- print 'Failed to connect'
+ print('Failed to connect')
#define player
self.theSocket.send(player + '-' + str(color) + '\n')
if self.verbose:
- print 'Sending:', player + '-' + str(color)
+ print('Sending:', player + '-' + str(color))
accept = ""
while len (accept) < 2:
accept = self.theSocket.recv(1000)
@@ -56,17 +56,17 @@ def getAction(self):
# get the suggested move from the java player:
if self.verbose:
- print 'Sending:', js
+ print('Sending:', js)
self.theSocket.send(js + '\n')
jr = ""
if self.verbose:
- print 'Waiting for server',
+ print('Waiting for server',)
while len (jr) < 2:
jr = self.theSocket.recv(1000)
if self.verbose:
- print '.',
+ print('.',)
if self.verbose:
- print " received.", jr
+ print(" received.", jr)
chosen = eval(jr)
assert self.game.isLegal(self.color, chosen)
diff --git a/pybrain/rl/environments/twoplayergames/capturegameplayers/moduledecision.py b/pybrain/rl/environments/twoplayergames/capturegameplayers/moduledecision.py
index 142072961..65b49e567 100644
--- a/pybrain/rl/environments/twoplayergames/capturegameplayers/moduledecision.py
+++ b/pybrain/rl/environments/twoplayergames/capturegameplayers/moduledecision.py
@@ -42,11 +42,11 @@ def newEpisode(self):
def _legalizeIt(self, a):
""" draw index from an array of values, filtering out illegal moves. """
if not min(a) >= 0:
- print a
- print min(a)
- print self.module.params
- print self.module.inputbuffer
- print self.module.outputbuffer
+ print(a)
+ print(min(a))
+ print(self.module.params)
+ print(self.module.inputbuffer)
+ print(self.module.outputbuffer)
raise Exception('Non-positive value in array?')
legals = self.game.getLegals(self.color)
vals = ones(len(a))*(-100)*(1+self.temperature)
diff --git a/pybrain/rl/environments/twoplayergames/gomokuplayers/__init__.py b/pybrain/rl/environments/twoplayergames/gomokuplayers/__init__.py
index 4edf57b8f..f2e0a2d40 100644
--- a/pybrain/rl/environments/twoplayergames/gomokuplayers/__init__.py
+++ b/pybrain/rl/environments/twoplayergames/gomokuplayers/__init__.py
@@ -1,3 +1,3 @@
-from randomplayer import RandomGomokuPlayer
-from killing import KillingGomokuPlayer
-from moduledecision import ModuleDecidingPlayer
\ No newline at end of file
+from pybrain.rl.environments.twoplayergames.gomokuplayers.randomplayer import RandomGomokuPlayer
+from pybrain.rl.environments.twoplayergames.gomokuplayers.killing import KillingGomokuPlayer
+from pybrain.rl.environments.twoplayergames.gomokuplayers.moduledecision import ModuleDecidingPlayer
\ No newline at end of file
diff --git a/pybrain/rl/environments/twoplayergames/gomokuplayers/moduledecision.py b/pybrain/rl/environments/twoplayergames/gomokuplayers/moduledecision.py
index 2b979efa3..2859cb6ed 100644
--- a/pybrain/rl/environments/twoplayergames/gomokuplayers/moduledecision.py
+++ b/pybrain/rl/environments/twoplayergames/gomokuplayers/moduledecision.py
@@ -42,11 +42,11 @@ def newEpisode(self):
def _legalizeIt(self, a):
""" draw index from an array of values, filtering out illegal moves. """
if not min(a) >= 0:
- print a
- print min(a)
- print self.module.params
- print self.module.inputbuffer
- print self.module.outputbuffer
+ print(a)
+ print(min(a))
+ print(self.module.params)
+ print(self.module.inputbuffer)
+ print(self.module.outputbuffer)
raise Exception('No positve value in array?')
legals = self.game.getLegals(self.color)
vals = ones(len(a))*(-100)*(1+self.temperature)
diff --git a/pybrain/rl/environments/twoplayergames/tasks/__init__.py b/pybrain/rl/environments/twoplayergames/tasks/__init__.py
index 4d25d6fe5..83921110a 100644
--- a/pybrain/rl/environments/twoplayergames/tasks/__init__.py
+++ b/pybrain/rl/environments/twoplayergames/tasks/__init__.py
@@ -1,6 +1,6 @@
-from capturetask import CaptureGameTask
-from handicaptask import HandicapCaptureTask
-from relativetask import RelativeCaptureTask
-from gomokutask import GomokuTask
-from relativegomokutask import RelativeGomokuTask
-from pentetask import PenteTask
+from pybrain.rl.environments.twoplayergames.tasks.capturetask import CaptureGameTask
+from pybrain.rl.environments.twoplayergames.tasks.handicaptask import HandicapCaptureTask
+from pybrain.rl.environments.twoplayergames.tasks.relativetask import RelativeCaptureTask
+from pybrain.rl.environments.twoplayergames.tasks.gomokutask import GomokuTask
+from pybrain.rl.environments.twoplayergames.tasks.relativegomokutask import RelativeGomokuTask
+from pybrain.rl.environments.twoplayergames.tasks.pentetask import PenteTask
diff --git a/pybrain/rl/environments/twoplayergames/tasks/handicaptask.py b/pybrain/rl/environments/twoplayergames/tasks/handicaptask.py
index 287922fcf..3e411a866 100644
--- a/pybrain/rl/environments/twoplayergames/tasks/handicaptask.py
+++ b/pybrain/rl/environments/twoplayergames/tasks/handicaptask.py
@@ -120,8 +120,8 @@ def f(self, player):
h = HandicapCaptureTask(4, opponentStart=False)
p1 = RandomCapturePlayer(h.env)
p1 = KillingPlayer(h.env)
- print h(p1)
- print h.results
- print h.winProp(0)
- print h.winProp(1)
+ print(h(p1))
+ print(h.results)
+ print(h.winProp(0))
+ print(h.winProp(1))
diff --git a/pybrain/rl/environments/twoplayergames/tasks/relativegomokutask.py b/pybrain/rl/environments/twoplayergames/tasks/relativegomokutask.py
index d34c02d36..97d342181 100644
--- a/pybrain/rl/environments/twoplayergames/tasks/relativegomokutask.py
+++ b/pybrain/rl/environments/twoplayergames/tasks/relativegomokutask.py
@@ -107,7 +107,7 @@ def _oneGame(self, preset = None):
moves = self.env.movesDone
win = self.env.winner == self.player.color
if self.verbose:
- print 'Preset:', preset, 'T:', self.temp, 'Win:', win, 'after', moves, 'moves.'
+ print('Preset:', preset, 'T:', self.temp, 'Win:', win, 'after', moves, 'moves.')
res = 1 - self.numMovesCoeff * (moves -self.minmoves)/(self.maxmoves-self.minmoves)
if win:
return res
@@ -120,11 +120,11 @@ def _oneGame(self, preset = None):
net1 = CaptureGameNetwork(hsize = 1)
net2 = CaptureGameNetwork(hsize = 1)
r = RelativeGomokuTask(7, maxGames = 10, useNetworks = True)
- print r(net1, net2)
- print r(net2, net1)
- print r.env
+ print(r(net1, net2))
+ print(r(net2, net1))
+ print(r.env)
r.maxGames = 50
- print r(net1, net2)
- print r(net2, net1)
- print r.env
+ print(r(net1, net2))
+ print(r(net2, net1))
+ print(r.env)
diff --git a/pybrain/rl/environments/twoplayergames/tasks/relativetask.py b/pybrain/rl/environments/twoplayergames/tasks/relativetask.py
index d7ae6cfb2..9e3eedea2 100644
--- a/pybrain/rl/environments/twoplayergames/tasks/relativetask.py
+++ b/pybrain/rl/environments/twoplayergames/tasks/relativetask.py
@@ -136,7 +136,7 @@ def _oneGame(self, preset=None):
moves = self.env.movesDone
win = self.env.winner == self.player.color
if self.verbose:
- print 'Preset:', preset, 'T:', self.temp, 'Win:', win, 'after', moves, 'moves.'
+ print('Preset:', preset, 'T:', self.temp, 'Win:', win, 'after', moves, 'moves.')
res = 1 - self.numMovesCoeff * (moves - self.minmoves) / (self.maxmoves - self.minmoves)
if win:
return res
@@ -150,17 +150,17 @@ def _oneGame(self, preset=None):
net1 = CaptureGameNetwork(hsize=1)
net2 = CaptureGameNetwork(hsize=1)
- #print net1.params
- #print net2.params
+ #print(net1.params)
+ #print(net2.params)
r = RelativeCaptureTask(5, maxGames=40, useNetworks=True,
presetGamesProportion=0.5)
- print r(net1, net2)
- print r(net2, net1)
+ print(r(net1, net2))
+ print(r(net2, net1))
r.maxGames = 200
- print r(net1, net2)
- print r(net2, net1)
+ print(r(net1, net2))
+ print(r(net2, net1))
diff --git a/pybrain/rl/experiments/__init__.py b/pybrain/rl/experiments/__init__.py
index 29b1415cb..c9d315ac2 100644
--- a/pybrain/rl/experiments/__init__.py
+++ b/pybrain/rl/experiments/__init__.py
@@ -1,3 +1,3 @@
-from experiment import Experiment
-from episodic import EpisodicExperiment
-from continuous import ContinuousExperiment
\ No newline at end of file
+from pybrain.rl.experiments.experiment import Experiment
+from pybrain.rl.experiments.episodic import EpisodicExperiment
+from pybrain.rl.experiments.continuous import ContinuousExperiment
\ No newline at end of file
diff --git a/pybrain/rl/experiments/continuous.py b/pybrain/rl/experiments/continuous.py
index 7f98ee38e..86a36c251 100644
--- a/pybrain/rl/experiments/continuous.py
+++ b/pybrain/rl/experiments/continuous.py
@@ -1,6 +1,6 @@
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
-from experiment import Experiment
+from pybrain.rl.experiments.experiment import Experiment
class ContinuousExperiment(Experiment):
diff --git a/pybrain/rl/explorers/__init__.py b/pybrain/rl/explorers/__init__.py
index 623a75ff9..3850f34e3 100644
--- a/pybrain/rl/explorers/__init__.py
+++ b/pybrain/rl/explorers/__init__.py
@@ -1,2 +1,2 @@
-from discrete.__init__ import *
-from continuous.__init__ import *
\ No newline at end of file
+from pybrain.rl.explorers.discrete.__init__ import *
+from pybrain.rl.explorers.continuous.__init__ import *
\ No newline at end of file
diff --git a/pybrain/rl/explorers/continuous/__init__.py b/pybrain/rl/explorers/continuous/__init__.py
index 9a602c275..4c675e6ee 100644
--- a/pybrain/rl/explorers/continuous/__init__.py
+++ b/pybrain/rl/explorers/continuous/__init__.py
@@ -1 +1 @@
-from normal import NormalExplorer
\ No newline at end of file
+from pybrain.rl.explorers.continuous.normal import NormalExplorer
\ No newline at end of file
diff --git a/pybrain/rl/explorers/discrete/__init__.py b/pybrain/rl/explorers/discrete/__init__.py
index e27fe738d..4e3d4c472 100644
--- a/pybrain/rl/explorers/discrete/__init__.py
+++ b/pybrain/rl/explorers/discrete/__init__.py
@@ -1,3 +1,3 @@
-from boltzmann import BoltzmannExplorer
-from egreedy import EpsilonGreedyExplorer
-from discretesde import DiscreteStateDependentExplorer
\ No newline at end of file
+from pybrain.rl.explorers.discrete.boltzmann import BoltzmannExplorer
+from pybrain.rl.explorers.discrete.egreedy import EpsilonGreedyExplorer
+from pybrain.rl.explorers.discrete.discretesde import DiscreteStateDependentExplorer
\ No newline at end of file
diff --git a/pybrain/rl/learners/__init__.py b/pybrain/rl/learners/__init__.py
index 4c4f3b0cc..9884242c9 100644
--- a/pybrain/rl/learners/__init__.py
+++ b/pybrain/rl/learners/__init__.py
@@ -1,3 +1,3 @@
-from directsearch.__init__ import *
-from valuebased.__init__ import *
-from modelbased.__init__ import *
+from pybrain.rl.learners.directsearch.__init__ import *
+from pybrain.rl.learners.valuebased.__init__ import *
+from pybrain.rl.learners.modelbased.__init__ import *
diff --git a/pybrain/rl/learners/directsearch/__init__.py b/pybrain/rl/learners/directsearch/__init__.py
index aedb6fa99..39d6e0cad 100644
--- a/pybrain/rl/learners/directsearch/__init__.py
+++ b/pybrain/rl/learners/directsearch/__init__.py
@@ -1,6 +1,6 @@
-from rwr import RWR
-from enac import ENAC
-from reinforce import Reinforce
+from pybrain.rl.learners.directsearch.rwr import RWR
+from pybrain.rl.learners.directsearch.enac import ENAC
+from pybrain.rl.learners.directsearch.reinforce import Reinforce
# TODO: also black-box optimizers -- but this leads to circular imports...
# from pybrain.optimization.__init__ import *
diff --git a/pybrain/rl/learners/directsearch/enac.py b/pybrain/rl/learners/directsearch/enac.py
index d5a3d3cd2..fcab0c29a 100644
--- a/pybrain/rl/learners/directsearch/enac.py
+++ b/pybrain/rl/learners/directsearch/enac.py
@@ -20,7 +20,7 @@ def calculateGradient(self):
X = ones((self.dataset.getNumSequences(), self.loglh.getDimension('loglh') + 1), float)
# collect sufficient statistics
- print self.dataset.getNumSequences()
+ print(self.dataset.getNumSequences())
for n in range(self.dataset.getNumSequences()):
_state, _action, reward = self.dataset.getSequence(n)
seqidx = ravel(self.dataset['sequence_index'])
diff --git a/pybrain/rl/learners/directsearch/gpomdp.py b/pybrain/rl/learners/directsearch/gpomdp.py
index ce5ae1705..6316bab6f 100644
--- a/pybrain/rl/learners/directsearch/gpomdp.py
+++ b/pybrain/rl/learners/directsearch/gpomdp.py
@@ -29,7 +29,7 @@ def calculateGradient(self):
seqcount[t, :] += 1
baselines = baselines / seqcount
- # print baselines
+ # print(baselines)
for seq in range(self.ds.getNumSequences()):
_, _, rewards, loglhs = self.ds.getSequence(seq)
for t in range(len(rewards)):
diff --git a/pybrain/rl/learners/directsearch/rwr.py b/pybrain/rl/learners/directsearch/rwr.py
index 8ce4a8532..1445d13a8 100644
--- a/pybrain/rl/learners/directsearch/rwr.py
+++ b/pybrain/rl/learners/directsearch/rwr.py
@@ -114,7 +114,7 @@ def learn(self, batches):
for b in range(batches):
if self.verbose:
print
- print 'Batch', b + 1
+ print('Batch', b + 1)
self.reset()
self.learnOneBatch()
self.totalEpisodes += self.batchSize
@@ -126,7 +126,7 @@ def learn(self, batches):
rws += (sum(tmp) / float(len(tmp)))
self.greedyAvg.append(rws / self.greedyRuns)
if self.verbose:
- print '::', round(rws / self.greedyRuns, 5), '::'
+ print('::', round(rws / self.greedyRuns, 5), '::')
def learnOneBatch(self):
# collect a batch of runs as experience
@@ -172,9 +172,9 @@ def learnOneBatch(self):
avgR0 = mean(r0s)
avgReward /= self.batchSize
if self.verbose:
- print '***', round(avgLen, 3), '***', '(avg init exp. return:', round(avgR0, 5), ')',
- print 'avg reward', round(avgReward, 5), '(tau:', round(self.tau, 3), ')'
- print lens
+ print('***', round(avgLen, 3), '***', '(avg init exp. return:', round(avgR0, 5), ')',)
+ print('avg reward', round(avgReward, 5), '(tau:', round(self.tau, 3), ')')
+ print(lens)
# storage:
self.rewardAvg.append(avgReward)
self.lengthAvg.append(avgLen)
@@ -199,9 +199,9 @@ def learnOneBatch(self):
#
#
# # train the value estimating network
-# if self.verbose: print 'Old value error: ', self.vbp.testOnData()
+# if self.verbose: print('Old value error: ', self.vbp.testOnData())
# self.vbp.trainEpochs(self.valueTrainEpochs)
-# if self.verbose: print 'New value error: ', self.vbp.testOnData()
+# if self.verbose: print('New value error: ', self.vbp.testOnData())
#
# # produce the values and analyze
# rminusvs = []
diff --git a/pybrain/rl/learners/modelbased/__init__.py b/pybrain/rl/learners/modelbased/__init__.py
index 94b67c6a0..223e54a3a 100644
--- a/pybrain/rl/learners/modelbased/__init__.py
+++ b/pybrain/rl/learners/modelbased/__init__.py
@@ -1,2 +1,2 @@
-from policyiteration import trueValues, trueQValues, policyIteration
-from leastsquares import LSTD_values, LSTD_Qvalues, LSPI_policy, LSTD_PI_policy
\ No newline at end of file
+from pybrain.rl.learners.modelbased.policyiteration import trueValues, trueQValues, policyIteration
+from pybrain.rl.learners.modelbased.leastsquares import LSTD_values, LSTD_Qvalues, LSPI_policy, LSTD_PI_policy
\ No newline at end of file
diff --git a/pybrain/rl/learners/modelbased/leastsquares.py b/pybrain/rl/learners/modelbased/leastsquares.py
index 1715d57c7..9600f3bf4 100644
--- a/pybrain/rl/learners/modelbased/leastsquares.py
+++ b/pybrain/rl/learners/modelbased/leastsquares.py
@@ -20,7 +20,7 @@
from numpy.matlib import repmat
-from policyiteration import randomPolicy, greedyQPolicy, collapsedTransitions, policyIteration
+from pybrain.rl.learners.modelbased.policyiteration import randomPolicy, greedyQPolicy, collapsedTransitions, policyIteration
def trueFeatureStats(T, R, fMap, discountFactor, stateProp=1, MAT_LIMIT=1e8):
diff --git a/pybrain/rl/learners/valuebased/__init__.py b/pybrain/rl/learners/valuebased/__init__.py
index 3cecd5f92..92b47f46e 100644
--- a/pybrain/rl/learners/valuebased/__init__.py
+++ b/pybrain/rl/learners/valuebased/__init__.py
@@ -1,5 +1,5 @@
-from interface import ActionValueTable, ActionValueNetwork
-from nfq import NFQ
-from q import Q
-from qlambda import QLambda
-from sarsa import SARSA
\ No newline at end of file
+from pybrain.rl.learners.valuebased.interface import ActionValueTable, ActionValueNetwork
+from pybrain.rl.learners.valuebased.nfq import NFQ
+from pybrain.rl.learners.valuebased.q import Q
+from pybrain.rl.learners.valuebased.qlambda import QLambda
+from pybrain.rl.learners.valuebased.sarsa import SARSA
\ No newline at end of file
diff --git a/pybrain/rl/learners/valuebased/linearfa.py b/pybrain/rl/learners/valuebased/linearfa.py
index 6c0a4dac1..377856958 100644
--- a/pybrain/rl/learners/valuebased/linearfa.py
+++ b/pybrain/rl/learners/valuebased/linearfa.py
@@ -100,9 +100,9 @@ class Q_LinFA(LinearFALearner):
def _updateWeights(self, state, action, reward, next_state):
""" state and next_state are vectors, action is an integer. """
td_error = reward + self.rewardDiscount * max(dot(self._theta, next_state)) - dot(self._theta[action], state)
- #print action, reward, td_error,self._theta[action], state, dot(self._theta[action], state)
- #print self.learningRate * td_error * state
- #print
+ #print(action, reward, td_error,self._theta[action], state, dot(self._theta[action], state))
+ #print(self.learningRate * td_error * state)
+ #print()
self._theta[action] += self.learningRate * td_error * state
@@ -262,9 +262,9 @@ def testSingleStateFullDiscounted(self):
gamma=0, lr=0.25)
if self.verbose:
for x, l in r:
- print x
+ print(x)
for a in l:
- print fListToString(a[0], 2)
+ print(fListToString(a[0], 2) )
for _, l in r:
self.assertAlmostEquals(min(l[0][0]), 1, places=0)
self.assertAlmostEquals(max(l[0][0]), 1, places=0)
@@ -279,9 +279,9 @@ def testSingleState(self):
lr=0.2, _lambda=0.5, gamma=0.5)
if self.verbose:
for x, l in r:
- print x
+ print(x)
for a in l:
- print fListToString(a[0], 2)
+ print(fListToString(a[0], 2) )
for _, l in r:
self.assertAlmostEquals(min(l[0][0]), max(l[0][0]), places=0)
self.assertAlmostEquals(min(l[1][0]), max(l[1][0]), places=0)
@@ -293,9 +293,9 @@ def testSingleAction(self):
num_interactions=1000, lr=0.1, _lambda=0.5, gamma=0.5)
if self.verbose:
for x, l in r:
- print x
+ print(x)
for a in l:
- print fListToString(a, 2)
+ print(fListToString(a, 2) )
for _, l in r:
self.assertAlmostEquals(min(l[0]), max(l[0]), places=0)
self.assertAlmostEquals(min(l[1]), max(l[1]), places=0)
@@ -307,9 +307,9 @@ def testSimple(self):
lr=0.1, _lambda=0.5, gamma=0.5)
if self.verbose:
for x, l in r:
- print x
+ print(x)
for a in l:
- print fListToString(a[0], 2)
+ print(fListToString(a[0], 2) )
for _, l in r:
self.assertAlmostEquals(min(l[0][0]), max(l[0][0]), places=0)
self.assertAlmostEquals(min(l[1][0]), max(l[1][0]), places=0)
diff --git a/pybrain/structure/__init__.py b/pybrain/structure/__init__.py
index 51f42d064..39d961311 100644
--- a/pybrain/structure/__init__.py
+++ b/pybrain/structure/__init__.py
@@ -1,4 +1,4 @@
-from connections.__init__ import *
-from modules.__init__ import *
-from networks.__init__ import *
-from modulemesh import ModuleMesh
\ No newline at end of file
+from pybrain.structure.connections.__init__ import *
+from pybrain.structure.modules.__init__ import *
+from pybrain.structure.networks.__init__ import *
+from pybrain.structure.modulemesh import ModuleMesh
\ No newline at end of file
diff --git a/pybrain/structure/connections/__init__.py b/pybrain/structure/connections/__init__.py
index 57372343f..d7acf5b57 100644
--- a/pybrain/structure/connections/__init__.py
+++ b/pybrain/structure/connections/__init__.py
@@ -1,5 +1,5 @@
-from full import FullConnection
-from identity import IdentityConnection
-from shared import SharedFullConnection, MotherConnection, SharedConnection
-from linear import LinearConnection
-from fullnotself import FullNotSelfConnection
\ No newline at end of file
+from pybrain.structure.connections.full import FullConnection
+from pybrain.structure.connections.identity import IdentityConnection
+from pybrain.structure.connections.shared import SharedFullConnection, MotherConnection, SharedConnection
+from pybrain.structure.connections.linear import LinearConnection
+from pybrain.structure.connections.fullnotself import FullNotSelfConnection
\ No newline at end of file
diff --git a/pybrain/structure/connections/full.py b/pybrain/structure/connections/full.py
index 755212f58..4ee38112f 100644
--- a/pybrain/structure/connections/full.py
+++ b/pybrain/structure/connections/full.py
@@ -2,7 +2,7 @@
from scipy import reshape, dot, outer
-from connection import Connection
+from pybrain.structure.connections.connection import Connection
from pybrain.structure.parametercontainer import ParameterContainer
diff --git a/pybrain/structure/connections/identity.py b/pybrain/structure/connections/identity.py
index 9323e5e2a..f6f74f505 100644
--- a/pybrain/structure/connections/identity.py
+++ b/pybrain/structure/connections/identity.py
@@ -1,6 +1,6 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from connection import Connection
+from pybrain.structure.connections.connection import Connection
class IdentityConnection(Connection):
diff --git a/pybrain/structure/connections/shared.py b/pybrain/structure/connections/shared.py
index 07939641c..6605954d2 100644
--- a/pybrain/structure/connections/shared.py
+++ b/pybrain/structure/connections/shared.py
@@ -1,9 +1,9 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
from pybrain.structure.parametercontainer import ParameterContainer
-from connection import Connection
-from full import FullConnection
-from subsampling import SubsamplingConnection
+from pybrain.structure.connections.connection import Connection
+from pybrain.structure.connections.full import FullConnection
+from pybrain.structure.connections.subsampling import SubsamplingConnection
class OwnershipViolation(Exception):
diff --git a/pybrain/structure/connections/subsampling.py b/pybrain/structure/connections/subsampling.py
index b4f8ba7f0..9929c46e4 100644
--- a/pybrain/structure/connections/subsampling.py
+++ b/pybrain/structure/connections/subsampling.py
@@ -1,6 +1,6 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from connection import Connection
+from pybrain.structure.connections.connection import Connection
from pybrain.structure.parametercontainer import ParameterContainer
from scipy import average
diff --git a/pybrain/structure/evolvables/maskedmodule.py b/pybrain/structure/evolvables/maskedmodule.py
index 90d641beb..0f3e7035a 100644
--- a/pybrain/structure/evolvables/maskedmodule.py
+++ b/pybrain/structure/evolvables/maskedmodule.py
@@ -1,6 +1,6 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from maskedparameters import MaskedParameters
+from pybrain.structure.evolvables.maskedparameters import MaskedParameters
from pybrain.structure.modules.module import Module
diff --git a/pybrain/structure/evolvables/maskedparameters.py b/pybrain/structure/evolvables/maskedparameters.py
index d135c7bad..caf0020aa 100644
--- a/pybrain/structure/evolvables/maskedparameters.py
+++ b/pybrain/structure/evolvables/maskedparameters.py
@@ -3,7 +3,7 @@
from scipy import zeros, randn
from random import random, sample, gauss
-from topology import TopologyEvolvable
+from pybrain.structure.evolvables.topology import TopologyEvolvable
class MaskedParameters(TopologyEvolvable):
diff --git a/pybrain/structure/evolvables/topology.py b/pybrain/structure/evolvables/topology.py
index ed92a9772..e1e6016ff 100644
--- a/pybrain/structure/evolvables/topology.py
+++ b/pybrain/structure/evolvables/topology.py
@@ -1,7 +1,7 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
from pybrain.utilities import abstractMethod
-from evolvable import Evolvable
+from pybrain.structure.evolvables.evolvable import Evolvable
from pybrain.structure.parametercontainer import ParameterContainer
diff --git a/pybrain/structure/modulemesh.py b/pybrain/structure/modulemesh.py
index 2447d0bc8..70cc22043 100644
--- a/pybrain/structure/modulemesh.py
+++ b/pybrain/structure/modulemesh.py
@@ -1,7 +1,7 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
from pybrain.utilities import iterCombinations, Named
-from moduleslice import ModuleSlice
+from pybrain.structure.moduleslice import ModuleSlice
class ModuleMesh(Named):
diff --git a/pybrain/structure/modules/__init__.py b/pybrain/structure/modules/__init__.py
index 67663ff58..178d10ce2 100644
--- a/pybrain/structure/modules/__init__.py
+++ b/pybrain/structure/modules/__init__.py
@@ -1,15 +1,15 @@
-from biasunit import BiasUnit
-from gate import GateLayer, DoubleGateLayer, MultiplicationLayer, SwitchLayer
-from gaussianlayer import GaussianLayer
-from linearlayer import LinearLayer
-from lstm import LSTMLayer
-from mdlstm import MDLSTMLayer
-from mdrnnlayer import MdrnnLayer
-from sigmoidlayer import SigmoidLayer
-from softmax import SoftmaxLayer, PartialSoftmaxLayer
-from statedependentlayer import StateDependentLayer
-from tanhlayer import TanhLayer
-from kohonen import KohonenMap
-from table import Table
-from module import Module
-from relulayer import ReluLayer
+from pybrain.structure.modules.biasunit import BiasUnit
+from pybrain.structure.modules.gate import GateLayer, DoubleGateLayer, MultiplicationLayer, SwitchLayer
+from pybrain.structure.modules.gaussianlayer import GaussianLayer
+from pybrain.structure.modules.linearlayer import LinearLayer
+from pybrain.structure.modules.lstm import LSTMLayer
+from pybrain.structure.modules.mdlstm import MDLSTMLayer
+from pybrain.structure.modules.mdrnnlayer import MdrnnLayer
+from pybrain.structure.modules.sigmoidlayer import SigmoidLayer
+from pybrain.structure.modules.softmax import SoftmaxLayer, PartialSoftmaxLayer
+from pybrain.structure.modules.statedependentlayer import StateDependentLayer
+from pybrain.structure.modules.tanhlayer import TanhLayer
+from pybrain.structure.modules.kohonen import KohonenMap
+from pybrain.structure.modules.table import Table
+from pybrain.structure.modules.module import Module
+from pybrain.structure.modules.relulayer import ReluLayer
diff --git a/pybrain/structure/modules/biasunit.py b/pybrain/structure/modules/biasunit.py
index 971c77cf3..32378ff3d 100644
--- a/pybrain/structure/modules/biasunit.py
+++ b/pybrain/structure/modules/biasunit.py
@@ -1,7 +1,7 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from neuronlayer import NeuronLayer
-from module import Module
+from pybrain.structure.modules.neuronlayer import NeuronLayer
+from pybrain.structure.modules.module import Module
class BiasUnit(NeuronLayer):
diff --git a/pybrain/structure/modules/gaussianlayer.py b/pybrain/structure/modules/gaussianlayer.py
index 01fe2e7cc..499a4fa6a 100644
--- a/pybrain/structure/modules/gaussianlayer.py
+++ b/pybrain/structure/modules/gaussianlayer.py
@@ -1,7 +1,7 @@
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
from scipy import random
-from neuronlayer import NeuronLayer
+from pybrain.structure.modules.neuronlayer import NeuronLayer
from pybrain.tools.functions import expln, explnPrime
from pybrain.structure.parametercontainer import ParameterContainer
diff --git a/pybrain/structure/modules/kohonen.py b/pybrain/structure/modules/kohonen.py
index 88a671946..2fcc47788 100644
--- a/pybrain/structure/modules/kohonen.py
+++ b/pybrain/structure/modules/kohonen.py
@@ -4,7 +4,7 @@
from scipy.ndimage import minimum_position
from scipy import mgrid, zeros, tile, array, floor, sum
-from module import Module
+from pybrain.structure.modules.module import Module
class KohonenMap(Module):
diff --git a/pybrain/structure/modules/linearlayer.py b/pybrain/structure/modules/linearlayer.py
index 2aa2bd114..a2e8b4922 100644
--- a/pybrain/structure/modules/linearlayer.py
+++ b/pybrain/structure/modules/linearlayer.py
@@ -1,6 +1,6 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from neuronlayer import NeuronLayer
+from pybrain.structure.modules.neuronlayer import NeuronLayer
class LinearLayer(NeuronLayer):
diff --git a/pybrain/structure/modules/lstm.py b/pybrain/structure/modules/lstm.py
index 1d5f0792d..599d7e2b5 100644
--- a/pybrain/structure/modules/lstm.py
+++ b/pybrain/structure/modules/lstm.py
@@ -2,8 +2,8 @@
from scipy import tanh
-from neuronlayer import NeuronLayer
-from module import Module
+from pybrain.structure.modules.neuronlayer import NeuronLayer
+from pybrain.structure.modules.module import Module
from pybrain.structure.parametercontainer import ParameterContainer
from pybrain.tools.functions import sigmoid, sigmoidPrime, tanhPrime
diff --git a/pybrain/structure/modules/mdlstm.py b/pybrain/structure/modules/mdlstm.py
index 14acb7ddd..a3336b832 100644
--- a/pybrain/structure/modules/mdlstm.py
+++ b/pybrain/structure/modules/mdlstm.py
@@ -2,8 +2,8 @@
from scipy import zeros, tanh
-from neuronlayer import NeuronLayer
-from module import Module
+from pybrain.structure.modules.neuronlayer import NeuronLayer
+from pybrain.structure.modules.module import Module
from pybrain.structure.parametercontainer import ParameterContainer
from pybrain.tools.functions import sigmoid, sigmoidPrime, tanhPrime
from pybrain.structure.moduleslice import ModuleSlice
diff --git a/pybrain/structure/modules/neuronlayer.py b/pybrain/structure/modules/neuronlayer.py
index 85e2d9523..cdfc2aa82 100644
--- a/pybrain/structure/modules/neuronlayer.py
+++ b/pybrain/structure/modules/neuronlayer.py
@@ -1,6 +1,6 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from module import Module
+from pybrain.structure.modules.module import Module
class NeuronLayer(Module):
diff --git a/pybrain/structure/modules/relulayer.py b/pybrain/structure/modules/relulayer.py
index 53a3376e0..4304804c2 100644
--- a/pybrain/structure/modules/relulayer.py
+++ b/pybrain/structure/modules/relulayer.py
@@ -1,6 +1,6 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from neuronlayer import NeuronLayer
+from pybrain.structure.modules.neuronlayer import NeuronLayer
class ReluLayer(NeuronLayer):
""" Layer of rectified linear units (relu). """
diff --git a/pybrain/structure/modules/sigmoidlayer.py b/pybrain/structure/modules/sigmoidlayer.py
index 3c4e5e5df..4c3568e40 100644
--- a/pybrain/structure/modules/sigmoidlayer.py
+++ b/pybrain/structure/modules/sigmoidlayer.py
@@ -1,6 +1,6 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from neuronlayer import NeuronLayer
+from pybrain.structure.modules.neuronlayer import NeuronLayer
from pybrain.tools.functions import sigmoid
diff --git a/pybrain/structure/modules/softmax.py b/pybrain/structure/modules/softmax.py
index ef0650008..0d28f4f8c 100644
--- a/pybrain/structure/modules/softmax.py
+++ b/pybrain/structure/modules/softmax.py
@@ -3,7 +3,7 @@
import scipy
-from neuronlayer import NeuronLayer
+from pybrain.structure.modules.neuronlayer import NeuronLayer
from pybrain.tools.functions import safeExp
diff --git a/pybrain/structure/modules/softsign.py b/pybrain/structure/modules/softsign.py
index 4ccaeeba9..9a8f60788 100644
--- a/pybrain/structure/modules/softsign.py
+++ b/pybrain/structure/modules/softsign.py
@@ -1,6 +1,6 @@
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
-from neuronlayer import NeuronLayer
+from pybrain.structure.modules.neuronlayer import NeuronLayer
class SoftSignLayer(NeuronLayer):
""" softsign activation function as described in X. Glorot and Y.
diff --git a/pybrain/structure/modules/statedependentlayer.py b/pybrain/structure/modules/statedependentlayer.py
index 267edcf74..dada78e25 100644
--- a/pybrain/structure/modules/statedependentlayer.py
+++ b/pybrain/structure/modules/statedependentlayer.py
@@ -2,7 +2,7 @@
from scipy import random, asarray, zeros, dot
-from neuronlayer import NeuronLayer
+from pybrain.structure.modules.neuronlayer import NeuronLayer
from pybrain.tools.functions import expln, explnPrime
from pybrain.structure.parametercontainer import ParameterContainer
diff --git a/pybrain/structure/modules/tanhlayer.py b/pybrain/structure/modules/tanhlayer.py
index 594909123..69a44b006 100644
--- a/pybrain/structure/modules/tanhlayer.py
+++ b/pybrain/structure/modules/tanhlayer.py
@@ -2,7 +2,7 @@
from scipy import tanh
-from neuronlayer import NeuronLayer
+from pybrain.structure.modules.neuronlayer import NeuronLayer
class TanhLayer(NeuronLayer):
diff --git a/pybrain/structure/networks/__init__.py b/pybrain/structure/networks/__init__.py
index 0ae2dc2de..dd212dd4f 100644
--- a/pybrain/structure/networks/__init__.py
+++ b/pybrain/structure/networks/__init__.py
@@ -1,7 +1,7 @@
-from swiping import SwipingNetwork
-from borderswiping import BorderSwipingNetwork
-from neurondecomposable import NeuronDecomposableNetwork
-from feedforward import FeedForwardNetwork
-from recurrent import RecurrentNetwork
-from network import Network
-from bidirectional import BidirectionalNetwork
+from pybrain.structure.networks.swiping import SwipingNetwork
+from pybrain.structure.networks.borderswiping import BorderSwipingNetwork
+from pybrain.structure.networks.neurondecomposable import NeuronDecomposableNetwork
+from pybrain.structure.networks.feedforward import FeedForwardNetwork
+from pybrain.structure.networks.recurrent import RecurrentNetwork
+from pybrain.structure.networks.network import Network
+from pybrain.structure.networks.bidirectional import BidirectionalNetwork
diff --git a/pybrain/structure/networks/borderswiping.py b/pybrain/structure/networks/borderswiping.py
index ea19282da..c0c74b396 100644
--- a/pybrain/structure/networks/borderswiping.py
+++ b/pybrain/structure/networks/borderswiping.py
@@ -2,7 +2,7 @@
from scipy import zeros
-from swiping import SwipingNetwork
+from pybrain.structure.networks.swiping import SwipingNetwork
from pybrain.structure.modules import BiasUnit
from pybrain.structure.connections.shared import MotherConnection, SharedFullConnection
from pybrain.utilities import iterCombinations, tupleRemoveItem, reachable, decrementAny
diff --git a/pybrain/structure/networks/convolutional.py b/pybrain/structure/networks/convolutional.py
index b07816be9..e8f306fdb 100644
--- a/pybrain/structure/networks/convolutional.py
+++ b/pybrain/structure/networks/convolutional.py
@@ -65,18 +65,18 @@ def _buildStructure(self, inputdim, insize, inlayer, convSize, numFeatureMaps):
]
res = N.activate(ravel(array(input)))
res = res.reshape(4, 4)
- print N['pad'].inputbuffer[0].reshape(6, 6, 2)[:, :, 0]
- print res
+ print(N['pad'].inputbuffer[0].reshape(6, 6, 2)[:, :, 0])
+ print(res)
t = CaptureGameTask(4)
- print t(N)
+ print(t(N))
if False:
N = SimpleConvolutionalNetwork(4, 2, 5)
- print N
+ print(N)
res = N.activate(ravel(array(input)))
res = res.reshape(3, 3)
- print res
+ print(res)
diff --git a/pybrain/structure/networks/network.py b/pybrain/structure/networks/network.py
index 155c72d21..2f11dfddc 100644
--- a/pybrain/structure/networks/network.py
+++ b/pybrain/structure/networks/network.py
@@ -181,7 +181,7 @@ def _topologicalSort(self):
# Create a directed graph, including a counter of incoming connections.
graph = {}
for node in self.modules:
- if not graph.has_key(node):
+ if node not in graph:
# Zero incoming connections.
graph[node] = [0]
for c in chain(*self.connections.values()):
@@ -280,7 +280,7 @@ def convertToFastNetwork(self):
try:
from arac.pybrainbridge import _RecurrentNetwork, _FeedForwardNetwork #@UnresolvedImport
except:
- print "No fast networks available."
+ print("No fast networks available.")
return None
net = self.copy()
@@ -306,7 +306,7 @@ def convertToFastNetwork(self):
try:
cnet.sortModules()
except ValueError:
- print "Network cannot be converted."
+ print("Network cannot be converted.")
return None
cnet.owner = cnet
diff --git a/pybrain/structure/networks/swiping.py b/pybrain/structure/networks/swiping.py
index fd2f61ebd..eba829b27 100644
--- a/pybrain/structure/networks/swiping.py
+++ b/pybrain/structure/networks/swiping.py
@@ -124,12 +124,12 @@ def _printPredefined(self, dic=None, indent=0):
if dic == None:
dic = self.predefined
for k, val in sorted(dic.items()):
- print ' ' * indent, k,
+ print(' ' * indent, k,)
if isinstance(val, dict):
- print ':'
+ print(':')
self._printPredefined(val, indent + 2)
elif isinstance(val, MotherConnection):
- print val.params
+ print(val.params)
else:
- print val
+ print(val)
diff --git a/pybrain/supervised/__init__.py b/pybrain/supervised/__init__.py
index ab544630b..c9fa381cc 100644
--- a/pybrain/supervised/__init__.py
+++ b/pybrain/supervised/__init__.py
@@ -1 +1 @@
-from trainers.__init__ import *
\ No newline at end of file
+from pybrain.supervised.trainers.__init__ import *
\ No newline at end of file
diff --git a/pybrain/supervised/evolino/filter.py b/pybrain/supervised/evolino/filter.py
index a1ea9d98e..0a3bdeab8 100644
--- a/pybrain/supervised/evolino/filter.py
+++ b/pybrain/supervised/evolino/filter.py
@@ -1,8 +1,8 @@
__author__ = 'Michael Isik'
-from gfilter import Filter, SimpleMutation
-from variate import CauchyVariate
-from population import SimplePopulation
+from pybrain.supervised.evolino.gfilter import Filter, SimpleMutation
+from pybrain.supervised.evolino.variate import CauchyVariate
+from pybrain.supervised.evolino.population import SimplePopulation
from pybrain.tools.validation import Validator
from pybrain.tools.kwargsprocessor import KWArgsProcessor
@@ -114,7 +114,7 @@ def apply(self, population):
net.setGenome(individual.getGenome())
fitness = self._evaluateNet(net, dataset, self.wtRatio)
if self.verbosity > 1:
- print "Calculated fitness for individual", id(individual), " is ", fitness
+ print("Calculated fitness for individual", id(individual), " is ", fitness)
# set the individual fitness
population.setIndividualFitness(individual, fitness)
diff --git a/pybrain/supervised/evolino/gfilter.py b/pybrain/supervised/evolino/gfilter.py
index f6c338658..6f0995f6c 100644
--- a/pybrain/supervised/evolino/gfilter.py
+++ b/pybrain/supervised/evolino/gfilter.py
@@ -1,7 +1,7 @@
__author__ = 'Michael Isik'
-from variate import UniformVariate, GaussianVariate
+from pybrain.supervised.evolino.variate import UniformVariate, GaussianVariate
class Filter(object):
""" Base class for all kinds of operators on the population during the
@@ -92,7 +92,7 @@ def _manipulateValue(self, value):
"""
self.mutationVariate.x0 = value
newval = self.mutationVariate.getSample()
-# print "MUTATED: ", value, "--->", newval
+# print("MUTATED: ", value, "--->", newval)
return newval
diff --git a/pybrain/supervised/evolino/individual.py b/pybrain/supervised/evolino/individual.py
index e02806cfa..1942aab79 100644
--- a/pybrain/supervised/evolino/individual.py
+++ b/pybrain/supervised/evolino/individual.py
@@ -1,7 +1,7 @@
__author__ = 'Michael Isik'
-from gindividual import Individual
+from pybrain.supervised.evolino.gindividual import Individual
from copy import copy, deepcopy
diff --git a/pybrain/supervised/evolino/networkwrapper.py b/pybrain/supervised/evolino/networkwrapper.py
index a2bbb46cf..96c62ef11 100644
--- a/pybrain/supervised/evolino/networkwrapper.py
+++ b/pybrain/supervised/evolino/networkwrapper.py
@@ -56,7 +56,7 @@ def _washout(self, input, target, first_idx=None, last_idx=None):
full_inp = self._createFullInput(input[i], backprojection)
self._activateNetwork(full_inp)
raw_out = self._getRawOutput()
-# print "RAWOUT: ", full_inp, " --> ", raw_out, self._getLastOutput()
+# print("RAWOUT: ", full_inp, " --> ", raw_out, self._getLastOutput())
raw_outputs.append(array(raw_out))
self._setLastOutput(target[i])
@@ -68,7 +68,7 @@ def _activateNetwork(self, input):
assert len(input) == self._network.indim
output = self._network.activate(input)
self.offset = self._network.offset
-# print "INNNNNNN=", input, " OUTPP=", output
+# print("INNNNNNN=", input, " OUTPP=", output)
return output
def activate(self, input):
@@ -78,7 +78,7 @@ def activate(self, input):
backprojection *= self.backprojectionFactor
full_inp = self._createFullInput(input, backprojection)
out = self._activateNetwork(full_inp)
-# print "AAAAAACT: ", full_inp, "-->", out
+# print("AAAAAACT: ", full_inp, "-->", out)
# self._setLastOutput(last_out*5)
@@ -119,8 +119,8 @@ def calculateOutput(self, dataset, washout_calculation_ratio=(1, 2)):
# for i in xrange(washout_steps, len(input)):
for inp in input[washout_steps:]:
out = self.activate(inp)
-# print out
-# print inp
+# print(out)
+# print(inp)
inputs.append(inp)
outputs.append(out)
@@ -129,7 +129,7 @@ def calculateOutput(self, dataset, washout_calculation_ratio=(1, 2)):
collected_input = append(collected_input, inputs, axis=0)
else:
collected_input = array(inputs)
-# print collected_input; exit()
+# print(collected_input; exit())
if collected_output is not None:
collected_output = append(collected_output, outputs, axis=0)
@@ -500,7 +500,7 @@ def getLastHiddenLayer(self):
layers = []
for c in self.getConnections():
if c.outmod is outlayer:
-# print c.inmod
+# print(c.inmod)
layers.append(c.inmod)
assert len(layers) == 1
diff --git a/pybrain/supervised/evolino/population.py b/pybrain/supervised/evolino/population.py
index c3e3c5556..dc9a6c005 100644
--- a/pybrain/supervised/evolino/population.py
+++ b/pybrain/supervised/evolino/population.py
@@ -1,8 +1,8 @@
__author__ = 'Michael Isik'
-from gpopulation import Population, SimplePopulation
-from gfilter import Randomization
-from individual import EvolinoIndividual, EvolinoSubIndividual
+from pybrain.supervised.evolino.gpopulation import Population, SimplePopulation
+from pybrain.supervised.evolino.gfilter import Randomization
+from pybrain.supervised.evolino.individual import EvolinoIndividual, EvolinoSubIndividual
from pybrain.tools.kwargsprocessor import KWArgsProcessor
diff --git a/pybrain/supervised/knn/lsh/minhash.py b/pybrain/supervised/knn/lsh/minhash.py
index aa3491fa8..08458fa35 100644
--- a/pybrain/supervised/knn/lsh/minhash.py
+++ b/pybrain/supervised/knn/lsh/minhash.py
@@ -40,7 +40,7 @@ def jacardCoefficient(a, b):
1 or 0.
"""
if a.shape != b.shape:
- raise ValueError, "Arrays must be of same shape"
+ raise ValueError("Arrays must be of same shape")
length = a.shape[0]
a = a.astype(bool)
@@ -88,9 +88,9 @@ def _firstOne(self, arr):
def _checkItem(self, item):
if item.ndim != 1:
- raise ValueError, "Only one dimensional arrays are supported"
+ raise ValueError("Only one dimensional arrays are supported")
if item.shape != (self.dim,):
- raise ValueError, "Array has wrong size"
+ raise ValueError("Array has wrong size")
def _hash(self, item):
"""Return a hash for item based on the internal permutations.
diff --git a/pybrain/supervised/trainers/__init__.py b/pybrain/supervised/trainers/__init__.py
index 66621e670..100a391e0 100644
--- a/pybrain/supervised/trainers/__init__.py
+++ b/pybrain/supervised/trainers/__init__.py
@@ -1,3 +1,3 @@
-from trainer import Trainer
-from backprop import BackpropTrainer
-from rprop import RPropMinusTrainer
+from pybrain.supervised.trainers.trainer import Trainer
+from pybrain.supervised.trainers.backprop import BackpropTrainer
+from pybrain.supervised.trainers.rprop import RPropMinusTrainer
diff --git a/pybrain/supervised/trainers/backprop.py b/pybrain/supervised/trainers/backprop.py
index 0d3f4f6f2..4426f6cfa 100644
--- a/pybrain/supervised/trainers/backprop.py
+++ b/pybrain/supervised/trainers/backprop.py
@@ -3,7 +3,7 @@
from scipy import dot, argmax
from random import shuffle
from math import isnan
-from trainer import Trainer
+from pybrain.supervised.trainers.trainer import Trainer
from pybrain.utilities import fListToString
from pybrain.auxiliary import GradientDescent
@@ -67,7 +67,7 @@ def train(self):
self.module.resetDerivatives()
if self.verbose:
- print "Total error:", errors / ponderation
+ print("Total error:", errors / ponderation)
if self.batchlearning:
self.module._setParameters(self.descent(self.module.derivs))
self.epoch += 1
@@ -127,7 +127,7 @@ def _checkGradient(self, dataset=None, silent=False):
r = zip(analyticalDerivs, numericalDerivs)
res.append(r)
if not silent:
- print r
+ print(r)
return res
def testOnData(self, dataset=None, verbose=False):
@@ -139,7 +139,7 @@ def testOnData(self, dataset=None, verbose=False):
dataset = self.ds
dataset.reset()
if verbose:
- print '\nTesting on data:'
+ print('\nTesting on data:')
errors = []
importances = []
ponderatedErrors = []
@@ -150,13 +150,13 @@ def testOnData(self, dataset=None, verbose=False):
errors.append(e)
ponderatedErrors.append(e / i)
if verbose:
- print 'All errors:', ponderatedErrors
+ print('All errors:', ponderatedErrors)
assert sum(importances) > 0
avgErr = sum(errors) / sum(importances)
if verbose:
- print 'Average error:', avgErr
- print ('Max error:', max(ponderatedErrors), 'Median error:',
- sorted(ponderatedErrors)[len(errors) / 2])
+ print('Average error:', avgErr)
+ print(('Max error:', max(ponderatedErrors), 'Median error:',
+ sorted(ponderatedErrors)[len(errors) / 2]))
return avgErr
def testOnClassData(self, dataset=None, verbose=False,
@@ -252,6 +252,6 @@ def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
#self.trainingErrors.append(self.testOnData(trainingData))
self.ds = dataset
if verbose:
- print 'train-errors:', fListToString(self.trainingErrors, 6)
- print 'valid-errors:', fListToString(self.validationErrors, 6)
+ print('train-errors:', fListToString(self.trainingErrors, 6))
+ print('valid-errors:', fListToString(self.validationErrors, 6))
return self.trainingErrors[:bestepoch], self.validationErrors[:1 + bestepoch]
diff --git a/pybrain/supervised/trainers/evolino.py b/pybrain/supervised/trainers/evolino.py
index 8a1437f56..a3af86c1f 100644
--- a/pybrain/supervised/trainers/evolino.py
+++ b/pybrain/supervised/trainers/evolino.py
@@ -3,7 +3,7 @@
from numpy import Infinity
-from trainer import Trainer
+from pybrain.supervised.trainers.trainer import Trainer
from pybrain.supervised.evolino.population import EvolinoPopulation
from pybrain.supervised.evolino.individual import EvolinoSubIndividual
from pybrain.supervised.evolino.filter import EvolinoEvaluation, EvolinoSelection, EvolinoReproduction, EvolinoBurstMutation
@@ -121,7 +121,7 @@ def train(self):
self.totalepochs += 1
if self.totalepochs - self._max_fitness_epoch >= self.nBurstMutationEpochs:
- if self.verbosity: print "RUNNING BURST MUTATION"
+ if self.verbosity: print("RUNNING BURST MUTATION")
self.burstMutate()
self._max_fitness_epoch = self.totalepochs
@@ -130,11 +130,11 @@ def train(self):
filter.apply(self._population)
if self._max_fitness < self.evaluation.max_fitness:
- if self.verbosity: print "GAINED FITNESS: ", self._max_fitness, " -->" , self.evaluation.max_fitness, "\n"
+ if self.verbosity: print("GAINED FITNESS: ", self._max_fitness, " -->" , self.evaluation.max_fitness, "\n")
self._max_fitness = self.evaluation.max_fitness
self._max_fitness_epoch = self.totalepochs
else:
- if self.verbosity: print "DIDN'T GAIN FITNESS:", "best =", self._max_fitness, " current-best = ", self.evaluation.max_fitness, "\n"
+ if self.verbosity: print("DIDN'T GAIN FITNESS:", "best =", self._max_fitness, " current-best = ", self.evaluation.max_fitness, "\n")
def burstMutate(self):
self.burstMutation.apply(self._population)
diff --git a/pybrain/supervised/trainers/rprop.py b/pybrain/supervised/trainers/rprop.py
index 0898c00db..4407ede21 100644
--- a/pybrain/supervised/trainers/rprop.py
+++ b/pybrain/supervised/trainers/rprop.py
@@ -43,8 +43,10 @@ def train(self):
errors += e
ponderation += p
if self.verbose:
- print "epoch %6d total error %12.5g avg weight %12.5g" % (self.epoch, errors / ponderation,
- sqrt((self.module.params ** 2).mean()))
+ print("epoch {epoch:6d} total error {error:12.5g} avg weight {weight:12.5g}".format(
+ epoch=self.epoch,
+ error=errors / ponderation,
+ weight=sqrt((self.module.params ** 2).mean())))
self.module._setParameters(self.descent(self.module.derivs - self.weightdecay * self.module.params))
self.epoch += 1
self.totalepochs += 1
diff --git a/pybrain/supervised/trainers/svmtrainer.py b/pybrain/supervised/trainers/svmtrainer.py
index 556eb1cb7..bc11da5e6 100644
--- a/pybrain/supervised/trainers/svmtrainer.py
+++ b/pybrain/supervised/trainers/svmtrainer.py
@@ -262,7 +262,7 @@ def cmp (x, y):
x = dbarr[:, 0]
dimy = len(x[x == x[0]])
dimx = x.size / dimy
- print 'plotting: ', dimx, dimy
+ print('plotting: ', dimx, dimy)
x = x.reshape(dimx, dimy)
y = dbarr[:, 1]
y = y.reshape(dimx, dimy)
diff --git a/pybrain/tests/helpers.py b/pybrain/tests/helpers.py
index d9cb752d9..3e16e7217 100644
--- a/pybrain/tests/helpers.py
+++ b/pybrain/tests/helpers.py
@@ -41,7 +41,7 @@ def gradientCheck(module, tolerance=0.0001, dataset=None):
""" check the gradient of a module with a randomly generated dataset,
(and, in the case of a network, determine which modules contain incorrect derivatives). """
if module.paramdim == 0:
- print 'Module has no parameters'
+ print('Module has no parameters')
return True
if dataset:
d = dataset
@@ -59,18 +59,18 @@ def gradientCheck(module, tolerance=0.0001, dataset=None):
precision[i] += abs((p[0] + p[1]) / (p[0] - p[1]))
precision /= len(res)
if max(precision) < tolerance:
- print 'Perfect gradient'
+ print('Perfect gradient')
return True
else:
- print 'Incorrect gradient', precision
+ print('Incorrect gradient', precision)
if isinstance(module, Network):
index = 0
for m in module._containerIterator():
if max(precision[index:index + m.paramdim]) > tolerance:
- print 'Incorrect module:', m, res[-1][index:index + m.paramdim]
+ print('Incorrect module:', m, res[-1][index:index + m.paramdim])
index += m.paramdim
else:
- print res
+ print(res)
return False
@@ -78,13 +78,13 @@ def netCompare(net1, net2, forwardpasses=1, verbose=False):
identical = True
if str(net2) == str(net1):
if verbose:
- print 'Same representation'
+ print('Same representation')
else:
identical = False
if verbose:
- print net2
- print '-' * 80
- print net1
+ print(net2)
+ print('-' * 80)
+ print(net1)
outN = zeros(net2.outdim)
outEnd = zeros(net1.outdim)
@@ -97,21 +97,21 @@ def netCompare(net1, net2, forwardpasses=1, verbose=False):
if sum(map(abs, outN - outEnd)) < 1e-9:
if verbose:
- print 'Same function'
+ print('Same function')
else:
identical = False
if verbose:
- print outN
- print outEnd
+ print(outN)
+ print(outEnd)
if net2.__class__ == net1.__class__:
if verbose:
- print 'Same class'
+ print('Same class')
else:
identical = False
if verbose:
- print net2.__class__
- print net1.__class__
+ print(net2.__class__)
+ print(net1.__class__)
return identical
diff --git a/pybrain/tests/optimizationtest.py b/pybrain/tests/optimizationtest.py
index 1b23345f2..d0e8576d7 100644
--- a/pybrain/tests/optimizationtest.py
+++ b/pybrain/tests/optimizationtest.py
@@ -220,8 +220,8 @@ def testOnEvolvable(algo):
def testAll(tests, allalgos, tolerant=True):
countgood = 0
for i, algo in enumerate(sorted(allalgos)):
- print "%d, %s:" % (i + 1, algo.__name__)
- print ' ' * int(log10(i + 1) + 2),
+ print("%d, %s:" % (i + 1, algo.__name__))
+ print(' ' * int(log10(i + 1) + 2),)
good = True
messages = []
for t in tests:
@@ -233,21 +233,21 @@ def testAll(tests, allalgos, tolerant=True):
res = e
if res is True:
- print '.',
+ print('.',)
else:
good = False
messages.append(res)
- print 'F',
+ print('F',)
if good:
countgood += 1
- print '--- OK.'
+ print('--- OK.')
else:
- print '--- NOT OK.'
+ print('--- NOT OK.')
for m in messages:
if m is not None:
- print ' ' * int(log10(i + 1) + 2), '->', m
+ print(' ' * int(log10(i + 1) + 2), '->', m)
print
- print 'Summary:', countgood, '/', len(allalgos), 'of test were passed.'
+ print('Summary:', countgood, '/', len(allalgos), 'of test were passed.')
@@ -260,9 +260,9 @@ def testAll(tests, allalgos, tolerant=True):
),
globals().values())
- print 'Optimization algorithms to be tested:', len(allalgos)
+ print('Optimization algorithms to be tested:', len(allalgos))
print
- print 'Note: this collection of tests may take quite some time.'
+ print('Note: this collection of tests may take quite some time.')
print
tests = [testInterface,
diff --git a/pybrain/tests/runtests.py b/pybrain/tests/runtests.py
index 3918b7ea1..f49a7f9a0 100644
--- a/pybrain/tests/runtests.py
+++ b/pybrain/tests/runtests.py
@@ -99,12 +99,12 @@ def make_test_suite():
untests = [(m, md) for m, md in modules if md]
modules = [m for m, md in modules if not md]
- # Print out modules that are missing dependencies
+ # print(out modules that are missing dependencies)
for module, miss_dep in untests: # Mr Dep is not around, though
logging.warning('Module %s is missing dependencies: %s' % (
module.__name__, ', '.join(miss_dep)))
- # Print out a list of tests that are found
+ # print(out a list of tests that are found)
for m in modules:
logging.info('Tests found: %s' % m.__name__)
diff --git a/pybrain/tests/unittests/_test_equivalence_to_ctypes.py b/pybrain/tests/unittests/_test_equivalence_to_ctypes.py
index 2013a6b46..13b8fd5f5 100644
--- a/pybrain/tests/unittests/_test_equivalence_to_ctypes.py
+++ b/pybrain/tests/unittests/_test_equivalence_to_ctypes.py
@@ -114,12 +114,12 @@ def testEquivalence(net):
if net.name == '22':
h = net['hidden0']
ch = cnet['hidden0']
- print 'ni', input, net.inputbuffer.T
- print 'ci', input, cnet.inputbuffer.T
- print 'hni', h.inputbuffer.T[0]
- print 'hci', ch.inputbuffer.T[0]
- print 'hnout', h.outputbuffer.T[0]
- print 'hcout', ch.outputbuffer.T[0]
+ print('ni', input, net.inputbuffer.T)
+ print('ci', input, cnet.inputbuffer.T)
+ print('hni', h.inputbuffer.T[0])
+ print('hci', ch.inputbuffer.T[0])
+ print('hnout', h.outputbuffer.T[0])
+ print('hcout', ch.outputbuffer.T[0])
print
else:
@@ -129,10 +129,10 @@ def testEquivalence(net):
if epsilonCheck(sum(res - cres), 0.001):
return True
else:
- print 'in-net', net.inputbuffer.T
- print 'in-arac', cnet.inputbuffer.T
- print 'out-net', net.outputbuffer.T
- print 'out-arac', cnet.outputbuffer.T
+ print('in-net', net.inputbuffer.T)
+ print('in-arac', cnet.inputbuffer.T)
+ print('out-net', net.outputbuffer.T)
+ print('out-arac', cnet.outputbuffer.T)
return (res, cres)
diff --git a/pybrain/tests/unittests/datasets/test_datasets_datasets.py b/pybrain/tests/unittests/datasets/test_datasets_datasets.py
index c4dc09423..96e8a521b 100644
--- a/pybrain/tests/unittests/datasets/test_datasets_datasets.py
+++ b/pybrain/tests/unittests/datasets/test_datasets_datasets.py
@@ -45,7 +45,7 @@
[ 7., 7.]])]
>>> p = reversed(range(4))
- >>> print '\\n'.join(repr(b) for b in d.batches('input', 2, p))
+ >>> print('\\n'.join(repr(b) for b in d.batches('input', 2, p)))
array([[ 6., 6.],
[ 7., 7.]])
array([[ 4., 4.],
@@ -71,7 +71,7 @@
>>> d.addSample([1,0])
>>> d.addSample([1,1])
>>> for sample in d:
- ... print sample
+ ... print(sample)
...
[array([ 0., 0.])]
[array([ 0., 1.])]
@@ -160,14 +160,14 @@ def saveInvariant(dataset):
equal = True
for (k, v), (k_, v_) in zip(orig_array_data, rec_array_data):
if k != k_:
- print "Differing keys: %s <=> %s" % (dataset.dataset.keys(),
- rec_array_data.dataset.keys())
+ print("Differing keys: %s <=> %s" % (dataset.dataset.keys(),
+ rec_array_data.dataset.keys()))
equal = False
break
if not (v == v_).all():
- print "Differing values for %s" % k
- print v
- print v_
+ print("Differing values for %s" % k)
+ print(v)
+ print(v_)
equal = False
break
@@ -185,8 +185,8 @@ def saveInvariant(dataset):
if rec_dict == orig_dict:
return True
else:
- print rec_dict
- print orig_dict
+ print(rec_dict)
+ print(orig_dict)
return False
diff --git a/pybrain/tests/unittests/rl/environments/twoplayergames/test_capture_game.py b/pybrain/tests/unittests/rl/environments/twoplayergames/test_capture_game.py
index d7beaeacc..5e7aa5588 100644
--- a/pybrain/tests/unittests/rl/environments/twoplayergames/test_capture_game.py
+++ b/pybrain/tests/unittests/rl/environments/twoplayergames/test_capture_game.py
@@ -3,7 +3,7 @@
Initialize a capturegame
>>> from pybrain.rl.environments.twoplayergames import CaptureGame
>>> c = CaptureGame(5)
- >>> print c
+ >>> print(c)
. . . . .
. . . . .
. . . . .
@@ -34,7 +34,7 @@
-1
Check if all the values are right:
- >>> print c
+ >>> print(c)
x X O . .
X X O . .
O O . . .
diff --git a/pybrain/tests/unittests/rl/environments/twoplayergames/test_pente.py b/pybrain/tests/unittests/rl/environments/twoplayergames/test_pente.py
index 0500085d3..59e34430c 100644
--- a/pybrain/tests/unittests/rl/environments/twoplayergames/test_pente.py
+++ b/pybrain/tests/unittests/rl/environments/twoplayergames/test_pente.py
@@ -5,7 +5,7 @@
>>> from pybrain.rl.environments.twoplayergames.pente import PenteGame
>>> dim = 5
>>> c = PenteGame((dim, dim))
- >>> print c
+ >>> print(c)
_ _ _ _ _
_ _ _ _ _
_ _ * _ _
@@ -24,7 +24,7 @@
>>> c.performAction([1, (0,2)])
Show the updated board:
- >>> print c
+ >>> print(c)
_ # # _ _
* # * _ _
# * * _ _
@@ -41,7 +41,7 @@
Stepping between black stones is not deadly though:
>>> c.performAction([1, (2,3)])
>>> c.performAction([-1, (2,2)])
- >>> print c
+ >>> print(c)
* _ _ * _
* # _ _ _
# * * # _
@@ -68,7 +68,7 @@
-1
Check if all the values are right:
- >>> print c
+ >>> print(c)
* _ _ * *
* # _ x _
# * * # _
diff --git a/pybrain/tests/unittests/structure/modules/test_peephole_lstm.py b/pybrain/tests/unittests/structure/modules/test_peephole_lstm.py
index 5d8c86e78..380f2062c 100644
--- a/pybrain/tests/unittests/structure/modules/test_peephole_lstm.py
+++ b/pybrain/tests/unittests/structure/modules/test_peephole_lstm.py
@@ -74,7 +74,7 @@ def predictOutcome(net, input, output):
if epsilonCheck(res - output):
return True
else:
- print 'expected:', round(output, 7), '- got:', round(res, 7)
+ print('expected:', round(output, 7), '- got:', round(res, 7))
return False
diff --git a/pybrain/tests/unittests/structure/modules/test_simple_lstm_network.py b/pybrain/tests/unittests/structure/modules/test_simple_lstm_network.py
index df70e7973..db6099b06 100644
--- a/pybrain/tests/unittests/structure/modules/test_simple_lstm_network.py
+++ b/pybrain/tests/unittests/structure/modules/test_simple_lstm_network.py
@@ -3,7 +3,7 @@
Build a simple lstm network with peepholes:
>>> n = buildSimpleLSTMNetwork(True)
- >>> print n
+ >>> print(n)
simpleLstmNet
Modules:
[, , , ]
diff --git a/pybrain/tests/unittests/structure/modules/test_simple_mdlstm.py b/pybrain/tests/unittests/structure/modules/test_simple_mdlstm.py
index 1f80d77e4..73af48d16 100644
--- a/pybrain/tests/unittests/structure/modules/test_simple_mdlstm.py
+++ b/pybrain/tests/unittests/structure/modules/test_simple_mdlstm.py
@@ -3,7 +3,7 @@
Build a simple mdlstm network with peepholes:
>>> n = buildSimpleMDLSTMNetwork(True)
- >>> print n
+ >>> print(n)
simpleMDLstmNet
Modules:
[, , , ]
diff --git a/pybrain/tests/unittests/structure/networks/custom/test_capturegame_network.py b/pybrain/tests/unittests/structure/networks/custom/test_capturegame_network.py
index 1df177772..a48c51246 100644
--- a/pybrain/tests/unittests/structure/networks/custom/test_capturegame_network.py
+++ b/pybrain/tests/unittests/structure/networks/custom/test_capturegame_network.py
@@ -7,7 +7,7 @@
>>> n = CaptureGameNetwork(size = size, componentclass = MDLSTMLayer, hsize = 1, peepholes = False)
Check it's string representation
- >>> print n
+ >>> print(n)
CaptureGameNetwork-s2-h1-MDLSTMLayer--...
Modules:
[, , , ... , ]
@@ -18,9 +18,9 @@
Check some of the connections dimensionalities
>>> c1 = n.connections[n['hidden(1, 0, 3)']][0]
>>> c2 = n.connections[n['hidden(0, 1, 2)']][-1]
- >>> print c1.indim, c1.outdim
+ >>> print(c1.indim, c1.outdim)
1 1
- >>> print c2.indim, c2.outdim
+ >>> print(c2.indim, c2.outdim)
1 1
>>> n.paramdim
21
diff --git a/pybrain/tests/unittests/structure/networks/custom/test_convolutional_nets.py b/pybrain/tests/unittests/structure/networks/custom/test_convolutional_nets.py
index 0aab1dc0a..4a2eebfcd 100644
--- a/pybrain/tests/unittests/structure/networks/custom/test_convolutional_nets.py
+++ b/pybrain/tests/unittests/structure/networks/custom/test_convolutional_nets.py
@@ -5,7 +5,7 @@
>>> from pybrain.structure.networks.custom.convboard import ConvolutionalBoardNetwork
>>> from scipy import array, ravel, var
>>> N = ConvolutionalBoardNetwork(4, 3, 5)
- >>> print N.paramdim
+ >>> print(N.paramdim)
97
This is what a typical input would look like (on a 4x4 board)
@@ -24,7 +24,7 @@
The input of the first features (e.g. white stone presence) is in the middle, like we set it:
- >>> print inp[1:5,1:5]
+ >>> print(inp[1:5,1:5])
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 1. 0. 0.]
diff --git a/pybrain/tests/unittests/structure/networks/test_network_decomposition.py b/pybrain/tests/unittests/structure/networks/test_network_decomposition.py
index 001d40481..e425d7eaa 100644
--- a/pybrain/tests/unittests/structure/networks/test_network_decomposition.py
+++ b/pybrain/tests/unittests/structure/networks/test_network_decomposition.py
@@ -5,7 +5,7 @@
>>> n = buildDecomposableNetwork()
Check if it was built correctly
- >>> print n.paramdim
+ >>> print(n.paramdim)
12
>>> tmp = n.getDecomposition()
>>> tmp[2]
diff --git a/pybrain/tests/unittests/structure/networks/test_network_sort.py b/pybrain/tests/unittests/structure/networks/test_network_sort.py
index 44749bb09..22021bfa0 100644
--- a/pybrain/tests/unittests/structure/networks/test_network_sort.py
+++ b/pybrain/tests/unittests/structure/networks/test_network_sort.py
@@ -43,7 +43,7 @@
Is it the same ordering than our reference?
- >>> print ord3
+ >>> print(ord3)
[, , , , , , , , , ]
"""
diff --git a/pybrain/tests/unittests/structure/networks/test_recurrent_network.py b/pybrain/tests/unittests/structure/networks/test_recurrent_network.py
index dd18c420b..dd0e38b9b 100644
--- a/pybrain/tests/unittests/structure/networks/test_recurrent_network.py
+++ b/pybrain/tests/unittests/structure/networks/test_recurrent_network.py
@@ -3,7 +3,7 @@
Build a simple recurrent network:
>>> n = buildRecurrentNetwork()
- >>> print n
+ >>> print(n)
RecurrentNetwork
Modules:
[, , ]
diff --git a/pybrain/tests/unittests/test_utilities_dictionaries.py b/pybrain/tests/unittests/test_utilities_dictionaries.py
index c51c58820..090a0cff3 100644
--- a/pybrain/tests/unittests/test_utilities_dictionaries.py
+++ b/pybrain/tests/unittests/test_utilities_dictionaries.py
@@ -6,7 +6,7 @@
subDict produces a sub-dictionary, by removing some keys.
>>> d3 = subDict(d1, ['ones', 2, 4])
- >>> print sorted(d3.items())
+ >>> print(sorted(d3.items()))
[(2, [2, 4, 6, 8]), (4, 4), ('ones', [1, 1, 1, 'one'])]
We can also flip the selection, and limit the keys to the ones NOT in the list:
@@ -38,7 +38,7 @@
with their keys, not allowing for identical items,
but dealing with non-lists, and any types of keys and values.
- >>> for x in dictCombinations(d1): print sorted(x.items())
+ >>> for x in dictCombinations(d1): print(sorted(x.items()))
[(2, 2), (4, 4), (8, 'eight'), ('ones', 1)]
[(2, 4), (4, 4), (8, 'eight'), ('ones', 1)]
[(2, 6), (4, 4), (8, 'eight'), ('ones', 1)]
diff --git a/pybrain/tests/unittests/tools/test_rlgluebridge.py b/pybrain/tests/unittests/tools/test_rlgluebridge.py
index a434df288..ffb83064b 100644
--- a/pybrain/tests/unittests/tools/test_rlgluebridge.py
+++ b/pybrain/tests/unittests/tools/test_rlgluebridge.py
@@ -86,7 +86,7 @@
>>> agent.newEpisode()
I got a new episode
- >>> print agent.benchmark
+ >>> print(agent.benchmark)
Average Reward: dim(2, 1)
[[ 0.5]]
@@ -116,11 +116,11 @@ class PseudoPybrainAgent(LearningAgent):
def __init__(self): pass
def integrateObservation(self, obs):
- print "I saw %s" % obs
+ print("I saw %s" % obs)
def getAction(self):
action = array([2.7, -1])
- print "I did %s" % action
+ print("I did %s" % action)
return action
def giveReward(self, r):
@@ -130,13 +130,13 @@ def giveReward(self, r):
negative
:type r: double
"""
- print "I was given %.2f" % float(r)
+ print("I was given %.2f" % float(r))
def newEpisode(self):
- print "I got a new episode"
+ print("I got a new episode")
def reset(self):
- print "I was resetted"
+ print("I was resetted")
if __name__ == "__main__":
diff --git a/pybrain/tools/aptativeresampling.py b/pybrain/tools/aptativeresampling.py
index 8ee739904..19eca3b04 100644
--- a/pybrain/tools/aptativeresampling.py
+++ b/pybrain/tools/aptativeresampling.py
@@ -53,13 +53,13 @@ def testnes():
fun2 = AdaptiveResampler(fun, 10)
l = XNES(fun, x0, maxEvaluations=1100, storeAllEvaluations=True)
res = l.learn()
- print sum(res[0]**2)
+ print(sum(res[0]**2) )
pylab.plot(map(abs, l._allEvaluations))
l2 = XNES(fun2, x0, maxEvaluations=1100, storeAllEvaluations=True)
res = l2.learn()
- print sum(res[0]**2)
- print fun2.resample_over
+ print(sum(res[0]**2) )
+ print(fun2.resample_over)
pylab.plot(map(abs,l2._allEvaluations))
pylab.semilogy()
pylab.show()
diff --git a/pybrain/tools/customxml/networkreader.py b/pybrain/tools/customxml/networkreader.py
index ab6aaf516..8f23be741 100644
--- a/pybrain/tools/customxml/networkreader.py
+++ b/pybrain/tools/customxml/networkreader.py
@@ -89,8 +89,8 @@ def readBuildable(self, node):
try:
m = eval(mclass)(**argdict)
except:
- print 'Could not construct', mclass
- print 'with arguments:', argdict
+ print('Could not construct', mclass)
+ print('with arguments:', argdict)
return None
m.name = node.getAttribute('name')
self.readParams(node, m)
diff --git a/pybrain/tools/datasettools.py b/pybrain/tools/datasettools.py
index 3bb1982f2..601fd14ee 100644
--- a/pybrain/tools/datasettools.py
+++ b/pybrain/tools/datasettools.py
@@ -33,11 +33,11 @@ def convertSequenceToTimeWindows(DSseq, NewClass, winsize):
tar_win = target[k - 1, :]
DSwin.addSample(inp_win.flatten(), tar_win.flatten())
nsamples += 1
- ##print "added sample %d from sequence %d: %d - %d" %( nsamples, nseqs, k-winsize, k-1)
- print "samples in original dataset: ", len(DSseq)
- print "window size * nsamples = ", winsize * nsamples
- print "total data points in original data: ", len(DSseq) * DSseq.indim
- print "total data points in windowed dataset: ", len(DSwin) * DSwin.indim
+ ##print("added sample %d from sequence %d: %d - %d" %( nsamples, nseqs, k-winsize, k-1))
+ print("samples in original dataset: ", len(DSseq))
+ print("window size * nsamples = ", winsize * nsamples)
+ print("total data points in original data: ", len(DSseq) * DSseq.indim)
+ print("total data points in windowed dataset: ", len(DSwin) * DSwin.indim)
return DSwin
def windowSequenceEval(DS, winsz, result):
@@ -62,15 +62,15 @@ def windowSequenceEval(DS, winsz, result):
wrong += 1.
y += [0., 0.]
idx += 1
- #print "winidx: ", idx
+ #print("winidx: ", idx)
curr_x += winsz
x.append(curr_x)
seq_res.append(100. * correct / (correct + wrong))
- print "sequence %d correct: %g12.2%%" % (i, seq_res[-1])
+ print("sequence %d correct: %g12.2%%" % (i, seq_res[-1]))
seq_res = array(seq_res)
- print "total fraction of correct sequences: ", 100. * float((seq_res >= 0.5).sum()) / seq_res.size
+ print("total fraction of correct sequences: ", 100. * float((seq_res >= 0.5).sum()) / seq_res.size)
class DataSetNormalizer(object):
@@ -115,10 +115,10 @@ def normalizePattern(self, y):
def normalize(self, ds, field='input'):
""" normalize dataset or vector wrt. to stored min and max """
if self.dim <= 0:
- raise IndexError, "No normalization parameters defined!"
+ raise IndexError("No normalization parameters defined!")
dsdim = ds[field].shape[1]
if self.dim != dsdim:
- raise IndexError, "Dimension of normalization params does not match DataSet field!"
+ raise IndexError("Dimension of normalization params does not match DataSet field!")
newfeat = ds[field]
if self.meanstd:
for i in range(dsdim):
diff --git a/pybrain/tools/example_tools.py b/pybrain/tools/example_tools.py
index afd23fbef..fdfb0a33c 100644
--- a/pybrain/tools/example_tools.py
+++ b/pybrain/tools/example_tools.py
@@ -49,20 +49,21 @@ def printResults(self,resList, runs, updates):
if self.kind == "optimizer":
rLen = len(resList)
avReward = array(resList).sum()/rLen
- print "Parameters:", self.agent.learner._bestFound()
- print "Experiment:", runs,
- print " Evaluation:", (updates+1)*self.batch*self.prnts,
- print " BestReward:", self.agent.learner.bestEvaluation,
- print " AverageReward:", avReward
- print
+ print("Parameters:", self.agent.learner._bestFound())
+ print("Experiment:", runs,
+ " Evaluation:", (updates+1)*self.batch*self.prnts,
+ " BestReward:", self.agent.learner.bestEvaluation,
+ " AverageReward:", avReward)
+ print()
self.rl.append(avReward)
else:
avReward = resList
- #print "Parameters: ", self.agent.learner._bestFound()
- print "Step: ", runs, "/", (updates+1)*self.batch*self.prnts,
- #print "Best: ", self.agent.learner.bestEvaluation,
- print "Base: ", avReward
- #print
+ #print("Parameters: ", self.agent.learner._bestFound())
+ print(
+ "Step: ", runs, "/", (updates+1)*self.batch*self.prnts,
+ #"Best: ", self.agent.learner.bestEvaluation,
+ "Base: ", avReward)
+ #print()
self.rl.append(avReward)
def addExps(self):
diff --git a/pybrain/tools/filehandling.py b/pybrain/tools/filehandling.py
index c0b47b5df..bd30e7429 100644
--- a/pybrain/tools/filehandling.py
+++ b/pybrain/tools/filehandling.py
@@ -46,7 +46,7 @@ def pickleDumpDict(name, d):
f.close()
return True
except Exception, e:
- print 'Error writing into', name, ':', str(e)
+ print('Error writing into', name, ':', str(e))
return False
@@ -57,20 +57,20 @@ def pickleReadDict(name):
val = pickle.load(f)
f.close()
except Exception, e:
- print 'Nothing read from', name, ':', str(e)
+ print('Nothing read from', name, ':', str(e))
val = {}
return val
def addToDictFile(name, key, data, verbose=False):
if verbose:
- print '.',
+ print('.',)
d = pickleReadDict(name)
if key not in d:
d[key] = []
d[key].append(data)
pickleDumpDict(name, d)
if verbose:
- print ':'
+ print(':')
diff --git a/pybrain/tools/functions.py b/pybrain/tools/functions.py
index b87887f82..f70128327 100644
--- a/pybrain/tools/functions.py
+++ b/pybrain/tools/functions.py
@@ -72,7 +72,7 @@ def ranking(R):
""" Produces a linear ranking of the values in R. """
l = sorted(list(enumerate(R)), cmp=lambda a, b: cmp(a[1], b[1]))
l = sorted(list(enumerate(l)), cmp=lambda a, b: cmp(a[1], b[1]))
- return array(map(lambda (r, dummy): r, l))
+ return array(map(lambda kv: kv[0], l))
def expln(x):
diff --git a/pybrain/tools/gridsearch.py b/pybrain/tools/gridsearch.py
index 378649f3c..7afc3a613 100644
--- a/pybrain/tools/gridsearch.py
+++ b/pybrain/tools/gridsearch.py
@@ -70,7 +70,7 @@ def search(self):
perf = self._validate(params)
perfs[params] = perf
if self._verbosity > 0:
- print "validated:", params, " performance = ", perf
+ print("validated:", params, " performance = ", perf)
self._onStep()
@@ -99,7 +99,7 @@ def _calculateJobs(self):
linspaces.append(
self._permuteSequence(
list(linspace(self._min_params[i], self._max_params[i], self._n_steps[i]))))
-# print linspaces; exit(0)
+# print(linspaces; exit(0))
# linspaces = array(linspaces,float)
nr_c = len(linspaces[0])
nr_g = len(linspaces[1])
@@ -196,7 +196,7 @@ def search(self):
center = grid[max_idx]
if self._verbosity > 0:
print
- print "Found maximum at:", center, " performance = ", local_perf[max_idx]
+ print("Found maximum at:", center, " performance = ", local_perf[max_idx])
print
return center
@@ -207,7 +207,7 @@ def _validateWrapper(self, params):
"""
perf = self._validate(params)
if self._verbosity > 0:
- print "validated:", params, " performance = ", perf
+ print("validated:", params, " performance = ", perf)
self._performances[tuple(params)] = perf
return perf
diff --git a/pybrain/tools/kwargsprocessor.py b/pybrain/tools/kwargsprocessor.py
index d356c30f2..1c5a0203f 100644
--- a/pybrain/tools/kwargsprocessor.py
+++ b/pybrain/tools/kwargsprocessor.py
@@ -66,21 +66,21 @@ def __str__(self):
return str(dict(self.__dict__))
c1 = C(m=1)
- print 'c1 =', c1
+ print('c1 =', c1)
c2 = C(m=1, a=1, b=2)
- print 'c2 =', c2
+ print('c2 =', c2)
c3 = C(m=1, simple="hallo", a=11, b=22, c=55)
- print 'c3 =', c3
+ print('c3 =', c3)
- print "\nc3.b = ", c3.b
+ print("\nc3.b = ", c3.b)
try:
C() # will raise KeyError because mandatory keyword argument "m" is missing
except KeyError, k:
- print k
+ print(k)
diff --git a/pybrain/tools/mixtures/mogpuremax.py b/pybrain/tools/mixtures/mogpuremax.py
index 6bcdba0a8..05ff1e461 100644
--- a/pybrain/tools/mixtures/mogpuremax.py
+++ b/pybrain/tools/mixtures/mogpuremax.py
@@ -103,7 +103,7 @@ def getSample(self):
if abs(n) > 5.0: n = 0.0
y = (20.0 + n ** 2 - 10.0 * cos(2.0 * 3.1416 * n)) / 55.0 + random.normal(0, 0.2) #one dimensional rastrigin
m.learn(x, y)
- print m.alpha
- print m.mue
- print m.sigma
+ print(m.alpha)
+ print(m.mue)
+ print(m.sigma)
diff --git a/pybrain/tools/networking/udpconnection.py b/pybrain/tools/networking/udpconnection.py
index 076ca9e05..0b17ff4c6 100644
--- a/pybrain/tools/networking/udpconnection.py
+++ b/pybrain/tools/networking/udpconnection.py
@@ -38,14 +38,14 @@ def __init__(self, ip="127.0.0.1", port="21560", buf="1024"):
self.cIP = []
self.addrList = []
self.UDPOutSockList = []
- print "listening on port", self.inPort
+ print("listening on port", self.inPort)
# Adding a client to the list
def addClient(self, cIP):
self.cIP.append(cIP)
self.addrList.append((cIP, self.outPort))
self.UDPOutSockList.append(socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
- print "client", cIP, "connected"
+ print("client", cIP, "connected")
self.clients += 1
# Listen for clients
@@ -71,12 +71,12 @@ def listen(self):
if newClient:
self.addClient(cIP)
except:
- print "All clients disconnected"
+ print("All clients disconnected")
self.clients = 0
self.cIP = []
self.addrList = []
self.UDPOutSockList = []
- print "listening on port", self.inPort
+ print("listening on port", self.inPort)
# Sending the actual data too all clients
@@ -115,11 +115,11 @@ def listen(self, arrayList=None):
arrayList = eval(data)
return arrayList
except:
- print "Unsupported data format received from", self.outAddr, "!"
+ print("Unsupported data format received from", self.outAddr, "!")
return None
except:
- print "Server has quit!"
+ print("Server has quit!")
return None
# Try to recreate sockets
#self.createSockets()
diff --git a/pybrain/tools/neuralnets.py b/pybrain/tools/neuralnets.py
index 29ff86351..8a7666770 100644
--- a/pybrain/tools/neuralnets.py
+++ b/pybrain/tools/neuralnets.py
@@ -166,7 +166,7 @@ class NNclassifier(NNtools):
def __init__(self, DS, **kwargs):
""" Initialize the classifier: the least we need is the dataset to be classified. All keywords given are set as member variables. """
if not isinstance(DS, ClassificationDataSet):
- raise TypeError, 'Need a ClassificationDataSet to do classification!'
+ raise TypeError('Need a ClassificationDataSet to do classification!')
NNtools.__init__(self, DS, **kwargs)
self.nClasses = self.DS.nClasses # need this because targets may be altered later
self.clsnames = None
diff --git a/pybrain/tools/plotting/fitnessprogression.py b/pybrain/tools/plotting/fitnessprogression.py
index 14cff0d4c..7e7d6fed5 100644
--- a/pybrain/tools/plotting/fitnessprogression.py
+++ b/pybrain/tools/plotting/fitnessprogression.py
@@ -73,13 +73,13 @@ def relevantPart(l):
i += 1
nbRuns = len(flist)
- print name, nbRuns, 'runs',
+ print(name, nbRuns, 'runs',)
if targetcutoff != None:
if onlysuccessful:
# filter out unsuccessful runs
flist = filter(isSuccessful, flist)
- print ',', len(flist), 'of which were successful.'
+ print(',', len(flist), 'of which were successful.')
else:
print
# cut off irrelevant part
diff --git a/pybrain/tools/rlgluebridge.py b/pybrain/tools/rlgluebridge.py
index b74265bc3..18cb75e13 100644
--- a/pybrain/tools/rlgluebridge.py
+++ b/pybrain/tools/rlgluebridge.py
@@ -79,7 +79,7 @@ def __init__(self, klass, *args, **kwargs):
framework
"""
if not issubclass(klass, LearningAgent):
- raise ValueError, "Supply a LearningAgent as first argument"
+ raise ValueError("Supply a LearningAgent as first argument")
self.agent = klass(*args, **kwargs)
@@ -153,7 +153,7 @@ def agent_freeze(self):
def agent_message(self, message):
# Originally thought to enable dynamic methods for agents, but this
# does not make a lot of sense in a dynamic language (and in OO?)
- print "Message:", message
+ print("Message:", message)
def _getAction(self):
"""
diff --git a/pybrain/tools/shortcuts.py b/pybrain/tools/shortcuts.py
index fcc4bc9c9..3fcb2b88d 100644
--- a/pybrain/tools/shortcuts.py
+++ b/pybrain/tools/shortcuts.py
@@ -11,7 +11,7 @@
try:
from arac.pybrainbridge import _RecurrentNetwork, _FeedForwardNetwork
-except ImportError, e:
+except ImportError as e:
logging.info("No fast networks available: %s" % e)
diff --git a/pybrain/tools/svmdata.py b/pybrain/tools/svmdata.py
index 64f5067ea..abf21534c 100644
--- a/pybrain/tools/svmdata.py
+++ b/pybrain/tools/svmdata.py
@@ -155,6 +155,6 @@ def getClassHistogram(self):
d = SVMData()
d.clear()
d.loadSVMdata(r'M:\Data\Johan\svm\trials_scale.svm')
- print d.getSample()
- print d.getSample()
+ print(d.getSample())
+ print(d.getSample())
diff --git a/pybrain/tools/validation.py b/pybrain/tools/validation.py
index e52247b36..87340e85d 100644
--- a/pybrain/tools/validation.py
+++ b/pybrain/tools/validation.py
@@ -116,7 +116,7 @@ def getSequenceEnds(cls, dataset):
"""
sequence_ends = delete(dataset.getField('sequence_index') - 1, 0)
sequence_ends = append(sequence_ends, dataset.getLength() - 1)
-# print sequence_ends; exit()
+# print(sequence_ends; exit())
sequence_ends = array(sequence_ends)
return sequence_ends
@@ -311,7 +311,7 @@ def validate(self):
test_idxs = perms[i]
# train
- #print "training iteration", i
+ #print("training iteration", i)
train_ds = SupervisedDataSet(indim, outdim)
train_ds.setField("input" , inp[train_idxs])
train_ds.setField("target" , tar[train_idxs])
@@ -323,7 +323,7 @@ def validate(self):
trainer.trainEpochs(self._max_epochs)
# test
- #print "testing iteration", i
+ #print("testing iteration", i)
test_ds = SupervisedDataSet(indim, outdim)
test_ds.setField("input" , inp[test_idxs])
test_ds.setField("target" , tar[test_idxs])
@@ -373,7 +373,7 @@ def testOnSequenceData(module, dataset):
for j in xrange(len(output)):
# sum up the output values of one sequence
summed_output += output[j]
-# print j, output[j], " --> ", summed_output
+# print(j, output[j], " --> ", summed_output)
# if we reached the end of the sequence
if j in ends:
# convert summed_output and target to class labels
@@ -383,13 +383,13 @@ def testOnSequenceData(module, dataset):
# reset the summed_output to zeros
summed_output = zeros(dataset.outdim)
- ##print format % tuple(class_output)
- ##print format % tuple(class_target)
+ ##print(format % tuple(class_output))
+ ##print(format % tuple(class_target))
class_output = array(class_output)
class_target = array(class_target)
-# print class_target
-# print class_output
+# print(class_target)
+# print(class_output)
return Validator.classificationPerformance(class_output, class_target)
diff --git a/pybrain/unsupervised/trainers/rbm.py b/pybrain/unsupervised/trainers/rbm.py
index ab360e4d0..70bef58bd 100644
--- a/pybrain/unsupervised/trainers/rbm.py
+++ b/pybrain/unsupervised/trainers/rbm.py
@@ -57,7 +57,7 @@ def trainOnDataset(self, dataset):
zeros(self.rbm.hiddenDim), zeros(self.rbm.visibleDim)
for t in xrange(cfg.maxIter):
- #print "*** Iteration %2d **************************************" % t
+ #print("*** Iteration %2d **************************************" % t)
params = self.rbm.params
params = params.reshape((self.rbm.visibleDim, self.rbm.hiddenDim))
@@ -67,28 +67,28 @@ def trainOnDataset(self, dataset):
w, hb, vb = self.calcUpdateByRows(rows)
- #print "Delta: "
- #print "Weight: ",
- #print w
- #print "Visible bias: ",
- #print vb
- #print "Hidden bias: ",
- #print hb
- #print ""
+ #print("Delta: ")
+ #print("Weight: ",)
+ #print(w)
+ #print("Visible bias: ",)
+ #print(vb)
+ #print("Hidden bias: ",)
+ #print(hb)
+ #print("")
olduw = uw = olduw * mm + \
cfg.rWeights * (w - cfg.weightCost * params)
olduhb = uhb = olduhb * mm + cfg.rHidBias * hb
olduvb = uvb = olduvb * mm + cfg.rVisBias * vb
- #print "Delta after momentum: "
- #print "Weight: ",
- #print uw
- #print "Visible bias: ",
- #print uvb
- #print "Hidden bias: ",
- #print uhb
- #print ""
+ #print("Delta after momentum: ")
+ #print("Weight: ",)
+ #print(uw)
+ #print("Visible bias: ",)
+ #print(uvb)
+ #print("Hidden bias: ",)
+ #print(uhb)
+ #print("")
# update the parameters of the original rbm
params += uw
@@ -100,15 +100,15 @@ def trainOnDataset(self, dataset):
self.invRbm = self.rbm.invert()
self.invRbm.biasParams[:] = invBiasParams
- #print "Updated "
- #print "Weight: ",
- #print self.rbm.connections[self.rbm['visible']][0].params.reshape( \
+ #print("Updated ")
+ #print("Weight: ",)
+ #print(self.rbm.connections[self.rbm['visible']][0].params.reshape( \)
# (self.rbm.indim, self.rbm.outdim))
- #print "Visible bias: ",
- #print self.invRbm.connections[self.invRbm['bias']][0].params
- #print "Hidden bias: ",
- #print self.rbm.connections[self.rbm['bias']][0].params
- #print ""
+ #print("Visible bias: ",)
+ #print(self.invRbm.connections[self.invRbm['bias']][0].params)
+ #print("Hidden bias: ",)
+ #print(self.rbm.connections[self.rbm['bias']][0].params)
+ #print("")
def calcUpdateByRow(self, row):
"""This function trains the RBM using only one data row.
diff --git a/pybrain/utilities.py b/pybrain/utilities.py
index 19d74aa01..35495c667 100644
--- a/pybrain/utilities.py
+++ b/pybrain/utilities.py
@@ -12,7 +12,6 @@
from itertools import count
from math import sqrt
from random import random, choice
-from string import split
from scipy import where, array, exp, zeros, size, mat, median
@@ -40,7 +39,7 @@ def drawIndex(probs, tolerant=False):
if tolerant:
probs /= sum(probs)
else:
- print probs, 1 - sum(probs)
+ print(probs, 1 - sum(probs))
raise ValueError()
r = random()
s = 0
@@ -99,7 +98,7 @@ def setAllArgs(obj, argdict):
if xmlstore:
obj.argdict[n] = argdict[n]
else:
- print 'Warning: parameter name', n, 'not found!'
+ print('Warning: parameter name', n, 'not found!')
if xmlstore:
if not hasattr(obj, '_unknown_argdict'):
obj._unknown_argdict = {}
@@ -225,7 +224,7 @@ def _setName(self, newname):
def _generateName(self):
"""Return a unique name for this object."""
- return "%s-%i" % (self.__class__.__name__, self._nameIds.next())
+ return "%s-%i" % (self.__class__.__name__, next(self._nameIds))
def __repr__(self):
""" The default representation of a named object is its name. """
@@ -254,7 +253,7 @@ def confidenceIntervalSize(stdev, nbsamples):
def trace(func):
def inner(*args, **kwargs):
- print "%s: %s, %s" % (func.__name__, args, kwargs)
+ print("%s: %s, %s" % (func.__name__, args, kwargs))
return func(*args, **kwargs)
return inner
@@ -309,7 +308,7 @@ def newcall(*args, **kwargs):
result = oldcall(*args, **kwargs)
results.append(result)
if verbose:
- print result
+ print(result)
return result
obj.__class__.__call__ = newcall
return results
@@ -393,7 +392,7 @@ def n_to_one(arr):
def canonicClassString(x):
""" the __class__ attribute changed from old-style to new-style classes... """
if isinstance(x, object):
- return split(repr(x.__class__), "'")[1]
+ return repr(x.__class__).split("'")[1]
else:
return repr(x.__class__)
From c6be2e7fe2c94db0fe5fc88ac4aa09c42f919a64 Mon Sep 17 00:00:00 2001
From: adrinjalali
Date: Tue, 8 Apr 2014 11:31:33 +0200
Subject: [PATCH 54/83] fix the syntax error line 233
---
pybrain/optimization/distributionbased/fem.py | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/pybrain/optimization/distributionbased/fem.py b/pybrain/optimization/distributionbased/fem.py
index 69990c37a..a50e81167 100644
--- a/pybrain/optimization/distributionbased/fem.py
+++ b/pybrain/optimization/distributionbased/fem.py
@@ -230,7 +230,8 @@ def _learnStep(self):
if len(self.allsamples) < self.windowSize:
return
if self.verbose and len(self.allsamples) % 100 == 0:
- print(len(self.allsamples), min(self.fitnesses), max(self.fitnesses)#, self.alphas)
+ print(len(self.allsamples), min(self.fitnesses), max(self.fitnesses))
+ # print(len(self.allsamples), min(self.fitnesses), max(self.fitnesses)#, self.alphas)
updateSize = self._computeUpdateSize(self._computeDensities(sample), k)
self.allUpdateSizes.append(deepcopy(updateSize))
From 03803121e597032784220df02620a46e782ecaa6 Mon Sep 17 00:00:00 2001
From: adrinjalali
Date: Tue, 8 Apr 2014 11:34:16 +0200
Subject: [PATCH 55/83] Update fem.py
---
pybrain/optimization/distributionbased/fem.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pybrain/optimization/distributionbased/fem.py b/pybrain/optimization/distributionbased/fem.py
index a50e81167..64219cf65 100644
--- a/pybrain/optimization/distributionbased/fem.py
+++ b/pybrain/optimization/distributionbased/fem.py
@@ -230,7 +230,7 @@ def _learnStep(self):
if len(self.allsamples) < self.windowSize:
return
if self.verbose and len(self.allsamples) % 100 == 0:
- print(len(self.allsamples), min(self.fitnesses), max(self.fitnesses))
+ print(len(self.allsamples), min(self.fitnesses), max(self.fitnesses))
# print(len(self.allsamples), min(self.fitnesses), max(self.fitnesses)#, self.alphas)
updateSize = self._computeUpdateSize(self._computeDensities(sample), k)
From 8ced481138302136343a1af45790185c42e55349 Mon Sep 17 00:00:00 2001
From: Werner Beroux
Date: Fri, 25 Apr 2014 16:10:34 +0200
Subject: [PATCH 56/83] Refactor sequential dataset.
Simplify a bit but mostly use izip to improve the performances of very large
dataset.
Note: dataset.vectorformat seems not to be used and that's good because if
at least we can make sure vectorformat is never list, then dataset should
always use np.array data types. If dataset uses lists, the performances drop
drastically. If we know dataset only uses np.array (i.e., drop support for list)
then we can replace all ravel(x) by x.ravel() which is much faster. If we can
remove vectorformat completely, I guess we could replace x.ravel() by x which
is even faster.
---
pybrain/datasets/dataset.py | 11 ++++++++---
pybrain/datasets/sequential.py | 13 +++++++------
2 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/pybrain/datasets/dataset.py b/pybrain/datasets/dataset.py
index 9dd23d959..d8e8ba617 100644
--- a/pybrain/datasets/dataset.py
+++ b/pybrain/datasets/dataset.py
@@ -240,10 +240,15 @@ def getLinked(self, index=None):
def getField(self, label):
"""Return the entire field given by `label` as an array or list,
depending on user settings."""
+ # Note: label_data should always be a np.array, so this will never
+ # actually clone a list (performances are O(1)).
+ label_data = self.data[label][:self.endmarker[label]]
+
+ # Convert to list if requested.
if self.vectorformat == 'list':
- return self.data[label][:self.endmarker[label]].tolist()
- else:
- return self.data[label][:self.endmarker[label]]
+ label_data = label_data.tolist()
+
+ return label_data
def hasField(self, label):
"""Tell whether the field given by `label` exists."""
diff --git a/pybrain/datasets/sequential.py b/pybrain/datasets/sequential.py
index a24c6b580..14d223261 100644
--- a/pybrain/datasets/sequential.py
+++ b/pybrain/datasets/sequential.py
@@ -1,6 +1,7 @@
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
# $Id$
+from itertools import izip
from scipy import ravel, r_
from random import sample
@@ -41,11 +42,11 @@ def _getSequenceField(self, index, field):
seq = ravel(self.getField('sequence_index'))
if len(seq) == index + 1:
# user wants to access the last sequence, return until end of data
- return self.getField(field)[ravel(self.getField('sequence_index'))[index]:]
+ return self.getField(field)[seq[index]:]
if len(seq) < index + 1:
# sequence index beyond number of sequences. raise exception
raise IndexError('sequence does not exist.')
- return self.getField(field)[ravel(self.getField('sequence_index'))[index]:ravel(self.getField('sequence_index'))[index + 1]]
+ return self.getField(field)[seq[index]:seq[index + 1]]
def getSequence(self, index):
"""Returns the sequence given by `index`.
@@ -56,10 +57,10 @@ def getSequence(self, index):
def getSequenceIterator(self, index):
"""Return an iterator over the samples of the sequence specified by
- `index`."""
- fields = self.getSequence(index)
- for i in range(self.getSequenceLength(index)):
- yield [f[i] for f in fields]
+ `index`.
+
+ Each element is a tuple."""
+ return izip(*self.getSequence(index))
def endOfSequence(self, index):
"""Return True if the marker was moved over the last element of
From c290eb3dc7dfbace18e023f534c082bbafd1a13d Mon Sep 17 00:00:00 2001
From: Werner Beroux
Date: Fri, 25 Apr 2014 16:30:10 +0200
Subject: [PATCH 57/83] Add plotting functions for ClassificationDataSet.
Those plotting functions are a quick way to check visually how well a module
is classifying correctly a dataset.
---
pybrain/tools/plotting/classification.py | 104 +++++++++++++++++++++++
1 file changed, 104 insertions(+)
create mode 100644 pybrain/tools/plotting/classification.py
diff --git a/pybrain/tools/plotting/classification.py b/pybrain/tools/plotting/classification.py
new file mode 100644
index 000000000..fe792535f
--- /dev/null
+++ b/pybrain/tools/plotting/classification.py
@@ -0,0 +1,104 @@
+"""
+matplotlib helpers for ClassificationDataSet and classifiers in general.
+"""
+__author__ = 'Werner Beroux '
+
+import numpy as np
+import matplotlib.pyplot as plt
+
+class ClassificationDataSetPlot(object):
+ @staticmethod
+ def plot_module_classification_sequence_performance(module, dataset, sequence_index, bounds=(0, 1)):
+ """Plot all outputs and fill the value of the output of the correct category.
+
+ The grapth of a good classifier should be like all white, with all other
+ values very low. A graph with lot of black is a bad sign.
+
+ :param module: The module/network to plot.
+ :type module: pybrain.structure.modules.module.Module
+ :param dataset: Training dataset used as inputs and expected outputs.
+ :type dataset: SequenceClassificationDataSet
+ :param sequence_index: Sequence index to plot in the dataset.
+ :type sequence_index: int
+ :param bounds: Outputs lower and upper bound.
+ :type bounds: list
+ """
+ outputs = []
+ valid_output = []
+ module.reset()
+ for sample in dataset.getSequenceIterator(sequence_index):
+ out = module.activate(sample[0])
+ outputs.append(out)
+ valid_output.append(out[sample[1].argmax()])
+ plt.fill_between(range(len(valid_output)), 1, valid_output, facecolor='k', alpha=0.8)
+ plt.plot(outputs, linewidth=4, alpha=0.7)
+ plt.yticks(bounds)
+
+ @staticmethod
+ def plot_module_classification_dataset_performance(module, dataset, cols=4, bounds=(0, 1)):
+ """Do a plot_module_classification_sequence_performance() for all sequences in the dataset.
+ :param module: The module/network to plot.
+ :type module: pybrain.structure.modules.module.Module
+ :param dataset: Training dataset used as inputs and expected outputs.
+ :type dataset: SequenceClassificationDataSet
+ :param bounds: Outputs lower and upper bound.
+ :type bounds: list
+ """
+ # Outputs and detected category error for each sequence.
+ for i in range(dataset.getNumSequences()):
+ plt.subplot(ceil(dataset.getNumSequences() / float(cols)), cols, i)
+ ClassificationDataSetPlot.plot_module_classification_sequence_performance(module, dataset, i, bounds)
+
+ @staticmethod
+ def punchcard_module_classification_performance(module, dataset, s=800):
+ """Punshcard-like clasification performances.__add__(
+
+ Actual dataset target vs. estimated target by the module.
+ The graph of a good classfier module should a have no red dots visible:
+ - Red Dots: Target (only visible if the black dot doesn't cover it).
+ - Green Dots: Estimated classes confidences (size = outputs means).
+ - Black Dots: Single winnter-takes-all estimated target.
+
+ :param module: An object that has at least reset() and activate() methods.
+ :param dataset: A classification dataset. It should, for any given sequence, have a constant target.
+ :type dataset: ClassificationDataSet
+ """
+ # TODO: Could also show the variation for each dot
+ # (e.g., vertical errorbar of 2*stddev).
+ # TODO: Could keep together all sequences of a given class and somehow
+ # arrange them closer togther. Could then aggregate them and
+ # include horizontal errorbar.
+
+ def calculate_module_output_mean(module, inputs):
+ """Returns the mean of the module's outputs for a given input list."""
+ outputs = np.zeros(module.outdim)
+ module.reset()
+ for inpt in inputs:
+ outputs += module.activate(inpt)
+ return outputs / len(inputs)
+
+ num_sequences = dataset.getNumSequences()
+ actual = []
+ expected = []
+ confidence_x = []
+ confidence_s = []
+ correct = 0
+
+ for seq_i in xrange(num_sequences):
+ seq = dataset.getSequence(seq_i)
+ outputs_mean = calculate_module_output_mean(module, seq[0])
+ actual.append(np.argmax(outputs_mean))
+ confidence_s.append(np.array(outputs_mean))
+ confidence_x.append(np.ones(module.outdim) * seq_i)
+ # FIXME: np.argmax(seq[1]) == dataset.getSequenceClass(seq_i) is bugged for split SequenceClassificationDataSet.
+ expected.append(np.argmax(seq[1]))
+ if actual[-1] == expected[-1]:
+ correct += 1
+
+ plt.title('{}% Correct Classification (red dots mean bad classification)'.format(correct * 100 / num_sequences))
+ plt.xlabel('Sequence')
+ plt.ylabel('Class')
+ plt.scatter(range(num_sequences), expected, s=s, c='r', linewidths=0)
+ plt.scatter(range(num_sequences), actual, s=s, c='k')
+ plt.scatter(confidence_x, range(module.outdim) * num_sequences, s=s*np.array(confidence_s), c='g', linewidths=0, alpha=0.66)
+ plt.yticks(range(dataset.nClasses), dataset.class_labels)
From 23f7e3ea479179de924e0c7c9df329f8713a7328 Mon Sep 17 00:00:00 2001
From: "Jack Kelly (aka Daniel)"
Date: Mon, 13 Oct 2014 11:40:39 +0100
Subject: [PATCH 58/83] added MixtureDensity files from Martin Felder's commits
from 2008
---
.../neuralnets+svm/example_mixturedensity.py | 92 +++++++++++++++++++
pybrain/structure/modules/mixturedensity.py | 41 +++++++++
pybrain/supervised/trainers/mixturedensity.py | 80 ++++++++++++++++
3 files changed, 213 insertions(+)
create mode 100644 examples/supervised/neuralnets+svm/example_mixturedensity.py
create mode 100644 pybrain/structure/modules/mixturedensity.py
create mode 100644 pybrain/supervised/trainers/mixturedensity.py
diff --git a/examples/supervised/neuralnets+svm/example_mixturedensity.py b/examples/supervised/neuralnets+svm/example_mixturedensity.py
new file mode 100644
index 000000000..24cd982d6
--- /dev/null
+++ b/examples/supervised/neuralnets+svm/example_mixturedensity.py
@@ -0,0 +1,92 @@
+# $Id$
+# Train a mixture of Gaussians to approximate a multi-mode dataset.
+# It seems fairly easy to fall into some local minimum. Good solutions
+# have errors around -200.
+# This example reproduces Fig. 5.21 from Bishop (2006).
+__author__ = 'Martin Felder'
+
+import pylab as p
+import numpy as np
+from pybrain.structure.modules import LinearLayer, BiasUnit, SigmoidLayer
+from pybrain.structure import FullConnection, Network
+from pybrain.datasets import SupervisedDataSet
+from pybrain.supervised.trainers.mixturedensity import RPropMinusTrainerMix, BackpropTrainerMix
+from pybrain.structure.modules.mixturedensity import MixtureDensityLayer
+
+
+def multigaussian(x, mean, stddev):
+ """ return value of uncorrelated Gaussians at given scalar point
+ x: scalar, mean: vector, stddev: vector """
+ tmp = -0.5 * ((x-mean)/stddev)**2
+ return np.exp(tmp) / (np.sqrt(2.*np.pi) * stddev)
+
+
+if __name__ == '__main__':
+ # build a network
+ n = Network()
+ # linear input layer
+ n.addInputModule(LinearLayer(1, name='in'))
+ # output layer of type 'outclass'
+ n.addOutputModule(MixtureDensityLayer(dim=1, name='out', mix=3))
+ # add bias module and connection to out module
+ n.addModule(BiasUnit(name = 'bias'))
+ n.addConnection(FullConnection(n['bias'], n['out']))
+
+ # arbitrary number of hidden layers of type 'hiddenclass'
+ n.addModule(SigmoidLayer(5, name='hidden'))
+ n.addConnection(FullConnection(n['bias'], n['hidden']))
+
+ # network with hidden layer(s), connections from in to first hidden and last hidden to out
+ n.addConnection(FullConnection(n['in'], n['hidden']))
+ n.addConnection(FullConnection(n['hidden'], n['out']))
+ n.sortModules()
+ n._setParameters(np.random.uniform(-0.1,0.1, size=n.paramdim))
+
+ # build some data
+ y = np.arange(0.0, 1.0, 0.005).reshape(200,1)
+ x = y + 0.3*np.sin(2*np.pi*y) + np.random.uniform(-0.1,0.1,y.size).reshape(y.size,1)
+ dataset = SupervisedDataSet(1,1)
+ dataset.setField('input',x)
+ dataset.setField('target',y)
+
+ # train the network
+ #trainer = BackpropTrainerMix(n,dataset=dataset,learningrate=0.001, momentum=0.0, weightdecay=0.0001, batchlearning=False, verbose=True)
+ trainer = RPropMinusTrainerMix(n,dataset=dataset,verbose=True, weightdecay=0.05)
+ trainer.trainEpochs(200)
+
+ # plot the density and other stuff
+ p.subplot(223)
+ dens = []
+ #newx = np.arange(x.min(), x.max(), 0.01)
+ newx = np.arange(0.0, 1.0, 0.01)
+ newx = newx.reshape(newx.size,1)
+ dataset.setField('input', newx)
+ out = n.activateOnDataset(dataset)
+ for pars in out:
+ line = multigaussian(newx, pars[6:9], pars[3:6])
+ dens.append(line[:,0]*pars[0] +line[:,1]*pars[1] +line[:,2]*pars[2])
+
+ newx = newx.flatten()
+ dens = np.array(dens).transpose()
+ p.contourf(newx,newx,dens,30)
+ p.title("cond. probab. dens.")
+
+ p.subplot(221)
+ out = np.array(out)
+ p.plot(newx,out[:,0:3])
+ p.title("mixing coefficient")
+
+ p.subplot(222)
+ p.plot(newx,out[:,6:9])
+ p.title("means of Gaussians")
+
+ p.subplot(224)
+ p.scatter(x.flatten(),y.flatten(),marker='o',edgecolor='g',facecolors='none')
+ p.hold(True)
+ cmode = dens.argmax(axis=0)
+ p.plot(newx,newx[cmode],"or",markersize=3)
+ p.xlim(0,1)
+ p.ylim(0,1)
+ p.title("data and cond. mode")
+ p.show()
+
\ No newline at end of file
diff --git a/pybrain/structure/modules/mixturedensity.py b/pybrain/structure/modules/mixturedensity.py
new file mode 100644
index 000000000..f2e07aea0
--- /dev/null
+++ b/pybrain/structure/modules/mixturedensity.py
@@ -0,0 +1,41 @@
+# $Id$
+__author__ = 'Martin Felder'
+
+from neuronlayer import NeuronLayer
+from pybrain.tools.functions import safeExp
+
+
+class MixtureDensityLayer(NeuronLayer):
+ """ Mixture of Gaussians output layer (Bishop 2006, Ch. 5.6) with diagonal
+ covariance matrix.
+
+ Assumes inbuf contains K*3 neurons, with the first K giving the mixing
+ coefficients, the next K the standard deviations and the last K the means.
+ """
+
+ def __init__(self, dim, name = None, mix=5):
+ """Initialize mixture density layer - mix gives the number of Gaussians
+ to mix, dim is the dimension of the target(!) vector."""
+ nUnits = mix * (dim + 2) # mean vec + stddev and mixing coeff
+ NeuronLayer.__init__(self, nUnits, name)
+ self.nGaussians = mix
+ self.nDims = dim
+
+ def _forwardImplementation(self, inbuf, outbuf):
+ """Calculate layer outputs (Gaussian parameters etc., not function
+ values!) from given activations """
+ K = self.nGaussians
+ # Mixing parameters and stddevs
+ outbuf[0:K*2] = safeExp(inbuf[0:K*2])
+ outbuf[0:K] /= sum(outbuf[0:K])
+ # Means
+ outbuf[K*2:] = inbuf[K*2:]
+
+ def _backwardImplementation(self, outerr, inerr, outbuf, inbuf):
+ """Calculate the derivatives of output wrt. corresponding input
+ activations."""
+ # Cannot calculate because we would need the targets!
+ # ==> we just pass through the stuff from the trainer, who takes care
+ # of the rest
+ inerr[:] = outerr
+
diff --git a/pybrain/supervised/trainers/mixturedensity.py b/pybrain/supervised/trainers/mixturedensity.py
new file mode 100644
index 000000000..6b4c685fa
--- /dev/null
+++ b/pybrain/supervised/trainers/mixturedensity.py
@@ -0,0 +1,80 @@
+# $Id$
+__author__ = 'Martin Felder'
+
+import numpy as np
+from pybrain.supervised.trainers import RPropMinusTrainer, BackpropTrainer
+from pybrain.structure.modules.mixturedensity import MixtureDensityLayer
+
+def gaussian(x, mean, stddev):
+ """ return value of homogenous Gaussian at given vector point
+ x: vector, mean: vector, stddev: scalar """
+ tmp = -0.5 * sum(((x-mean)/stddev)**2)
+ return np.exp(tmp) / (np.power(2.*np.pi, 0.5*len(x)) * stddev)
+
+
+class BackpropTrainerMix(BackpropTrainer):
+ """ Trainer for mixture model network. See Bishop 2006, Eqn. 5.153-5.157.
+ Due to PyBrain conventions it is more convenient (if not pretty) to treat the
+ MixtureDensityLayer as having a linear transfer function, and calculate
+ its derivative here."""
+
+ def setData(self, dataset):
+ # different output dimension check
+ self.ds = dataset
+ if dataset:
+ assert dataset.indim == self.module.indim
+ assert dataset.outdim == self.module.modules[-1].nDims
+
+ def _calcDerivs(self, seq):
+ """ calculate derivatives assuming we have a Network with a MixtureDensityLayer as output """
+ assert isinstance(self.module.modules[-1], MixtureDensityLayer)
+
+ self.module.reset()
+ for time, sample in enumerate(seq):
+ input = sample[0]
+ self.module.inputbuffer[time] = input
+ self.module.forward()
+ error = 0
+ nDims = self.module.modules[-1].nDims
+ nGauss = self.module.modules[-1].nGaussians
+ gamma = []
+ means = []
+ stddevs = []
+ for time, sample in reversed(list(enumerate(seq))):
+ dummy, target = sample
+ par = self.module.outputbuffer[time] # parameters for mixture
+ # calculate error contributions from all Gaussians in the mixture
+ for k in range(nGauss):
+ coeff = par[k]
+ stddevs.append(par[k+nGauss])
+ idxm = 2*nGauss + k*nDims
+ means.append(par[idxm:idxm+nDims])
+ gamma.append(coeff * gaussian(target, means[-1], stddevs[-1]))
+
+ # calculate error for this pattern, and posterior for target
+ sumg = sum(gamma)
+ error -= np.log(sumg)
+ gamma = np.array(gamma)/sumg
+
+ invvariance = 1./par[nGauss:2*nGauss]**2
+ invstddev = 1./np.array(stddevs)
+
+ # calculate gradient wrt. mixture coefficients
+ grad_c = par[0:nGauss] - gamma
+
+ # calculate gradient wrt. standard deviations
+ grad_m = []
+ grad_s = []
+ for k in range(nGauss):
+ delta = means[k]-target
+ grad_m.append(gamma[k]*delta*invvariance[k])
+ grad_s.append(-gamma[k]*(np.dot(delta,delta)*invvariance[k]*invstddev[k] - invstddev[k]))
+
+ self.module.outputerror[time] = -np.r_[grad_c,grad_s,np.array(grad_m).flatten()]
+ self.module.backward()
+
+ return error, 1.0
+
+class RPropMinusTrainerMix(BackpropTrainerMix,RPropMinusTrainer):
+ """ RProp trainer for mixture model network. See Bishop 2006, Eqn. 5.153-5.157. """
+ dummy = 0
\ No newline at end of file
From 56f1404303651781d32af615816359af5c9ce62f Mon Sep 17 00:00:00 2001
From: "Jack Kelly (aka Daniel)"
Date: Mon, 13 Oct 2014 12:33:35 +0100
Subject: [PATCH 59/83] change NotImplemented to NotImplemetedError
---
pybrain/structure/networks/network.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/pybrain/structure/networks/network.py b/pybrain/structure/networks/network.py
index 2f11dfddc..f55dec156 100644
--- a/pybrain/structure/networks/network.py
+++ b/pybrain/structure/networks/network.py
@@ -167,10 +167,10 @@ def _setDerivatives(self, d, owner=None):
index += x.paramdim
def _forwardImplementation(self, inbuf, outbuf):
- raise NotImplemented("Must be implemented by subclass.")
+ raise NotImplementedError("Must be implemented by subclass.")
def _backwardImplementation(self, outerr, inerr, outbuf, inbuf):
- raise NotImplemented("Must be implemented by subclass.")
+ raise NotImplementedError("Must be implemented by subclass.")
def _topologicalSort(self):
"""Update the network structure and make .modulesSorted a topologically
From a97788b5d1e4b22512fed8ba12d03be66fb00b3c Mon Sep 17 00:00:00 2001
From: "Jack Kelly (aka Daniel)"
Date: Mon, 13 Oct 2014 12:34:07 +0100
Subject: [PATCH 60/83] example_mixturedensity.py now runs against latest
PyBRAIN code
---
.../neuralnets+svm/example_mixturedensity.py | 6 +++---
pybrain/supervised/trainers/mixturedensity.py | 11 ++++++-----
2 files changed, 9 insertions(+), 8 deletions(-)
diff --git a/examples/supervised/neuralnets+svm/example_mixturedensity.py b/examples/supervised/neuralnets+svm/example_mixturedensity.py
index 24cd982d6..e5bb82692 100644
--- a/examples/supervised/neuralnets+svm/example_mixturedensity.py
+++ b/examples/supervised/neuralnets+svm/example_mixturedensity.py
@@ -8,7 +8,7 @@
import pylab as p
import numpy as np
from pybrain.structure.modules import LinearLayer, BiasUnit, SigmoidLayer
-from pybrain.structure import FullConnection, Network
+from pybrain.structure import FullConnection, FeedForwardNetwork
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers.mixturedensity import RPropMinusTrainerMix, BackpropTrainerMix
from pybrain.structure.modules.mixturedensity import MixtureDensityLayer
@@ -23,7 +23,7 @@ def multigaussian(x, mean, stddev):
if __name__ == '__main__':
# build a network
- n = Network()
+ n = FeedForwardNetwork()
# linear input layer
n.addInputModule(LinearLayer(1, name='in'))
# output layer of type 'outclass'
@@ -89,4 +89,4 @@ def multigaussian(x, mean, stddev):
p.ylim(0,1)
p.title("data and cond. mode")
p.show()
-
\ No newline at end of file
+
diff --git a/pybrain/supervised/trainers/mixturedensity.py b/pybrain/supervised/trainers/mixturedensity.py
index 6b4c685fa..041280621 100644
--- a/pybrain/supervised/trainers/mixturedensity.py
+++ b/pybrain/supervised/trainers/mixturedensity.py
@@ -23,11 +23,11 @@ def setData(self, dataset):
self.ds = dataset
if dataset:
assert dataset.indim == self.module.indim
- assert dataset.outdim == self.module.modules[-1].nDims
+ assert dataset.outdim == self.module.modulesSorted[-1].nDims
def _calcDerivs(self, seq):
""" calculate derivatives assuming we have a Network with a MixtureDensityLayer as output """
- assert isinstance(self.module.modules[-1], MixtureDensityLayer)
+ assert isinstance(self.module.modulesSorted[-1], MixtureDensityLayer)
self.module.reset()
for time, sample in enumerate(seq):
@@ -35,8 +35,8 @@ def _calcDerivs(self, seq):
self.module.inputbuffer[time] = input
self.module.forward()
error = 0
- nDims = self.module.modules[-1].nDims
- nGauss = self.module.modules[-1].nGaussians
+ nDims = self.module.modulesSorted[-1].nDims
+ nGauss = self.module.modulesSorted[-1].nGaussians
gamma = []
means = []
stddevs = []
@@ -74,7 +74,8 @@ def _calcDerivs(self, seq):
self.module.backward()
return error, 1.0
+
class RPropMinusTrainerMix(BackpropTrainerMix,RPropMinusTrainer):
""" RProp trainer for mixture model network. See Bishop 2006, Eqn. 5.153-5.157. """
- dummy = 0
\ No newline at end of file
+ dummy = 0
From 5d167f2b5ca2f7559478d06195435eca46680111 Mon Sep 17 00:00:00 2001
From: "Jack Kelly (aka Daniel)"
Date: Tue, 14 Oct 2014 10:25:05 +0100
Subject: [PATCH 61/83] several small fixes
---
.../neuralnets+svm/example_mixturedensity.py | 60 ++++++++++++-------
pybrain/supervised/trainers/mixturedensity.py | 10 +++-
2 files changed, 44 insertions(+), 26 deletions(-)
diff --git a/examples/supervised/neuralnets+svm/example_mixturedensity.py b/examples/supervised/neuralnets+svm/example_mixturedensity.py
index e5bb82692..3214ad189 100644
--- a/examples/supervised/neuralnets+svm/example_mixturedensity.py
+++ b/examples/supervised/neuralnets+svm/example_mixturedensity.py
@@ -15,8 +15,11 @@
def multigaussian(x, mean, stddev):
- """ return value of uncorrelated Gaussians at given scalar point
- x: scalar, mean: vector, stddev: vector """
+ """Returns value of uncorrelated Gaussians at given scalar point.
+ x: scalar
+ mean: vector
+ stddev: vector
+ """
tmp = -0.5 * ((x-mean)/stddev)**2
return np.exp(tmp) / (np.sqrt(2.*np.pi) * stddev)
@@ -27,7 +30,8 @@ def multigaussian(x, mean, stddev):
# linear input layer
n.addInputModule(LinearLayer(1, name='in'))
# output layer of type 'outclass'
- n.addOutputModule(MixtureDensityLayer(dim=1, name='out', mix=3))
+ N_GAUSSIANS = 3
+ n.addOutputModule(MixtureDensityLayer(dim=1, name='out', mix=N_GAUSSIANS))
# add bias module and connection to out module
n.addModule(BiasUnit(name = 'bias'))
n.addConnection(FullConnection(n['bias'], n['out']))
@@ -36,57 +40,67 @@ def multigaussian(x, mean, stddev):
n.addModule(SigmoidLayer(5, name='hidden'))
n.addConnection(FullConnection(n['bias'], n['hidden']))
- # network with hidden layer(s), connections from in to first hidden and last hidden to out
+ # network with hidden layer(s), connections
+ # from in to first hidden and last hidden to out
n.addConnection(FullConnection(n['in'], n['hidden']))
n.addConnection(FullConnection(n['hidden'], n['out']))
n.sortModules()
- n._setParameters(np.random.uniform(-0.1,0.1, size=n.paramdim))
+ n._setParameters(np.random.uniform(-0.1, 0.1, size=n.paramdim))
# build some data
y = np.arange(0.0, 1.0, 0.005).reshape(200,1)
- x = y + 0.3*np.sin(2*np.pi*y) + np.random.uniform(-0.1,0.1,y.size).reshape(y.size,1)
- dataset = SupervisedDataSet(1,1)
- dataset.setField('input',x)
- dataset.setField('target',y)
+ x = (
+ y +
+ 0.3 * np.sin(2 * np.pi * y) +
+ np.random.uniform(-0.1, 0.1, y.size).reshape(y.size, 1)
+ )
+ dataset = SupervisedDataSet(1, 1)
+ dataset.setField('input', x)
+ dataset.setField('target', y)
# train the network
- #trainer = BackpropTrainerMix(n,dataset=dataset,learningrate=0.001, momentum=0.0, weightdecay=0.0001, batchlearning=False, verbose=True)
- trainer = RPropMinusTrainerMix(n,dataset=dataset,verbose=True, weightdecay=0.05)
+ trainer = RPropMinusTrainerMix(n, dataset=dataset, verbose=True,
+ weightdecay=0.05)
trainer.trainEpochs(200)
# plot the density and other stuff
- p.subplot(223)
+ p.subplot(2, 2, 3)
dens = []
- #newx = np.arange(x.min(), x.max(), 0.01)
newx = np.arange(0.0, 1.0, 0.01)
- newx = newx.reshape(newx.size,1)
+ newx = newx.reshape(newx.size, 1)
dataset.setField('input', newx)
out = n.activateOnDataset(dataset)
for pars in out:
- line = multigaussian(newx, pars[6:9], pars[3:6])
- dens.append(line[:,0]*pars[0] +line[:,1]*pars[1] +line[:,2]*pars[2])
+ stds = pars[N_GAUSSIANS:N_GAUSSIANS*2]
+ means = pars[N_GAUSSIANS*2:N_GAUSSIANS*3]
+ line = multigaussian(newx, means, stds)
+ density = line[:,0] * pars[0]
+ for gaussian in range(1, N_GAUSSIANS):
+ density += line[:, gaussian] * pars[gaussian]
+ dens.append(density)
newx = newx.flatten()
dens = np.array(dens).transpose()
- p.contourf(newx,newx,dens,30)
+ p.contourf(newx, newx, dens, 30)
p.title("cond. probab. dens.")
p.subplot(221)
out = np.array(out)
- p.plot(newx,out[:,0:3])
+ p.plot(newx, out[:,0:3])
p.title("mixing coefficient")
p.subplot(222)
- p.plot(newx,out[:,6:9])
+ p.plot(newx, out[:,6:9])
p.title("means of Gaussians")
p.subplot(224)
- p.scatter(x.flatten(),y.flatten(),marker='o',edgecolor='g',facecolors='none')
+ p.scatter(x.flatten(), y.flatten(),
+ marker='o', edgecolor='g', facecolors='none')
p.hold(True)
cmode = dens.argmax(axis=0)
- p.plot(newx,newx[cmode],"or",markersize=3)
- p.xlim(0,1)
- p.ylim(0,1)
+ p.plot(newx, newx[cmode], "or", markersize=3)
+ p.xlim(0, 1)
+ p.ylim(0, 1)
p.title("data and cond. mode")
p.show()
diff --git a/pybrain/supervised/trainers/mixturedensity.py b/pybrain/supervised/trainers/mixturedensity.py
index 041280621..11ad7dbd7 100644
--- a/pybrain/supervised/trainers/mixturedensity.py
+++ b/pybrain/supervised/trainers/mixturedensity.py
@@ -37,10 +37,14 @@ def _calcDerivs(self, seq):
error = 0
nDims = self.module.modulesSorted[-1].nDims
nGauss = self.module.modulesSorted[-1].nGaussians
- gamma = []
- means = []
- stddevs = []
for time, sample in reversed(list(enumerate(seq))):
+
+ # Should these three lines be inside this 'for' block
+ # or outside? I moved them inside - Jack
+ gamma = []
+ means = []
+ stddevs = []
+
dummy, target = sample
par = self.module.outputbuffer[time] # parameters for mixture
# calculate error contributions from all Gaussians in the mixture
From 4aca54ad0beb9fadc457f4cfb9b074e1d4c7d9eb Mon Sep 17 00:00:00 2001
From: SBretz
Date: Sun, 30 Nov 2014 00:19:59 -0500
Subject: [PATCH 62/83] Pybrain3 compatability fixes
---
docs/tutorials/blackboxoptimization.py | 58 +-
docs/tutorials/fnn.py | 16 +-
docs/tutorials/networks.py | 26 +-
.../multiobjective/constnsga2jpq.py | 34 +-
.../optimization/multiobjective/nsga2jpq.py | 34 +-
examples/optimization/optimizerinterface.py | 28 +-
examples/optimization/optimizers_for_rl.py | 13 +-
.../capturegame/evolvingplayer.py | 10 +-
.../capturegame/minitournament.py | 12 +-
examples/rl/environments/capturegame/pente.py | 6 +-
examples/rl/environments/cartpole/cart_nes.py | 5 +-
.../rl/environments/cartpole/play_cartpole.py | 4 +-
examples/rl/environments/linear_fa/bicycle.py | 4 +-
examples/rl/environments/linear_fa/xor.py | 14 +-
.../environments/shipsteer/shipbench_sde.py | 8 +-
examples/rl/valuebased/nfq.py | 11 +-
.../supervised/backprop/backpropanbncn.py | 4 +-
.../supervised/backprop/datasets/__init__.py | 6 +-
examples/supervised/backprop/parityrnn.py | 14 +-
.../supervised/evolino/lib/data_generator.py | 2 +-
.../supervised/evolino/superimposed_sine.py | 26 +-
.../neuralnets+svm/datasets/__init__.py | 2 +-
.../neuralnets+svm/datasets/datagenerator.py | 8 +-
.../supervised/neuralnets+svm/example_fnn.py | 8 +-
.../neuralnets+svm/example_mixturedensity.py | 212 +--
.../supervised/neuralnets+svm/example_rnn.py | 8 +-
.../supervised/neuralnets+svm/example_svm.py | 8 +-
.../jpq2layersReader.py | 4 +-
.../jpq2layersWriter.py | 8 +-
examples/unsupervised/gp.py | 8 +-
examples/unsupervised/lsh.py | 10 +-
examples/unsupervised/rbm.py | 9 +-
pybrain/auxiliary/gaussprocess.py | 10 +-
pybrain/auxiliary/kmeans.py | 5 +-
pybrain/datasets/classification.py | 20 +-
pybrain/datasets/dataset.py | 22 +-
pybrain/datasets/importance.py | 10 +-
pybrain/datasets/sequential.py | 6 +-
pybrain/datasets/supervised.py | 12 +-
.../distributionbased/__init__.py | 12 +-
.../optimization/distributionbased/cmaes.py | 16 +-
pybrain/optimization/distributionbased/fem.py | 10 +-
pybrain/optimization/distributionbased/nes.py | 6 +-
.../optimization/distributionbased/rank1.py | 13 +-
.../optimization/distributionbased/snes.py | 232 +--
pybrain/optimization/distributionbased/ves.py | 4 +-
.../optimization/distributionbased/xnes.py | 15 +-
.../optimization/finitedifference/__init__.py | 6 +-
pybrain/optimization/finitedifference/pgpe.py | 2 +-
pybrain/optimization/finitedifference/spsa.py | 2 +-
pybrain/optimization/memetic/__init__.py | 8 +-
.../memetic/innerinversememetic.py | 4 +-
pybrain/optimization/memetic/innermemetic.py | 2 +-
.../optimization/memetic/inversememetic.py | 2 +-
pybrain/optimization/memetic/memetic.py | 4 +-
pybrain/optimization/optimizer.py | 4 +-
.../populationbased/coevolution/__init__.py | 6 +-
.../coevolution/coevolution.py | 18 +-
.../coevolution/competitivecoevolution.py | 16 +-
.../coevolution/multipopulationcoevolution.py | 2 +-
pybrain/optimization/populationbased/es.py | 6 +-
pybrain/optimization/populationbased/ga.py | 12 +-
.../multiobjective/constnsga2.py | 8 +-
.../populationbased/multiobjective/nsga2.py | 8 +-
pybrain/rl/agents/linearfa.py | 2 +
.../rl/environments/cartpole/balancetask.py | 22 +-
.../rl/environments/cartpole/doublepole.py | 2 +-
.../cartpole/fast_version/cartpoleenv.py | 12 +-
.../cartpole/nonmarkovdoublepole.py | 4 +-
.../rl/environments/cartpole/nonmarkovpole.py | 2 +-
pybrain/rl/environments/classic/acrobot.py | 432 ++---
.../rl/environments/classic/mountaincar.py | 272 ++--
pybrain/rl/environments/classic/xor.py | 156 +-
.../rl/environments/flexcube/environment.py | 6 +-
pybrain/rl/environments/flexcube/tasks.py | 2 +-
pybrain/rl/environments/flexcube/viewer.py | 6 +-
.../rl/environments/functions/lennardjones.py | 4 +-
.../rl/environments/functions/multimodal.py | 4 +-
pybrain/rl/environments/functions/unimodal.py | 4 +-
pybrain/rl/environments/graphical.py | 2 +-
pybrain/rl/environments/mazes/maze.py | 2 +-
pybrain/rl/environments/mazes/polarmaze.py | 6 +-
.../rl/environments/mazes/tasks/cheesemaze.py | 2 +-
pybrain/rl/environments/mazes/tasks/maze.py | 2 +-
.../rl/environments/mazes/tasks/maze4x3.py | 2 +-
.../environments/mazes/tasks/maze89state.py | 2 +-
.../rl/environments/mazes/tasks/shuttle.py | 2 +-
pybrain/rl/environments/mazes/tasks/tiger.py | 2 +-
pybrain/rl/environments/mazes/tasks/tmaze.py | 2 +-
pybrain/rl/environments/ode/environment.py | 20 +-
pybrain/rl/environments/ode/instances/ccrl.py | 8 +-
pybrain/rl/environments/ode/tasks/ccrl.py | 4 +-
pybrain/rl/environments/ode/tools/xmltools.py | 14 +-
.../rl/environments/ode/tools/xodetools.py | 1402 +++++++++--------
pybrain/rl/environments/ode/viewer.py | 10 +-
pybrain/rl/environments/serverinterface.py | 2 +-
.../environments/shipsteer/northwardtask.py | 2 +-
pybrain/rl/environments/shipsteer/viewer.py | 4 +-
pybrain/rl/environments/simple/renderer.py | 2 +-
.../simplerace/simplecontroller.py | 4 +-
.../environments/simplerace/simpleracetask.py | 2 +-
.../environments/simplerace/simpleracetcp.py | 8 +-
.../twoplayergames/capturegame.py | 16 +-
.../capturegameplayers/clientwrapper.py | 14 +-
.../capturegameplayers/killing.py | 2 +-
.../capturegameplayers/moduledecision.py | 16 +-
.../capturegameplayers/nonsuicide.py | 2 +-
.../capturegameplayers/randomplayer.py | 2 +-
.../rl/environments/twoplayergames/gomoku.py | 8 +-
.../twoplayergames/gomokuplayers/killing.py | 2 +-
.../gomokuplayers/moduledecision.py | 14 +-
.../gomokuplayers/randomplayer.py | 2 +-
.../rl/environments/twoplayergames/pente.py | 4 +-
.../twoplayergames/tasks/handicaptask.py | 13 +-
.../twoplayergames/tasks/pentetask.py | 2 +-
.../tasks/relativegomokutask.py | 18 +-
.../twoplayergames/tasks/relativetask.py | 18 +-
pybrain/rl/experiments/queued.py | 2 +-
pybrain/rl/experiments/tournament.py | 6 +-
pybrain/rl/explorers/continuous/sde.py | 4 +-
pybrain/rl/learners/directsearch/enac.py | 5 +-
pybrain/rl/learners/directsearch/gpomdp.py | 2 +-
pybrain/rl/learners/directsearch/rwr.py | 12 +-
pybrain/rl/learners/meta/levinsearch.py | 2 +-
.../rl/learners/modelbased/leastsquares.py | 2 +-
pybrain/rl/learners/valuebased/linearfa.py | 14 +-
pybrain/structure/connections/full.py | 2 +-
pybrain/structure/connections/permutation.py | 8 +-
pybrain/structure/modulemesh.py | 7 +-
pybrain/structure/modules/evolinonetwork.py | 4 +-
pybrain/structure/modules/gate.py | 4 +-
pybrain/structure/modules/mdrnnlayer.py | 7 +-
pybrain/structure/modules/mixturedensity.py | 2 +-
.../structure/modules/statedependentlayer.py | 4 +-
pybrain/structure/networks/borderswiping.py | 10 +-
pybrain/structure/networks/convolutional.py | 8 +-
pybrain/structure/networks/custom/__init__.py | 2 +-
.../structure/networks/custom/convboard.py | 10 +-
pybrain/structure/networks/mdrnn.py | 11 +-
pybrain/structure/networks/network.py | 13 +-
.../structure/networks/neurondecomposable.py | 2 +-
pybrain/structure/networks/swiping.py | 8 +-
pybrain/supervised/evolino/filter.py | 8 +-
pybrain/supervised/evolino/gpopulation.py | 4 +-
pybrain/supervised/evolino/networkwrapper.py | 8 +-
pybrain/supervised/evolino/population.py | 2 +-
pybrain/supervised/knn/lsh/minhash.py | 6 +-
pybrain/supervised/knn/lsh/nearoptimal.py | 13 +-
pybrain/supervised/trainers/backprop.py | 15 +-
pybrain/supervised/trainers/evolino.py | 7 +-
pybrain/supervised/trainers/mixturedensity.py | 170 +-
pybrain/supervised/trainers/rprop.py | 6 +-
pybrain/supervised/trainers/svmtrainer.py | 15 +-
pybrain/tests/__init__.py | 4 +-
pybrain/tests/helpers.py | 12 +-
pybrain/tests/optimizationtest.py | 29 +-
pybrain/tests/runtests.py | 2 +-
pybrain/tests/testsuites.py | 4 +-
.../unittests/_test_equivalence_to_ctypes.py | 23 +-
.../datasets/test_datasets_datasets.py | 8 +-
.../populationbased/test_pso_ring.py | 4 +-
.../twoplayergames/test_capture_game.py | 12 +-
.../environments/twoplayergames/test_pente.py | 1 -
.../structure/modules/test_peephole_lstm.py | 3 +-
.../structure/modules/test_peephole_mdlstm.py | 2 +-
.../modules/test_simple_lstm_network.py | 1 -
.../structure/modules/test_simple_mdlstm.py | 1 -
.../custom/test_capturegame_network.py | 9 +-
.../custom/test_convolutional_nets.py | 8 +-
.../networks/test_network_decomposition.py | 1 -
.../structure/networks/test_network_sort.py | 3 +-
.../networks/test_recurrent_network.py | 1 -
.../supervised/knn/lsh/test_minhash.py | 9 +-
.../supervised/knn/lsh/test_nearoptimal.py | 2 +-
.../unittests/tools/test_rlgluebridge.py | 6 +-
pybrain/tools/aptativeresampling.py | 13 +-
pybrain/tools/customxml/__init__.py | 4 +-
pybrain/tools/customxml/handling.py | 10 +-
pybrain/tools/customxml/networkreader.py | 11 +-
pybrain/tools/customxml/networkwriter.py | 4 +-
pybrain/tools/datasets/mnist.py | 8 +-
pybrain/tools/datasettools.py | 17 +-
pybrain/tools/example_tools.py | 16 +-
pybrain/tools/filehandling.py | 13 +-
pybrain/tools/fisher.py | 2 +-
pybrain/tools/functions.py | 10 +-
pybrain/tools/gridsearch.py | 27 +-
pybrain/tools/ibp.py | 2 +-
pybrain/tools/kwargsprocessor.py | 17 +-
pybrain/tools/mixtures/mogpuremax.py | 8 +-
pybrain/tools/networking/udpconnection.py | 10 +-
pybrain/tools/nondominated.py | 44 +-
pybrain/tools/plotting/__init__.py | 6 +-
pybrain/tools/plotting/ciaoplot.py | 4 +-
pybrain/tools/plotting/classification.py | 12 +-
pybrain/tools/plotting/fitnessprogression.py | 23 +-
pybrain/tools/plotting/multiline.py | 2 +-
pybrain/tools/plotting/quickvariations.py | 8 +-
pybrain/tools/rankingfunctions.py | 6 +-
pybrain/tools/rlgluebridge.py | 8 +-
pybrain/tools/shortcuts.py | 6 +-
pybrain/tools/svmdata.py | 11 +-
pybrain/tools/validation.py | 8 +-
pybrain/unsupervised/trainers/deepbelief.py | 8 +-
pybrain/unsupervised/trainers/rbm.py | 2 +-
pybrain/utilities.py | 34 +-
206 files changed, 2374 insertions(+), 2222 deletions(-)
diff --git a/docs/tutorials/blackboxoptimization.py b/docs/tutorials/blackboxoptimization.py
index a2f1c23ef..7ebd31a1c 100644
--- a/docs/tutorials/blackboxoptimization.py
+++ b/docs/tutorials/blackboxoptimization.py
@@ -36,17 +36,17 @@
thetask = BalanceTask()
theparams = buildNetwork(thetask.outdim, thetask.indim, bias=False)
-print 'Subsequently, we attempt to solve the following task:'
-print thetask.__class__.__name__
+print('Subsequently, we attempt to solve the following task:')
+print(thetask.__class__.__name__)
if isinstance(theparams, Network):
- print '\nby finding good weights for this (simple) network:'
- print theparams
- print '\nwhich has', theparams.paramdim, 'trainable parameters. (the dimensions of its layers are:',
+ print('\nby finding good weights for this (simple) network:')
+ print(theparams)
+ print('\nwhich has', theparams.paramdim, 'trainable parameters. (the dimensions of its layers are:', end=' ')
for m in theparams.modules:
- print m.indim, ',',
- print ')\n'
+ print(m.indim, ',', end=' ')
+ print(')\n')
""" We allow every algorithm a limited number of evaluations. """
@@ -54,50 +54,50 @@
""" Standard function minimization: """
-print 'fmin', NelderMead(thetask, theparams, maxEvaluations=maxEvals).learn()
+print('fmin', NelderMead(thetask, theparams, maxEvaluations=maxEvals).learn())
""" The same, using some other algorithms
(note that the syntax for invoking them is always the same) """
-print 'CMA', CMAES(thetask, theparams, maxEvaluations=maxEvals).learn()
-print 'NES', ExactNES(thetask, theparams, maxEvaluations=maxEvals).learn()
-print 'FEM', FEM(thetask, theparams, maxEvaluations=maxEvals).learn()
-print 'Finite Differences', FiniteDifferences(thetask, theparams, maxEvaluations=maxEvals).learn()
-print 'SPSA', SimpleSPSA(thetask, theparams, maxEvaluations=maxEvals).learn()
-print 'PGPE', PGPE(thetask, theparams, maxEvaluations=maxEvals).learn()
+print('CMA', CMAES(thetask, theparams, maxEvaluations=maxEvals).learn())
+print('NES', ExactNES(thetask, theparams, maxEvaluations=maxEvals).learn())
+print('FEM', FEM(thetask, theparams, maxEvaluations=maxEvals).learn())
+print('Finite Differences', FiniteDifferences(thetask, theparams, maxEvaluations=maxEvals).learn())
+print('SPSA', SimpleSPSA(thetask, theparams, maxEvaluations=maxEvals).learn())
+print('PGPE', PGPE(thetask, theparams, maxEvaluations=maxEvals).learn())
""" Evolutionary methods fall in the Learner framework as well.
All the following are examples."""
-print 'HillClimber', HillClimber(thetask, theparams, maxEvaluations=maxEvals).learn()
-print 'WeightGuessing', WeightGuessing(thetask, theparams, maxEvaluations=maxEvals).learn()
-print 'ES 50+50', ES(thetask, theparams, maxEvaluations=maxEvals).learn()
+print('HillClimber', HillClimber(thetask, theparams, maxEvaluations=maxEvals).learn())
+print('WeightGuessing', WeightGuessing(thetask, theparams, maxEvaluations=maxEvals).learn())
+print('ES 50+50', ES(thetask, theparams, maxEvaluations=maxEvals).learn())
""" We can change some default parameters, e.g."""
-print 'ES 5+5', ES(thetask, theparams, mu=5, lambada=5, maxEvaluations=maxEvals).learn()
+print('ES 5+5', ES(thetask, theparams, mu=5, lambada=5, maxEvaluations=maxEvals).learn())
""" Memetic algorithms are a kind of meta-algorithm, doing topology mutations
on the top-level, and using other algorithms internally
as a kind of local search (default there: hill-climbing)."""
-print 'Memetic Climber', MemeticSearch(thetask, theparams, maxEvaluations=maxEvals).learn()
-print 'Memetic ES 50+50', MemeticSearch(thetask, theparams, maxEvaluations=maxEvals,
- localSearch=ES, localSteps=200).learn()
-print 'Memetic ES 5+5', MemeticSearch(thetask, theparams, maxEvaluations=maxEvals,
+print('Memetic Climber', MemeticSearch(thetask, theparams, maxEvaluations=maxEvals).learn())
+print('Memetic ES 50+50', MemeticSearch(thetask, theparams, maxEvaluations=maxEvals,
+ localSearch=ES, localSteps=200).learn())
+print('Memetic ES 5+5', MemeticSearch(thetask, theparams, maxEvaluations=maxEvals,
localSearch=ES,
- localSearchArgs={'mu': 5, 'lambada': 5}).learn()
-print 'Memetic NES', MemeticSearch(thetask, theparams, maxEvaluations=maxEvals,
+ localSearchArgs={'mu': 5, 'lambada': 5}).learn())
+print('Memetic NES', MemeticSearch(thetask, theparams, maxEvaluations=maxEvals,
localSearch=ExactNES,
- localSearchArgs={'batchSize': 20}).learn()
+ localSearchArgs={'batchSize': 20}).learn())
""" Inner memetic is the population based variant (on the topology level). """
-print 'Inner Memetic Climber', InnerMemeticSearch(thetask, theparams, maxEvaluations=maxEvals).learn()
-print 'Inner Memetic CMA', InnerMemeticSearch(thetask, theparams, maxEvaluations=maxEvals,
- localSearch=CMAES).learn()
+print('Inner Memetic Climber', InnerMemeticSearch(thetask, theparams, maxEvaluations=maxEvals).learn())
+print('Inner Memetic CMA', InnerMemeticSearch(thetask, theparams, maxEvaluations=maxEvals,
+ localSearch=CMAES).learn())
""" Inverse memetic algorithms do local search on topology mutations,
and weight changes in the top-level search. """
-print 'Inverse Memetic Climber', InverseMemeticSearch(thetask, theparams, maxEvaluations=maxEvals).learn()
+print('Inverse Memetic Climber', InverseMemeticSearch(thetask, theparams, maxEvaluations=maxEvals).learn())
diff --git a/docs/tutorials/fnn.py b/docs/tutorials/fnn.py
index d605ea059..6f319fe44 100644
--- a/docs/tutorials/fnn.py
+++ b/docs/tutorials/fnn.py
@@ -28,7 +28,7 @@
means = [(-1, 0), (2, 4), (3, 1)]
cov = [diag([1, 1]), diag([0.5, 1.2]), diag([1.5, 0.7])]
alldata = ClassificationDataSet(2, 1, nb_classes=3)
-for n in xrange(400):
+for n in range(400):
for klass in range(3):
input = multivariate_normal(means[klass], cov[klass])
alldata.addSample(input, [klass])
@@ -44,10 +44,10 @@
tstdata._convertToOneOfMany()
""" Test our dataset by printing a little information about it. """
-print "Number of training patterns: ", len(trndata)
-print "Input and output dimensions: ", trndata.indim, trndata.outdim
-print "First sample (input, target, class):"
-print trndata['input'][0], trndata['target'][0], trndata['class'][0]
+print("Number of training patterns: ", len(trndata))
+print("Input and output dimensions: ", trndata.indim, trndata.outdim)
+print("First sample (input, target, class):")
+print(trndata['input'][0], trndata['target'][0], trndata['class'][0])
""" Now build a feed-forward network with 5 hidden units. We use the a convenience
function for this. The input and output
@@ -78,7 +78,7 @@
X, Y = meshgrid(ticks, ticks)
# need column vectors in dataset, not arrays
griddata = ClassificationDataSet(2, 1, nb_classes=3)
-for i in xrange(X.size):
+for i in range(X.size):
griddata.addSample([X.ravel()[i], Y.ravel()[i]], [0])
griddata._convertToOneOfMany() # this is still needed to make the fnn feel comfy
@@ -96,9 +96,9 @@
tstresult = percentError(trainer.testOnClassData(
dataset=tstdata), tstdata['class'])
- print "epoch: %4d" % trainer.totalepochs, \
+ print("epoch: %4d" % trainer.totalepochs, \
" train error: %5.2f%%" % trnresult, \
- " test error: %5.2f%%" % tstresult
+ " test error: %5.2f%%" % tstresult)
""" Run our grid data through the FNN, get the most likely class
and shape it into a square array again. """
diff --git a/docs/tutorials/networks.py b/docs/tutorials/networks.py
index 6919a67e0..b521d41a0 100644
--- a/docs/tutorials/networks.py
+++ b/docs/tutorials/networks.py
@@ -48,21 +48,21 @@
""" Let's see what we did. """
-print n
+print(n)
""" One way of using the network is to call its 'activate()' method with an input to be transformed. """
-print n.activate([1, 2])
+print(n.activate([1, 2]))
""" We can access the trainable parameters (weights) of a connection directly, or read
all weights of the network at once. """
-print hidden2out.params
-print n.params
+print(hidden2out.params)
+print(n.params)
""" The former are the last slice of the latter. """
-print n.params[-3:] == hidden2out.params
+print(n.params[-3:] == hidden2out.params)
""" Ok, after having covered the basics, let's move on to some additional concepts.
First of all, we encourage you to name all modules, or connections you create, because that gives you
@@ -79,7 +79,7 @@
n2.sortModules()
""" Printouts look more concise and readable: """
-print n2
+print(n2)
""" There is an even quicker way to build networks though, as long as their structure is nothing
more fancy than a stack of fully connected layers: """
@@ -96,21 +96,21 @@
""" After every structural modification, if we want ot use the network, we call 'sortModules()' again"""
n2.sortModules()
-print n2
+print(n2)
""" As the network is now recurrent, successive activations produce different outputs: """
-print n2.activate([1, 2]),
-print n2.activate([1, 2]),
-print n2.activate([1, 2])
+print(n2.activate([1, 2]), end=' ')
+print(n2.activate([1, 2]), end=' ')
+print(n2.activate([1, 2]))
""" The 'reset()' method re-initializes the network, and with it sets the recurrent
activations to zero, so now we get the same results: """
n2.reset()
-print n2.activate([1, 2]),
-print n2.activate([1, 2]),
-print n2.activate([1, 2])
+print(n2.activate([1, 2]), end=' ')
+print(n2.activate([1, 2]), end=' ')
+print(n2.activate([1, 2]))
""" This is already a good coverage of the basics, but if you're an advanced user
you might want to find out about the possibilities of nesting networks within
diff --git a/examples/optimization/multiobjective/constnsga2jpq.py b/examples/optimization/multiobjective/constnsga2jpq.py
index bfdbd6dbd..3120219a9 100644
--- a/examples/optimization/multiobjective/constnsga2jpq.py
+++ b/examples/optimization/multiobjective/constnsga2jpq.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
""" An illustration of using the NSGA-II multi-objective optimization algorithm
on Constrained Multi-Objective Optimization benchmark function. """
@@ -28,12 +30,12 @@
# the optimization for a maximum of 25 generations
n = ConstMultiObjectiveGA(f, x0, storeAllEvaluations = True, populationSize = 100, eliteProportion = 1.0,
topProportion = 1.0, mutationProb = 1.0, mutationStdDev = 0.3, storeAllPopulations = True, allowEquality = False)
-print 'Start Learning'
+print('Start Learning')
n.learn(50)
-print 'End Learning'
+print('End Learning')
# plotting the results (blue = all evaluated points, red = resulting pareto front)
-print 'Plotting the Results'
-print 'All Evaluations.... take some time'
+print('Plotting the Results')
+print('All Evaluations.... take some time')
for x in n._allEvaluations:
if x[1]:
pylab.plot([x[0][0]], [x[0][1]], 'b.')
@@ -41,13 +43,13 @@
pylab.plot([x[0][0]], [x[0][1]], 'r.')
for x in n.bestEvaluation: pylab.plot([x[0][0]], [x[0][1]], 'go')
pylab.show()
-print 'Pareto Front'
+print('Pareto Front')
for x in n.bestEvaluation: pylab.plot([x[0][0]], [x[0][1]], 'go')
pylab.show()
-print '==========='
-print '= Results ='
-print '==========='
+print('===========')
+print('= Results =')
+print('===========')
'''
i=0
for gen in n._allGenerations:
@@ -56,13 +58,13 @@
print gen[1].keys()[j],gen[1].values()[j]
i+=1
'''
-print 'Population size ',n.populationSize
-print 'Elitism Proportion ',n.eliteProportion
-print 'Mutation Probability ',n.mutationProb
-print 'Mutation Std Deviation ',n.mutationStdDev
-print 'Objective Evaluation number ',n.numEvaluations
-print 'last generation Length of bestEvaluation ',len(n.bestEvaluation)
-print 'Best Evaluable : Best Evaluation'
+print('Population size ',n.populationSize)
+print('Elitism Proportion ',n.eliteProportion)
+print('Mutation Probability ',n.mutationProb)
+print('Mutation Std Deviation ',n.mutationStdDev)
+print('Objective Evaluation number ',n.numEvaluations)
+print('last generation Length of bestEvaluation ',len(n.bestEvaluation))
+print('Best Evaluable : Best Evaluation')
for i in range(len(n.bestEvaluation)):
assert len(n.bestEvaluation) == len(n.bestEvaluable)
- print n.bestEvaluable[i],':',n.bestEvaluation[i]
\ No newline at end of file
+ print(n.bestEvaluable[i],':',n.bestEvaluation[i])
\ No newline at end of file
diff --git a/examples/optimization/multiobjective/nsga2jpq.py b/examples/optimization/multiobjective/nsga2jpq.py
index 05dc55813..e6d7d98da 100644
--- a/examples/optimization/multiobjective/nsga2jpq.py
+++ b/examples/optimization/multiobjective/nsga2jpq.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
""" An illustration of using the NSGA-II multi-objective optimization algorithm
on Unconstrained Multi-Objective Optimization benchmark function. """
@@ -22,22 +24,22 @@
# the optimization for a maximum of 25 generations
n = MultiObjectiveGA(f, x0, storeAllEvaluations = True, populationSize = 50, eliteProportion = 1.0,
topProportion = 1.0, mutationProb = 0.5, mutationStdDev = 0.1, storeAllPopulations = True, allowEquality = False)
-print 'Start Learning'
+print('Start Learning')
n.learn(30)
-print 'End Learning'
+print('End Learning')
# plotting the results (blue = all evaluated points, red = resulting pareto front)
-print 'Plotting the Results'
-print 'All Evaluations'
+print('Plotting the Results')
+print('All Evaluations')
for x in n._allEvaluations: pylab.plot([x[0]], [x[1]], 'b.')
for x in n.bestEvaluation: pylab.plot([x[0]], [x[1]], 'ro')
pylab.show()
-print 'Pareto Front'
+print('Pareto Front')
for x in n.bestEvaluation: pylab.plot([x[0]], [x[1]], 'ro')
pylab.show()
-print '==========='
-print '= Results ='
-print '==========='
+print('===========')
+print('= Results =')
+print('===========')
'''
i=0
for gen in n._allGenerations:
@@ -46,13 +48,13 @@
print gen[1].keys()[j],gen[1].values()[j]
i+=1
'''
-print 'Population size ',n.populationSize
-print 'Elitism Proportion ',n.eliteProportion
-print 'Mutation Probability ',n.mutationProb
-print 'Mutation Std Deviation ',n.mutationStdDev
-print 'Objective Evaluation number ',n.numEvaluations
-print 'last generation Length of bestEvaluation ',len(n.bestEvaluation)
-print 'Best Evaluable : Best Evaluation'
+print('Population size ',n.populationSize)
+print('Elitism Proportion ',n.eliteProportion)
+print('Mutation Probability ',n.mutationProb)
+print('Mutation Std Deviation ',n.mutationStdDev)
+print('Objective Evaluation number ',n.numEvaluations)
+print('last generation Length of bestEvaluation ',len(n.bestEvaluation))
+print('Best Evaluable : Best Evaluation')
for i in range(len(n.bestEvaluation)):
assert len(n.bestEvaluation) == len(n.bestEvaluable)
- print n.bestEvaluable[i],':',n.bestEvaluation[i]
\ No newline at end of file
+ print(n.bestEvaluable[i],':',n.bestEvaluation[i])
\ No newline at end of file
diff --git a/examples/optimization/optimizerinterface.py b/examples/optimization/optimizerinterface.py
index 5e2f3ab31..2f2ec3a4a 100644
--- a/examples/optimization/optimizerinterface.py
+++ b/examples/optimization/optimizerinterface.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
"""
Illustrating the interface of black-box optimizers on a few simple problems:
@@ -29,7 +31,7 @@
#algo = MemeticSearch
#algo = NelderMead
algo = CMAES
-print 'Algorithm:', algo.__name__
+print('Algorithm:', algo.__name__)
# ------------------------
@@ -63,8 +65,8 @@
# but if none is the case this leads to an error:
try:
l = algo(lambda x: sum(x)**2)
-except ValueError, e:
- print 'Error caught:', e
+except ValueError as e:
+ print('Error caught:', e)
# Initialization can also take place in 2 steps, first with the settings and then with the
# evaluator function:
@@ -75,8 +77,8 @@
l = algo()
try:
l.learn(0)
-except AssertionError, e:
- print 'Error caught:', e
+except AssertionError as e:
+ print('Error caught:', e)
l.setEvaluator(f)
# no error anymore
l.learn(0)
@@ -103,7 +105,7 @@
# -----------------------
# Learning is even simpler:
-print l.learn(5)
+print(l.learn(5))
# The return values are the best point found, and its fitness
# (the argument indicates the number of learning steps/generations).
@@ -113,20 +115,20 @@
# a) maximal number of evaluations (accessible in .numEvaluations)
l = algo(f, maxEvaluations = 20)
l.learn()
-print l.learn(), 'in', l.numEvaluations, 'evaluations.'
+print(l.learn(), 'in', l.numEvaluations, 'evaluations.')
# b) desiredValue
l = algo(f, desiredEvaluation = 10)
-print l.learn(), ': fitness below 10 (we minimize the function).'
+print(l.learn(), ': fitness below 10 (we minimize the function).')
# c) maximal number of learning steps
l = algo(f, maxLearningSteps = 25)
l.learn()
-print l.learn(), 'in', l.numLearningSteps, 'learning steps.'
+print(l.learn(), 'in', l.numLearningSteps, 'learning steps.')
# it is possible to continue learning from where we left off, for a
# specific number of additional learning steps:
-print l.learn(75), 'in', l.numLearningSteps, 'total learning steps.'
+print(l.learn(75), 'in', l.numLearningSteps, 'total learning steps.')
# Finally you can set storage settings and then access all evaluations made
# during learning, e.g. for plotting:
@@ -134,9 +136,9 @@
l.learn()
try:
import pylab
- pylab.plot(map(abs,l._allEvaluations))
+ pylab.plot(list(map(abs,l._allEvaluations)))
pylab.semilogy()
pylab.show()
-except ImportError, e:
- print 'No plotting:', e
+except ImportError as e:
+ print('No plotting:', e)
diff --git a/examples/optimization/optimizers_for_rl.py b/examples/optimization/optimizers_for_rl.py
index e99356b6e..7b565ce57 100644
--- a/examples/optimization/optimizers_for_rl.py
+++ b/examples/optimization/optimizers_for_rl.py
@@ -1,3 +1,4 @@
+from __future__ import print_function
#!/usr/bin/env python
"""
Illustrating how to use optimization algorithms in a reinforcement learning framework.
@@ -36,10 +37,10 @@
exp = EpisodicExperiment(task, agent)
exp.doEpisodes(100)
-print 'Episodes learned from:', len(learner._allEvaluations)
+print('Episodes learned from:', len(learner._allEvaluations))
n, fit = learner._bestFound()
-print 'Best fitness found:', fit
-print 'with this network:'
-print n
-print 'containing these parameters:'
-print fListToString(n.params, 4)
+print('Best fitness found:', fit)
+print('with this network:')
+print(n)
+print('containing these parameters:')
+print(fListToString(n.params, 4))
diff --git a/examples/rl/environments/capturegame/evolvingplayer.py b/examples/rl/environments/capturegame/evolvingplayer.py
index 63df44c49..1d849518f 100644
--- a/examples/rl/environments/capturegame/evolvingplayer.py
+++ b/examples/rl/environments/capturegame/evolvingplayer.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
""" A script illustrating how to evolve a simple Capture-Game Player
which uses a MDRNN as network, with a simple ES algorithm."""
@@ -29,7 +31,7 @@
net = CaptureGameNetwork(size = size, hsize = 2, simpleborders = True)
net = CheaplyCopiable(net)
-print net.name, 'has', net.paramdim, 'trainable parameters.'
+print(net.name, 'has', net.paramdim, 'trainable parameters.')
learner = ES(task, net, mu = 5, lambada = 5,
verbose = True, evaluatorIsNoisy = True,
@@ -41,11 +43,11 @@
bignew = newnet.getBase().resizedTo(newsize)
bigold = net.getBase().resizedTo(newsize)
-print 'The rescaled network,', bignew.name, ', has', bignew.paramdim, 'trainable parameters.'
+print('The rescaled network,', bignew.name, ', has', bignew.paramdim, 'trainable parameters.')
newtask = CaptureGameTask(newsize, averageOverGames = 50, opponent = KillingPlayer)
-print 'Old net on big board score:', newtask(bigold)
-print 'New net on big board score:', newtask(bignew)
+print('Old net on big board score:', newtask(bigold))
+print('New net on big board score:', newtask(bignew))
# plot the progression
diff --git a/examples/rl/environments/capturegame/minitournament.py b/examples/rl/environments/capturegame/minitournament.py
index 114597711..f4c37769b 100644
--- a/examples/rl/environments/capturegame/minitournament.py
+++ b/examples/rl/environments/capturegame/minitournament.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
""" A little example script showing a Capture-Game tournament between
- a random player
@@ -31,19 +33,19 @@
javaAgent = ClientCapturePlayer(game, name = 'java')
agents.append(javaAgent)
except:
- print 'No Java server available.'
+ print('No Java server available.')
-print
-print 'Starting tournament...'
+print()
+print('Starting tournament...')
tourn = Tournament(game, agents)
tourn.organize(50)
-print tourn
+print(tourn)
# try a different network, and play again:
net.randomize()
tourn.reset()
tourn.organize(50)
-print tourn
+print(tourn)
diff --git a/examples/rl/environments/capturegame/pente.py b/examples/rl/environments/capturegame/pente.py
index 953287521..4dcc80c64 100644
--- a/examples/rl/environments/capturegame/pente.py
+++ b/examples/rl/environments/capturegame/pente.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
""" A little script illustrating how to use a (randomly initialized)
convolutional network to play a game of Pente. """
@@ -11,10 +13,10 @@
dim = 7
g = PenteGame((dim, dim))
-print g
+print(g)
n = ConvolutionalBoardNetwork(dim, 5, 3)
p1 = ModuleDecidingPlayer(n, g)
p2 = RandomGomokuPlayer(g)
p2.color = g.WHITE
g.playToTheEnd(p1, p2)
-print g
+print(g)
diff --git a/examples/rl/environments/cartpole/cart_nes.py b/examples/rl/environments/cartpole/cart_nes.py
index b60066513..8f8b12f11 100644
--- a/examples/rl/environments/cartpole/cart_nes.py
+++ b/examples/rl/environments/cartpole/cart_nes.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
#########################################################################
# Reinforcement Learning with NES on the CartPoleEnvironment
@@ -8,7 +10,6 @@
__author__ = "Thomas Rueckstiess, Frank Sehnke"
-
from pybrain.tools.example_tools import ExTools
from pybrain.tools.shortcuts import buildNetwork
from pybrain.rl.environments.cartpole import CartPoleEnvironment, BalanceTask
@@ -39,7 +40,7 @@
for updates in range(epis):
for i in range(prnts):
experiment.doEpisodes(batch)
- print "Epsilon : ", agent.learner.sigma
+ print("Epsilon : ", agent.learner.sigma)
et.printResults((agent.learner._allEvaluations)[-50:-1], runs, updates)
et.addExps()
et.showExps()
diff --git a/examples/rl/environments/cartpole/play_cartpole.py b/examples/rl/environments/cartpole/play_cartpole.py
index 6c02bc7af..cab6fa4c2 100644
--- a/examples/rl/environments/cartpole/play_cartpole.py
+++ b/examples/rl/environments/cartpole/play_cartpole.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
###########################################################################
# This program takes 4 parameters at the command line and runs the
@@ -50,7 +52,7 @@
ret.append( sum(reward, 0).item() )
# print results
-print ret, "mean:",mean(ret)
+print(ret, "mean:",mean(ret))
#env.getRenderer().stop()
diff --git a/examples/rl/environments/linear_fa/bicycle.py b/examples/rl/environments/linear_fa/bicycle.py
index 5fe26660e..06ad753fb 100644
--- a/examples/rl/environments/linear_fa/bicycle.py
+++ b/examples/rl/environments/linear_fa/bicycle.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
"""An attempt to implement Randlov and Alstrom (1998). They successfully
use reinforcement learning to balance a bicycle, and to control it to drive
to a specified goal location. Their work has been used since then by a few
@@ -415,7 +417,7 @@ def update_wheel_trajectories():
r = exp.doEpisodes(1)
perform_cumreward = task.getTotalReward()
perform_cumrewards.append(perform_cumreward)
- print 'PERFORMANCE: cumreward:', perform_cumreward, 'nsteps:', len(r[0])
+ print('PERFORMANCE: cumreward:', perform_cumreward, 'nsteps:', len(r[0]))
# Swap back the learning agent.
performance_agent.reset()
diff --git a/examples/rl/environments/linear_fa/xor.py b/examples/rl/environments/linear_fa/xor.py
index cf0d260d5..1ada017f8 100644
--- a/examples/rl/environments/linear_fa/xor.py
+++ b/examples/rl/environments/linear_fa/xor.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
""" Toy example for RL with linear function approximation.
This illustrates how a 'AND'-state-space can be solved, but not
an 'XOR' space.
@@ -42,9 +44,9 @@ def giveReward(self, r):
def runExp(gamma=0, epsilon=0.1, xor=False, lr = 0.02):
if xor:
- print "Attempting the XOR task"
+ print("Attempting the XOR task")
else:
- print "Attempting the AND task"
+ print("Attempting the AND task")
task = XORTask()
task.and_task = not xor
@@ -60,16 +62,16 @@ def runExp(gamma=0, epsilon=0.1, xor=False, lr = 0.02):
sofar = 0
for i in range(30):
exp.doInteractions(100)
- print exp.task.cumreward - sofar,
+ print(exp.task.cumreward - sofar, end=' ')
if i%10 == 9:
- print
+ print()
sofar = exp.task.cumreward
l._decayLearningRate()
if __name__ == "__main__":
runExp(xor=False)
- print
+ print()
runExp(xor=True)
- print
+ print()
runExp(xor=True)
\ No newline at end of file
diff --git a/examples/rl/environments/shipsteer/shipbench_sde.py b/examples/rl/environments/shipsteer/shipbench_sde.py
index eab61c509..cb20bca46 100644
--- a/examples/rl/environments/shipsteer/shipbench_sde.py
+++ b/examples/rl/environments/shipsteer/shipbench_sde.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
#########################################################################
# Reinforcement Learning with SPE on the ShipSteering Environment
@@ -64,7 +66,7 @@
experiment = EpisodicExperiment(task, agent)
# print weights at beginning
-print agent.module.params
+print(agent.module.params)
rewards = []
if useGraphics:
@@ -93,8 +95,8 @@
reward = mean(agent.history.getSumOverSequences('reward'))*task.rewardscale
if useGraphics:
pl.addData(0,x,reward)
- print agent.module.params
- print reward
+ print(agent.module.params)
+ print(reward)
#if reward > 3:
# pass
agent.learn()
diff --git a/examples/rl/valuebased/nfq.py b/examples/rl/valuebased/nfq.py
index 5cdf042c9..9abe1fce1 100644
--- a/examples/rl/valuebased/nfq.py
+++ b/examples/rl/valuebased/nfq.py
@@ -1,7 +1,8 @@
+from __future__ import print_function
+
#!/usr/bin/env python
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
-
from pybrain.rl.environments.cartpole import CartPoleEnvironment, DiscreteBalanceTask, CartPoleRenderer
from pybrain.rl.agents import LearningAgent
from pybrain.rl.experiments import EpisodicExperiment
@@ -63,8 +64,8 @@ def plotPerformance(values, fig):
if not render:
plotPerformance(performance, pf_fig)
- print "reward avg", r
- print "explorer epsilon", learner.explorer.epsilon
- print "num episodes", agent.history.getNumSequences()
- print "update step", len(performance)
+ print("reward avg", r)
+ print("explorer epsilon", learner.explorer.epsilon)
+ print("num episodes", agent.history.getNumSequences())
+ print("update step", len(performance))
diff --git a/examples/supervised/backprop/backpropanbncn.py b/examples/supervised/backprop/backpropanbncn.py
index 58963f1a3..bda7d7964 100644
--- a/examples/supervised/backprop/backpropanbncn.py
+++ b/examples/supervised/backprop/backpropanbncn.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
# A simple recurrent neural network that learns a simple sequential data set.
@@ -28,7 +30,7 @@ def testTraining():
t.trainOnDataset(d, 200)
# the resulting weights are in the network:
- print 'Final weights:', n.params
+ print('Final weights:', n.params)
if __name__ == '__main__':
testTraining()
\ No newline at end of file
diff --git a/examples/supervised/backprop/datasets/__init__.py b/examples/supervised/backprop/datasets/__init__.py
index 7ae2bfd46..b2122c988 100644
--- a/examples/supervised/backprop/datasets/__init__.py
+++ b/examples/supervised/backprop/datasets/__init__.py
@@ -1,3 +1,3 @@
-from xor import XORDataSet, SequentialXORDataSet
-from anbncn import AnBnCnDataSet
-from parity import ParityDataSet
\ No newline at end of file
+from .xor import XORDataSet, SequentialXORDataSet
+from .anbncn import AnBnCnDataSet
+from .parity import ParityDataSet
\ No newline at end of file
diff --git a/examples/supervised/backprop/parityrnn.py b/examples/supervised/backprop/parityrnn.py
index 278e9f28b..295ba1e68 100644
--- a/examples/supervised/backprop/parityrnn.py
+++ b/examples/supervised/backprop/parityrnn.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
""" A simple recurrent neural network that detects parity for arbitrary sequences. """
@@ -36,24 +38,24 @@ def evalRnnOnSeqDataset(net, DS, verbose = False, silent = False):
for i, t in seq:
res = net.activate(i)
if verbose:
- print t, res
+ print(t, res)
r += sum((t-res)**2)
samples += 1
if verbose:
- print '-'*20
+ print('-'*20)
r /= samples
if not silent:
- print 'MSE:', r
+ print('MSE:', r)
return r
if __name__ == "__main__":
N = buildParityNet()
DS = ParityDataSet()
evalRnnOnSeqDataset(N, DS, verbose = True)
- print '(preset weights)'
+ print('(preset weights)')
N.randomize()
evalRnnOnSeqDataset(N, DS)
- print '(random weights)'
+ print('(random weights)')
# Backprop improves the network performance, and sometimes even finds the global optimum.
@@ -61,4 +63,4 @@ def evalRnnOnSeqDataset(net, DS, verbose = False, silent = False):
bp = BackpropTrainer(N, DS, verbose = True)
bp.trainEpochs(5000)
evalRnnOnSeqDataset(N, DS)
- print '(backprop-trained weights)'
+ print('(backprop-trained weights)')
diff --git a/examples/supervised/evolino/lib/data_generator.py b/examples/supervised/evolino/lib/data_generator.py
index 25a60afaa..85dac3a80 100644
--- a/examples/supervised/evolino/lib/data_generator.py
+++ b/examples/supervised/evolino/lib/data_generator.py
@@ -34,7 +34,7 @@ def generateSuperimposedSineData( sinefreqs, space, yScales=None ):
data = sine.getFuncValues(space)
dataset.newSequence()
- for i in xrange(len(data)):
+ for i in range(len(data)):
dataset.addSample([], data[i])
return dataset
diff --git a/examples/supervised/evolino/superimposed_sine.py b/examples/supervised/evolino/superimposed_sine.py
index 2b49c94e1..7bd4b3828 100644
--- a/examples/supervised/evolino/superimposed_sine.py
+++ b/examples/supervised/evolino/superimposed_sine.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
@@ -10,9 +12,9 @@
from pybrain.supervised.trainers.evolino import EvolinoTrainer
from lib.data_generator import generateSuperimposedSineData
-print
-print "=== Learning to extrapolate 5 superimposed sine waves ==="
-print
+print()
+print("=== Learning to extrapolate 5 superimposed sine waves ===")
+print()
sinefreqs = ( 0.2, 0.311, 0.42, 0.51, 0.74 )
# sinefreqs = ( 0.2, 0.311, 0.42, 0.51, 0.74, 0.81 )
metascale = 8.
@@ -26,19 +28,19 @@
# === create training dataset
# the sequences must be stored in the target field
# the input field will be ignored
-print "creating training data"
+print("creating training data")
trnInputSpace = numpy.arange( 0*scale , 190*scale , stepsize )
trnData = generateSuperimposedSineData(sinefreqs, trnInputSpace)
# === create testing dataset
-print "creating test data"
+print("creating test data")
tstInputSpace = numpy.arange( 400*scale , 540*scale , stepsize)
tstData = generateSuperimposedSineData(sinefreqs, tstInputSpace)
# === create the evolino-network
-print "creating EvolinoNetwork"
+print("creating EvolinoNetwork")
net = EvolinoNetwork( trnData.outdim, 40 )
@@ -47,7 +49,7 @@
# === instantiate an evolino trainer
# it will train our network through evolutionary algorithms
-print "creating EvolinoTrainer"
+print("creating EvolinoTrainer")
trainer = EvolinoTrainer(
net,
dataset=trnData,
@@ -80,16 +82,16 @@
ion() # switch matplotlib to interactive mode
for i in range(3000):
- print "======================"
- print "====== NEXT RUN ======"
- print "======================"
+ print("======================")
+ print("====== NEXT RUN ======")
+ print("======================")
- print "=== TRAINING"
+ print("=== TRAINING")
# train the network for 1 epoch
trainer.trainEpochs( 1 )
- print "=== PLOTTING\n"
+ print("=== PLOTTING\n")
# calculate the nets output for train and the test data
trnSequenceOutput = net.extrapolate(trnSequenceWashout, len(trnSequenceTarget))
tstSequenceOutput = net.extrapolate(tstSequenceWashout, len(tstSequenceTarget))
diff --git a/examples/supervised/neuralnets+svm/datasets/__init__.py b/examples/supervised/neuralnets+svm/datasets/__init__.py
index 70bfb6e21..a52d6499c 100644
--- a/examples/supervised/neuralnets+svm/datasets/__init__.py
+++ b/examples/supervised/neuralnets+svm/datasets/__init__.py
@@ -1 +1 @@
-from datagenerator import generateNoisySines, generateGridData, generateClassificationData, plotData
\ No newline at end of file
+from .datagenerator import generateNoisySines, generateGridData, generateClassificationData, plotData
\ No newline at end of file
diff --git a/examples/supervised/neuralnets+svm/datasets/datagenerator.py b/examples/supervised/neuralnets+svm/datasets/datagenerator.py
index 654cd8d4b..aeb929ef1 100644
--- a/examples/supervised/neuralnets+svm/datasets/datagenerator.py
+++ b/examples/supervised/neuralnets+svm/datasets/datagenerator.py
@@ -19,7 +19,7 @@ def generateClassificationData(size, nClasses=3):
cov = [diag([1,1]), diag([0.5,1.2]), diag([1.5,0.7])]
dataset = ClassificationDataSet(2, 1, nb_classes=nClasses)
- for _ in xrange(size):
+ for _ in range(size):
for c in range(3):
input = multivariate_normal(means[c],cov[c])
dataset.addSample(input, [c%nClasses])
@@ -51,14 +51,14 @@ def generateNoisySines( npoints, nseq, noise=0.3 ):
y1 = np.sin(x+rand(1)*3.)
y2 = np.sin(x/2.+rand(1)*3.)
DS = SequenceClassificationDataSet(1,1, nb_classes=2)
- for _ in xrange(nseq):
+ for _ in range(nseq):
DS.newSequence()
buf = rand(npoints)*noise + y1 + (rand(1)-0.5)*noise
- for i in xrange(npoints):
+ for i in range(npoints):
DS.addSample([buf[i]],[0])
DS.newSequence()
buf = rand(npoints)*noise + y2 + (rand(1)-0.5)*noise
- for i in xrange(npoints):
+ for i in range(npoints):
DS.addSample([buf[i]],[1])
return DS
diff --git a/examples/supervised/neuralnets+svm/example_fnn.py b/examples/supervised/neuralnets+svm/example_fnn.py
index 1ecaf50ec..c20a5b29c 100644
--- a/examples/supervised/neuralnets+svm/example_fnn.py
+++ b/examples/supervised/neuralnets+svm/example_fnn.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
# Example script for feed-forward network usage in PyBrain.
__author__ = "Martin Felder"
@@ -9,7 +11,7 @@
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure.modules import SoftmaxLayer
-from datasets import generateGridData, generateClassificationData, plotData
+from .datasets import generateGridData, generateClassificationData, plotData
# load the training data set
trndata = generateClassificationData(250)
@@ -42,9 +44,9 @@
dataset=tstdata ), tstdata['class'] )
# print the result
- print "epoch: %4d" % trainer.totalepochs, \
+ print("epoch: %4d" % trainer.totalepochs, \
" train error: %5.2f%%" % trnresult, \
- " test error: %5.2f%%" % tstresult
+ " test error: %5.2f%%" % tstresult)
# run our grid data through the FNN, get the most likely class
# and shape it into an array
diff --git a/examples/supervised/neuralnets+svm/example_mixturedensity.py b/examples/supervised/neuralnets+svm/example_mixturedensity.py
index 3214ad189..c9577ae35 100644
--- a/examples/supervised/neuralnets+svm/example_mixturedensity.py
+++ b/examples/supervised/neuralnets+svm/example_mixturedensity.py
@@ -1,106 +1,106 @@
-# $Id$
-# Train a mixture of Gaussians to approximate a multi-mode dataset.
-# It seems fairly easy to fall into some local minimum. Good solutions
-# have errors around -200.
-# This example reproduces Fig. 5.21 from Bishop (2006).
-__author__ = 'Martin Felder'
-
-import pylab as p
-import numpy as np
-from pybrain.structure.modules import LinearLayer, BiasUnit, SigmoidLayer
-from pybrain.structure import FullConnection, FeedForwardNetwork
-from pybrain.datasets import SupervisedDataSet
-from pybrain.supervised.trainers.mixturedensity import RPropMinusTrainerMix, BackpropTrainerMix
-from pybrain.structure.modules.mixturedensity import MixtureDensityLayer
-
-
-def multigaussian(x, mean, stddev):
- """Returns value of uncorrelated Gaussians at given scalar point.
- x: scalar
- mean: vector
- stddev: vector
- """
- tmp = -0.5 * ((x-mean)/stddev)**2
- return np.exp(tmp) / (np.sqrt(2.*np.pi) * stddev)
-
-
-if __name__ == '__main__':
- # build a network
- n = FeedForwardNetwork()
- # linear input layer
- n.addInputModule(LinearLayer(1, name='in'))
- # output layer of type 'outclass'
- N_GAUSSIANS = 3
- n.addOutputModule(MixtureDensityLayer(dim=1, name='out', mix=N_GAUSSIANS))
- # add bias module and connection to out module
- n.addModule(BiasUnit(name = 'bias'))
- n.addConnection(FullConnection(n['bias'], n['out']))
-
- # arbitrary number of hidden layers of type 'hiddenclass'
- n.addModule(SigmoidLayer(5, name='hidden'))
- n.addConnection(FullConnection(n['bias'], n['hidden']))
-
- # network with hidden layer(s), connections
- # from in to first hidden and last hidden to out
- n.addConnection(FullConnection(n['in'], n['hidden']))
- n.addConnection(FullConnection(n['hidden'], n['out']))
- n.sortModules()
- n._setParameters(np.random.uniform(-0.1, 0.1, size=n.paramdim))
-
- # build some data
- y = np.arange(0.0, 1.0, 0.005).reshape(200,1)
- x = (
- y +
- 0.3 * np.sin(2 * np.pi * y) +
- np.random.uniform(-0.1, 0.1, y.size).reshape(y.size, 1)
- )
- dataset = SupervisedDataSet(1, 1)
- dataset.setField('input', x)
- dataset.setField('target', y)
-
- # train the network
- trainer = RPropMinusTrainerMix(n, dataset=dataset, verbose=True,
- weightdecay=0.05)
- trainer.trainEpochs(200)
-
- # plot the density and other stuff
- p.subplot(2, 2, 3)
- dens = []
- newx = np.arange(0.0, 1.0, 0.01)
- newx = newx.reshape(newx.size, 1)
- dataset.setField('input', newx)
- out = n.activateOnDataset(dataset)
- for pars in out:
- stds = pars[N_GAUSSIANS:N_GAUSSIANS*2]
- means = pars[N_GAUSSIANS*2:N_GAUSSIANS*3]
- line = multigaussian(newx, means, stds)
- density = line[:,0] * pars[0]
- for gaussian in range(1, N_GAUSSIANS):
- density += line[:, gaussian] * pars[gaussian]
- dens.append(density)
-
- newx = newx.flatten()
- dens = np.array(dens).transpose()
- p.contourf(newx, newx, dens, 30)
- p.title("cond. probab. dens.")
-
- p.subplot(221)
- out = np.array(out)
- p.plot(newx, out[:,0:3])
- p.title("mixing coefficient")
-
- p.subplot(222)
- p.plot(newx, out[:,6:9])
- p.title("means of Gaussians")
-
- p.subplot(224)
- p.scatter(x.flatten(), y.flatten(),
- marker='o', edgecolor='g', facecolors='none')
- p.hold(True)
- cmode = dens.argmax(axis=0)
- p.plot(newx, newx[cmode], "or", markersize=3)
- p.xlim(0, 1)
- p.ylim(0, 1)
- p.title("data and cond. mode")
- p.show()
-
+# $Id$
+# Train a mixture of Gaussians to approximate a multi-mode dataset.
+# It seems fairly easy to fall into some local minimum. Good solutions
+# have errors around -200.
+# This example reproduces Fig. 5.21 from Bishop (2006).
+__author__ = 'Martin Felder'
+
+import pylab as p
+import numpy as np
+from pybrain.structure.modules import LinearLayer, BiasUnit, SigmoidLayer
+from pybrain.structure import FullConnection, FeedForwardNetwork
+from pybrain.datasets import SupervisedDataSet
+from pybrain.supervised.trainers.mixturedensity import RPropMinusTrainerMix, BackpropTrainerMix
+from pybrain.structure.modules.mixturedensity import MixtureDensityLayer
+
+
+def multigaussian(x, mean, stddev):
+ """Returns value of uncorrelated Gaussians at given scalar point.
+ x: scalar
+ mean: vector
+ stddev: vector
+ """
+ tmp = -0.5 * ((x-mean)/stddev)**2
+ return np.exp(tmp) / (np.sqrt(2.*np.pi) * stddev)
+
+
+if __name__ == '__main__':
+ # build a network
+ n = FeedForwardNetwork()
+ # linear input layer
+ n.addInputModule(LinearLayer(1, name='in'))
+ # output layer of type 'outclass'
+ N_GAUSSIANS = 3
+ n.addOutputModule(MixtureDensityLayer(dim=1, name='out', mix=N_GAUSSIANS))
+ # add bias module and connection to out module
+ n.addModule(BiasUnit(name = 'bias'))
+ n.addConnection(FullConnection(n['bias'], n['out']))
+
+ # arbitrary number of hidden layers of type 'hiddenclass'
+ n.addModule(SigmoidLayer(5, name='hidden'))
+ n.addConnection(FullConnection(n['bias'], n['hidden']))
+
+ # network with hidden layer(s), connections
+ # from in to first hidden and last hidden to out
+ n.addConnection(FullConnection(n['in'], n['hidden']))
+ n.addConnection(FullConnection(n['hidden'], n['out']))
+ n.sortModules()
+ n._setParameters(np.random.uniform(-0.1, 0.1, size=n.paramdim))
+
+ # build some data
+ y = np.arange(0.0, 1.0, 0.005).reshape(200,1)
+ x = (
+ y +
+ 0.3 * np.sin(2 * np.pi * y) +
+ np.random.uniform(-0.1, 0.1, y.size).reshape(y.size, 1)
+ )
+ dataset = SupervisedDataSet(1, 1)
+ dataset.setField('input', x)
+ dataset.setField('target', y)
+
+ # train the network
+ trainer = RPropMinusTrainerMix(n, dataset=dataset, verbose=True,
+ weightdecay=0.05)
+ trainer.trainEpochs(200)
+
+ # plot the density and other stuff
+ p.subplot(2, 2, 3)
+ dens = []
+ newx = np.arange(0.0, 1.0, 0.01)
+ newx = newx.reshape(newx.size, 1)
+ dataset.setField('input', newx)
+ out = n.activateOnDataset(dataset)
+ for pars in out:
+ stds = pars[N_GAUSSIANS:N_GAUSSIANS*2]
+ means = pars[N_GAUSSIANS*2:N_GAUSSIANS*3]
+ line = multigaussian(newx, means, stds)
+ density = line[:,0] * pars[0]
+ for gaussian in range(1, N_GAUSSIANS):
+ density += line[:, gaussian] * pars[gaussian]
+ dens.append(density)
+
+ newx = newx.flatten()
+ dens = np.array(dens).transpose()
+ p.contourf(newx, newx, dens, 30)
+ p.title("cond. probab. dens.")
+
+ p.subplot(221)
+ out = np.array(out)
+ p.plot(newx, out[:,0:3])
+ p.title("mixing coefficient")
+
+ p.subplot(222)
+ p.plot(newx, out[:,6:9])
+ p.title("means of Gaussians")
+
+ p.subplot(224)
+ p.scatter(x.flatten(), y.flatten(),
+ marker='o', edgecolor='g', facecolors='none')
+ p.hold(True)
+ cmode = dens.argmax(axis=0)
+ p.plot(newx, newx[cmode], "or", markersize=3)
+ p.xlim(0, 1)
+ p.ylim(0, 1)
+ p.title("data and cond. mode")
+ p.show()
+
diff --git a/examples/supervised/neuralnets+svm/example_rnn.py b/examples/supervised/neuralnets+svm/example_rnn.py
index 6baeecea1..f6b3c9046 100644
--- a/examples/supervised/neuralnets+svm/example_rnn.py
+++ b/examples/supervised/neuralnets+svm/example_rnn.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
# Example script for recurrent network usage in PyBrain.
__author__ = "Martin Felder"
@@ -11,7 +13,7 @@
from pybrain.tools.validation import testOnSequenceData
from pybrain.tools.shortcuts import buildNetwork
-from datasets import generateNoisySines
+from .datasets import generateNoisySines
# create training and test data
trndata = generateNoisySines(50, 40)
@@ -28,11 +30,11 @@
##trainer = BackpropTrainer( rnn, dataset=trndata, verbose=True, momentum=0.9, learningrate=0.00001 )
# carry out the training
-for i in xrange(100):
+for i in range(100):
trainer.trainEpochs( 2 )
trnresult = 100. * (1.0-testOnSequenceData(rnn, trndata))
tstresult = 100. * (1.0-testOnSequenceData(rnn, tstdata))
- print "train error: %5.2f%%" % trnresult, ", test error: %5.2f%%" % tstresult
+ print("train error: %5.2f%%" % trnresult, ", test error: %5.2f%%" % tstresult)
# just for reference, plot the first 5 timeseries
plot(trndata['input'][0:250,:],'-o')
diff --git a/examples/supervised/neuralnets+svm/example_svm.py b/examples/supervised/neuralnets+svm/example_svm.py
index db91d8e9c..e859e92b8 100644
--- a/examples/supervised/neuralnets+svm/example_svm.py
+++ b/examples/supervised/neuralnets+svm/example_svm.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
""" Example script for SVM classification using PyBrain and LIBSVM
CAVEAT: Needs the libsvm Python file svm.py and the corresponding (compiled) library to reside in the Python path! """
@@ -17,7 +19,7 @@
from pybrain.supervised.trainers.svmtrainer import SVMTrainer
# import some local stuff
-from datasets import generateClassificationData, plotData, generateGridData
+from .datasets import generateClassificationData, plotData, generateGridData
logging.basicConfig(level=logging.INFO, filename=join('.','testrun.log'),
format='%(asctime)s %(levelname)s %(message)s')
@@ -42,7 +44,7 @@
# pass data sets through the SVM to get performance
trnresult = percentError( svm.activateOnDataset(trndata), trndata['target'] )
tstresult = percentError( svm.activateOnDataset(tstdata), tstdata['target'] )
-print "sigma: %7g, C: %7g, train error: %5.2f%%, test error: %5.2f%%" % (2.0**log2g, 2.0**log2C, trnresult, tstresult)
+print("sigma: %7g, C: %7g, train error: %5.2f%%, test error: %5.2f%%" % (2.0**log2g, 2.0**log2C, trnresult, tstresult))
# generate a grid dataset
griddat, X, Y = generateGridData(x=[-4,8,0.1],y=[-2,3,0.1])
@@ -52,7 +54,7 @@
Z = svm.activateOnDataset(griddat, values=True)
# the output format is a bit weird... make it into a decent array
-Z = p.array([z.values()[0] for z in Z]).reshape(X.shape)
+Z = p.array([list(z.values())[0] for z in Z]).reshape(X.shape)
# make a 2d plot of training data with an decision value contour overlay
fig = p.figure()
diff --git a/examples/supervised/test_network_read_write/jpq2layersReader.py b/examples/supervised/test_network_read_write/jpq2layersReader.py
index be90f0a8c..c0e5fb49a 100644
--- a/examples/supervised/test_network_read_write/jpq2layersReader.py
+++ b/examples/supervised/test_network_read_write/jpq2layersReader.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
from pybrain.structure import FeedForwardNetwork
from pybrain.tools.validation import ModuleValidator,Validator
from pybrain.utilities import percentError
@@ -64,7 +66,7 @@ def myplot(trns,ctrns = None,tsts = None,ctsts = None,iter = 0):
#calculate the test DataSet based on the trained Neural Network
ctsts = mv.calculateModuleOutput(n,tsts)
tserr = v.MSE(ctsts,tsts['target'])
- print 'MSE error on TSTS:',tserr
+ print('MSE error on TSTS:',tserr)
myplot(trndata,tsts = tsts,ctsts = ctsts)
pylab.show()
diff --git a/examples/supervised/test_network_read_write/jpq2layersWriter.py b/examples/supervised/test_network_read_write/jpq2layersWriter.py
index 15344f2d5..b80ae5d42 100644
--- a/examples/supervised/test_network_read_write/jpq2layersWriter.py
+++ b/examples/supervised/test_network_read_write/jpq2layersWriter.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
from pybrain.structure import FeedForwardNetwork
from pybrain.structure import LinearLayer, SigmoidLayer
from pybrain.structure import BiasUnit,TanhLayer
@@ -103,14 +105,14 @@ def myplot(trns,ctrns,tsts = None,ctsts = None,iter = 0):
#train the neural network from the train DataSet
cterrori=1.0
-print "trainer momentum:"+str(mom)
+print("trainer momentum:"+str(mom))
for iter in range(25):
t.trainOnDataset(trndata, 1000)
ctrndata = mv.calculateModuleOutput(n,trndata)
cterr = v.MSE(ctrndata,trndata['target'])
relerr = abs(cterr-cterrori)
cterrori = cterr
- print 'iteration:',iter+1,'MSE error:',cterr
+ print('iteration:',iter+1,'MSE error:',cterr)
myplot(trndata,ctrndata,iter=iter+1)
if cterr < 1.e-5 or relerr < 1.e-7:
break
@@ -124,7 +126,7 @@ def myplot(trns,ctrns,tsts = None,ctsts = None,iter = 0):
#calculate the test DataSet based on the trained Neural Network
ctsts = mv.calculateModuleOutput(n,tsts)
tserr = v.MSE(ctsts,tsts['target'])
-print 'MSE error on TSTS:',tserr
+print('MSE error on TSTS:',tserr)
myplot(trndata,ctrndata,tsts,ctsts)
pylab.show()
diff --git a/examples/unsupervised/gp.py b/examples/unsupervised/gp.py
index 663d7ee94..078f6a578 100644
--- a/examples/unsupervised/gp.py
+++ b/examples/unsupervised/gp.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#!/usr/bin/env python
""" A simple example on how to use the GaussianProcess class
in pybrain, for one and two dimensions. """
@@ -30,7 +32,7 @@
# you can also test the gp on single points, but this deletes the
# original testing grid. it can be restored with a call to _buildGrid()
-print gp.testOnArray(array([[0.4]]))
+print(gp.testOnArray(array([[0.4]])))
# --- example on how to use the GP in 2 dimensions
@@ -41,12 +43,12 @@
x,y = mgrid[0:5:4j, 0:5:4j]
z = cos(x)*sin(y)
-(x, y, z) = map(ravel, [x, y, z])
+(x, y, z) = list(map(ravel, [x, y, z]))
for i,j,k in zip(x, y, z):
ds.addSample([i, j], [k])
-print "preparing plots. this can take a few seconds..."
+print("preparing plots. this can take a few seconds...")
gp.trainOnDataset(ds)
gp.plotCurves()
diff --git a/examples/unsupervised/lsh.py b/examples/unsupervised/lsh.py
index 737e323e7..9630c0614 100644
--- a/examples/unsupervised/lsh.py
+++ b/examples/unsupervised/lsh.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
-from __future__ import division
+
__author__ = 'Justin Bayer, bayer.justin@googlemail.com'
@@ -31,7 +31,7 @@ def makePoint():
vectorLength = lambda x: dot(x.T, x)
return point, 'blue' if vectorLength(point - center) < 25 else 'red'
- return [makePoint() for _ in xrange(amount)]
+ return [makePoint() for _ in range(amount)]
if __name__ == '__main__':
@@ -64,8 +64,8 @@ def makePoint():
result = []
width, height = 2**5, 2**5
grid = (array([i / width * 10, j / height * 10])
- for i in xrange(width)
- for j in xrange(height))
+ for i in range(width)
+ for j in range(height))
projected_grid = [(p, dot(p, proj)) for p in grid]
# Just to fake random access
@@ -95,7 +95,7 @@ def makePoint():
plot(xs_blue, ys_blue, 'bo')
show()
- ballsizes = (len(ball) for ball in m.balls.itervalues())
+ ballsizes = (len(ball) for ball in m.balls.values())
logging.info("Sizes of the balls: " + " ".join(str(i) for i in ballsizes))
logging.info("Finished")
diff --git a/examples/unsupervised/rbm.py b/examples/unsupervised/rbm.py
index dc394bd66..2a4ad6c04 100644
--- a/examples/unsupervised/rbm.py
+++ b/examples/unsupervised/rbm.py
@@ -1,9 +1,10 @@
+from __future__ import print_function
+
#!/usr/bin/env python
""" Miniscule restricted Boltzmann machine usage example """
__author__ = 'Justin S Bayer, bayer.justin@googlemail.com'
-
from pybrain.structure.networks.rbm import Rbm
from pybrain.unsupervised.trainers.rbm import (RbmGibbsTrainerConfig,
RbmBernoulliTrainer)
@@ -19,8 +20,8 @@
rbm = Rbm.fromDims(6, 1)
trainer = RbmBernoulliTrainer(rbm, ds, cfg)
-print rbm.params, rbm.biasParams
-for _ in xrange(50):
+print(rbm.params, rbm.biasParams)
+for _ in range(50):
trainer.train()
-print rbm.params, rbm.biasParams
+print(rbm.params, rbm.biasParams)
diff --git a/pybrain/auxiliary/gaussprocess.py b/pybrain/auxiliary/gaussprocess.py
index 59830d59c..0deda606c 100644
--- a/pybrain/auxiliary/gaussprocess.py
+++ b/pybrain/auxiliary/gaussprocess.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de; Christian Osendorfer, osendorf@in.tum.de'
@@ -51,7 +53,7 @@ def _kernel(self, a, b):
def _buildGrid(self):
(start, stop, step) = (self.start, self.stop, self.step)
""" returns a mgrid type of array for 'dim' dimensions """
- if isinstance(start, (int, long, float, complex)):
+ if isinstance(start, (int, float, complex)):
dimstr = 'start:stop:step, '*self.indim
else:
assert len(start) == len(stop) == len(step)
@@ -184,7 +186,7 @@ def plotCurves(self, showSamples=False, force2D=True):
ax.plot3D(ravel(self.trainx[:, 0]), ravel(self.trainx[:, 1]), ravel(self.trainy), 'ro')
# plot mean
- (x, y, z) = map(lambda m: m.reshape(sqrt(len(m)), sqrt(len(m))), (self.testx[:, 0], self.testx[:, 1], self.pred_mean))
+ (x, y, z) = [m.reshape(sqrt(len(m)), sqrt(len(m))) for m in (self.testx[:, 0], self.testx[:, 1], self.pred_mean)]
ax.plot_wireframe(x, y, z, colors='gray')
return ax
@@ -224,7 +226,7 @@ def plotCurves(self, showSamples=False, force2D=True):
# you can also test the gp on single points, but this deletes the
# original testing grid. it can be restored with a call to _buildGrid()
- print(gp.testOnArray(array([[0.4]])))
+ print((gp.testOnArray(array([[0.4]]))))
# --- example on how to use the GP in 2 dimensions
@@ -235,7 +237,7 @@ def plotCurves(self, showSamples=False, force2D=True):
x, y = mgrid[0:5:4j, 0:5:4j]
z = cos(x) * sin(y)
- (x, y, z) = map(ravel, [x, y, z])
+ (x, y, z) = list(map(ravel, [x, y, z]))
for i, j, k in zip(x, y, z):
ds.addSample([i, j], [k])
diff --git a/pybrain/auxiliary/kmeans.py b/pybrain/auxiliary/kmeans.py
index f526e57d4..c13e16892 100644
--- a/pybrain/auxiliary/kmeans.py
+++ b/pybrain/auxiliary/kmeans.py
@@ -15,6 +15,7 @@
from scipy.cluster.vq import kmeans2
from scipy import random, array
from scipy.linalg import norm
+from functools import reduce
def kinit(X, k):
'init k seeds according to kmeans++'
@@ -30,13 +31,13 @@ def kinit(X, k):
for i in range(n):
'Dsum = sum_{x in X} min(D(x)^2,||x-xi||^2)'
Dsum = reduce(lambda x, y:x + y,
- (min(D[j], norm(X[j] - X[i]) ** 2) for j in xrange(n)))
+ (min(D[j], norm(X[j] - X[i]) ** 2) for j in range(n)))
if bestDsum < 0 or Dsum < bestDsum:
bestDsum, bestIdx = Dsum, i
centers.append (X[bestIdx])
- D = [min(D[i], norm(X[i] - X[bestIdx]) ** 2) for i in xrange(n)]
+ D = [min(D[i], norm(X[i] - X[bestIdx]) ** 2) for i in range(n)]
return array (centers)
diff --git a/pybrain/datasets/classification.py b/pybrain/datasets/classification.py
index 0b29b240e..c2b52e5f8 100644
--- a/pybrain/datasets/classification.py
+++ b/pybrain/datasets/classification.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = "Martin Felder, felder@in.tum.de"
from numpy import zeros, where, ravel, r_, single
@@ -212,7 +214,7 @@ def __init__(self, inp, target, nb_classes=0, class_labels=None):
# calculate class histogram, if we already have data
self.calculateStatistics()
self.nClasses = nb_classes
- self.class_labels = range(self.nClasses) if class_labels is None else class_labels
+ self.class_labels = list(range(self.nClasses)) if class_labels is None else class_labels
# copy classes (targets may be changed into other representation)
self.setField('class', self.getField('target'))
@@ -309,7 +311,7 @@ def save_netcdf(self, flo, **kwargs):
'numSeqs': self.getNumSequences(),
'maxLabelLength': 2 }
dims = {}
- for name, sz in dimsize.iteritems():
+ for name, sz in dimsize.items():
dims[name] = d.def_dim(name, sz)
# Create a netCDF record variables
@@ -331,7 +333,7 @@ def save_netcdf(self, flo, **kwargs):
seqLengths[:] = [self.getSequenceLength(i) for i in range(self.getNumSequences())]
# Close file
- print("wrote netCDF file " + filename)
+ print(("wrote netCDF file " + filename))
d.close()
@@ -345,15 +347,15 @@ def save_netcdf(self, flo, **kwargs):
dataset.appendLinked([ 0.20, 0.90 ] , [2])
dataset.calculateStatistics()
- print("class histogram:", dataset.classHist)
- print("# of classes:", dataset.nClasses)
- print("class 1 is: ", dataset.getClass(1))
- print("targets: ", dataset.getField('target'))
+ print(("class histogram:", dataset.classHist))
+ print(("# of classes:", dataset.nClasses))
+ print(("class 1 is: ", dataset.getClass(1)))
+ print(("targets: ", dataset.getField('target')))
dataset._convertToOneOfMany(bounds=[0, 1])
print("converted targets: ")
- print(dataset.getField('target'))
+ print((dataset.getField('target')))
dataset._convertToClassNb()
- print("reconverted to original:", dataset.getField('target'))
+ print(("reconverted to original:", dataset.getField('target')))
diff --git a/pybrain/datasets/dataset.py b/pybrain/datasets/dataset.py
index d8e8ba617..6a118902b 100644
--- a/pybrain/datasets/dataset.py
+++ b/pybrain/datasets/dataset.py
@@ -1,4 +1,4 @@
-from __future__ import with_statement
+
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
@@ -252,11 +252,11 @@ def getField(self, label):
def hasField(self, label):
"""Tell whether the field given by `label` exists."""
- return self.data.has_key(label)
+ return label in self.data
def getFieldNames(self):
"""Return the names of the currently defined fields."""
- return self.data.keys()
+ return list(self.data.keys())
def convertField(self, label, newtype):
"""Convert the given field to a different data type."""
@@ -297,7 +297,7 @@ def reconstruct(cls, filename):
"""Read an incomplete data set (option arraysonly) into the given one. """
# FIXME: Obsolete! Kept here because of some old files...
obj = cls(1, 1)
- for key, val in pickle.load(file(filename)).iteritems():
+ for key, val in pickle.load(file(filename)).items():
obj.setField(key, val)
return obj
@@ -341,14 +341,14 @@ def batches(self, label, n, permutation=None):
number_of_batches = full_batches if rest == 0 else full_batches + 1
# We make one iterator for the startindexes ...
- startindexes = (i * n for i in xrange(number_of_batches))
+ startindexes = (i * n for i in range(number_of_batches))
# ... and one for the stop indexes
- stopindexes = (((i + 1) * n) for i in xrange(number_of_batches - 1))
+ stopindexes = (((i + 1) * n) for i in range(number_of_batches - 1))
# The last stop index is the last element of the list (last batch
# might not be filled completely)
stopindexes = chain(stopindexes, [len(self)])
# Now combine them
- indexes = zip(startindexes, stopindexes)
+ indexes = list(zip(startindexes, stopindexes))
# Shuffle them according to the permutation if one is given
if permutation is not None:
@@ -359,15 +359,15 @@ def batches(self, label, n, permutation=None):
def randomBatches(self, label, n):
"""Like .batches(), but the order is random."""
- permutation = random.shuffle(range(len(self)))
+ permutation = random.shuffle(list(range(len(self))))
return self.batches(label, n, permutation)
def replaceNansByMeans(self):
"""Replace all not-a-number entries in the dataset by the means of the
corresponding column."""
- for d in self.data.itervalues():
+ for d in self.data.values():
means = scipy.nansum(d[:self.getLength()], axis=0) / self.getLength()
- for i in xrange(self.getLength()):
- for j in xrange(d.dim):
+ for i in range(self.getLength()):
+ for j in range(d.dim):
if not scipy.isfinite(d[i, j]):
d[i, j] = means[j]
diff --git a/pybrain/datasets/importance.py b/pybrain/datasets/importance.py
index d6acee37e..bd8235ff4 100644
--- a/pybrain/datasets/importance.py
+++ b/pybrain/datasets/importance.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
from scipy import ones, dot
@@ -40,9 +42,9 @@ def _evaluateSequence(self, f, seq, verbose = False):
totalError += e
ponderation += sum(importance)
if verbose:
- print( 'out: ', fListToString(list(res)))
- print( 'correct: ', fListToString(target))
- print( 'importance:', fListToString(importance))
- print( 'error: % .8f' % e)
+ print(( 'out: ', fListToString(list(res))))
+ print(( 'correct: ', fListToString(target)))
+ print(( 'importance:', fListToString(importance)))
+ print(( 'error: % .8f' % e))
return totalError, ponderation
diff --git a/pybrain/datasets/sequential.py b/pybrain/datasets/sequential.py
index 14d223261..9c4f25da1 100644
--- a/pybrain/datasets/sequential.py
+++ b/pybrain/datasets/sequential.py
@@ -1,7 +1,7 @@
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
# $Id$
-from itertools import izip
+
from scipy import ravel, r_
from random import sample
@@ -60,7 +60,7 @@ def getSequenceIterator(self, index):
`index`.
Each element is a tuple."""
- return izip(*self.getSequence(index))
+ return zip(*self.getSequence(index))
def endOfSequence(self, index):
"""Return True if the marker was moved over the last element of
@@ -194,7 +194,7 @@ def splitWithProportion(self, proportion=0.5):
The first dataset will have a fraction given by `proportion` of the
dataset."""
l = self.getNumSequences()
- leftIndices = sample(range(l), int(l * proportion))
+ leftIndices = sample(list(range(l)), int(l * proportion))
leftDs = self.copy()
leftDs.clear()
rightDs = leftDs.copy()
diff --git a/pybrain/datasets/supervised.py b/pybrain/datasets/supervised.py
index 6dc3b9ca7..dfb184c04 100644
--- a/pybrain/datasets/supervised.py
+++ b/pybrain/datasets/supervised.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
from random import sample
@@ -61,7 +63,7 @@ def setField(self, label, arr, **kwargs):
def _provideSequences(self):
"""Return an iterator over sequence lists, although the dataset contains
only single samples."""
- return iter(map(lambda x: [x], iter(self)))
+ return iter([[x] for x in iter(self)])
def evaluateMSE(self, f, **args):
"""Evaluate the predictions of a function on the dataset and return the
@@ -85,9 +87,9 @@ def _evaluateSequence(self, f, seq, verbose = False):
totalError += e
ponderation += len(target)
if verbose:
- print( 'out: ', fListToString( list( res ) ))
- print( 'correct:', fListToString( target ))
- print( 'error: % .8f' % e)
+ print(( 'out: ', fListToString( list( res ) )))
+ print(( 'correct:', fListToString( target )))
+ print(( 'error: % .8f' % e))
return totalError, ponderation
def evaluateModuleMSE(self, module, averageOver = 1, **args):
@@ -102,7 +104,7 @@ def evaluateModuleMSE(self, module, averageOver = 1, **args):
def splitWithProportion(self, proportion = 0.5):
"""Produce two new datasets, the first one containing the fraction given
by `proportion` of the samples."""
- leftIndices = set(sample(range(len(self)), int(len(self)*proportion)))
+ leftIndices = set(sample(list(range(len(self))), int(len(self)*proportion)))
leftDs = self.copy()
leftDs.clear()
rightDs = leftDs.copy()
diff --git a/pybrain/optimization/distributionbased/__init__.py b/pybrain/optimization/distributionbased/__init__.py
index 9acd01f85..0b40c5107 100644
--- a/pybrain/optimization/distributionbased/__init__.py
+++ b/pybrain/optimization/distributionbased/__init__.py
@@ -1,6 +1,6 @@
-from cmaes import CMAES
-from fem import FEM
-from nes import ExactNES, OriginalNES
-from ves import VanillaGradientEvolutionStrategies
-from xnes import XNES
-from snes import SNES
+from .cmaes import CMAES
+from .fem import FEM
+from .nes import ExactNES, OriginalNES
+from .ves import VanillaGradientEvolutionStrategies
+from .xnes import XNES
+from .snes import SNES
diff --git a/pybrain/optimization/distributionbased/cmaes.py b/pybrain/optimization/distributionbased/cmaes.py
index 299b34f81..81e963c7f 100644
--- a/pybrain/optimization/distributionbased/cmaes.py
+++ b/pybrain/optimization/distributionbased/cmaes.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch; Sun Yi, yi@idsia.ch'
from numpy import floor, log, eye, zeros, array, sqrt, sum, dot, tile, outer, real
@@ -29,7 +31,7 @@ def _additionalInit(self):
# Strategy parameter setting: Selection
# population size, offspring number
self.mu = int(floor(self.batchSize / 2)) # number of parents/points for recombination
- self.weights = log(self.mu + 1) - log(array(xrange(1, self.mu + 1))) # use array
+ self.weights = log(self.mu + 1) - log(array(range(1, self.mu + 1))) # use array
self.weights /= sum(self.weights) # normalize recombination weights array
self.muEff = sum(self.weights) ** 2 / sum(power(self.weights, 2)) # variance-effective size of mu
@@ -57,15 +59,15 @@ def _learnStep(self):
arx = tile(self.center.reshape(self.numParameters, 1), (1, self.batchSize))\
+ self.stepSize * dot(dot(self.B, self.D), arz)
arfitness = zeros(self.batchSize)
- for k in xrange(self.batchSize):
+ for k in range(self.batchSize):
arfitness[k] = self._oneEvaluation(arx[:, k])
# Sort by fitness and compute weighted mean into center
arfitness, arindex = sorti(arfitness) # minimization
arz = arz[:, arindex]
arx = arx[:, arindex]
- arzsel = arz[:, xrange(self.mu)]
- arxsel = arx[:, xrange(self.mu)]
+ arzsel = arz[:, range(self.mu)]
+ arxsel = arx[:, range(self.mu)]
arxmut = arxsel - tile(self.center.reshape(self.numParameters, 1), (1, self.mu))
zmean = dot(arzsel, self.weights)
@@ -123,8 +125,8 @@ def batchSize(self):
def sorti(vect):
""" sort, but also return the indices-changes """
- tmp = sorted(map(lambda (x, y): (y, x), enumerate(ravel(vect))))
- res1 = array(map(lambda x: x[0], tmp))
- res2 = array(map(lambda x: int(x[1]), tmp))
+ tmp = sorted([(x_y[1], x_y[0]) for x_y in enumerate(ravel(vect))])
+ res1 = array([x[0] for x in tmp])
+ res2 = array([int(x[1]) for x in tmp])
return res1, res2
diff --git a/pybrain/optimization/distributionbased/fem.py b/pybrain/optimization/distributionbased/fem.py
index 64219cf65..86edfb990 100644
--- a/pybrain/optimization/distributionbased/fem.py
+++ b/pybrain/optimization/distributionbased/fem.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Daan Wierstra and Tom Schaul'
from scipy import dot, rand, ones, eye, zeros, outer, isnan, multiply, argmax, product, log
@@ -71,7 +73,7 @@ def _additionalInit(self):
self.mus.append(rand(xdim) * (self.rangemaxs - self.rangemins) + self.rangemins)
self.sigmas.append(dot(eye(xdim), self.initCovariances))
- self.samples = range(self.windowSize)
+ self.samples = list(range(self.windowSize))
self.fitnesses = zeros(self.windowSize)
self.generation = 0
self.allsamples = []
@@ -219,8 +221,8 @@ def _updateShaping(self):
self.shapingFunction.setParameter(possible[argmax(matchValues)])
if len(self.allsamples) % 100 == 0:
- print(possible[argmax(matchValues)])
- print(fListToString(matchValues, 3))
+ print((possible[argmax(matchValues)]))
+ print((fListToString(matchValues, 3)))
def _learnStep(self):
k = len(self.allsamples) % self.windowSize
@@ -230,7 +232,7 @@ def _learnStep(self):
if len(self.allsamples) < self.windowSize:
return
if self.verbose and len(self.allsamples) % 100 == 0:
- print(len(self.allsamples), min(self.fitnesses), max(self.fitnesses))
+ print((len(self.allsamples), min(self.fitnesses), max(self.fitnesses)))
# print(len(self.allsamples), min(self.fitnesses), max(self.fitnesses)#, self.alphas)
updateSize = self._computeUpdateSize(self._computeDensities(sample), k)
diff --git a/pybrain/optimization/distributionbased/nes.py b/pybrain/optimization/distributionbased/nes.py
index 57edba9f2..712b4ed21 100644
--- a/pybrain/optimization/distributionbased/nes.py
+++ b/pybrain/optimization/distributionbased/nes.py
@@ -1,7 +1,7 @@
__author__ = 'Daan Wierstra, Tom Schaul and Sun Yi'
-from ves import VanillaGradientEvolutionStrategies
+from .ves import VanillaGradientEvolutionStrategies
from pybrain.utilities import triu2flat, blockCombine
from scipy.linalg import inv, pinv2
from scipy import outer, dot, multiply, zeros, diag, mat, sum
@@ -52,7 +52,7 @@ def _calcBatchUpdate(self, fitnesses):
u[j] = dot(G, u[j])
V[j, :] = dot(G, V[j, :])
j -= 1
- for k in reversed(range(d - 1)):
+ for k in reversed(list(range(d - 1))):
p = invSigma[k + 1:, k]
w = invSigma[k, k]
wg = w + invA[k, k] ** 2
@@ -77,7 +77,7 @@ def _calcBatchUpdate(self, fitnesses):
update = zeros(self.numDistrParams)
vsquare = multiply(V, V)
j = self.numDistrParams - 1
- for k in reversed(range(self.numParameters)):
+ for k in reversed(list(range(self.numParameters))):
b0 = sum(vsquare[j - (d - k - 1):j + 1, :], 0)
b = dot(b0, fitnesses) / sum(b0)
update[j - (d - k - 1):j + 1] = dot(V[j - (d - k - 1):j + 1, :], (fitnesses - b))
diff --git a/pybrain/optimization/distributionbased/rank1.py b/pybrain/optimization/distributionbased/rank1.py
index ba1ef31e8..5d675bafe 100644
--- a/pybrain/optimization/distributionbased/rank1.py
+++ b/pybrain/optimization/distributionbased/rank1.py
@@ -1,5 +1,6 @@
-__author__ = 'Tom Schaul, Tobias Glasmachers'
+from __future__ import print_function
+__author__ = 'Tom Schaul, Tobias Glasmachers'
from scipy import dot, array, randn, exp, floor, log, sqrt, ones, multiply, log2
@@ -78,7 +79,7 @@ def _population(self):
def _currentEvaluations(self):
fits = [self._allEvaluations[i] for i in self._pointers]
if self._wasOpposed:
- fits = map(lambda x:-x, fits)
+ fits = [-x for x in fits]
return fits
def _produceSample(self):
@@ -87,16 +88,16 @@ def _produceSample(self):
def _produceSamples(self):
""" Append batch size new samples and evaluate them. """
tmp = [self._sample2base(self._produceSample()) for _ in range(self.batchSize)]
- map(self._oneEvaluation, tmp)
+ list(map(self._oneEvaluation, tmp))
self._pointers = list(range(len(self._allEvaluated) - self.batchSize, len(self._allEvaluated)))
def _notify(self):
""" Provide some feedback during the run. """
if self.verbose:
if self.numEvaluations % self.verboseGaps == 0:
- print('Step:', self.numLearningSteps, 'best:', self.bestEvaluation,
+ print(('Step:', self.numLearningSteps, 'best:', self.bestEvaluation,
'logVar', round(self._logDetA, 3),
- 'log|vector|', round(log(dot(self._principalVector, self._principalVector))/2, 3))
+ 'log|vector|', round(log(dot(self._principalVector, self._principalVector))/2, 3)))
if self.listener is not None:
self.listener(self.bestEvaluable, self.bestEvaluation)
@@ -112,7 +113,7 @@ def _learnStep(self):
W = [s[:-1] + u * s[-1] for s in samples]
points = [self._center+exp(a) *w for w in W]
- map(self._oneEvaluation, points)
+ list(map(self._oneEvaluation, points))
self._pointers = list(range(len(self._allEvaluated) - self.batchSize, len(self._allEvaluated)))
diff --git a/pybrain/optimization/distributionbased/snes.py b/pybrain/optimization/distributionbased/snes.py
index 04501c4b9..131789406 100644
--- a/pybrain/optimization/distributionbased/snes.py
+++ b/pybrain/optimization/distributionbased/snes.py
@@ -1,116 +1,118 @@
-__author__ = 'Tom Schaul, tom@idsia.ch'
-
-from pybrain.optimization.distributionbased.distributionbased import DistributionBasedOptimizer
-from scipy import dot, exp, log, sqrt, floor, ones, randn
-from pybrain.tools.rankingfunctions import HansenRanking
-
-
-class SNES(DistributionBasedOptimizer):
- """ Separable NES (diagonal).
- [As described in Schaul, Glasmachers and Schmidhuber (GECCO'11)]
- """
-
- # parameters, which can be set but have a good (adapted) default value
- centerLearningRate = 1.0
- covLearningRate = None
- batchSize = None
- uniformBaseline = True
- shapingFunction = HansenRanking()
- initVariance = 1.
-
- # fixed settings
- mustMaximize = True
- storeAllEvaluations = True
- storeAllEvaluated = True
-
- # for very long runs, we don't want to run out of memory
- clearStorage = False
-
- # minimal setting where to abort the search
- varianceCutoff = 1e-20
-
- def _stoppingCriterion(self):
- if DistributionBasedOptimizer._stoppingCriterion(self):
- return True
- elif max(abs(self._sigmas)) < self.varianceCutoff:
- return True
- else:
- return False
-
- def _initLearningRate(self):
- """ Careful, robust default value. """
- return 0.6 * (3 + log(self.numParameters)) / 3 / sqrt(self.numParameters)
-
- def _initBatchSize(self):
- """ as in CMA-ES """
- return 4 + int(floor(3 * log(self.numParameters)))
-
- def _additionalInit(self):
- if self.covLearningRate is None:
- self.covLearningRate = self._initLearningRate()
- if self.batchSize is None:
- self.batchSize = self._initBatchSize()
-
- self._center = self._initEvaluable.copy()
- self._sigmas = ones(self.numParameters) * self.initVariance
-
- @property
- def _population(self):
- if self._wasUnwrapped:
- return [self._allEvaluated[i].params for i in self._pointers]
- else:
- return [self._allEvaluated[i] for i in self._pointers]
-
- @property
- def _currentEvaluations(self):
- fits = [self._allEvaluations[i] for i in self._pointers]
- if self._wasOpposed:
- fits = map(lambda x:-x, fits)
- return fits
-
- def _produceSample(self):
- return randn(self.numParameters)
-
- def _sample2base(self, sample):
- """ How does a sample look in the outside (base problem) coordinate system? """
- return self._sigmas * sample + self._center
-
- def _base2sample(self, e):
- """ How does the point look in the present one reference coordinates? """
- return (e - self._center) / self._sigmas
-
- def _produceSamples(self):
- """ Append batch size new samples and evaluate them. """
- if self.clearStorage:
- self._allEvaluated = []
- self._allEvaluations = []
-
- tmp = [self._sample2base(self._produceSample()) for _ in range(self.batchSize)]
- map(self._oneEvaluation, tmp)
- self._pointers = list(range(len(self._allEvaluated) - self.batchSize, len(self._allEvaluated)))
-
- def _learnStep(self):
- # produce samples
- self._produceSamples()
- samples = map(self._base2sample, self._population)
-
- #compute utilities
- utilities = self.shapingFunction(self._currentEvaluations)
- utilities /= sum(utilities) # make the utilities sum to 1
- if self.uniformBaseline:
- utilities -= 1. / self.batchSize
-
- # update center
- dCenter = dot(utilities, samples)
- self._center += self.centerLearningRate * self._sigmas * dCenter
-
- # update variances
- covGradient = dot(utilities, [s ** 2 - 1 for s in samples])
- dA = 0.5 * self.covLearningRate * covGradient
- self._sigmas = self._sigmas * exp(dA)
-
-
-if __name__ == "__main__":
- from pybrain.rl.environments.functions.unimodal import ElliFunction
- print(SNES(ElliFunction(100), ones(100), verbose=True).learn())
+from __future__ import print_function
+
+__author__ = 'Tom Schaul, tom@idsia.ch'
+
+from pybrain.optimization.distributionbased.distributionbased import DistributionBasedOptimizer
+from scipy import dot, exp, log, sqrt, floor, ones, randn
+from pybrain.tools.rankingfunctions import HansenRanking
+
+
+class SNES(DistributionBasedOptimizer):
+ """ Separable NES (diagonal).
+ [As described in Schaul, Glasmachers and Schmidhuber (GECCO'11)]
+ """
+
+ # parameters, which can be set but have a good (adapted) default value
+ centerLearningRate = 1.0
+ covLearningRate = None
+ batchSize = None
+ uniformBaseline = True
+ shapingFunction = HansenRanking()
+ initVariance = 1.
+
+ # fixed settings
+ mustMaximize = True
+ storeAllEvaluations = True
+ storeAllEvaluated = True
+
+ # for very long runs, we don't want to run out of memory
+ clearStorage = False
+
+ # minimal setting where to abort the search
+ varianceCutoff = 1e-20
+
+ def _stoppingCriterion(self):
+ if DistributionBasedOptimizer._stoppingCriterion(self):
+ return True
+ elif max(abs(self._sigmas)) < self.varianceCutoff:
+ return True
+ else:
+ return False
+
+ def _initLearningRate(self):
+ """ Careful, robust default value. """
+ return 0.6 * (3 + log(self.numParameters)) / 3 / sqrt(self.numParameters)
+
+ def _initBatchSize(self):
+ """ as in CMA-ES """
+ return 4 + int(floor(3 * log(self.numParameters)))
+
+ def _additionalInit(self):
+ if self.covLearningRate is None:
+ self.covLearningRate = self._initLearningRate()
+ if self.batchSize is None:
+ self.batchSize = self._initBatchSize()
+
+ self._center = self._initEvaluable.copy()
+ self._sigmas = ones(self.numParameters) * self.initVariance
+
+ @property
+ def _population(self):
+ if self._wasUnwrapped:
+ return [self._allEvaluated[i].params for i in self._pointers]
+ else:
+ return [self._allEvaluated[i] for i in self._pointers]
+
+ @property
+ def _currentEvaluations(self):
+ fits = [self._allEvaluations[i] for i in self._pointers]
+ if self._wasOpposed:
+ fits = [-x for x in fits]
+ return fits
+
+ def _produceSample(self):
+ return randn(self.numParameters)
+
+ def _sample2base(self, sample):
+ """ How does a sample look in the outside (base problem) coordinate system? """
+ return self._sigmas * sample + self._center
+
+ def _base2sample(self, e):
+ """ How does the point look in the present one reference coordinates? """
+ return (e - self._center) / self._sigmas
+
+ def _produceSamples(self):
+ """ Append batch size new samples and evaluate them. """
+ if self.clearStorage:
+ self._allEvaluated = []
+ self._allEvaluations = []
+
+ tmp = [self._sample2base(self._produceSample()) for _ in range(self.batchSize)]
+ list(map(self._oneEvaluation, tmp))
+ self._pointers = list(range(len(self._allEvaluated) - self.batchSize, len(self._allEvaluated)))
+
+ def _learnStep(self):
+ # produce samples
+ self._produceSamples()
+ samples = list(map(self._base2sample, self._population))
+
+ #compute utilities
+ utilities = self.shapingFunction(self._currentEvaluations)
+ utilities /= sum(utilities) # make the utilities sum to 1
+ if self.uniformBaseline:
+ utilities -= 1. / self.batchSize
+
+ # update center
+ dCenter = dot(utilities, samples)
+ self._center += self.centerLearningRate * self._sigmas * dCenter
+
+ # update variances
+ covGradient = dot(utilities, [s ** 2 - 1 for s in samples])
+ dA = 0.5 * self.covLearningRate * covGradient
+ self._sigmas = self._sigmas * exp(dA)
+
+
+if __name__ == "__main__":
+ from pybrain.rl.environments.functions.unimodal import ElliFunction
+ print((SNES(ElliFunction(100), ones(100), verbose=True).learn()))
\ No newline at end of file
diff --git a/pybrain/optimization/distributionbased/ves.py b/pybrain/optimization/distributionbased/ves.py
index 0112eeb0c..4851294cf 100644
--- a/pybrain/optimization/distributionbased/ves.py
+++ b/pybrain/optimization/distributionbased/ves.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Daan Wierstra and Tom Schaul'
from scipy import eye, multiply, ones, dot, array, outer, rand, zeros, diag, randn, exp
@@ -166,7 +168,7 @@ def _batchLearn(self):
self.maxLearningSteps = self.numLearningSteps
if self.verbose:
- print('Evals:', self.numEvaluations,)
+ print(('Evals:', self.numEvaluations,))
self.allCenters.append(self.x.copy())
self.allFactorSigmas.append(self.factorSigma.copy())
diff --git a/pybrain/optimization/distributionbased/xnes.py b/pybrain/optimization/distributionbased/xnes.py
index 9583b97cd..17ea97cf8 100644
--- a/pybrain/optimization/distributionbased/xnes.py
+++ b/pybrain/optimization/distributionbased/xnes.py
@@ -1,5 +1,6 @@
-__author__ = 'Tom Schaul, Sun Yi, Tobias Glasmachers'
+from __future__ import print_function
+__author__ = 'Tom Schaul, Sun Yi, Tobias Glasmachers'
from pybrain.tools.rankingfunctions import HansenRanking
from pybrain.optimization.distributionbased.distributionbased import DistributionBasedOptimizer
@@ -58,7 +59,7 @@ def _learnStep(self):
utilities /= sum(utilities) # make the utilities sum to 1
if self.uniformBaseline:
utilities -= 1./self.batchSize
- samples = array(map(self._base2sample, self._population))
+ samples = array(list(map(self._base2sample, self._population)))
dCenter = dot(samples.T, utilities)
covGradient = dot(array([outer(s,s) - I for s in samples]).T, utilities)
@@ -92,7 +93,7 @@ def _population(self):
def _currentEvaluations(self):
fits = [self._allEvaluations[i] for i in self._pointers]
if self._wasOpposed:
- fits = map(lambda x:-x, fits)
+ fits = [-x for x in fits]
return fits
def _produceSample(self):
@@ -124,11 +125,11 @@ def _produceSamples(self):
[self._oneEvaluation(self._sample2base(self._produceSample())) for _ in range(self.batchSize)]
self._pointers = list(range(len(self._allEvaluated)-self.batchSize, len(self._allEvaluated)))
else:
- reuseindices, newpoints = importanceMixing(map(self._base2sample, self._currentEvaluations),
+ reuseindices, newpoints = importanceMixing(list(map(self._base2sample, self._currentEvaluations)),
self._oldpdf, self._newpdf, self._produceSample, self.forcedRefresh)
[self._oneEvaluation(self._sample2base(s)) for s in newpoints]
self._pointers = ([self._pointers[i] for i in reuseindices]+
- range(len(self._allEvaluated)-self.batchSize+len(reuseindices), len(self._allEvaluated)))
+ list(range(len(self._allEvaluated)-self.batchSize+len(reuseindices), len(self._allEvaluated))))
self._allGenSteps.append(self._allGenSteps[-1]+self.batchSize-len(reuseindices))
self._allPointers.append(self._pointers)
@@ -140,5 +141,5 @@ def _produceSamples(self):
dim = 10
f = RosenbrockFunction(dim)
l = XNES(f, -ones(dim))
- print(l.learn())
- print('Evaluations needed:', len(l._allEvaluations))
+ print((l.learn()))
+ print(('Evaluations needed:', len(l._allEvaluations)))
diff --git a/pybrain/optimization/finitedifference/__init__.py b/pybrain/optimization/finitedifference/__init__.py
index 34e83e6f9..d6f75c782 100644
--- a/pybrain/optimization/finitedifference/__init__.py
+++ b/pybrain/optimization/finitedifference/__init__.py
@@ -1,3 +1,3 @@
-from fd import FiniteDifferences
-from spsa import SimpleSPSA
-from pgpe import PGPE
\ No newline at end of file
+from .fd import FiniteDifferences
+from .spsa import SimpleSPSA
+from .pgpe import PGPE
\ No newline at end of file
diff --git a/pybrain/optimization/finitedifference/pgpe.py b/pybrain/optimization/finitedifference/pgpe.py
index 427aaff4b..c6da77db5 100644
--- a/pybrain/optimization/finitedifference/pgpe.py
+++ b/pybrain/optimization/finitedifference/pgpe.py
@@ -3,7 +3,7 @@
from scipy import ones, random
from pybrain.auxiliary import GradientDescent
-from fd import FiniteDifferences
+from .fd import FiniteDifferences
class PGPE(FiniteDifferences):
diff --git a/pybrain/optimization/finitedifference/spsa.py b/pybrain/optimization/finitedifference/spsa.py
index 44d5eacaa..72fb6d24a 100644
--- a/pybrain/optimization/finitedifference/spsa.py
+++ b/pybrain/optimization/finitedifference/spsa.py
@@ -2,7 +2,7 @@
from scipy import random
-from fd import FiniteDifferences
+from .fd import FiniteDifferences
class SimpleSPSA(FiniteDifferences):
diff --git a/pybrain/optimization/memetic/__init__.py b/pybrain/optimization/memetic/__init__.py
index 83995d1a0..d408c533f 100644
--- a/pybrain/optimization/memetic/__init__.py
+++ b/pybrain/optimization/memetic/__init__.py
@@ -1,4 +1,4 @@
-from inversememetic import InverseMemeticSearch
-from innerinversememetic import InnerInverseMemeticSearch
-from memetic import MemeticSearch
-from innermemetic import InnerMemeticSearch
\ No newline at end of file
+from .inversememetic import InverseMemeticSearch
+from .innerinversememetic import InnerInverseMemeticSearch
+from .memetic import MemeticSearch
+from .innermemetic import InnerMemeticSearch
\ No newline at end of file
diff --git a/pybrain/optimization/memetic/innerinversememetic.py b/pybrain/optimization/memetic/innerinversememetic.py
index 6b5b465bd..61e4c83b7 100644
--- a/pybrain/optimization/memetic/innerinversememetic.py
+++ b/pybrain/optimization/memetic/innerinversememetic.py
@@ -1,7 +1,7 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from innermemetic import InnerMemeticSearch
-from inversememetic import InverseMemeticSearch
+from .innermemetic import InnerMemeticSearch
+from .inversememetic import InverseMemeticSearch
class InnerInverseMemeticSearch(InnerMemeticSearch, InverseMemeticSearch):
""" inverse of inner memetic search"""
diff --git a/pybrain/optimization/memetic/innermemetic.py b/pybrain/optimization/memetic/innermemetic.py
index 0b9d0d7b5..4f98bc7d0 100644
--- a/pybrain/optimization/memetic/innermemetic.py
+++ b/pybrain/optimization/memetic/innermemetic.py
@@ -1,6 +1,6 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from memetic import MemeticSearch
+from .memetic import MemeticSearch
from pybrain.optimization.populationbased.es import ES
diff --git a/pybrain/optimization/memetic/inversememetic.py b/pybrain/optimization/memetic/inversememetic.py
index 88c53ac63..053e35d78 100644
--- a/pybrain/optimization/memetic/inversememetic.py
+++ b/pybrain/optimization/memetic/inversememetic.py
@@ -1,6 +1,6 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from memetic import MemeticSearch
+from .memetic import MemeticSearch
class InverseMemeticSearch(MemeticSearch):
diff --git a/pybrain/optimization/memetic/memetic.py b/pybrain/optimization/memetic/memetic.py
index 4517e660e..74ab11e2e 100644
--- a/pybrain/optimization/memetic/memetic.py
+++ b/pybrain/optimization/memetic/memetic.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
from pybrain.optimization.optimizer import BlackBoxOptimizer, TopologyOptimizer
@@ -53,7 +55,7 @@ def _learnStep(self):
def _notify(self):
HillClimber._notify(self)
if self.verbose:
- print(' Bits on in best mask:', sum(self.bestEvaluable.mask))
+ print((' Bits on in best mask:', sum(self.bestEvaluable.mask)))
@property
def batchSize(self):
diff --git a/pybrain/optimization/optimizer.py b/pybrain/optimization/optimizer.py
index 6204db06b..d6faf3ff4 100644
--- a/pybrain/optimization/optimizer.py
+++ b/pybrain/optimization/optimizer.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
from scipy import array, randn, ndarray, isinf, isnan, isscalar
@@ -292,7 +294,7 @@ def _stoppingCriterion(self):
def _notify(self):
""" Provide some feedback during the run. """
if self.verbose:
- print('Step:', self.numLearningSteps, 'best:', self.bestEvaluation)
+ print(('Step:', self.numLearningSteps, 'best:', self.bestEvaluation))
if self.listener is not None:
self.listener(self.bestEvaluable, self.bestEvaluation)
diff --git a/pybrain/optimization/populationbased/coevolution/__init__.py b/pybrain/optimization/populationbased/coevolution/__init__.py
index 7d3e52fb4..52e4e6afb 100644
--- a/pybrain/optimization/populationbased/coevolution/__init__.py
+++ b/pybrain/optimization/populationbased/coevolution/__init__.py
@@ -1,3 +1,3 @@
-from coevolution import Coevolution
-from competitivecoevolution import CompetitiveCoevolution
-from multipopulationcoevolution import MultiPopulationCoevolution
\ No newline at end of file
+from .coevolution import Coevolution
+from .competitivecoevolution import CompetitiveCoevolution
+from .multipopulationcoevolution import MultiPopulationCoevolution
\ No newline at end of file
diff --git a/pybrain/optimization/populationbased/coevolution/coevolution.py b/pybrain/optimization/populationbased/coevolution/coevolution.py
index e9942cbf1..b7f3aa633 100644
--- a/pybrain/optimization/populationbased/coevolution/coevolution.py
+++ b/pybrain/optimization/populationbased/coevolution/coevolution.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
from scipy import argmax, array
@@ -90,10 +92,10 @@ def _oneGeneration(self):
self.hallOfFitnesses.append(bestFits)
if self.verbose:
- print('Generation', self.generation)
- print(' relat. fits:', fListToString(sorted(fitnesses), 4))
+ print(('Generation', self.generation))
+ print((' relat. fits:', fListToString(sorted(fitnesses), 4)))
if len(best.params) < 20:
- print(' best params:', fListToString(best.params, 4))
+ print((' best params:', fListToString(best.params, 4)))
self.pop = self._selectAndReproduce(self.pop, fitnesses)
@@ -153,11 +155,11 @@ def _extendPopulation(self, seeds, size):
def _selectAndReproduce(self, pop, fits):
""" apply selection and reproduction to host population, according to their fitness."""
# combine population with their fitness, then sort, only by fitness
- s = zip(fits, pop)
+ s = list(zip(fits, pop))
shuffle(s)
s.sort(key=lambda x:-x[0])
# select...
- selected = map(lambda x: x[1], s[:self._numSelected()])
+ selected = [x[1] for x in s[:self._numSelected()]]
# ... and reproduce
if self.elitism:
newpop = self._extendPopulation(selected, self.populationSize)
@@ -224,7 +226,7 @@ def _sharedSampling(self, numSelect, selectFrom, relativeTo):
if self._beats(p, opp) > 0:
beaten.append(opp)
tmp[p] = beaten
- beatlist = map(lambda (p, beaten): (len(beaten), self._globalScore(p), p), tmp.items())
+ beatlist = [(len(p_beaten[1]), self._globalScore(p_beaten[0]), p_beaten[0]) for p_beaten in list(tmp.items())]
shuffle(beatlist)
beatlist.sort(key=lambda x: x[:2])
best = beatlist[-1][2]
@@ -287,5 +289,5 @@ def _stepsPerGeneration(self):
x.allOpponents[3] = [2, 4]
x.allOpponents[4] = [3]
x.allOpponents[5] = [2]
- print(x._sharedSampling(4, [1, 2, 3, 4, 5], [1, 2, 3, 4, 6, 7, 8, 9]))
- print('should be', [4, 1, 2, 5])
+ print((x._sharedSampling(4, [1, 2, 3, 4, 5], [1, 2, 3, 4, 6, 7, 8, 9])))
+ print(('should be', [4, 1, 2, 5]))
diff --git a/pybrain/optimization/populationbased/coevolution/competitivecoevolution.py b/pybrain/optimization/populationbased/coevolution/competitivecoevolution.py
index 042ece9ee..39c27bdfd 100644
--- a/pybrain/optimization/populationbased/coevolution/competitivecoevolution.py
+++ b/pybrain/optimization/populationbased/coevolution/competitivecoevolution.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
from pybrain.optimization.coevolution.coevolution import Coevolution
@@ -15,8 +17,8 @@ def _initPopulation(self, seeds):
for s in seeds:
s.parent = None
if len(seeds) > 1:
- s1 = seeds[:len(seeds) / 2]
- s2 = seeds[len(seeds) / 2:]
+ s1 = seeds[:len(seeds) // 2]
+ s2 = seeds[len(seeds) // 2:]
else:
# not enough seeds: randomize
s1 = seeds
@@ -77,7 +79,7 @@ def _evaluatePopulation(self):
fit = self._competitiveSharedFitness(self.pop, self.parasitePop)
if hoFtournSize > 0:
fitHof = self._competitiveSharedFitness(self.pop, self.hallOfFame)
- fit = map(lambda (f1, f2): tournSize * f1 + hoFtournSize * f2, zip(fit, fitHof))
+ fit = [tournSize * f1_f2[0] + hoFtournSize * f1_f2[1] for f1_f2 in zip(fit, fitHof)]
return fit
def _oneGeneration(self):
@@ -118,8 +120,8 @@ def b(x, y):
b(4, 5)
b(4, 7)
b(8, 4)
- print(C.pop)
- print(C.parasitePop)
- print(' ', fListToString(C._competitiveSharedFitness(C.pop, C.parasitePop), 2))
- print('should be:', fListToString([0.83, 0.00, 1.33, 0.83], 2))
+ print((C.pop))
+ print((C.parasitePop))
+ print((' ', fListToString(C._competitiveSharedFitness(C.pop, C.parasitePop), 2)))
+ print(('should be:', fListToString([0.83, 0.00, 1.33, 0.83], 2)))
diff --git a/pybrain/optimization/populationbased/coevolution/multipopulationcoevolution.py b/pybrain/optimization/populationbased/coevolution/multipopulationcoevolution.py
index b7d853fdc..9d0895ad2 100644
--- a/pybrain/optimization/populationbased/coevolution/multipopulationcoevolution.py
+++ b/pybrain/optimization/populationbased/coevolution/multipopulationcoevolution.py
@@ -35,7 +35,7 @@ def _evaluatePopulation(self):
if other == self.pop:
continue
# TODO: parametrize
- bestPart = len(other)/2
+ bestPart = len(other)//2
if bestPart < 1:
bestPart = 1
self._doTournament(self.pop, other[:bestPart], self.tournamentSize)
diff --git a/pybrain/optimization/populationbased/es.py b/pybrain/optimization/populationbased/es.py
index 59e9652bc..c2405b769 100644
--- a/pybrain/optimization/populationbased/es.py
+++ b/pybrain/optimization/populationbased/es.py
@@ -25,7 +25,7 @@ def _additionalInit(self):
self.hallOfFame = []
# population is a list of (fitness, individual) tuples.
self.population = [(self._oneEvaluation(self._initEvaluable), self._initEvaluable)] * self._popsize
- map(self._replaceByMutation, range(1, self._popsize))
+ list(map(self._replaceByMutation, list(range(1, self._popsize))))
self._sortPopulation()
@property
@@ -53,9 +53,9 @@ def _learnStep(self):
# mutate the offspring
if self.elitism:
- map(self._replaceByMutation, range(self.mu, self._popsize))
+ list(map(self._replaceByMutation, list(range(self.mu, self._popsize))))
else:
- map(self._replaceByMutation, range(self._popsize))
+ list(map(self._replaceByMutation, list(range(self._popsize))))
self._sortPopulation()
diff --git a/pybrain/optimization/populationbased/ga.py b/pybrain/optimization/populationbased/ga.py
index 833f4cc92..ad83e4d91 100644
--- a/pybrain/optimization/populationbased/ga.py
+++ b/pybrain/optimization/populationbased/ga.py
@@ -71,7 +71,7 @@ def crossOverOld(self, parents, nbChildren):
children.append(p1)
else:
p2 = choice(parents)
- point = choice(range(xdim-1))
+ point = choice(list(range(xdim-1)))
point += 1
res = zeros(xdim)
res[:point] = p1[:point]
@@ -95,14 +95,14 @@ def crossOver(self, parents, nbChildren):
xdim = self.numParameters
shuffle(parents)
children = []
- for i in range(len(parents)/2):
+ for i in range(len(parents)//2):
p1 = parents[i]
p2 = parents[i+(len(parents)/2)]
if xdim < 2:
children.append(p1)
children.append(p2)
else:
- point = choice(range(xdim-1))
+ point = choice(list(range(xdim-1)))
point += 1
res = zeros(xdim)
res[:point] = p1[:point]
@@ -123,7 +123,7 @@ def childexist(self,indiv,pop):
if isinstance(pop,list):
for i in range(len(pop)):
if all((abs(indiv[k] - pop[i][k])/(self.maxs[k]-self.mins[k]))
- < 1.e-7 for k in xrange(self.numParameters)):
+ < 1.e-7 for k in range(self.numParameters)):
return True
return False
@@ -189,10 +189,10 @@ def select(self):
:return: list of selected parents """
if not self.tournament:
- tmp = zip(self.fitnesses, self.currentpop)
+ tmp = list(zip(self.fitnesses, self.currentpop))
tmp.sort(key = lambda x: x[0])
tmp2 = list(reversed(tmp))[:self.selectionSize]
- return map(lambda x: x[1], tmp2)
+ return [x[1] for x in tmp2]
else:
# TODO: tournament selection
raise NotImplementedError()
diff --git a/pybrain/optimization/populationbased/multiobjective/constnsga2.py b/pybrain/optimization/populationbased/multiobjective/constnsga2.py
index 5f99536f7..c34636229 100644
--- a/pybrain/optimization/populationbased/multiobjective/constnsga2.py
+++ b/pybrain/optimization/populationbased/multiobjective/constnsga2.py
@@ -42,11 +42,11 @@ def _learnStep(self):
self._allGenerations.append((self.currentpop, self.fitnesses))
if self.elitism:
- self.bestEvaluable = list(const_non_dominated_front(map(tuple, self.currentpop),
+ self.bestEvaluable = list(const_non_dominated_front(list(map(tuple, self.currentpop)),
key=lambda x: self.fitnesses[x],
allowequality = self.allowEquality))
else:
- self.bestEvaluable = list(const_non_dominated_front(map(tuple, self.currentpop)+self.bestEvaluable,
+ self.bestEvaluable = list(const_non_dominated_front(list(map(tuple, self.currentpop))+self.bestEvaluable,
key=lambda x: self.fitnesses[x],
allowequality = self.allowEquality))
self.bestEvaluation = [self.fitnesses[indiv] for indiv in self.bestEvaluable]
@@ -61,8 +61,8 @@ def _learnStep(self):
self.produceOffspring()
def select(self):
- return map(array, nsga2select(map(tuple, self.currentpop), self.fitnesses,
- self.selectionSize, self.allowEquality))
+ return list(map(array, nsga2select(list(map(tuple, self.currentpop)), self.fitnesses,
+ self.selectionSize, self.allowEquality)))
diff --git a/pybrain/optimization/populationbased/multiobjective/nsga2.py b/pybrain/optimization/populationbased/multiobjective/nsga2.py
index 7bdbfc03c..3cc7fbaf8 100644
--- a/pybrain/optimization/populationbased/multiobjective/nsga2.py
+++ b/pybrain/optimization/populationbased/multiobjective/nsga2.py
@@ -44,11 +44,11 @@ def _learnStep(self):
self._allGenerations.append((self.currentpop, self.fitnesses))
if self.elitism:
- self.bestEvaluable = list(non_dominated_front(map(tuple, self.currentpop),
+ self.bestEvaluable = list(non_dominated_front(list(map(tuple, self.currentpop)),
key=lambda x: self.fitnesses[x],
allowequality = self.allowEquality))
else:
- self.bestEvaluable = list(non_dominated_front(map(tuple, self.currentpop)+self.bestEvaluable,
+ self.bestEvaluable = list(non_dominated_front(list(map(tuple, self.currentpop))+self.bestEvaluable,
key=lambda x: self.fitnesses[x],
allowequality = self.allowEquality))
self.bestEvaluation = [self.fitnesses[indiv] for indiv in self.bestEvaluable]
@@ -56,8 +56,8 @@ def _learnStep(self):
self.produceOffspring()
def select(self):
- return map(array, nsga2select(map(tuple, self.currentpop), self.fitnesses,
- self.selectionSize, self.allowEquality))
+ return list(map(array, nsga2select(list(map(tuple, self.currentpop)), self.fitnesses,
+ self.selectionSize, self.allowEquality)))
diff --git a/pybrain/rl/agents/linearfa.py b/pybrain/rl/agents/linearfa.py
index 8f149bcaf..6215a0623 100644
--- a/pybrain/rl/agents/linearfa.py
+++ b/pybrain/rl/agents/linearfa.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
from pybrain.rl.agents.logging import LoggingAgent
diff --git a/pybrain/rl/environments/cartpole/balancetask.py b/pybrain/rl/environments/cartpole/balancetask.py
index afad5e911..32fffb091 100644
--- a/pybrain/rl/environments/cartpole/balancetask.py
+++ b/pybrain/rl/environments/cartpole/balancetask.py
@@ -6,7 +6,7 @@
from pybrain.rl.environments.cartpole.nonmarkovpole import NonMarkovPoleEnvironment
from pybrain.rl.environments.cartpole.doublepole import DoublePoleEnvironment
from pybrain.rl.environments import EpisodicTask
-from cartpole import CartPoleEnvironment
+from .cartpole import CartPoleEnvironment
from pybrain.utilities import crossproduct
@@ -45,7 +45,7 @@ def performAction(self, action):
EpisodicTask.performAction(self, action)
def isFinished(self):
- if max(map(abs, self.env.getPoleAngles())) > 0.7:
+ if max(list(map(abs, self.env.getPoleAngles()))) > 0.7:
# pole has fallen
return True
elif abs(self.env.getCartPosition()) > 2.4:
@@ -57,7 +57,7 @@ def isFinished(self):
return False
def getReward(self):
- angles = map(abs, self.env.getPoleAngles())
+ angles = list(map(abs, self.env.getPoleAngles()))
s = abs(self.env.getCartPosition())
reward = 0
if min(angles) < 0.05 and abs(s) < 0.05:
@@ -75,7 +75,7 @@ def setMaxLength(self, n):
class JustBalanceTask(BalanceTask):
""" this task does not require the cart to be moved to the middle. """
def getReward(self):
- angles = map(abs, self.env.getPoleAngles())
+ angles = list(map(abs, self.env.getPoleAngles()))
s = abs(self.env.getCartPosition())
if min(angles) < 0.05:
reward = 0
@@ -90,7 +90,7 @@ class EasyBalanceTask(BalanceTask):
""" this task is a bit easier to learn because it gives gradual feedback
about the distance to the centre. """
def getReward(self):
- angles = map(abs, self.env.getPoleAngles())
+ angles = list(map(abs, self.env.getPoleAngles()))
s = abs(self.env.getCartPosition())
if min(angles) < 0.05 and abs(s) < 0.05:
reward = 0
@@ -131,11 +131,11 @@ def getObservation(self):
return sensors
def performAction(self, action):
- action = action - (self.numActions-1)/2.
+ action = action - (self.numActions-1)//2.
BalanceTask.performAction(self, action)
def getReward(self):
- angles = map(abs, self.env.getPoleAngles())
+ angles = list(map(abs, self.env.getPoleAngles()))
s = abs(self.env.getCartPosition())
if min(angles) < 0.05: # and abs(s) < 0.05:
reward = 1.0
@@ -148,7 +148,7 @@ def getReward(self):
class DiscreteNoHelpTask(DiscreteBalanceTask):
def getReward(self):
- angles = map(abs, self.env.getPoleAngles())
+ angles = list(map(abs, self.env.getPoleAngles()))
s = abs(self.env.getCartPosition())
if max(angles) > 0.7 or abs(s) > 2.4:
reward = -1. * (self.N - self.t)
@@ -219,7 +219,7 @@ class DiscreteBalanceTaskRBF(DiscreteBalanceTask):
CENTERS = array(crossproduct([[-pi/4, 0, pi/4], [1, 0, -1]]))
def getReward(self):
- angles = map(abs, self.env.getPoleAngles())
+ angles = list(map(abs, self.env.getPoleAngles()))
if max(angles) > 1.6:
reward = -1.
else:
@@ -227,7 +227,7 @@ def getReward(self):
return reward
def isFinished(self):
- if max(map(abs, self.env.getPoleAngles())) > 1.6:
+ if max(list(map(abs, self.env.getPoleAngles()))) > 1.6:
return True
elif self.t >= self.N:
return True
@@ -236,7 +236,7 @@ def isFinished(self):
def getObservation(self):
res = ones(1+len(self.CENTERS))
sensors = self.env.getSensors()[:-2]
- res[1:] = exp(-array(map(norm, self.CENTERS-sensors))**2/2)
+ res[1:] = exp(-array(list(map(norm, self.CENTERS-sensors)))**2/2)
return res
@property
diff --git a/pybrain/rl/environments/cartpole/doublepole.py b/pybrain/rl/environments/cartpole/doublepole.py
index 81bcac945..ed6409af8 100644
--- a/pybrain/rl/environments/cartpole/doublepole.py
+++ b/pybrain/rl/environments/cartpole/doublepole.py
@@ -1,6 +1,6 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from cartpole import CartPoleEnvironment
+from .cartpole import CartPoleEnvironment
from pybrain.rl.environments import Environment
diff --git a/pybrain/rl/environments/cartpole/fast_version/cartpoleenv.py b/pybrain/rl/environments/cartpole/fast_version/cartpoleenv.py
index 7cc334253..7d285af81 100644
--- a/pybrain/rl/environments/cartpole/fast_version/cartpoleenv.py
+++ b/pybrain/rl/environments/cartpole/fast_version/cartpoleenv.py
@@ -10,7 +10,7 @@
try:
import cartpolewrap as impl
-except ImportError, e:
+except ImportError as e:
logging.error("FastCartPoleTask is wrapping C code that needs to be compiled - it's simple: run .../cartpolecompile.py")
raise e
@@ -81,18 +81,18 @@ def outdim(self):
def getReward(self):
r = 1. + impl.getR()
if self.verbose:
- print(' +r', r,)
+ print((' +r', r,))
return r
def isFinished(self):
if self.verbose:
- print(' -finished?', impl.isFinished())
+ print((' -finished?', impl.isFinished()))
return impl.isFinished()
def getObservation(self):
obs = array(impl.getObs())
if self.verbose:
- print('obs', obs)
+ print(('obs', obs))
obs.resize(self.outdim)
if self.extraObservations:
cartpos = obs[-1]
@@ -114,12 +114,12 @@ def getObservation(self):
obs[-self.extraRandoms:] = randn(self.extraRandoms)
if self.verbose:
- print('obs', obs)
+ print(('obs', obs))
return obs
def performAction(self, action):
if self.verbose:
- print('act', action)
+ print(('act', action))
impl.performAction(action[0])
self.addReward()
diff --git a/pybrain/rl/environments/cartpole/nonmarkovdoublepole.py b/pybrain/rl/environments/cartpole/nonmarkovdoublepole.py
index 7b364f98a..6011946a9 100644
--- a/pybrain/rl/environments/cartpole/nonmarkovdoublepole.py
+++ b/pybrain/rl/environments/cartpole/nonmarkovdoublepole.py
@@ -1,7 +1,7 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from doublepole import DoublePoleEnvironment
-from nonmarkovpole import NonMarkovPoleEnvironment
+from .doublepole import DoublePoleEnvironment
+from .nonmarkovpole import NonMarkovPoleEnvironment
class NonMarkovDoublePoleEnvironment(DoublePoleEnvironment, NonMarkovPoleEnvironment):
diff --git a/pybrain/rl/environments/cartpole/nonmarkovpole.py b/pybrain/rl/environments/cartpole/nonmarkovpole.py
index efca4bb71..301a5929d 100644
--- a/pybrain/rl/environments/cartpole/nonmarkovpole.py
+++ b/pybrain/rl/environments/cartpole/nonmarkovpole.py
@@ -1,6 +1,6 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
-from cartpole import CartPoleEnvironment
+from .cartpole import CartPoleEnvironment
class NonMarkovPoleEnvironment(CartPoleEnvironment):
diff --git a/pybrain/rl/environments/classic/acrobot.py b/pybrain/rl/environments/classic/acrobot.py
index ae987f9c4..d778884de 100644
--- a/pybrain/rl/environments/classic/acrobot.py
+++ b/pybrain/rl/environments/classic/acrobot.py
@@ -1,216 +1,216 @@
-__author__ = 'Tom Schaul, tom@idsia.ch'
-
-"""
-Adaptation of the Acrobot Environment
-from the "FAReinforcement" library
-of Jose Antonio Martin H. (version 1.0).
-"""
-
-from scipy import pi, array, cos, sin
-from pybrain.rl.environments.episodic import EpisodicTask
-
-
-class AcrobotTask(EpisodicTask):
- """ TODO: not currently episodic: success just reinitializes it. """
- input_ranges = [[-pi, pi], [-pi, pi], [-4 * pi, 4 * pi], [-9 * pi, 9 * pi]]
- reward_ranges = [[-1.0, 1000.0]]
-
- # The current real values of the state
- maxSpeed1 = 4 * pi
- maxSpeed2 = 9 * pi
- m1 = 1.0
- m2 = 1.0
- l1 = 1.0
- l2 = 1.0
- l1Square = l1 * l1
- l2Square = l2 * l2
- lc1 = 0.5
- lc2 = 0.5
- lc1Square = lc1 * lc1
- lc2Square = lc2 * lc2
- I1 = 1.0
- I2 = 1.0
- g = 9.8
- delta_t = 0.05
-
- #The number of actions.
- action_list = (-1.0 , 0.0 , 1.0)
- nactions = len(action_list)
-
- # angles, velocities and a bias
- nsenses = 5
-
- # number of steps of the current trial
- steps = 0
- maxSteps = 999
-
- # number of the current episode
- episode = 0
-
- target = 1.5
-
- easy_rewards = False
-
- resetOnSuccess = False
-
- def __init__(self):
- self.reset()
- self.cumreward = 0
-
- def getObservation(self):
- return array(self.state + [pi])/(pi)
-
- def performAction(self, action):
- if self.done > 0:
- self.done += 1
- else:
- self.state = self.DoAction(action, self.state)
- self.r, self.done = self.GetReward(self.state)
- self.cumreward += self.r
-
- def reset(self):
- self.state = self.GetInitialState()
-
- def getReward(self):
- return self.r
-
- def isFinished(self):
- if self.done>=3 and self.resetOnSuccess:
- self.reset()
- return False
- else:
- return self.done>=3
-
- def GetInitialState(self):
- s = [0, 0, 0, 0]
- self.StartEpisode()
- return s
-
- def StartEpisode(self):
- self.steps = 0
- self.episode = self.episode + 1
- self.done = 0
-
- def GetReward(self, x):
- # r: the returned reward.
- # f: true if the car reached the goal, otherwise f is false
- y_acrobot = [0, 0, 0]
-
- theta1 = x[0]
- theta2 = x[1]
- y_acrobot[1] = y_acrobot[0] - cos(theta1)
- y_acrobot[2] = y_acrobot[1] - cos(theta2)
- #print(y_acrobot)
- #goal
- goal = y_acrobot[0] + self.target
- if self.easy_rewards:
- r = y_acrobot[2]
- else:
- #r = -0.01
- r = 0
- f = 0
-
- if y_acrobot[2] >= goal:
- if self.easy_rewards:
- r = 10 * y_acrobot[2]
- else:
- r = 1
- f = 1
-
- if self.steps >= self.maxSteps:
- f = 5
- #r = -1
-
- return r, f
-
- def DoAction(self, a, x):
- self.steps = self.steps + 1
- torque = self.action_list[a]
-
- # Parameters for simulation
- theta1, theta2, theta1_dot, theta2_dot = x
-
- for _ in range(4):
- d1 = self.m1 * self.lc1Square + self.m2 * (self.l1Square + self.lc2Square + 2 * self.l1 * self.lc2 * cos(theta2)) + self.I1 + self.I2
- d2 = self.m2 * (self.lc2Square + self.l1 * self.lc2 * cos(theta2)) + self.I2
-
- phi2 = self.m2 * self.lc2 * self.g * cos(theta1 + theta2 - pi / 2.0)
- phi1 = -self.m2 * self.l1 * self.lc2 * theta2_dot * sin(theta2) * (theta2_dot - 2 * theta1_dot) + (self.m1 * self.lc1 + self.m2 * self.l1) * self.g * cos(theta1 - (pi / 2.0)) + phi2
-
- accel2 = (torque + phi1 * (d2 / d1) - self.m2 * self.l1 * self.lc2 * theta1_dot * theta1_dot * sin(theta2) - phi2)
- accel2 = accel2 / (self.m2 * self.lc2Square + self.I2 - (d2 * d2 / d1))
- accel1 = -(d2 * accel2 + phi1) / d1
-
- theta1_dot = theta1_dot + accel1 * self.delta_t
-
- if theta1_dot < -self.maxSpeed1:
- theta1_dot = -self.maxSpeed1
-
- if theta1_dot > self.maxSpeed1:
- theta1_dot = self.maxSpeed1
-
- theta1 = theta1 + theta1_dot * self.delta_t
- theta2_dot = theta2_dot + accel2 * self.delta_t
-
- if theta2_dot < -self.maxSpeed2:
- theta2_dot = -self.maxSpeed2
-
- if theta2_dot > self.maxSpeed2:
- theta2_dot = self.maxSpeed2
-
- theta2 = theta2 + theta2_dot * self.delta_t
-
- # bounded angles?
- #if theta1 < -pi:
- # theta1 = -pi
- #elif theta1 > pi:
- # theta1 = pi
- if theta1 < -pi:
- theta1 += 2*pi
- elif theta1 > pi:
- theta1 -= 2*pi
- if theta2 < -pi:
- theta2 += 2*pi
- elif theta2 > pi:
- theta2 -= 2*pi
-
- xp = [theta1, theta2, theta1_dot, theta2_dot]
-
- return xp
-
-
-class SimpleAcrobot(AcrobotTask):
-
- target = -0.5
-
-class VerySimpleAcrobot(AcrobotTask):
-
- target = -1.
-
-
-class SingleArmSwinger(AcrobotTask):
- """ Variant with one piece fixed."""
-
- nsenses = 3
-
- resetOnSuccess = False
-
- target = 1.95
- maxSteps = 99
-
- def GetInitialState(self):
-
- s = [pi, 0, 0, 0]
- self.StartEpisode()
- return s
-
- def getObservation(self):
- return array(self.state[2:] + [pi])/(pi)
-
- def performAction(self, action):
- AcrobotTask.performAction(self, action)
- # re-fix the upper part of the arm
- _, theta2, _, theta2_dot = self.state
- self.state = [pi, theta2, 0, theta2_dot]
-
-
+__author__ = 'Tom Schaul, tom@idsia.ch'
+
+"""
+Adaptation of the Acrobot Environment
+from the "FAReinforcement" library
+of Jose Antonio Martin H. (version 1.0).
+"""
+
+from scipy import pi, array, cos, sin
+from pybrain.rl.environments.episodic import EpisodicTask
+
+
+class AcrobotTask(EpisodicTask):
+ """ TODO: not currently episodic: success just reinitializes it. """
+ input_ranges = [[-pi, pi], [-pi, pi], [-4 * pi, 4 * pi], [-9 * pi, 9 * pi]]
+ reward_ranges = [[-1.0, 1000.0]]
+
+ # The current real values of the state
+ maxSpeed1 = 4 * pi
+ maxSpeed2 = 9 * pi
+ m1 = 1.0
+ m2 = 1.0
+ l1 = 1.0
+ l2 = 1.0
+ l1Square = l1 * l1
+ l2Square = l2 * l2
+ lc1 = 0.5
+ lc2 = 0.5
+ lc1Square = lc1 * lc1
+ lc2Square = lc2 * lc2
+ I1 = 1.0
+ I2 = 1.0
+ g = 9.8
+ delta_t = 0.05
+
+ #The number of actions.
+ action_list = (-1.0 , 0.0 , 1.0)
+ nactions = len(action_list)
+
+ # angles, velocities and a bias
+ nsenses = 5
+
+ # number of steps of the current trial
+ steps = 0
+ maxSteps = 999
+
+ # number of the current episode
+ episode = 0
+
+ target = 1.5
+
+ easy_rewards = False
+
+ resetOnSuccess = False
+
+ def __init__(self):
+ self.reset()
+ self.cumreward = 0
+
+ def getObservation(self):
+ return array(self.state + [pi])/(pi)
+
+ def performAction(self, action):
+ if self.done > 0:
+ self.done += 1
+ else:
+ self.state = self.DoAction(action, self.state)
+ self.r, self.done = self.GetReward(self.state)
+ self.cumreward += self.r
+
+ def reset(self):
+ self.state = self.GetInitialState()
+
+ def getReward(self):
+ return self.r
+
+ def isFinished(self):
+ if self.done>=3 and self.resetOnSuccess:
+ self.reset()
+ return False
+ else:
+ return self.done>=3
+
+ def GetInitialState(self):
+ s = [0, 0, 0, 0]
+ self.StartEpisode()
+ return s
+
+ def StartEpisode(self):
+ self.steps = 0
+ self.episode = self.episode + 1
+ self.done = 0
+
+ def GetReward(self, x):
+ # r: the returned reward.
+ # f: true if the car reached the goal, otherwise f is false
+ y_acrobot = [0, 0, 0]
+
+ theta1 = x[0]
+ theta2 = x[1]
+ y_acrobot[1] = y_acrobot[0] - cos(theta1)
+ y_acrobot[2] = y_acrobot[1] - cos(theta2)
+ #print(y_acrobot)
+ #goal
+ goal = y_acrobot[0] + self.target
+ if self.easy_rewards:
+ r = y_acrobot[2]
+ else:
+ #r = -0.01
+ r = 0
+ f = 0
+
+ if y_acrobot[2] >= goal:
+ if self.easy_rewards:
+ r = 10 * y_acrobot[2]
+ else:
+ r = 1
+ f = 1
+
+ if self.steps >= self.maxSteps:
+ f = 5
+ #r = -1
+
+ return r, f
+
+ def DoAction(self, a, x):
+ self.steps = self.steps + 1
+ torque = self.action_list[a]
+
+ # Parameters for simulation
+ theta1, theta2, theta1_dot, theta2_dot = x
+
+ for _ in range(4):
+ d1 = self.m1 * self.lc1Square + self.m2 * (self.l1Square + self.lc2Square + 2 * self.l1 * self.lc2 * cos(theta2)) + self.I1 + self.I2
+ d2 = self.m2 * (self.lc2Square + self.l1 * self.lc2 * cos(theta2)) + self.I2
+
+ phi2 = self.m2 * self.lc2 * self.g * cos(theta1 + theta2 - pi / 2.0)
+ phi1 = -self.m2 * self.l1 * self.lc2 * theta2_dot * sin(theta2) * (theta2_dot - 2 * theta1_dot) + (self.m1 * self.lc1 + self.m2 * self.l1) * self.g * cos(theta1 - (pi / 2.0)) + phi2
+
+ accel2 = (torque + phi1 * (d2 / d1) - self.m2 * self.l1 * self.lc2 * theta1_dot * theta1_dot * sin(theta2) - phi2)
+ accel2 = accel2 / (self.m2 * self.lc2Square + self.I2 - (d2 * d2 / d1))
+ accel1 = -(d2 * accel2 + phi1) / d1
+
+ theta1_dot = theta1_dot + accel1 * self.delta_t
+
+ if theta1_dot < -self.maxSpeed1:
+ theta1_dot = -self.maxSpeed1
+
+ if theta1_dot > self.maxSpeed1:
+ theta1_dot = self.maxSpeed1
+
+ theta1 = theta1 + theta1_dot * self.delta_t
+ theta2_dot = theta2_dot + accel2 * self.delta_t
+
+ if theta2_dot < -self.maxSpeed2:
+ theta2_dot = -self.maxSpeed2
+
+ if theta2_dot > self.maxSpeed2:
+ theta2_dot = self.maxSpeed2
+
+ theta2 = theta2 + theta2_dot * self.delta_t
+
+ # bounded angles?
+ #if theta1 < -pi:
+ # theta1 = -pi
+ #elif theta1 > pi:
+ # theta1 = pi
+ if theta1 < -pi:
+ theta1 += 2*pi
+ elif theta1 > pi:
+ theta1 -= 2*pi
+ if theta2 < -pi:
+ theta2 += 2*pi
+ elif theta2 > pi:
+ theta2 -= 2*pi
+
+ xp = [theta1, theta2, theta1_dot, theta2_dot]
+
+ return xp
+
+
+class SimpleAcrobot(AcrobotTask):
+
+ target = -0.5
+
+class VerySimpleAcrobot(AcrobotTask):
+
+ target = -1.
+
+
+class SingleArmSwinger(AcrobotTask):
+ """ Variant with one piece fixed."""
+
+ nsenses = 3
+
+ resetOnSuccess = False
+
+ target = 1.95
+ maxSteps = 99
+
+ def GetInitialState(self):
+
+ s = [pi, 0, 0, 0]
+ self.StartEpisode()
+ return s
+
+ def getObservation(self):
+ return array(self.state[2:] + [pi])/(pi)
+
+ def performAction(self, action):
+ AcrobotTask.performAction(self, action)
+ # re-fix the upper part of the arm
+ _, theta2, _, theta2_dot = self.state
+ self.state = [pi, theta2, 0, theta2_dot]
+
+
diff --git a/pybrain/rl/environments/classic/mountaincar.py b/pybrain/rl/environments/classic/mountaincar.py
index cc29c1d20..4f4cc2f99 100644
--- a/pybrain/rl/environments/classic/mountaincar.py
+++ b/pybrain/rl/environments/classic/mountaincar.py
@@ -1,136 +1,136 @@
-__author__ = 'Tom Schaul, tom@idsia.ch'
-
-"""
-Adaptation of the MountainCar Environment
-from the "FAReinforcement" library
-of Jose Antonio Martin H. (version 1.0).
-"""
-
-from scipy import array, cos
-from pybrain.rl.environments.episodic import EpisodicTask
-
-
-class MountainCar(EpisodicTask):
- # The current real values of the state
- cur_pos = -0.5
- cur_vel = 0.0
- cur_state = [cur_pos, cur_vel]
-
- #The number of actions.
- action_list = (-1.0 , 0.0 , 1.0)
- nactions = len(action_list)
-
- nsenses = 3
-
- # number of steps of the current trial
- steps = 0
-
- # number of the current episode
- episode = 0
-
- # Goal Position
- goalPos = 0.45
-
- maxSteps = 999
-
- resetOnSuccess = False
-
- def __init__(self):
- self.nactions = len(self.action_list)
- self.reset()
- self.cumreward = 0
-
- def reset(self):
- self.state = self.GetInitialState()
-
- def getObservation(self):
- #print(array([self.state[0], self.state[1] * 100, 1]))
- return array([self.state[0], self.state[1] * 100, 1])
-
- def performAction(self, action):
- if self.done > 0:
- self.done += 1
- else:
- self.state = self.DoAction(action, self.state)
- self.r, self.done = self.GetReward(self.state)
- self.cumreward += self.r
-
- def getReward(self):
- return self.r
-
- def GetInitialState(self):
- self.StartEpisode()
- return [-0.5, 0.]
-
- def StartEpisode(self):
- self.steps = 0
- self.episode = self.episode + 1
- self.done = 0
-
- def isFinished(self):
- if self.done>=3 and self.resetOnSuccess:
- self.reset()
- return False
- else:
- return self.done>=3
-
-
- def GetReward(self, s):
- # MountainCarGetReward returns the reward at the current state
- # x: a vector of position and velocity of the car
- # r: the returned reward.
- # f: true if the car reached the goal, otherwise f is false
-
- position = s[0]
- vel = s[1]
- # bound for position; the goal is to reach position = 0.45
- bpright = self.goalPos
-
- r = 0
- f = 0
-
- if position >= bpright:
- r = 1
- f = 1
-
- if self.steps >= self.maxSteps:
- f = 5
-
- return r, f
-
- def DoAction(self, a, s):
- #MountainCarDoAction: executes the action (a) into the mountain car
- # acti: is the force to be applied to the car
- # x: is the vector containning the position and speed of the car
- # xp: is the vector containing the new position and velocity of the car
- #print('action',a)
- #print('state',s)
- force = self.action_list[a]
-
- self.steps = self.steps + 1
-
- position = s[0]
- speed = s[1]
-
- # bounds for position
- bpleft = -1.4
-
- # bounds for speed
- bsleft = -0.07
- bsright = 0.07
-
- speedt1 = speed + (0.001 * force) + (-0.0025 * cos(3.0 * position))
-
- if speedt1 < bsleft:
- speedt1 = bsleft
- elif speedt1 > bsright:
- speedt1 = bsright
-
- post1 = position + speedt1
-
- if post1 <= bpleft:
- post1 = bpleft
- speedt1 = 0.0
-
- return [post1, speedt1]
-
+__author__ = 'Tom Schaul, tom@idsia.ch'
+
+"""
+Adaptation of the MountainCar Environment
+from the "FAReinforcement" library
+of Jose Antonio Martin H. (version 1.0).
+"""
+
+from scipy import array, cos
+from pybrain.rl.environments.episodic import EpisodicTask
+
+
+class MountainCar(EpisodicTask):
+ # The current real values of the state
+ cur_pos = -0.5
+ cur_vel = 0.0
+ cur_state = [cur_pos, cur_vel]
+
+ #The number of actions.
+ action_list = (-1.0 , 0.0 , 1.0)
+ nactions = len(action_list)
+
+ nsenses = 3
+
+ # number of steps of the current trial
+ steps = 0
+
+ # number of the current episode
+ episode = 0
+
+ # Goal Position
+ goalPos = 0.45
+
+ maxSteps = 999
+
+ resetOnSuccess = False
+
+ def __init__(self):
+ self.nactions = len(self.action_list)
+ self.reset()
+ self.cumreward = 0
+
+ def reset(self):
+ self.state = self.GetInitialState()
+
+ def getObservation(self):
+ #print(array([self.state[0], self.state[1] * 100, 1]))
+ return array([self.state[0], self.state[1] * 100, 1])
+
+ def performAction(self, action):
+ if self.done > 0:
+ self.done += 1
+ else:
+ self.state = self.DoAction(action, self.state)
+ self.r, self.done = self.GetReward(self.state)
+ self.cumreward += self.r
+
+ def getReward(self):
+ return self.r
+
+ def GetInitialState(self):
+ self.StartEpisode()
+ return [-0.5, 0.]
+
+ def StartEpisode(self):
+ self.steps = 0
+ self.episode = self.episode + 1
+ self.done = 0
+
+ def isFinished(self):
+ if self.done>=3 and self.resetOnSuccess:
+ self.reset()
+ return False
+ else:
+ return self.done>=3
+
+
+ def GetReward(self, s):
+ # MountainCarGetReward returns the reward at the current state
+ # x: a vector of position and velocity of the car
+ # r: the returned reward.
+ # f: true if the car reached the goal, otherwise f is false
+
+ position = s[0]
+ vel = s[1]
+ # bound for position; the goal is to reach position = 0.45
+ bpright = self.goalPos
+
+ r = 0
+ f = 0
+
+ if position >= bpright:
+ r = 1
+ f = 1
+
+ if self.steps >= self.maxSteps:
+ f = 5
+
+ return r, f
+
+ def DoAction(self, a, s):
+ #MountainCarDoAction: executes the action (a) into the mountain car
+ # acti: is the force to be applied to the car
+ # x: is the vector containning the position and speed of the car
+ # xp: is the vector containing the new position and velocity of the car
+ #print('action',a)
+ #print('state',s)
+ force = self.action_list[a]
+
+ self.steps = self.steps + 1
+
+ position = s[0]
+ speed = s[1]
+
+ # bounds for position
+ bpleft = -1.4
+
+ # bounds for speed
+ bsleft = -0.07
+ bsright = 0.07
+
+ speedt1 = speed + (0.001 * force) + (-0.0025 * cos(3.0 * position))
+
+ if speedt1 < bsleft:
+ speedt1 = bsleft
+ elif speedt1 > bsright:
+ speedt1 = bsright
+
+ post1 = position + speedt1
+
+ if post1 <= bpleft:
+ post1 = bpleft
+ speedt1 = 0.0
+
+ return [post1, speedt1]
+
diff --git a/pybrain/rl/environments/classic/xor.py b/pybrain/rl/environments/classic/xor.py
index 1911cb7b8..98ef39525 100644
--- a/pybrain/rl/environments/classic/xor.py
+++ b/pybrain/rl/environments/classic/xor.py
@@ -1,79 +1,79 @@
-__author__ = 'Tom Schaul, tom@idsia.ch'
-
-from pybrain.rl.environments.episodic import EpisodicTask
-from scipy import array
-from random import randint, random
-
-
-class XORTask(EpisodicTask):
- """ Continuous task, producing binary observations, taking a single, binary action
- rewarding the agent whenever action = xor(obs).
- """
-
- nactions = 2
- nsenses = 3
-
- randomorder = False
-
- and_task = False
- stochasticity = 0
-
- def __init__(self):
- self.r = 0
- self._counter = 0
-
- def getObservation(self):
- if self.randomorder:
- self.obs = array([randint(0,1), randint(0,1), 1])
- else:
- self.obs = array([self._counter%2, (self._counter/2)%2, 1])
- self._counter += 1
- return self.obs
-
- def performAction(self, action):
- if ((self.and_task and (action == self.obs[0] & self.obs[1]))
- or (not self.and_task and action == self.obs[0] ^ self.obs[1])):
- self.r = 1
- else:
- self.r = -1
- #print(self.obs, action, self.r )
- self.addReward()
-
- def getReward(self):
- if random() < self.stochasticity:
- return -self.r
- else:
- return self.r
-
- def isFinished(self):
- return False
-
-
-
-class XORChainTask(XORTask):
- """ Continuous task, producing binary observations, taking a single, binary action
- rewarding the agent whenever action = xor(obs).
- It is a chain, going back to the initial state whenever the bad action is taken.
- Reward increases as we move along the chain.
- """
-
- reward_cutoff = 0
-
- def __init__(self):
- self.r = 0
- self.state = 0
-
- def getObservation(self):
- self.obs = array([self.state%2, (self.state/2)%2, 1])
- return self.obs
-
- def performAction(self, action):
- if ((self.and_task and action == self.obs[0] & self.obs[1])
- or (not self.and_task and action == self.obs[0] ^ self.obs[1])):
- self.r = -1+2*(self.state >= self.reward_cutoff)
- self.state = min(self.state+1, 3)
- else:
- self.r = -1
- self.state = 0
- self.addReward()
+__author__ = 'Tom Schaul, tom@idsia.ch'
+
+from pybrain.rl.environments.episodic import EpisodicTask
+from scipy import array
+from random import randint, random
+
+
+class XORTask(EpisodicTask):
+ """ Continuous task, producing binary observations, taking a single, binary action
+ rewarding the agent whenever action = xor(obs).
+ """
+
+ nactions = 2
+ nsenses = 3
+
+ randomorder = False
+
+ and_task = False
+ stochasticity = 0
+
+ def __init__(self):
+ self.r = 0
+ self._counter = 0
+
+ def getObservation(self):
+ if self.randomorder:
+ self.obs = array([randint(0,1), randint(0,1), 1])
+ else:
+ self.obs = array([self._counter%2, (self._counter/2)%2, 1])
+ self._counter += 1
+ return self.obs
+
+ def performAction(self, action):
+ if ((self.and_task and (action == self.obs[0] & self.obs[1]))
+ or (not self.and_task and action == self.obs[0] ^ self.obs[1])):
+ self.r = 1
+ else:
+ self.r = -1
+ #print(self.obs, action, self.r )
+ self.addReward()
+
+ def getReward(self):
+ if random() < self.stochasticity:
+ return -self.r
+ else:
+ return self.r
+
+ def isFinished(self):
+ return False
+
+
+
+class XORChainTask(XORTask):
+ """ Continuous task, producing binary observations, taking a single, binary action
+ rewarding the agent whenever action = xor(obs).
+ It is a chain, going back to the initial state whenever the bad action is taken.
+ Reward increases as we move along the chain.
+ """
+
+ reward_cutoff = 0
+
+ def __init__(self):
+ self.r = 0
+ self.state = 0
+
+ def getObservation(self):
+ self.obs = array([self.state%2, (self.state/2)%2, 1])
+ return self.obs
+
+ def performAction(self, action):
+ if ((self.and_task and action == self.obs[0] & self.obs[1])
+ or (not self.and_task and action == self.obs[0] ^ self.obs[1])):
+ self.r = -1+2*(self.state >= self.reward_cutoff)
+ self.state = min(self.state+1, 3)
+ else:
+ self.r = -1
+ self.state = 0
+ self.addReward()
\ No newline at end of file
diff --git a/pybrain/rl/environments/flexcube/environment.py b/pybrain/rl/environments/flexcube/environment.py
index 302210462..27c96311a 100644
--- a/pybrain/rl/environments/flexcube/environment.py
+++ b/pybrain/rl/environments/flexcube/environment.py
@@ -1,6 +1,6 @@
__author__ = 'Frank Sehnke, sehnke@in.tum.de'
-import sensors
+from . import sensors
import threading
from pybrain.utilities import threaded
from pybrain.tools.networking.udpconnection import UDPServer
@@ -72,7 +72,7 @@ def reset(self):
self.vel = zeros((8, 3), float)
idx0 = arange(8).repeat(8)
- idx1 = array(range(8) * 8)
+ idx1 = array(list(range(8)) * 8)
self.difM = self.pos[idx0, :] - self.pos[idx1, :] #vectors from all points to all other points
self.springM = sqrt((self.difM ** 2).sum(axis=1)).reshape(64, 1)
self.distM = self.springM.copy() #distance matrix
@@ -147,7 +147,7 @@ def euler(self):
#Distances of new state
idx0 = arange(8).repeat(8)
- idx1 = array(range(8) * 8)
+ idx1 = array(list(range(8)) * 8)
self.difM = self.pos[idx0, :] - self.pos[idx1, :] #vectors from all points to all other points
self.distM = sqrt((self.difM ** 2).sum(axis=1)).reshape(64, 1) #distance matrix
diff --git a/pybrain/rl/environments/flexcube/tasks.py b/pybrain/rl/environments/flexcube/tasks.py
index 9b2efa277..ee889af2a 100644
--- a/pybrain/rl/environments/flexcube/tasks.py
+++ b/pybrain/rl/environments/flexcube/tasks.py
@@ -18,7 +18,7 @@
from pybrain.rl.environments import EpisodicTask
from scipy import array, r_, clip
-import sensors
+from . import sensors
#Task basis class
class NoRewardTask(EpisodicTask):
diff --git a/pybrain/rl/environments/flexcube/viewer.py b/pybrain/rl/environments/flexcube/viewer.py
index fd2a2c843..dc8cf6e17 100644
--- a/pybrain/rl/environments/flexcube/viewer.py
+++ b/pybrain/rl/environments/flexcube/viewer.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Frank Sehnke, sehnke@in.tum.de'
#########################################################################
@@ -28,7 +30,7 @@
from OpenGL.GL import * #@UnusedWildImport
from OpenGL.GLE import * #@UnusedWildImport
from OpenGL.GLU import * #@UnusedWildImport
-import objects3d
+from . import objects3d
from time import sleep
from scipy import ones, array
from pybrain.tools.networking.udpconnection import UDPClient
@@ -73,7 +75,7 @@ def saveTo(self, filename, format="JPEG"):
image = Image.fromstring("RGB", (width, height), data)
image = image.transpose(Image.FLIP_TOP_BOTTOM)
image.save(filename, format)
- print('Saved image to ', filename)
+ print(('Saved image to ', filename))
return image
# the render method containing the Glut mainloop
diff --git a/pybrain/rl/environments/functions/lennardjones.py b/pybrain/rl/environments/functions/lennardjones.py
index dcc356ee1..861ccde7a 100644
--- a/pybrain/rl/environments/functions/lennardjones.py
+++ b/pybrain/rl/environments/functions/lennardjones.py
@@ -11,7 +11,7 @@ class LennardJones(MultiModalFunction):
input are the Cartesian coordinates of all atoms."""
def f(self, x):
- N = self.xdim / 3
+ N = self.xdim // 3
coords = x.reshape((N, 3))
distances = sqrt(scipy.sum((tile(coords, (N, 1, 1)) - swapaxes(tile(coords, (N, 1, 1)), 0, 1)) ** 2, axis=2)) + eye(N)
return 2 * sum(ravel(distances ** -12 - distances ** -6))
@@ -28,7 +28,7 @@ def _exampleConfig(self, numatoms, noise=0.05, edge=2.):
@property
def desiredValue(self):
- N = self.xdim / 3
+ N = self.xdim // 3
return self.BEST_KNOWN_TABLE[N] + 1e-5
BEST_KNOWN_TABLE = {0:0, 1:0,
diff --git a/pybrain/rl/environments/functions/multimodal.py b/pybrain/rl/environments/functions/multimodal.py
index 7256bc1e3..f8d746e91 100644
--- a/pybrain/rl/environments/functions/multimodal.py
+++ b/pybrain/rl/environments/functions/multimodal.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
""" The functions implemented here are standard benchmarks from literature. """
__author__ = 'Tom Schaul, tom@idsia.ch'
@@ -146,7 +148,7 @@ class GallagherGauss101MeFunction(MultiModalFunction):
def __init__(self, *args, **kwargs):
MultiModalFunction.__init__(self, *args, **kwargs)
- print(self.numPeaks, self.xdim)
+ print((self.numPeaks, self.xdim))
self._opts = [(rand(self.xdim) - 0.5) * 8]
self._opts.extend([(rand(self.xdim) - 0.5) * 9.8 for _ in range(self.numPeaks-1)])
alphas = [power(self.maxCond, 2 * i / float(self.numPeaks - 2)) for i in range(self.numPeaks - 1)]
diff --git a/pybrain/rl/environments/functions/unimodal.py b/pybrain/rl/environments/functions/unimodal.py
index f5e822b1d..b84261903 100644
--- a/pybrain/rl/environments/functions/unimodal.py
+++ b/pybrain/rl/environments/functions/unimodal.py
@@ -75,7 +75,7 @@ def __init__(self, *args, **kwargs):
self.xopt = (rand(self.xdim) - 0.5) * 9.8
def f(self, x):
- from transformations import BBOBTransformationFunction
+ from .transformations import BBOBTransformationFunction
quad = (x*self.xopt > 0)
sz = 100 * x * quad + x * (quad==False)
return power(BBOBTransformationFunction.oscillatify(dot(sz, sz)), 0.9)
@@ -124,7 +124,7 @@ class GlasmachersFunction(FunctionEnvironment):
xdimMin = 2
def f(self, x):
- m = self.xdim/2
+ m = self.xdim//2
a = self.c * norm(x[:m])
b = norm(x[m:])
return a + b + sqrt(2*a*b+b**2)
diff --git a/pybrain/rl/environments/graphical.py b/pybrain/rl/environments/graphical.py
index 4dbffb3a9..6ab426516 100644
--- a/pybrain/rl/environments/graphical.py
+++ b/pybrain/rl/environments/graphical.py
@@ -1,6 +1,6 @@
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
-from environment import Environment
+from .environment import Environment
class GraphicalEnvironment(Environment):
""" Special type of environment that has graphical output and therefore needs a renderer.
diff --git a/pybrain/rl/environments/mazes/maze.py b/pybrain/rl/environments/mazes/maze.py
index 267a0a2f1..53a00b80e 100644
--- a/pybrain/rl/environments/mazes/maze.py
+++ b/pybrain/rl/environments/mazes/maze.py
@@ -78,7 +78,7 @@ def _moveInDir(self, pos, dir):
def performAction(self, action):
if self.stochAction > 0:
if random() < self.stochAction:
- action = choice(range(len(self.allActions)))
+ action = choice(list(range(len(self.allActions))))
tmp = self._moveInDir(self.perseus, self.allActions[action])
if self.mazeTable[tmp] == False:
self.perseus = tmp
diff --git a/pybrain/rl/environments/mazes/polarmaze.py b/pybrain/rl/environments/mazes/polarmaze.py
index 2fff9e19b..5f13f3515 100644
--- a/pybrain/rl/environments/mazes/polarmaze.py
+++ b/pybrain/rl/environments/mazes/polarmaze.py
@@ -3,7 +3,7 @@
from scipy import zeros
from random import choice, random
-from maze import Maze
+from .maze import Maze
class PolarMaze(Maze):
@@ -23,12 +23,12 @@ class PolarMaze(Maze):
def reset(self):
Maze.reset(self)
- self.perseusDir = choice(range(4))
+ self.perseusDir = choice(list(range(4)))
def performAction(self, action):
if self.stochAction > 0:
if random() < self.stochAction:
- action = choice(range(len(PolarMaze.allActions)))
+ action = choice(list(range(len(PolarMaze.allActions))))
act = PolarMaze.allActions[action]
self.bang = False
if act == self.Forward:
diff --git a/pybrain/rl/environments/mazes/tasks/cheesemaze.py b/pybrain/rl/environments/mazes/tasks/cheesemaze.py
index fe499021c..4b8bd6aa8 100644
--- a/pybrain/rl/environments/mazes/tasks/cheesemaze.py
+++ b/pybrain/rl/environments/mazes/tasks/cheesemaze.py
@@ -2,7 +2,7 @@
from scipy import zeros, array
-from maze import MazeTask
+from .maze import MazeTask
class CheeseMaze(MazeTask):
diff --git a/pybrain/rl/environments/mazes/tasks/maze.py b/pybrain/rl/environments/mazes/tasks/maze.py
index 284697544..8b91c160b 100644
--- a/pybrain/rl/environments/mazes/tasks/maze.py
+++ b/pybrain/rl/environments/mazes/tasks/maze.py
@@ -2,7 +2,7 @@
from scipy import array
-from pomdp import POMDPTask
+from .pomdp import POMDPTask
from pybrain.rl.environments.mazes import Maze
from pybrain.rl.environments.task import Task
diff --git a/pybrain/rl/environments/mazes/tasks/maze4x3.py b/pybrain/rl/environments/mazes/tasks/maze4x3.py
index 420d438c7..5fbfe304c 100644
--- a/pybrain/rl/environments/mazes/tasks/maze4x3.py
+++ b/pybrain/rl/environments/mazes/tasks/maze4x3.py
@@ -3,7 +3,7 @@
from random import random, choice
from scipy import array, zeros
-from maze import MazeTask
+from .maze import MazeTask
class FourByThreeMaze(MazeTask):
diff --git a/pybrain/rl/environments/mazes/tasks/maze89state.py b/pybrain/rl/environments/mazes/tasks/maze89state.py
index 840090279..c6bc2f47a 100644
--- a/pybrain/rl/environments/mazes/tasks/maze89state.py
+++ b/pybrain/rl/environments/mazes/tasks/maze89state.py
@@ -2,7 +2,7 @@
from scipy import array
-from maze import MazeTask
+from .maze import MazeTask
from pybrain.rl.environments.mazes import PolarMaze
diff --git a/pybrain/rl/environments/mazes/tasks/shuttle.py b/pybrain/rl/environments/mazes/tasks/shuttle.py
index cb3c4ab19..f13a6feea 100644
--- a/pybrain/rl/environments/mazes/tasks/shuttle.py
+++ b/pybrain/rl/environments/mazes/tasks/shuttle.py
@@ -3,7 +3,7 @@
from scipy import array, zeros
from random import random
-from maze import MazeTask
+from .maze import MazeTask
from pybrain.rl.environments.mazes import PolarMaze
diff --git a/pybrain/rl/environments/mazes/tasks/tiger.py b/pybrain/rl/environments/mazes/tasks/tiger.py
index 2f9747c7e..6d0b05623 100644
--- a/pybrain/rl/environments/mazes/tasks/tiger.py
+++ b/pybrain/rl/environments/mazes/tasks/tiger.py
@@ -3,7 +3,7 @@
from random import choice, random
from scipy import array
-from pomdp import POMDPTask
+from .pomdp import POMDPTask
class TigerTask(POMDPTask):
diff --git a/pybrain/rl/environments/mazes/tasks/tmaze.py b/pybrain/rl/environments/mazes/tasks/tmaze.py
index 6dd33e7f4..6dda09195 100644
--- a/pybrain/rl/environments/mazes/tasks/tmaze.py
+++ b/pybrain/rl/environments/mazes/tasks/tmaze.py
@@ -3,7 +3,7 @@
from scipy import array, zeros
from random import choice
-from maze import MazeTask
+from .maze import MazeTask
class TMaze(MazeTask):
diff --git a/pybrain/rl/environments/ode/environment.py b/pybrain/rl/environments/ode/environment.py
index 7bdbc518f..87040feee 100644
--- a/pybrain/rl/environments/ode/environment.py
+++ b/pybrain/rl/environments/ode/environment.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
import sys, time
@@ -6,8 +8,8 @@
import ode #@UnresolvedImport
from pybrain.rl.environments.environment import Environment
-from tools.configgrab import ConfigGrabber
-import sensors, actuators
+from .tools.configgrab import ConfigGrabber
+from . import sensors, actuators
from pybrain.utilities import threaded
import threading
import warnings
@@ -167,7 +169,7 @@ def centerOn(self, name):
self.getRenderer().setCenterObj(self.root.namedChild(name).getODEObject())
except KeyError:
# name not found, unset centerObj
- print("Warning: Cannot center on " + name)
+ print(("Warning: Cannot center on " + name))
self.centerObj = None
def loadXODE(self, filename, reload=False):
@@ -182,7 +184,7 @@ def loadXODE(self, filename, reload=False):
world = filter(lambda x: isinstance(x, xode.parser.World), self.root.getChildren())[0]
except IndexError:
# malicious format, no world tag found
- print("no tag found in " + filename + ". quitting.")
+ print(("no tag found in " + filename + ". quitting."))
sys.exit()
self.world = world.getODEObject()
self._setWorldParameters()
@@ -191,7 +193,7 @@ def loadXODE(self, filename, reload=False):
space = filter(lambda x: isinstance(x, xode.parser.Space), world.getChildren())[0]
except IndexError:
# malicious format, no space tag found
- print("no tag found in " + filename + ". quitting.")
+ print(("no tag found in " + filename + ". quitting."))
sys.exit()
self.space = space.getODEObject()
@@ -203,7 +205,7 @@ def loadXODE(self, filename, reload=False):
print("-------[body/mass list]-----")
for (body, _) in self.body_geom:
try:
- print(body.name, body.getMass())
+ print((body.name, body.getMass()))
except AttributeError:
print("")
@@ -221,7 +223,7 @@ def loadConfig(self, filename, reload=False):
self.passpairs.append(eval(passpairstring))
if self.verbosity > 0:
print("-------[pass tuples]--------")
- print(self.passpairs)
+ print((self.passpairs))
print("----------------------------")
#
@@ -238,7 +240,7 @@ def loadConfig(self, filename, reload=False):
# find first object with that name
obj = self.root.namedChild(jointName).getODEObject()
except IndexError:
- print("ERROR: Could not affix object '" + jointName + "' to environment!")
+ print(("ERROR: Could not affix object '" + jointName + "' to environment!"))
sys.exit(1)
if isinstance(obj, ode.Joint):
# if it is a joint, use this joint to fix to environment
@@ -273,7 +275,7 @@ def loadConfig(self, filename, reload=False):
try:
self.addSensor(eval('sensors.' + s))
except AttributeError:
- print(dir(sensors))
+ print((dir(sensors)))
warnings.warn("Sensor name with name " + s + " not found. skipped.")
else:
for s in self.sensors:
diff --git a/pybrain/rl/environments/ode/instances/ccrl.py b/pybrain/rl/environments/ode/instances/ccrl.py
index 828742b23..499ec258b 100644
--- a/pybrain/rl/environments/ode/instances/ccrl.py
+++ b/pybrain/rl/environments/ode/instances/ccrl.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Frank Sehnke, sehnke@in.tum.de'
from pybrain.rl.environments.ode import ODEEnvironment, sensors, actuators
@@ -95,7 +97,7 @@ def loadXODE(self, filename, reload=False):
world = filter(lambda x: isinstance(x, xode.parser.World), self.root.getChildren())[0]
except IndexError:
# malicious format, no world tag found
- print("no tag found in " + filename + ". quitting.")
+ print(("no tag found in " + filename + ". quitting."))
sys.exit()
self.world = world.getODEObject()
self._setWorldParameters()
@@ -104,7 +106,7 @@ def loadXODE(self, filename, reload=False):
space = filter(lambda x: isinstance(x, xode.parser.Space), world.getChildren())[0]
except IndexError:
# malicious format, no space tag found
- print("no tag found in " + filename + ". quitting.")
+ print(("no tag found in " + filename + ". quitting."))
sys.exit()
self.space = space.getODEObject()
@@ -122,7 +124,7 @@ def loadXODE(self, filename, reload=False):
print("-------[body/mass list]-----")
for (body, _) in self.body_geom:
try:
- print(body.name, body.getMass())
+ print((body.name, body.getMass()))
except AttributeError:
print("")
diff --git a/pybrain/rl/environments/ode/tasks/ccrl.py b/pybrain/rl/environments/ode/tasks/ccrl.py
index d13451be1..7378e9904 100644
--- a/pybrain/rl/environments/ode/tasks/ccrl.py
+++ b/pybrain/rl/environments/ode/tasks/ccrl.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Frank Sehnke, sehnke@in.tum.de'
from pybrain.rl.environments import EpisodicTask
@@ -129,7 +131,7 @@ def getReward(self):
self.dist[8] = 0.0
dis = 100.0/((self.dist[:3] ** 2).sum()+0.1)
nig = 10.0/((self.dist[3:] ** 2).sum()+0.1)
- if self.env.stepCounter == self.epiLen: print("Grip:", grip, "Dis:", dis, "Nig:", nig, "Table:", self.tableFlag)
+ if self.env.stepCounter == self.epiLen: print(("Grip:", grip, "Dis:", dis, "Nig:", nig, "Table:", self.tableFlag))
return (10 + grip + nig + dis + self.tableFlag) / float(self.epiLen) #-dis
#else:
# return (25.0 - dis) / float(self.epiLen) + (grip / nig - float(self.env.tableSum)) * 0.1 #+self.grepRew (10.0-dis)/float(self.epiLen)+
diff --git a/pybrain/rl/environments/ode/tools/xmltools.py b/pybrain/rl/environments/ode/tools/xmltools.py
index b481a69cc..765ef055d 100644
--- a/pybrain/rl/environments/ode/tools/xmltools.py
+++ b/pybrain/rl/environments/ode/tools/xmltools.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
import sys
@@ -123,7 +125,7 @@ def getCurrentSubtags(self):
def hasSubtag(self, name=None):
"""determine whether current tag contains other tags, and returns
the tag with a matching name (if name is given) or True (if not)"""
- if self.tag.has_key('Icontain'):
+ if 'Icontain' in self.tag:
if name is None:
return(True)
else:
@@ -135,14 +137,14 @@ def hasSubtag(self, name=None):
def getSubtag(self, name=None):
"""determine whether current tag contains other tags, and returns
the tag with a matching name (if name is given) or None (if not)"""
- if self.tag.has_key('Icontain'):
+ if 'Icontain' in self.tag:
for subtag in self.tag['Icontain']:
if subtag.name == name: return(subtag)
return(None)
def nbAttributes(self):
"""return number of user attributes the current tag has"""
- nAttr = len(self.tag.keys()) - 1
+ nAttr = len(list(self.tag.keys())) - 1
if self.hasSubtag():
nAttr -= 1
return nAttr
@@ -151,7 +153,7 @@ def nbAttributes(self):
def scale(self, sc, scaleset=set([]), exclude=set([])):
"""for all tags not in the exclude set, scale all attributes whose names are in scaleset by the given factor"""
if self.name not in exclude:
- for name, val in self.tag.iteritems():
+ for name, val in self.tag.items():
if name in scaleset:
self.tag[name] = val * sc
if self.hasSubtag():
@@ -162,7 +164,7 @@ def scale(self, sc, scaleset=set([]), exclude=set([])):
def write(self, file, depth=0):
"""parse XML structure recursively and append to the output fileID,
increasing the offset (tabs) while descending into the tree"""
- if not self.tag.has_key('myName'):
+ if 'myName' not in self.tag:
print("Error parsing XML structure: Tag name missing!")
sys.exit(1)
# find number of attributes (disregarding special keys)
@@ -173,7 +175,7 @@ def write(self, file, depth=0):
# print(start tag, with attributes if present)
if nAttr > 0:
file.write(self._tab * depth + "<" + self.tag['myName'] + " " + \
- ' '.join([name + '="' + str(val) + '"' for name, val in self.tag.iteritems() \
+ ' '.join([name + '="' + str(val) + '"' for name, val in self.tag.items() \
if name != 'myName' and name != 'Icontain']) + endmark + '\n')
else:
file.write(self._tab * depth + "<" + self.tag['myName'] + ">\n")
diff --git a/pybrain/rl/environments/ode/tools/xodetools.py b/pybrain/rl/environments/ode/tools/xodetools.py
index bec109f1a..8c07fe8ef 100644
--- a/pybrain/rl/environments/ode/tools/xodetools.py
+++ b/pybrain/rl/environments/ode/tools/xodetools.py
@@ -1,700 +1,702 @@
-__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
-
-import sys
-from xmltools import XMLstruct
-from math import asin, cos, sin, pi, degrees, radians, pow
-from scipy import array, matrix, sqrt
-import random
-
-class XODEfile(XMLstruct):
- """
- Creates a (virtual) XODE file, into which bodies, joints and custom
- parameters can be inserted. This file can be merged at a defined level
- with other instances of itself, and written to disk in the
- standard format.
-
- $Id:xodetools.py 150 2007-04-11 13:42:47Z ruecksti $
- """
-
- def __init__(self, name, **kwargs):
- """initialize the XODE structure with a name and the world and
- space tags"""
- self._xodename = name
- self._centerOn = None
- self._affixToEnvironment = None
- # sensors is a list of ['type', [args], {kwargs}]
- self.sensors = []
- # sensor elements is a list of joints to be used as pressure sensors
- self.sensorElements = []
- self._nSensorElements = 0
- self._pass = {} # dict of sets containing objects allowed to pass
- self._colors = [] # list of tuples ('name', (r,g,b))
- XMLstruct.__init__(self, 'world')
- self.insert('space')
- # TODO: insert palm, support, etc. (derived class)
-
- def _mass2dens(self, shape, size, mass):
- """converts a mass into a density"""
- if shape == 'box':
- return mass / float(size[0] * size[1] * size[2])
- elif shape == 'cylinder' or shape == 'cappedCylinder':
- if shape == 'cylinder':
- return mass / (12.56637061 * size[0] * size[0] * size[1])
- else:
- return mass / (size[0] * size[0] * (12.56637061 * (size[1] - 2 * size[0]) + 4.18879020 * size[0]))
- elif shape == 'sphere':
- return mass / (4.18879020 * pow(size[0], 3))
- else:
- print("Unknown shape: " + shape + " not implemented!")
- sys.exit(1)
-
- def _dens2mass(self, shape, size, dens):
- """converts a density into a mass"""
- if shape == 'box':
- return dens * float(size[0] * size[1] * size[2])
- elif shape == 'cylinder' or shape == 'cappedCylinder':
- if shape == 'cylinder':
- return dens * (12.56637061 * size[0] * size[0] * size[1])
- else:
- return dens * (size[0] * size[0] * (12.56637061 * (size[1] - 2 * size[0]) + 4.18879020 * size[0]))
- elif shape == 'sphere':
- return dens * (4.18879020 * pow(size[0], 3))
- else:
- print("Unknown shape: " + shape + " not implemented!")
- sys.exit(1)
-
-
- def insertBody(self, bname, shape, size, density, pos=[0, 0, 0], passSet=None, euler=None, mass=None, color=None):
- """Inserts a body with the given custom name and one of the standard
- shapes. The size and pos parameters are given as xyz-lists or tuples.
- euler are three rotation angles (degrees),
- if mass is given, density is calculated automatically"""
- self.insert('body', {'name': bname})
- if color is not None:
- self._colors.append((bname, color))
- self.insert('transform')
- self.insert('position', {'x':pos[0], 'y':pos[1], 'z':pos[2]})
- if euler is not None:
- self.up()
- self.insert('rotation')
- self.insert('euler', {'x':euler[0], 'y':euler[1], 'z':euler[2], 'aformat':'degrees'})
- self.up()
- self.up(2)
- self.insert('mass')
- if shape == 'box':
- dims = {'sizex':size[0], 'sizey':size[1], 'sizez':size[2]}
- elif shape == 'cylinder' or shape == 'cappedCylinder':
- dims = {'radius':size[0], 'length':size[1]}
- elif shape == 'sphere':
- dims = {'radius':size[0]}
- else:
- print("Unknown shape: " + shape + " not implemented!")
- sys.exit(1)
- if mass is not None:
- density = self._mass2dens(shape, size, mass)
-
- self.insert('mass_shape', {'density': density})
- self.insert(shape, dims)
- self.up(3)
- self.insert('geom')
- self.insert(shape, dims)
- self.up(3)
- # add the body to a matching pass set
- if passSet is not None:
- for pset in passSet:
- try:
- self._pass[pset].add(bname)
- except KeyError:
- self._pass[pset] = set([bname])
-
-
-
- def insertJoint(self, body1, body2, type, axis=None, anchor=(0, 0, 0), rel=False, name=None):
- """Inserts a joint of given type linking the two bodies. Default name is
- a "_"-concatenation of the body names. The anchor is a xyz-tuple, rel is
- a boolean specifying whether the anchor coordinates refer to the body's origin,
- axis parameters have to be provided as a dictionary."""
- if name is None: name = body1 + "_" + body2
- if rel:
- abs = 'false'
- else:
- abs = 'true'
-
- self.insert('joint', {'name': name })
- self.insert('link1', {'body': body1})
- self.up()
- self.insert('link2', {'body': body2})
- self.up()
- self.insert(type)
- if type == 'fixed':
- self.insert(None) # empty subtag, seems to be needed by xode parser
- elif type == 'ball':
- self.insert('anchor', {'x':anchor[0], 'y':anchor[1], 'z':anchor[2], 'absolute':abs})
- self.up()
- elif type == 'slider':
- self.insert('axis', axis)
- self.up()
- elif type == 'hinge':
- self.insert('axis', axis)
- self.up()
- self.insert('anchor', {'x':anchor[0], 'y':anchor[1], 'z':anchor[2], 'absolute':abs})
- self.up()
- else:
- print("Sorry, joint type " + type + " not yet implemented!")
- sys.exit()
- self.up(2)
- return name
-
-
- def insertFloor(self, y= -0.5):
- """inserts a bodiless floor at given y offset"""
- self.insert('geom', {'name': 'floor'})
- self.insert('plane', {'a': 0, 'b': 1, 'c': 0, 'd': y})
- self.up(2)
-
- def insertPressureSensorElement(self, parent, name=None, shape='cappedCylinder', size=[0.16, 0.5], pos=[0, 0, 0], euler=[0, 0, 0], dens=1, \
- mass=None, passSet=[], stiff=10.0):
- """Insert one single pressure sensor element of the given shape, size, density, etc.
- The sliding axis is by default oriented along the z-axis, which is also the default for cylinder shapes.
- You have to rotate the sensor into the correct orientation - the sliding axis will be rotated accordingly.
- Stiffness of the sensor's spring is set via stiff, whereby damping is calculated automatically to prevent
- oscillations."""
-
- if name is None:
- name = 'psens' + str(self._nSensorElements)
- if mass is None:
- mass = self._dens2mass(shape, size, dens)
- else:
- dens = self._mass2dens(shape, size, mass)
-
- self._nSensorElements += 1
- h = 0.02 # temporal stepwidth
-
- self.insertBody(name, shape, size, dens, pos=pos, euler=euler, passSet=passSet)
-
- # In the aperiodic limit case, we have
- # kp = a kd^2 / 4m i.e. kd = 2 sqrt(m kp/a)
- # where kp is the spring constant, kd is the dampening constant, and m is the mass of the oscillator.
- # For practical purposes, it is often better if kp is a few percent stronger such that the sensor
- # resets itself faster (but still does not oscillate a lot), thus a~=1.02.
- # ERP = h kp / (h kp + kd)
- # CFM = 1 / (h kp + kd) = ERP / h kp
- # For assumed mass of finger of 0.5kg, kp=10, kd=4.5 is approx. the non-oscillatory case.
- kd = 2.0 * sqrt(mass * stiff / 1.02)
- ERP = h * stiff / (h * stiff + kd)
- CFM = ERP / (h * stiff)
-
- # Furthermore, compute the sliding axis direction from the Euler angles (x-convention, see
- # http://mathworld.wolfram.com/EulerAngles.html): Without rotation, the axis is along
- # the z axis, just like a cylinder's axis
- w = array(euler) * pi / 180
- #A = matrix([[cos(w[0]), sin(w[0]), 0], [-sin(w[0]),cos(w[0]),0],[0,0,1]])
- #A = matrix([[1,0,0], [0,cos(w[1]), sin(w[1])], [0, -sin(w[1]),cos(w[1])]]) * A
- #A = matrix([[cos(w[2]), sin(w[2]), 0], [-sin(w[2]),cos(w[2]),0],[0,0,1]]) * A
- # hmmm, it seems XODE is rather using the y-convention here:
- A = matrix([[-sin(w[0]), cos(w[0]), 0], [-cos(w[0]), -sin(w[0]), 0], [0, 0, 1]])
- A = matrix([[1, 0, 0], [0, cos(w[1]), sin(w[1])], [0, -sin(w[1]), cos(w[1])]]) * A
- A = matrix([[sin(w[2]), -cos(w[2]), 0], [cos(w[2]), sin(w[2]), 0], [0, 0, 1]]) * A
- ax = ((A * matrix([0, 0, 1]).getT()).flatten().tolist())[0]
-
- jname = self.insertJoint(name, parent, 'slider', \
- axis={'x':ax[0], 'y':ax[1], 'z':ax[2], "HiStop":0.0, "LowStop":0.0, "StopERP":ERP, "StopCFM":CFM })
- self.sensorElements.append(jname)
- return name
-
- def attachSensor(self, type, *args, **kwargs):
- """adds a sensor with the given type, arguments and keywords, see sensors module for details"""
- self.sensors.append([type, args, kwargs])
-
-
- def merge(self, xodefile, joinLevel='space'):
- """Merge a second XODE file into this one, at the specified
- level (which must exist in both files). The passpair lists are
- also joined. Upon return, the current tag for both objects
- is the one given."""
- self.top()
- if not self.downTo(joinLevel):
- print("Error: Cannot merge " + self.name + " at level " + joinLevel)
- xodefile.top()
- if not xodefile.downTo(joinLevel):
- print("Error: Cannot merge " + xodefile.name + " at level " + joinLevel)
- self.insertMulti(xodefile.getCurrentSubtags())
- self._pass.update(xodefile.getPassList())
-
- def centerOn(self, name):
- self._centerOn = name
-
-
- def affixToEnvironment(self, name):
- self._affixToEnvironment = name
-
- def getPassList(self):
- return(self._pass)
-
- def scaleModel(self, sc):
- """scales all spatial dimensions by the given factor
- FIXME: quaternions may cause problems, which are currently ignored"""
- # scale these attributes...
- scaleset = set(['x', 'y', 'z', 'a', 'b', 'c', 'd', 'sizex', 'sizey', 'sizez', 'length', 'radius'])
- # ... unless contained in these tags (in which case they specify angles)
- exclude = set(['euler', 'finiteRotation', 'axisangle'])
- self.scale(sc, scaleset, exclude)
-
-
- def writeCustomParameters(self, f):
- """writes our custom parameters into an XML comment"""
- f.write('\n')
-
-
- def writeXODE(self, filename=None):
- """writes the created structure (plus header and footer) to file with
- the given basename (.xode is appended)"""
- if filename is None: filename = self._xodename
- f = file(filename + '.xode', 'wb') # <-- wb here ensures Linux compatibility
- f.write('\n')
- f.write('\n\n')
- self.write(f)
- f.write('\n')
- self.writeCustomParameters(f)
- f.close()
- print("Wrote " + filename + '.xode')
-
-class XODEfinger(XODEfile):
-
- def __init__(self, name, **kwargs):
- """Creates one finger on a fixed palm, and adds some sensors"""
- XODEfile.__init__(self, name, **kwargs)
- # create the hand and finger
- self.insertBody('palm', 'box', [10, 2, 10], 5, pos=[3.75, 4, 0], passSet=['pal'])
- self.insertBody('sample', 'box', [10, 0.2, 40], 5, pos=[0, 0.4, 0], passSet=['sam'])
- self.insertJoint('palm', 'sample', 'fixed', name='palm_support')
- self.insertJoint('palm', 'sample', 'slider', axis={'x':0, 'y':0, 'z':1, 'FMax':11000.0, "HiStop":100.0, "LowStop":-100.0})
- self.insertBody('finger1_link0', 'cappedCylinder', [1, 7.5], 5, pos=[0, 4, 8.75], passSet=['pal', 'f1'])
- self.insertBody('finger1_link1', 'cappedCylinder', [1, 4], 5, pos=[0, 4, 14.5], passSet=['f1', 'f2'])
- self.insertBody('fingertip', 'cappedCylinder', [1, 2.9], 5, pos=[0, 4, 17.95], passSet=['f2', 'haptic'])
- self.insertJoint('palm', 'finger1_link0', 'hinge', \
- axis={'x':-1, 'y':0, 'z':0, "HiStop":8, "LowStop":0.0}, anchor=(0, 4, 5))
- self.insertJoint('finger1_link0', 'finger1_link1', 'hinge', \
- axis={'x':-1, 'y':0, 'z':0, "HiStop":15, "LowStop":0.0}, anchor=(0, 4, 12.5))
- self.insertJoint('finger1_link1', 'fingertip', 'hinge', \
- axis={'x':-1, 'y':0, 'z':0, "HiStop":15, "LowStop":0.0}, anchor=(0, 4, 16.5))
- self.centerOn('fingertip')
- self.affixToEnvironment('palm_support')
- self.insertFloor()
- # add one group of haptic sensors
- self._nSensorElements = 0
- self.sensorElements = []
- self.sensorGroupName = None
- self.insertHapticSensors()
- # give some structure to the sample
- self.insertSampleStructure(**kwargs)
-
- def insertHapticSensorsRandom(self):
- """insert haptic sensors at random locations"""
- self.sensorGroupName = 'haptic'
- for _ in range(5):
- self.insertHapticSensor(dx=random.uniform(-0.65, 0.65), dz=random.uniform(-0.4, 0.2))
- ##self.insertHapticSensor(dx=-0.055)
-
-
- def insertHapticSensors(self):
- """insert haptic sensors at predetermined locations
- (check using testhapticsensorslocations.py)"""
- self.sensorGroupName = 'haptic'
- x = [0.28484253596392306, -0.59653176701550947, -0.36877718203650889, 0.50549219349016294, -0.22467390532644882, 0.051978612692656596, -0.18287341960589126, 0.40477910340060383, 0.56041266484490182, -0.47806390012776134]
- z = [-0.20354546253333045, -0.23178541627964597, 0.04632154813480549, -0.27525024891443889, -0.20352571063065863, -0.07930554411063101, 0.025260779785407084, 0.091906227805625964, -0.031751424859005839, -0.0034220681106161277]
- nSens = len(x)
- for _ in range(nSens):
- self.insertHapticSensor(dx=x.pop(), dz=z.pop())
-
-
- def insertSampleStructure(self, angle=None):
- pass
-
- def getSensors(self):
- """return the list of haptic sensors defined"""
- return(self.sensorElements)
-
- def insertHapticSensor(self, ctr=(0, 4, 18.5), dx=0.0, dz=0.0):
- """insert one single haptic sensor"""
- name = 'haptic' + str(self._nSensorElements)
- self._nSensorElements += 1
- # centers of haptic sensors lie on a cylinder of fixed radius (slightly bigger than fingertip)
- R = 1.2
- alpha = asin(dx / R)
- dy = -R * cos(alpha)
- pos = [dx, dy, dz]
- rot = [90, degrees(alpha), 0]
- # ERP = h kp / (h kp + kd)
- # CFM = 1 / (h kp + kd) = ERP / h kp
- _h = 0.01 # temporal stepwidth
- # CHECKME: unused!
- # for assumed mass of finger of 0.5kg, kp=10, kd=4.5 is approx. the non-oscillatory case
- self.insertBody(name, 'cappedCylinder', [0.08, 0.5], 7, pos=[ctr[0] + pos[0], ctr[1] + pos[1], ctr[2] + pos[2]], \
- euler=rot, passSet=['haptic'])
- jname = 'finger1_' + name
- self.insertJoint('fingertip', name, 'slider', name=jname, \
- axis={'x':-dx, 'y':-dy, 'z':0, "HiStop":0.0, "LowStop":0.0, "StopERP":0.022, "StopCFM":0.22 })
- self.sensorElements.append(jname)
-
-class XODEhand(XODEfile):
-
- def __init__(self, name, **kwargs):
- """Creates hand with fingertip and palm sensors -- palm up"""
- XODEfile.__init__(self, name, **kwargs)
- # create the hand and finger
- self.insertBody('palm', 'box', [10, 2, 10], 30, pos=[0, 0, 0], passSet=['pal'])
- self.insertBody('pressure', 'box', [8, 0.5, 8], 30, pos=[0, 1, 0], passSet=['pal'])
- self.insertBody('finger0_link0', 'cappedCylinder', [1, 7.5], 5, pos=[-8.75, 0, -2.5], euler=[0, 90, 0], passSet=['pal', 'f01'])
- self.insertBody('finger0_link1', 'cappedCylinder', [1, 4], 5, pos=[-14.5, 0, -2.5], euler=[0, 90, 0], passSet=['f01', 'f02'])
- self.insertBody('finger0_link2', 'cappedCylinder', [1, 2.9], 5, pos=[-17.95, 0, -2.5], euler=[0, 90, 0], passSet=['f02', 'f03'])
- self.insertBody('finger0_link3', 'sphere', [1], 5, pos=[-19, 0, -2.5], passSet=['f03'])
- self.insertBody('finger1_link0', 'cappedCylinder', [1, 7.5], 5, pos=[-3.75, 0, 8.75], passSet=['pal', 'f11'])
- self.insertBody('finger1_link1', 'cappedCylinder', [1, 4], 5, pos=[-3.75, 0, 14.5], passSet=['f11', 'f12'])
- self.insertBody('finger1_link2', 'cappedCylinder', [1, 2.9], 5, pos=[-3.75, 0, 17.95], passSet=['f12', 'f13'])
- self.insertBody('finger1_link3', 'sphere', [1], 5, pos=[-3.75, 0, 19], passSet=['f13'])
- self.insertBody('finger2_link0', 'cappedCylinder', [1, 7.5], 5, pos=[0, 0, 8.75], passSet=['pal', 'f21'])
- self.insertBody('finger2_link1', 'cappedCylinder', [1, 4], 5, pos=[0, 0, 14.5], passSet=['f21', 'f22'])
- self.insertBody('finger2_link2', 'cappedCylinder', [1, 2.9], 5, pos=[0, 0, 17.95], passSet=['f22', 'f23'])
- self.insertBody('finger2_link3', 'sphere', [1], 5, pos=[0, 0, 19], passSet=['f23'])
- self.insertBody('finger3_link0', 'cappedCylinder', [1, 7.5], 5, pos=[3.75, 0, 8.75], passSet=['pal', 'f31'])
- self.insertBody('finger3_link1', 'cappedCylinder', [1, 4], 5, pos=[3.75, 0, 14.5], passSet=['f31', 'f32'])
- self.insertBody('finger3_link2', 'cappedCylinder', [1, 2.9], 5, pos=[3.75, 0, 17.95], passSet=['f32', 'f33'])
- self.insertBody('finger3_link3', 'sphere', [1], 5, pos=[3.75, 0, 19], passSet=['f33'])
- self.insertJoint('palm', 'pressure', 'slider', axis={'x':0, 'y':1, 'z':0, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
- self.insertJoint('palm', 'finger0_link0', 'hinge', axis={'x':0, 'y':0, 'z':1, "HiStop":1.5, "LowStop":0.0}, anchor=(-5, 0, -2.5))
- self.insertJoint('finger0_link0', 'finger0_link1', 'hinge', axis={'x':0, 'y':0, 'z':1, "HiStop":1.5, "LowStop":0.0}, anchor=(-12.5, 0, -2.5))
- self.insertJoint('finger0_link1', 'finger0_link2', 'hinge', axis={'x':0, 'y':0, 'z':1, "HiStop":1.5, "LowStop":0.0}, anchor=(-16.5, 0, -2.5))
- self.insertJoint('finger0_link2', 'finger0_link3', 'slider', axis={'x':1, 'y':0, 'z':0, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
- self.insertJoint('palm', 'finger1_link0', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(3.75, 0, 5))
- self.insertJoint('finger1_link0', 'finger1_link1', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(3.75, 0, 12.5))
- self.insertJoint('finger1_link1', 'finger1_link2', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(3.75, 0, 16.5))
- self.insertJoint('finger1_link2', 'finger1_link3', 'slider', axis={'x':0, 'y':0, 'z':1, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
- self.insertJoint('palm', 'finger2_link0', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(0, 0, 5))
- self.insertJoint('finger2_link0', 'finger2_link1', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(0, 0, 12.5))
- self.insertJoint('finger2_link1', 'finger2_link2', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(0, 0, 16.5))
- self.insertJoint('finger2_link2', 'finger2_link3', 'slider', axis={'x':0, 'y':0, 'z':1, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
- self.insertJoint('palm', 'finger3_link0', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-3.75, 0, 5))
- self.insertJoint('finger3_link0', 'finger3_link1', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-3.75, 0, 12.5))
- self.insertJoint('finger3_link1', 'finger3_link2', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-3.75, 0, 16.5))
- self.insertJoint('finger3_link2', 'finger3_link3', 'slider', axis={'x':0, 'y':0, 'z':1, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
- self.centerOn('palm')
- self.insertFloor(y= -1)
- # add one group of haptic sensors
- self._nSensorElements = 0
- self.sensorElements = []
- self.sensorGroupName = None
-
-
-class XODEhandflip(XODEfile):
-
- def __init__(self, name, **kwargs):
- """Creates hand with fingertip and palm sensors -- palm down"""
- XODEfile.__init__(self, name, **kwargs)
- # create the hand and finger
- self.insertBody('palm', 'box', [10, 2, 10], 10, pos=[0, 0, 0], passSet=['pal'])
- self.insertBody('pressure', 'box', [8, 0.5, 8], 10, pos=[0, -1, 0], passSet=['pal'])
- self.insertBody('finger0_link0', 'cappedCylinder', [1, 7.5], 5, pos=[-8.75, 0, -2.5], euler=[0, 90, 0], passSet=['pal', 'f01'])
- self.insertBody('finger0_link1', 'cappedCylinder', [1, 4], 5, pos=[-14.5, 0, -2.5], euler=[0, 90, 0], passSet=['f01', 'f02'])
- self.insertBody('finger0_link2', 'cappedCylinder', [1, 2.9], 5, pos=[-17.95, 0, -2.5], euler=[0, 90, 0], passSet=['f02', 'f03'])
- self.insertBody('finger0_link3', 'sphere', [1], 5, pos=[-19, 0, -2.5], passSet=['f03'])
- self.insertBody('finger1_link0', 'cappedCylinder', [1, 7.5], 5, pos=[-3.75, 0, 8.75], passSet=['pal', 'f11'])
- self.insertBody('finger1_link1', 'cappedCylinder', [1, 4], 5, pos=[-3.75, 0, 14.5], passSet=['f11', 'f12'])
- self.insertBody('finger1_link2', 'cappedCylinder', [1, 2.9], 5, pos=[-3.75, 0, 17.95], passSet=['f12', 'f13'])
- self.insertBody('finger1_link3', 'sphere', [1], 5, pos=[-3.75, 0, 19], passSet=['f13'])
- self.insertBody('finger2_link0', 'cappedCylinder', [1, 7.5], 5, pos=[0, 0, 8.75], passSet=['pal', 'f21'])
- self.insertBody('finger2_link1', 'cappedCylinder', [1, 4], 5, pos=[0, 0, 14.5], passSet=['f21', 'f22'])
- self.insertBody('finger2_link2', 'cappedCylinder', [1, 2.9], 5, pos=[0, 0, 17.95], passSet=['f22', 'f23'])
- self.insertBody('finger2_link3', 'sphere', [1], 5, pos=[0, 0, 19], passSet=['f23'])
- self.insertBody('finger3_link0', 'cappedCylinder', [1, 7.5], 5, pos=[3.75, 0, 8.75], passSet=['pal', 'f31'])
- self.insertBody('finger3_link1', 'cappedCylinder', [1, 4], 5, pos=[3.75, 0, 14.5], passSet=['f31', 'f32'])
- self.insertBody('finger3_link2', 'cappedCylinder', [1, 2.9], 5, pos=[3.75, 0, 17.95], passSet=['f32', 'f33'])
- self.insertBody('finger3_link3', 'sphere', [1], 5, pos=[3.75, 0, 19], passSet=['f33'])
- ## funny finger config with bestNetwork provided (try it ;)
- ##self.insertJoint('palm','pressure','slider', axis={'x':0,'y':-1,'z':0,"HiStop":0,"LowStop":-0.5, "StopERP":0.999,"StopCFM":0.002})
- self.insertJoint('palm', 'pressure', 'slider', axis={'x':0, 'y':1, 'z':0, "HiStop":0, "LowStop":0, "StopERP":0.999, "StopCFM":0.002})
- self.insertJoint('palm', 'finger0_link0', 'hinge', axis={'x':0, 'y':0, 'z':-1, "HiStop":1.5, "LowStop":0.0}, anchor=(-5, 0, -2.5))
- self.insertJoint('finger0_link0', 'finger0_link1', 'hinge', axis={'x':0, 'y':0, 'z':-1, "HiStop":1.5, "LowStop":0.0}, anchor=(-12.5, 0, -2.5))
- self.insertJoint('finger0_link1', 'finger0_link2', 'hinge', axis={'x':0, 'y':0, 'z':-1, "HiStop":1.5, "LowStop":0.0}, anchor=(-16.5, 0, -2.5))
- self.insertJoint('finger0_link2', 'finger0_link3', 'slider', axis={'x':1, 'y':0, 'z':0, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
- self.insertJoint('palm', 'finger1_link0', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(3.75, 0, 5))
- self.insertJoint('finger1_link0', 'finger1_link1', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(3.75, 0, 12.5))
- self.insertJoint('finger1_link1', 'finger1_link2', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(3.75, 0, 16.5))
- self.insertJoint('finger1_link2', 'finger1_link3', 'slider', axis={'x':0, 'y':0, 'z':1, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
- self.insertJoint('palm', 'finger2_link0', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(0, 0, 5))
- self.insertJoint('finger2_link0', 'finger2_link1', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(0, 0, 12.5))
- self.insertJoint('finger2_link1', 'finger2_link2', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(0, 0, 16.5))
- self.insertJoint('finger2_link2', 'finger2_link3', 'slider', axis={'x':0, 'y':0, 'z':1, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
- self.insertJoint('palm', 'finger3_link0', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-3.75, 0, 5))
- self.insertJoint('finger3_link0', 'finger3_link1', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-3.75, 0, 12.5))
- self.insertJoint('finger3_link1', 'finger3_link2', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-3.75, 0, 16.5))
- self.insertJoint('finger3_link2', 'finger3_link3', 'slider', axis={'x':0, 'y':0, 'z':1, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
- self.centerOn('palm')
- self.insertFloor(y= -1.25)
- # add one group of haptic sensors
- self._nSensorElements = 0
- self.sensorElements = []
- self.sensorGroupName = None
-
-
-class HapticTestSetupWithRidges(XODEfinger):
-
- def insertSampleStructure(self, angle=30, std=0.05, dist=0.9, **kwargs):
- """create some ridges on the sample"""
- for i in range(16):
- name = 'ridge' + str(i)
- self.insertBody(name, 'cappedCylinder', [0.2, 10], 5, pos=[0, 0.5, random.gauss(15 - dist * i, std)], euler=[0, angle, 0], passSet=['sam'])
- self.insertJoint('sample', name, 'fixed')
-
-
-class HapticTestSetupWithSpheres(XODEfinger):
-
- def insertSampleStructure(self, xoffs=0.0, std=0.025, dist=0.9, **kwargs):
- """create four rows of spheres on the sample"""
- dx = [dist * k for k in [-1, 0, 1]]
- dz = [dist * k * 0.5 for k in [0, 1, 0]]
- for i in range(16):
- for k in range(3):
- x = random.gauss(dx[k] + xoffs, std)
- z = random.gauss(15 - dist * i + dz[k], std)
- name = 'sphere' + str(i) + str(k)
- self.insertBody(name, 'sphere', [0.2], 5, pos=[x, 0.5, z], passSet=['sam'])
- self.insertJoint('sample', name, 'fixed')
-
-
-class HapticTestSetupWithSpirals(XODEfinger):
-
- def insertSampleStructure(self, std=0.05, xoffs=0.0, dist=1.0, **kwargs):
- """create elongated spiral pattern"""
- rg = 50
- phi = [2.3 + sqrt(f) * pi * 10 / sqrt(rg) for f in range(rg)]
- r = [sqrt(f) * 2.5 / sqrt(rg) for f in range(rg)]
- for k in range(rg):
- x = random.gauss(cos(phi[k]) * r[k], std)
- z = random.gauss(5 + sin(phi[k]) * r[k] * 3, std)
- name = 'sphere' + str(k)
- self.insertBody(name, 'sphere', [0.2], 5, pos=[x, 0.5, z], passSet=['sam'])
- self.insertJoint('sample', name, 'fixed')
-
-
-class HapticTestSetupWithSine(XODEfinger):
-
- def insertSampleStructure(self, angle=0, std=0.05, xoffs=0.0, dist=1.0, **kwargs):
- """create rotated sine pattern"""
- rg = 50
- z = [f * 10.0 / rg for f in range(rg)]
- x = [sin(f * 2) * sin(f / 3) * 3.5 for f in z]
- z = [f - 5 for f in z]
- for i in range(rg):
- r = sqrt(x[i] * x[i] + z[i] * z[i])
- if r > 0:
- phi = asin(x[i] / r)
- if z[i] < 0: phi = pi - phi
- phi += radians(angle)
- x[i] = random.gauss(sin(phi) * r, std)
- z[i] = random.gauss(cos(phi) * r, std)
- name = 'sphere' + str(i)
- self.insertBody(name, 'sphere', [0.2], 5, pos=[x[i], 0.5, z[i]], passSet=['sam'])
- self.insertJoint('sample', name, 'fixed')
-
-class XODEJohnnie(XODEfile):
-
- def __init__(self, name, **kwargs):
- """Creates hand with fingertip and palm sensors -- palm up"""
- XODEfile.__init__(self, name, **kwargs)
- # create the hand and finger
- self.insertBody('palm', 'box', [4.12, 3.0, 2], 30, pos=[0, 0, 0], passSet=['total'], mass=3.356)
- self.insertBody('neck', 'cappedCylinder', [0.25, 5.6], 5, pos=[0, 2.8, 0], euler=[90, 0, 0], passSet=['total'], mass=0.1)
- self.insertBody('head', 'box', [3.0, 1.2, 1.5], 30, pos=[0, 4.0, 0], passSet=['total'], mass=0.1)
- self.insertBody('arm_left', 'cappedCylinder', [0.25, 7.5], 5, pos=[2.06, -2.89, 0], euler=[90, 0, 0], passSet=['total'], mass=2.473)
- self.insertBody('arm_right', 'cappedCylinder', [0.25, 7.5], 5, pos=[-2.06, -2.89, 0], euler=[90, 0, 0], passSet=['total'], mass=2.473)
- self.insertBody('hip', 'cappedCylinder', [0.25, 3.2], 5, pos=[0, -1.6, 0], euler=[90, 0, 0], passSet=['total'], mass=0.192)
- self.insertBody('pelvis', 'cappedCylinder', [0.25, 2.4], 5, pos=[0, -3.2, 0], euler=[0, 90, 0], passSet=['total'], mass=1.0)
- self.insertBody('pelLeft', 'cappedCylinder', [0.25, 0.8], 5, pos=[1.2, -3.6, 0], euler=[90, 0, 0], passSet=['total'], mass=2.567)
- self.insertBody('pelRight', 'cappedCylinder', [0.25, 0.8], 5, pos=[-1.2, -3.6, 0], euler=[90, 0, 0], passSet=['total'], mass=2.567)
- self.insertBody('tibiaLeft', 'cappedCylinder', [0.25, 4.4], 5, pos=[1.2, -6.2, 0], euler=[90, 0, 0], passSet=['total'], mass=5.024)
- self.insertBody('tibiaRight', 'cappedCylinder', [0.25, 4.4], 5, pos=[-1.2, -6.2, 0], euler=[90, 0, 0], passSet=['total'], mass=5.024)
- self.insertBody('sheenLeft', 'cappedCylinder', [0.25, 3.8], 5, pos=[1.2, -10.3, 0], euler=[90, 0, 0], passSet=['total'], mass=3.236)
- self.insertBody('sheenRight', 'cappedCylinder', [0.25, 3.8], 5, pos=[-1.2, -10.3, 0], euler=[90, 0, 0], passSet=['total'], mass=3.236)
- self.insertBody('footLeft', 'box', [2.2, 0.4, 2.6], 3, pos=[1.2, -12.2, 0.75], passSet=['total'], mass=1.801)
- self.insertBody('footRight', 'box', [2.2, 0.4, 2.6], 3, pos=[-1.2, -12.2, 0.75], passSet=['total'], mass=1.801)
- self.insertJoint('palm', 'neck', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(0, 0, 0))
- self.insertJoint('neck', 'head', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(0, 2.8, 0))
- self.insertJoint('palm', 'arm_left', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(2.06, 0.86, 0))
- self.insertJoint('palm', 'arm_right', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(-2.06, 0.86, 0))
- self.insertJoint('palm', 'hip', 'hinge', axis={'x':0, 'y':1, 'z':0, "HiStop":0.5, "LowStop":-0.5}, anchor=(0, -1.6, 0))
- self.insertJoint('hip', 'pelvis', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(0, -3.2, 0))
- self.insertJoint('pelvis', 'pelLeft', 'hinge', axis={'x':0, 'y':0, 'z':-1, "HiStop":0.5, "LowStop":0.0}, anchor=(1.2, -3.2, 0))
- self.insertJoint('pelvis', 'pelRight', 'hinge', axis={'x':0, 'y':0, 'z':1, "HiStop":0.5, "LowStop":0.0}, anchor=(-1.2, -3.2, 0))
- self.insertJoint('pelLeft', 'tibiaLeft', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(1.2, -4.0, 0))
- self.insertJoint('pelRight', 'tibiaRight', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-1.2, -4.0, 0))
- self.insertJoint('tibiaLeft', 'sheenLeft', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(1.2, -8.4, 0))
- self.insertJoint('tibiaRight', 'sheenRight', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-1.2, -8.4, 0))
- self.insertJoint('sheenLeft', 'footLeft', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":0.25, "LowStop":-0.25}, anchor=(1.2, -12.2, 0))
- self.insertJoint('sheenRight', 'footRight', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":0.25, "LowStop":-0.25}, anchor=(-1.2, -12.2, 0))
- self.centerOn('palm')
- self.insertFloor(y= -12.7)
- # add one group of haptic sensors
- self._nSensorElements = 0
- self.sensorElements = []
- self.sensorGroupName = None
-
-class XODESLR(XODEfile):
- def __init__(self, name, **kwargs):
- """Creates hand with fingertip and palm sensors -- palm up"""
- XODEfile.__init__(self, name, **kwargs)
- # create the hand and finger
- self.insertBody('body', 'box', [7.0, 16.0, 10.0], 30, pos=[0, 0, 2.0], passSet=['total'], mass=15.0, color=(0.5, 0.5, 0.4, 1.0))
- #right arm
- self.insertBody('shoulderUpRight', 'cappedCylinder', [0.5, 2.0], 5, pos=[2.5, 7.0, -3.5], euler=[0, 90, 0], passSet=['rightSh', 'total'], mass=0.25)
- self.insertBody('shoulderLRRight', 'cappedCylinder', [0.5, 2.0], 5, pos=[3.5, 6.0, -3.5], euler=[90, 0, 0], passSet=['rightSh'], mass=0.25)
- self.insertBody('shoulderPRRight', 'cappedCylinder', [0.5, 2.0], 5, pos=[3.5, 4.0, -3.5], euler=[90, 0, 0], passSet=['rightSh'], mass=0.25)
- self.insertBody('armUpRight', 'cappedCylinder', [0.5, 2.0], 5, pos=[3.5, 2.0, -3.5], euler=[90, 0, 0], passSet=['rightAr', 'rightSh'], mass=0.25)
- self.insertBody('armPRRight', 'cappedCylinder', [0.5, 2.0], 5, pos=[3.5, 0.0, -3.5], euler=[90, 0, 0], passSet=['rightAr'], mass=0.25)
- self.insertBody('handUpRight', 'cappedCylinder', [0.5, 2.0], 5, pos=[3.5, -2.0, -3.5], euler=[90, 0, 0], passSet=['rightAr', 'rightHa'], mass=0.25)
- #right hand
- self.insertBody('palmRight', 'box', [1.5, 0.25, 0.5], 30, pos=[3.5, -3.0, -3.5], passSet=['rightHa'], mass=0.1, color=(0.6, 0.6, 0.3, 1.0))
- self.insertBody('fingerRight1', 'box', [0.25, 1.0, 0.5], 30, pos=[4.0, -3.5, -3.5], passSet=['rightHa'], mass=0.1, color=(0.6, 0.6, 0.3, 1.0))
- self.insertBody('fingerRight2', 'box', [0.25, 1.0, 0.5], 30, pos=[3.0, -3.5, -3.5], passSet=['rightHa'], mass=0.1, color=(0.6, 0.6, 0.3, 1.0))
-
- #left arm
- self.insertBody('shoulderUpLeft', 'cappedCylinder', [0.5, 2.0], 5, pos=[-2.5, 7.0, -3.5], euler=[0, 90, 0], passSet=['leftSh', 'total'], mass=0.25)
- self.insertBody('shoulderLRLeft', 'cappedCylinder', [0.5, 2.0], 5, pos=[-3.5, 6.0, -3.5], euler=[90, 0, 0], passSet=['leftSh'], mass=0.25)
- self.insertBody('shoulderPRLeft', 'cappedCylinder', [0.5, 2.0], 5, pos=[-3.5, 4.0, -3.5], euler=[90, 0, 0], passSet=['leftSh'], mass=0.25)
- self.insertBody('armUpLeft', 'cappedCylinder', [0.5, 2.0], 5, pos=[-3.5, 2.0, -3.5], euler=[90, 0, 0], passSet=['leftAr', 'leftSh'], mass=0.25)
- self.insertBody('armPRLeft', 'cappedCylinder', [0.5, 2.0], 5, pos=[-3.5, 0.0, -3.5], euler=[90, 0, 0], passSet=['leftAr'], mass=0.25)
- self.insertBody('handUpLeft', 'cappedCylinder', [0.5, 2.0], 5, pos=[-3.5, -2.0, -3.5], euler=[90, 0, 0], passSet=['leftAr', 'leftHa'], mass=0.25)
- #left hand
- self.insertBody('palmLeft', 'box', [1.5, 0.25, 0.5], 30, pos=[-3.5, -3.0, -3.5], passSet=['leftHa'], mass=0.1, color=(0.6, 0.6, 0.3, 1.0))
- self.insertBody('fingerLeft1', 'box', [0.25, 1.0, 0.5], 30, pos=[-4.0, -3.5, -3.5], passSet=['leftHa'], mass=0.1, color=(0.6, 0.6, 0.3, 1.0))
- self.insertBody('fingerLeft2', 'box', [0.25, 1.0, 0.5], 30, pos=[-3.0, -3.5, -3.5], passSet=['leftHa'], mass=0.1, color=(0.6, 0.6, 0.3, 1.0))
-
- #Joints right
- self.insertJoint('body', 'shoulderUpRight', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(2.5, 7.0, -3.5))
- self.insertJoint('shoulderUpRight', 'shoulderLRRight', 'hinge', axis={'x':0, 'y':0, 'z':1}, anchor=(3.5, 7.0, -3.5))
- self.insertJoint('shoulderLRRight', 'shoulderPRRight', 'hinge', axis={'x':0, 'y':1, 'z':0}, anchor=(3.5, 5.0, -3.5))
- self.insertJoint('shoulderPRRight', 'armUpRight', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(3.5, 3.0, -3.5))
- self.insertJoint('armUpRight', 'armPRRight', 'hinge', axis={'x':0, 'y':1, 'z':0}, anchor=(3.5, 1.0, -3.5))
- self.insertJoint('armPRRight', 'handUpRight', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(3.5, -1.0, -3.5))
- self.insertJoint('handUpRight', 'palmRight', 'hinge', axis={'x':0, 'y':1, 'z':0}, anchor=(3.5, -3.0, -3.5))
- self.insertJoint('palmRight', 'fingerRight1', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(4.0, -3.5, -3.5))
- self.insertJoint('palmRight', 'fingerRight2', 'hinge', axis={'x':0, 'y':0, 'z':1}, anchor=(3.0, -3.0, -3.5))
-
- #Joints left
- self.insertJoint('body', 'shoulderUpLeft', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(-2.5, 7.0, -3.5))
- self.insertJoint('shoulderUpLeft', 'shoulderLRLeft', 'hinge', axis={'x':0, 'y':0, 'z':1}, anchor=(-3.5, 7.0, -3.5))
- self.insertJoint('shoulderLRLeft', 'shoulderPRLeft', 'hinge', axis={'x':0, 'y':1, 'z':0}, anchor=(-3.5, 5.0, -3.5))
- self.insertJoint('shoulderPRLeft', 'armUpLeft', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(-3.5, 3.0, -3.5))
- self.insertJoint('armUpLeft', 'armPRLeft', 'hinge', axis={'x':0, 'y':1, 'z':0}, anchor=(-3.5, 1.0, -3.5))
- self.insertJoint('armPRLeft', 'handUpLeft', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(-3.5, -1.0, -3.5))
- self.insertJoint('handUpLeft', 'palmLeft', 'hinge', axis={'x':0, 'y':1, 'z':0}, anchor=(-3.5, -3.0, -3.5))
- self.insertJoint('palmLeft', 'fingerLeft1', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-4.0, -3.5, -3.5))
- self.insertJoint('palmLeft', 'fingerLeft2', 'hinge', axis={'x':0, 'y':0, 'z':1}, anchor=(-3.0, -3.0, -3.5))
-
- self.centerOn('body')
- self.insertFloor(y= -8.0)
- # add one group of haptic sensors
- self._nSensorElements = 0
- self.sensorElements = []
- self.sensorGroupName = None
-
-class XODELSRTable(XODESLR): #XODESLR
- def __init__(self, name, **kwargs):
- XODESLR.__init__(self, name, **kwargs)
- # create table
- self.insertBody('plate', 'box', [15.0, 1.0, 8.0], 30, pos=[-12.5, 0.5, -14.0], passSet=['table'], mass=2.0, color=(0.4, 0.25, 0.0, 1.0))
- self.insertBody('leg1', 'box', [0.5, 8.0, 0.5], 30, pos=[-19.5, -4.0, -17.5], passSet=['table'], mass=0.3, color=(0.6, 0.8, 0.8, 0.8))
- self.insertJoint('plate', 'leg1', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-19.5, 0.0, -17.5))
- self.insertBody('leg2', 'box', [0.5, 8.0, 0.5], 30, pos=[-5.5, -4.0, -17.5], passSet=['table'], mass=0.3, color=(0.6, 0.8, 0.8, 0.8))
- self.insertJoint('plate', 'leg2', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-5.5, 0.0, -17.5))
- self.insertBody('leg3', 'box', [0.5, 8.0, 0.5], 30, pos=[-5.5, -4.0, -10.5], passSet=['table'], mass=0.3, color=(0.6, 0.8, 0.8, 0.8))
- self.insertJoint('plate', 'leg3', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-5.5, 0.0, -10.5))
- self.insertBody('leg4', 'box', [0.5, 8.0, 0.5], 30, pos=[-19.5, -4.0, -10.5], passSet=['table'], mass=0.3, color=(0.6, 0.8, 0.8, 0.8))
- self.insertJoint('plate', 'leg4', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-19.5, 0.0, -10.5))
-
-class XODELSRGlas(XODELSRTable): #XODESLR
- def __init__(self, name, **kwargs):
- XODELSRTable.__init__(self, name, **kwargs)
- # create glass + coaster (necessary because cylinder collision has a bug)
- self.insertBody('objectP00', 'cylinder', [0.2, 1], 30, pos=[-6.5, 1.51 , -11.0], passSet=['object'], mass=0.2, euler=[90, 0, 0], color=(0.6, 0.6, 0.8, 0.5))
- self.insertBody('objectP01', 'box', [0.45, 0.02, 0.45], 30, pos=[-6.5, 1.01, -11.0], passSet=['object'], mass=0.01)
- self.insertBody('objectP02', 'box', [0.45, 0.02, 0.45], 30, pos=[-6.5, 2.01, -11.0], passSet=['object'], mass=0.01)
- self.insertJoint('objectP00', 'objectP01', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-6.5, 1.01, -11.0))
- self.insertJoint('objectP00', 'objectP02', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-6.5, 2.01, -11.0))
-
-class XODELSRPlate(XODELSRTable): #XODESLR
- def __init__(self, name, **kwargs):
- XODELSRTable.__init__(self, name, **kwargs)
- # create plate
- # plate ground
- bX = 1.0 #width of plate floor
- bY = 0.05 #height of plate floor
- bZ = 1.0 #depth of plate floor
- #plate sides
- sX = 0.5 #width of plate side
- sY = bY #height of plate side
- sZ = 1.0 #depth of plate side
- #position of plate
- pX = -6.5
- pY = 1.02
- pZ = -11.0
- #stuff
- m = 0.05 #mass per part
- c = (0.6, 0.6, 0.8, 0.95) #color of object
- dif = sX / (2.0 * sqrt(5)) #
-
- self.insertBody('objectP00', 'box', [bX, bY, bZ], 30, pos=[pX, pY, pZ], passSet=['object'], mass=m, color=c)
- self.insertBody('objectP01', 'box', [sX, sY, sZ], 30, pos=[pX - bX * 0.5 - 2.0 * dif, pY + dif, pZ], passSet=['object'], mass=m, euler=[0, 0, 22.5], color=c)
- self.insertJoint('objectP00', 'objectP01', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(pX - bX * 0.5, pY, pZ))
-
- self.insertBody('objectP02', 'box', [sX, sY, sZ], 30, pos=[pX + bX * 0.5 + 2.0 * dif, pY + dif, pZ], passSet=['object'], mass=m, euler=[0, 0, -22.5], color=c)
- self.insertJoint('objectP00', 'objectP02', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(pX + bX * 0.5, pY, pZ))
-
- self.insertBody('objectP03', 'box', [sX, sY, sZ], 30, pos=[pX, pY + dif, pZ + bZ * 0.5 + 2.0 * dif], passSet=['object'], mass=m, euler=[0, 90, -22.5], color=c)
- self.insertJoint('objectP00', 'objectP03', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(pX, pY, pZ + bZ * 0.5))
-
- self.insertBody('objectP04', 'box', [sX, sY, sZ], 30, pos=[pX, pY + dif, pZ - bZ * 0.5 - 2.0 * dif], passSet=['object'], mass=m, euler=[0, 90, 22.5], color=c)
- self.insertJoint('objectP00', 'objectP04', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(pX, pY, pZ - bZ * 0.5))
-
-if __name__ == '__main__' :
-
- table = XODELSRPlate('../models/ccrlPlate')
-
- #z = XODESLR('../models/slr')
- #z = XODEhand('hand_mal_10')
- #z = XODEhandflip('handflip')
- #z = XODEhandflip('handflip')
- #z.scaleModel(0.5)
-
- table.writeXODE()
-
+from __future__ import print_function
+
+__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
+
+import sys
+from .xmltools import XMLstruct
+from math import asin, cos, sin, pi, degrees, radians, pow
+from scipy import array, matrix, sqrt
+import random
+
+class XODEfile(XMLstruct):
+ """
+ Creates a (virtual) XODE file, into which bodies, joints and custom
+ parameters can be inserted. This file can be merged at a defined level
+ with other instances of itself, and written to disk in the
+ standard format.
+
+ $Id:xodetools.py 150 2007-04-11 13:42:47Z ruecksti $
+ """
+
+ def __init__(self, name, **kwargs):
+ """initialize the XODE structure with a name and the world and
+ space tags"""
+ self._xodename = name
+ self._centerOn = None
+ self._affixToEnvironment = None
+ # sensors is a list of ['type', [args], {kwargs}]
+ self.sensors = []
+ # sensor elements is a list of joints to be used as pressure sensors
+ self.sensorElements = []
+ self._nSensorElements = 0
+ self._pass = {} # dict of sets containing objects allowed to pass
+ self._colors = [] # list of tuples ('name', (r,g,b))
+ XMLstruct.__init__(self, 'world')
+ self.insert('space')
+ # TODO: insert palm, support, etc. (derived class)
+
+ def _mass2dens(self, shape, size, mass):
+ """converts a mass into a density"""
+ if shape == 'box':
+ return mass / float(size[0] * size[1] * size[2])
+ elif shape == 'cylinder' or shape == 'cappedCylinder':
+ if shape == 'cylinder':
+ return mass / (12.56637061 * size[0] * size[0] * size[1])
+ else:
+ return mass / (size[0] * size[0] * (12.56637061 * (size[1] - 2 * size[0]) + 4.18879020 * size[0]))
+ elif shape == 'sphere':
+ return mass / (4.18879020 * pow(size[0], 3))
+ else:
+ print(("Unknown shape: " + shape + " not implemented!"))
+ sys.exit(1)
+
+ def _dens2mass(self, shape, size, dens):
+ """converts a density into a mass"""
+ if shape == 'box':
+ return dens * float(size[0] * size[1] * size[2])
+ elif shape == 'cylinder' or shape == 'cappedCylinder':
+ if shape == 'cylinder':
+ return dens * (12.56637061 * size[0] * size[0] * size[1])
+ else:
+ return dens * (size[0] * size[0] * (12.56637061 * (size[1] - 2 * size[0]) + 4.18879020 * size[0]))
+ elif shape == 'sphere':
+ return dens * (4.18879020 * pow(size[0], 3))
+ else:
+ print(("Unknown shape: " + shape + " not implemented!"))
+ sys.exit(1)
+
+
+ def insertBody(self, bname, shape, size, density, pos=[0, 0, 0], passSet=None, euler=None, mass=None, color=None):
+ """Inserts a body with the given custom name and one of the standard
+ shapes. The size and pos parameters are given as xyz-lists or tuples.
+ euler are three rotation angles (degrees),
+ if mass is given, density is calculated automatically"""
+ self.insert('body', {'name': bname})
+ if color is not None:
+ self._colors.append((bname, color))
+ self.insert('transform')
+ self.insert('position', {'x':pos[0], 'y':pos[1], 'z':pos[2]})
+ if euler is not None:
+ self.up()
+ self.insert('rotation')
+ self.insert('euler', {'x':euler[0], 'y':euler[1], 'z':euler[2], 'aformat':'degrees'})
+ self.up()
+ self.up(2)
+ self.insert('mass')
+ if shape == 'box':
+ dims = {'sizex':size[0], 'sizey':size[1], 'sizez':size[2]}
+ elif shape == 'cylinder' or shape == 'cappedCylinder':
+ dims = {'radius':size[0], 'length':size[1]}
+ elif shape == 'sphere':
+ dims = {'radius':size[0]}
+ else:
+ print(("Unknown shape: " + shape + " not implemented!"))
+ sys.exit(1)
+ if mass is not None:
+ density = self._mass2dens(shape, size, mass)
+
+ self.insert('mass_shape', {'density': density})
+ self.insert(shape, dims)
+ self.up(3)
+ self.insert('geom')
+ self.insert(shape, dims)
+ self.up(3)
+ # add the body to a matching pass set
+ if passSet is not None:
+ for pset in passSet:
+ try:
+ self._pass[pset].add(bname)
+ except KeyError:
+ self._pass[pset] = set([bname])
+
+
+
+ def insertJoint(self, body1, body2, type, axis=None, anchor=(0, 0, 0), rel=False, name=None):
+ """Inserts a joint of given type linking the two bodies. Default name is
+ a "_"-concatenation of the body names. The anchor is a xyz-tuple, rel is
+ a boolean specifying whether the anchor coordinates refer to the body's origin,
+ axis parameters have to be provided as a dictionary."""
+ if name is None: name = body1 + "_" + body2
+ if rel:
+ abs = 'false'
+ else:
+ abs = 'true'
+
+ self.insert('joint', {'name': name })
+ self.insert('link1', {'body': body1})
+ self.up()
+ self.insert('link2', {'body': body2})
+ self.up()
+ self.insert(type)
+ if type == 'fixed':
+ self.insert(None) # empty subtag, seems to be needed by xode parser
+ elif type == 'ball':
+ self.insert('anchor', {'x':anchor[0], 'y':anchor[1], 'z':anchor[2], 'absolute':abs})
+ self.up()
+ elif type == 'slider':
+ self.insert('axis', axis)
+ self.up()
+ elif type == 'hinge':
+ self.insert('axis', axis)
+ self.up()
+ self.insert('anchor', {'x':anchor[0], 'y':anchor[1], 'z':anchor[2], 'absolute':abs})
+ self.up()
+ else:
+ print(("Sorry, joint type " + type + " not yet implemented!"))
+ sys.exit()
+ self.up(2)
+ return name
+
+
+ def insertFloor(self, y= -0.5):
+ """inserts a bodiless floor at given y offset"""
+ self.insert('geom', {'name': 'floor'})
+ self.insert('plane', {'a': 0, 'b': 1, 'c': 0, 'd': y})
+ self.up(2)
+
+ def insertPressureSensorElement(self, parent, name=None, shape='cappedCylinder', size=[0.16, 0.5], pos=[0, 0, 0], euler=[0, 0, 0], dens=1, \
+ mass=None, passSet=[], stiff=10.0):
+ """Insert one single pressure sensor element of the given shape, size, density, etc.
+ The sliding axis is by default oriented along the z-axis, which is also the default for cylinder shapes.
+ You have to rotate the sensor into the correct orientation - the sliding axis will be rotated accordingly.
+ Stiffness of the sensor's spring is set via stiff, whereby damping is calculated automatically to prevent
+ oscillations."""
+
+ if name is None:
+ name = 'psens' + str(self._nSensorElements)
+ if mass is None:
+ mass = self._dens2mass(shape, size, dens)
+ else:
+ dens = self._mass2dens(shape, size, mass)
+
+ self._nSensorElements += 1
+ h = 0.02 # temporal stepwidth
+
+ self.insertBody(name, shape, size, dens, pos=pos, euler=euler, passSet=passSet)
+
+ # In the aperiodic limit case, we have
+ # kp = a kd^2 / 4m i.e. kd = 2 sqrt(m kp/a)
+ # where kp is the spring constant, kd is the dampening constant, and m is the mass of the oscillator.
+ # For practical purposes, it is often better if kp is a few percent stronger such that the sensor
+ # resets itself faster (but still does not oscillate a lot), thus a~=1.02.
+ # ERP = h kp / (h kp + kd)
+ # CFM = 1 / (h kp + kd) = ERP / h kp
+ # For assumed mass of finger of 0.5kg, kp=10, kd=4.5 is approx. the non-oscillatory case.
+ kd = 2.0 * sqrt(mass * stiff / 1.02)
+ ERP = h * stiff / (h * stiff + kd)
+ CFM = ERP / (h * stiff)
+
+ # Furthermore, compute the sliding axis direction from the Euler angles (x-convention, see
+ # http://mathworld.wolfram.com/EulerAngles.html): Without rotation, the axis is along
+ # the z axis, just like a cylinder's axis
+ w = array(euler) * pi / 180
+ #A = matrix([[cos(w[0]), sin(w[0]), 0], [-sin(w[0]),cos(w[0]),0],[0,0,1]])
+ #A = matrix([[1,0,0], [0,cos(w[1]), sin(w[1])], [0, -sin(w[1]),cos(w[1])]]) * A
+ #A = matrix([[cos(w[2]), sin(w[2]), 0], [-sin(w[2]),cos(w[2]),0],[0,0,1]]) * A
+ # hmmm, it seems XODE is rather using the y-convention here:
+ A = matrix([[-sin(w[0]), cos(w[0]), 0], [-cos(w[0]), -sin(w[0]), 0], [0, 0, 1]])
+ A = matrix([[1, 0, 0], [0, cos(w[1]), sin(w[1])], [0, -sin(w[1]), cos(w[1])]]) * A
+ A = matrix([[sin(w[2]), -cos(w[2]), 0], [cos(w[2]), sin(w[2]), 0], [0, 0, 1]]) * A
+ ax = ((A * matrix([0, 0, 1]).getT()).flatten().tolist())[0]
+
+ jname = self.insertJoint(name, parent, 'slider', \
+ axis={'x':ax[0], 'y':ax[1], 'z':ax[2], "HiStop":0.0, "LowStop":0.0, "StopERP":ERP, "StopCFM":CFM })
+ self.sensorElements.append(jname)
+ return name
+
+ def attachSensor(self, type, *args, **kwargs):
+ """adds a sensor with the given type, arguments and keywords, see sensors module for details"""
+ self.sensors.append([type, args, kwargs])
+
+
+ def merge(self, xodefile, joinLevel='space'):
+ """Merge a second XODE file into this one, at the specified
+ level (which must exist in both files). The passpair lists are
+ also joined. Upon return, the current tag for both objects
+ is the one given."""
+ self.top()
+ if not self.downTo(joinLevel):
+ print(("Error: Cannot merge " + self.name + " at level " + joinLevel))
+ xodefile.top()
+ if not xodefile.downTo(joinLevel):
+ print(("Error: Cannot merge " + xodefile.name + " at level " + joinLevel))
+ self.insertMulti(xodefile.getCurrentSubtags())
+ self._pass.update(xodefile.getPassList())
+
+ def centerOn(self, name):
+ self._centerOn = name
+
+
+ def affixToEnvironment(self, name):
+ self._affixToEnvironment = name
+
+ def getPassList(self):
+ return(self._pass)
+
+ def scaleModel(self, sc):
+ """scales all spatial dimensions by the given factor
+ FIXME: quaternions may cause problems, which are currently ignored"""
+ # scale these attributes...
+ scaleset = set(['x', 'y', 'z', 'a', 'b', 'c', 'd', 'sizex', 'sizey', 'sizez', 'length', 'radius'])
+ # ... unless contained in these tags (in which case they specify angles)
+ exclude = set(['euler', 'finiteRotation', 'axisangle'])
+ self.scale(sc, scaleset, exclude)
+
+
+ def writeCustomParameters(self, f):
+ """writes our custom parameters into an XML comment"""
+ f.write('\n')
+
+
+ def writeXODE(self, filename=None):
+ """writes the created structure (plus header and footer) to file with
+ the given basename (.xode is appended)"""
+ if filename is None: filename = self._xodename
+ f = file(filename + '.xode', 'wb') # <-- wb here ensures Linux compatibility
+ f.write('\n')
+ f.write('\n\n')
+ self.write(f)
+ f.write('\n')
+ self.writeCustomParameters(f)
+ f.close()
+ print(("Wrote " + filename + '.xode'))
+
+class XODEfinger(XODEfile):
+
+ def __init__(self, name, **kwargs):
+ """Creates one finger on a fixed palm, and adds some sensors"""
+ XODEfile.__init__(self, name, **kwargs)
+ # create the hand and finger
+ self.insertBody('palm', 'box', [10, 2, 10], 5, pos=[3.75, 4, 0], passSet=['pal'])
+ self.insertBody('sample', 'box', [10, 0.2, 40], 5, pos=[0, 0.4, 0], passSet=['sam'])
+ self.insertJoint('palm', 'sample', 'fixed', name='palm_support')
+ self.insertJoint('palm', 'sample', 'slider', axis={'x':0, 'y':0, 'z':1, 'FMax':11000.0, "HiStop":100.0, "LowStop":-100.0})
+ self.insertBody('finger1_link0', 'cappedCylinder', [1, 7.5], 5, pos=[0, 4, 8.75], passSet=['pal', 'f1'])
+ self.insertBody('finger1_link1', 'cappedCylinder', [1, 4], 5, pos=[0, 4, 14.5], passSet=['f1', 'f2'])
+ self.insertBody('fingertip', 'cappedCylinder', [1, 2.9], 5, pos=[0, 4, 17.95], passSet=['f2', 'haptic'])
+ self.insertJoint('palm', 'finger1_link0', 'hinge', \
+ axis={'x':-1, 'y':0, 'z':0, "HiStop":8, "LowStop":0.0}, anchor=(0, 4, 5))
+ self.insertJoint('finger1_link0', 'finger1_link1', 'hinge', \
+ axis={'x':-1, 'y':0, 'z':0, "HiStop":15, "LowStop":0.0}, anchor=(0, 4, 12.5))
+ self.insertJoint('finger1_link1', 'fingertip', 'hinge', \
+ axis={'x':-1, 'y':0, 'z':0, "HiStop":15, "LowStop":0.0}, anchor=(0, 4, 16.5))
+ self.centerOn('fingertip')
+ self.affixToEnvironment('palm_support')
+ self.insertFloor()
+ # add one group of haptic sensors
+ self._nSensorElements = 0
+ self.sensorElements = []
+ self.sensorGroupName = None
+ self.insertHapticSensors()
+ # give some structure to the sample
+ self.insertSampleStructure(**kwargs)
+
+ def insertHapticSensorsRandom(self):
+ """insert haptic sensors at random locations"""
+ self.sensorGroupName = 'haptic'
+ for _ in range(5):
+ self.insertHapticSensor(dx=random.uniform(-0.65, 0.65), dz=random.uniform(-0.4, 0.2))
+ ##self.insertHapticSensor(dx=-0.055)
+
+
+ def insertHapticSensors(self):
+ """insert haptic sensors at predetermined locations
+ (check using testhapticsensorslocations.py)"""
+ self.sensorGroupName = 'haptic'
+ x = [0.28484253596392306, -0.59653176701550947, -0.36877718203650889, 0.50549219349016294, -0.22467390532644882, 0.051978612692656596, -0.18287341960589126, 0.40477910340060383, 0.56041266484490182, -0.47806390012776134]
+ z = [-0.20354546253333045, -0.23178541627964597, 0.04632154813480549, -0.27525024891443889, -0.20352571063065863, -0.07930554411063101, 0.025260779785407084, 0.091906227805625964, -0.031751424859005839, -0.0034220681106161277]
+ nSens = len(x)
+ for _ in range(nSens):
+ self.insertHapticSensor(dx=x.pop(), dz=z.pop())
+
+
+ def insertSampleStructure(self, angle=None):
+ pass
+
+ def getSensors(self):
+ """return the list of haptic sensors defined"""
+ return(self.sensorElements)
+
+ def insertHapticSensor(self, ctr=(0, 4, 18.5), dx=0.0, dz=0.0):
+ """insert one single haptic sensor"""
+ name = 'haptic' + str(self._nSensorElements)
+ self._nSensorElements += 1
+ # centers of haptic sensors lie on a cylinder of fixed radius (slightly bigger than fingertip)
+ R = 1.2
+ alpha = asin(dx / R)
+ dy = -R * cos(alpha)
+ pos = [dx, dy, dz]
+ rot = [90, degrees(alpha), 0]
+ # ERP = h kp / (h kp + kd)
+ # CFM = 1 / (h kp + kd) = ERP / h kp
+ _h = 0.01 # temporal stepwidth
+ # CHECKME: unused!
+ # for assumed mass of finger of 0.5kg, kp=10, kd=4.5 is approx. the non-oscillatory case
+ self.insertBody(name, 'cappedCylinder', [0.08, 0.5], 7, pos=[ctr[0] + pos[0], ctr[1] + pos[1], ctr[2] + pos[2]], \
+ euler=rot, passSet=['haptic'])
+ jname = 'finger1_' + name
+ self.insertJoint('fingertip', name, 'slider', name=jname, \
+ axis={'x':-dx, 'y':-dy, 'z':0, "HiStop":0.0, "LowStop":0.0, "StopERP":0.022, "StopCFM":0.22 })
+ self.sensorElements.append(jname)
+
+class XODEhand(XODEfile):
+
+ def __init__(self, name, **kwargs):
+ """Creates hand with fingertip and palm sensors -- palm up"""
+ XODEfile.__init__(self, name, **kwargs)
+ # create the hand and finger
+ self.insertBody('palm', 'box', [10, 2, 10], 30, pos=[0, 0, 0], passSet=['pal'])
+ self.insertBody('pressure', 'box', [8, 0.5, 8], 30, pos=[0, 1, 0], passSet=['pal'])
+ self.insertBody('finger0_link0', 'cappedCylinder', [1, 7.5], 5, pos=[-8.75, 0, -2.5], euler=[0, 90, 0], passSet=['pal', 'f01'])
+ self.insertBody('finger0_link1', 'cappedCylinder', [1, 4], 5, pos=[-14.5, 0, -2.5], euler=[0, 90, 0], passSet=['f01', 'f02'])
+ self.insertBody('finger0_link2', 'cappedCylinder', [1, 2.9], 5, pos=[-17.95, 0, -2.5], euler=[0, 90, 0], passSet=['f02', 'f03'])
+ self.insertBody('finger0_link3', 'sphere', [1], 5, pos=[-19, 0, -2.5], passSet=['f03'])
+ self.insertBody('finger1_link0', 'cappedCylinder', [1, 7.5], 5, pos=[-3.75, 0, 8.75], passSet=['pal', 'f11'])
+ self.insertBody('finger1_link1', 'cappedCylinder', [1, 4], 5, pos=[-3.75, 0, 14.5], passSet=['f11', 'f12'])
+ self.insertBody('finger1_link2', 'cappedCylinder', [1, 2.9], 5, pos=[-3.75, 0, 17.95], passSet=['f12', 'f13'])
+ self.insertBody('finger1_link3', 'sphere', [1], 5, pos=[-3.75, 0, 19], passSet=['f13'])
+ self.insertBody('finger2_link0', 'cappedCylinder', [1, 7.5], 5, pos=[0, 0, 8.75], passSet=['pal', 'f21'])
+ self.insertBody('finger2_link1', 'cappedCylinder', [1, 4], 5, pos=[0, 0, 14.5], passSet=['f21', 'f22'])
+ self.insertBody('finger2_link2', 'cappedCylinder', [1, 2.9], 5, pos=[0, 0, 17.95], passSet=['f22', 'f23'])
+ self.insertBody('finger2_link3', 'sphere', [1], 5, pos=[0, 0, 19], passSet=['f23'])
+ self.insertBody('finger3_link0', 'cappedCylinder', [1, 7.5], 5, pos=[3.75, 0, 8.75], passSet=['pal', 'f31'])
+ self.insertBody('finger3_link1', 'cappedCylinder', [1, 4], 5, pos=[3.75, 0, 14.5], passSet=['f31', 'f32'])
+ self.insertBody('finger3_link2', 'cappedCylinder', [1, 2.9], 5, pos=[3.75, 0, 17.95], passSet=['f32', 'f33'])
+ self.insertBody('finger3_link3', 'sphere', [1], 5, pos=[3.75, 0, 19], passSet=['f33'])
+ self.insertJoint('palm', 'pressure', 'slider', axis={'x':0, 'y':1, 'z':0, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
+ self.insertJoint('palm', 'finger0_link0', 'hinge', axis={'x':0, 'y':0, 'z':1, "HiStop":1.5, "LowStop":0.0}, anchor=(-5, 0, -2.5))
+ self.insertJoint('finger0_link0', 'finger0_link1', 'hinge', axis={'x':0, 'y':0, 'z':1, "HiStop":1.5, "LowStop":0.0}, anchor=(-12.5, 0, -2.5))
+ self.insertJoint('finger0_link1', 'finger0_link2', 'hinge', axis={'x':0, 'y':0, 'z':1, "HiStop":1.5, "LowStop":0.0}, anchor=(-16.5, 0, -2.5))
+ self.insertJoint('finger0_link2', 'finger0_link3', 'slider', axis={'x':1, 'y':0, 'z':0, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
+ self.insertJoint('palm', 'finger1_link0', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(3.75, 0, 5))
+ self.insertJoint('finger1_link0', 'finger1_link1', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(3.75, 0, 12.5))
+ self.insertJoint('finger1_link1', 'finger1_link2', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(3.75, 0, 16.5))
+ self.insertJoint('finger1_link2', 'finger1_link3', 'slider', axis={'x':0, 'y':0, 'z':1, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
+ self.insertJoint('palm', 'finger2_link0', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(0, 0, 5))
+ self.insertJoint('finger2_link0', 'finger2_link1', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(0, 0, 12.5))
+ self.insertJoint('finger2_link1', 'finger2_link2', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(0, 0, 16.5))
+ self.insertJoint('finger2_link2', 'finger2_link3', 'slider', axis={'x':0, 'y':0, 'z':1, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
+ self.insertJoint('palm', 'finger3_link0', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-3.75, 0, 5))
+ self.insertJoint('finger3_link0', 'finger3_link1', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-3.75, 0, 12.5))
+ self.insertJoint('finger3_link1', 'finger3_link2', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-3.75, 0, 16.5))
+ self.insertJoint('finger3_link2', 'finger3_link3', 'slider', axis={'x':0, 'y':0, 'z':1, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
+ self.centerOn('palm')
+ self.insertFloor(y= -1)
+ # add one group of haptic sensors
+ self._nSensorElements = 0
+ self.sensorElements = []
+ self.sensorGroupName = None
+
+
+class XODEhandflip(XODEfile):
+
+ def __init__(self, name, **kwargs):
+ """Creates hand with fingertip and palm sensors -- palm down"""
+ XODEfile.__init__(self, name, **kwargs)
+ # create the hand and finger
+ self.insertBody('palm', 'box', [10, 2, 10], 10, pos=[0, 0, 0], passSet=['pal'])
+ self.insertBody('pressure', 'box', [8, 0.5, 8], 10, pos=[0, -1, 0], passSet=['pal'])
+ self.insertBody('finger0_link0', 'cappedCylinder', [1, 7.5], 5, pos=[-8.75, 0, -2.5], euler=[0, 90, 0], passSet=['pal', 'f01'])
+ self.insertBody('finger0_link1', 'cappedCylinder', [1, 4], 5, pos=[-14.5, 0, -2.5], euler=[0, 90, 0], passSet=['f01', 'f02'])
+ self.insertBody('finger0_link2', 'cappedCylinder', [1, 2.9], 5, pos=[-17.95, 0, -2.5], euler=[0, 90, 0], passSet=['f02', 'f03'])
+ self.insertBody('finger0_link3', 'sphere', [1], 5, pos=[-19, 0, -2.5], passSet=['f03'])
+ self.insertBody('finger1_link0', 'cappedCylinder', [1, 7.5], 5, pos=[-3.75, 0, 8.75], passSet=['pal', 'f11'])
+ self.insertBody('finger1_link1', 'cappedCylinder', [1, 4], 5, pos=[-3.75, 0, 14.5], passSet=['f11', 'f12'])
+ self.insertBody('finger1_link2', 'cappedCylinder', [1, 2.9], 5, pos=[-3.75, 0, 17.95], passSet=['f12', 'f13'])
+ self.insertBody('finger1_link3', 'sphere', [1], 5, pos=[-3.75, 0, 19], passSet=['f13'])
+ self.insertBody('finger2_link0', 'cappedCylinder', [1, 7.5], 5, pos=[0, 0, 8.75], passSet=['pal', 'f21'])
+ self.insertBody('finger2_link1', 'cappedCylinder', [1, 4], 5, pos=[0, 0, 14.5], passSet=['f21', 'f22'])
+ self.insertBody('finger2_link2', 'cappedCylinder', [1, 2.9], 5, pos=[0, 0, 17.95], passSet=['f22', 'f23'])
+ self.insertBody('finger2_link3', 'sphere', [1], 5, pos=[0, 0, 19], passSet=['f23'])
+ self.insertBody('finger3_link0', 'cappedCylinder', [1, 7.5], 5, pos=[3.75, 0, 8.75], passSet=['pal', 'f31'])
+ self.insertBody('finger3_link1', 'cappedCylinder', [1, 4], 5, pos=[3.75, 0, 14.5], passSet=['f31', 'f32'])
+ self.insertBody('finger3_link2', 'cappedCylinder', [1, 2.9], 5, pos=[3.75, 0, 17.95], passSet=['f32', 'f33'])
+ self.insertBody('finger3_link3', 'sphere', [1], 5, pos=[3.75, 0, 19], passSet=['f33'])
+ ## funny finger config with bestNetwork provided (try it ;)
+ ##self.insertJoint('palm','pressure','slider', axis={'x':0,'y':-1,'z':0,"HiStop":0,"LowStop":-0.5, "StopERP":0.999,"StopCFM":0.002})
+ self.insertJoint('palm', 'pressure', 'slider', axis={'x':0, 'y':1, 'z':0, "HiStop":0, "LowStop":0, "StopERP":0.999, "StopCFM":0.002})
+ self.insertJoint('palm', 'finger0_link0', 'hinge', axis={'x':0, 'y':0, 'z':-1, "HiStop":1.5, "LowStop":0.0}, anchor=(-5, 0, -2.5))
+ self.insertJoint('finger0_link0', 'finger0_link1', 'hinge', axis={'x':0, 'y':0, 'z':-1, "HiStop":1.5, "LowStop":0.0}, anchor=(-12.5, 0, -2.5))
+ self.insertJoint('finger0_link1', 'finger0_link2', 'hinge', axis={'x':0, 'y':0, 'z':-1, "HiStop":1.5, "LowStop":0.0}, anchor=(-16.5, 0, -2.5))
+ self.insertJoint('finger0_link2', 'finger0_link3', 'slider', axis={'x':1, 'y':0, 'z':0, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
+ self.insertJoint('palm', 'finger1_link0', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(3.75, 0, 5))
+ self.insertJoint('finger1_link0', 'finger1_link1', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(3.75, 0, 12.5))
+ self.insertJoint('finger1_link1', 'finger1_link2', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(3.75, 0, 16.5))
+ self.insertJoint('finger1_link2', 'finger1_link3', 'slider', axis={'x':0, 'y':0, 'z':1, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
+ self.insertJoint('palm', 'finger2_link0', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(0, 0, 5))
+ self.insertJoint('finger2_link0', 'finger2_link1', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(0, 0, 12.5))
+ self.insertJoint('finger2_link1', 'finger2_link2', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(0, 0, 16.5))
+ self.insertJoint('finger2_link2', 'finger2_link3', 'slider', axis={'x':0, 'y':0, 'z':1, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
+ self.insertJoint('palm', 'finger3_link0', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-3.75, 0, 5))
+ self.insertJoint('finger3_link0', 'finger3_link1', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-3.75, 0, 12.5))
+ self.insertJoint('finger3_link1', 'finger3_link2', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-3.75, 0, 16.5))
+ self.insertJoint('finger3_link2', 'finger3_link3', 'slider', axis={'x':0, 'y':0, 'z':1, "HiStop":0, "LowStop":-0.5, "StopERP":0.999, "StopCFM":0.002})
+ self.centerOn('palm')
+ self.insertFloor(y= -1.25)
+ # add one group of haptic sensors
+ self._nSensorElements = 0
+ self.sensorElements = []
+ self.sensorGroupName = None
+
+
+class HapticTestSetupWithRidges(XODEfinger):
+
+ def insertSampleStructure(self, angle=30, std=0.05, dist=0.9, **kwargs):
+ """create some ridges on the sample"""
+ for i in range(16):
+ name = 'ridge' + str(i)
+ self.insertBody(name, 'cappedCylinder', [0.2, 10], 5, pos=[0, 0.5, random.gauss(15 - dist * i, std)], euler=[0, angle, 0], passSet=['sam'])
+ self.insertJoint('sample', name, 'fixed')
+
+
+class HapticTestSetupWithSpheres(XODEfinger):
+
+ def insertSampleStructure(self, xoffs=0.0, std=0.025, dist=0.9, **kwargs):
+ """create four rows of spheres on the sample"""
+ dx = [dist * k for k in [-1, 0, 1]]
+ dz = [dist * k * 0.5 for k in [0, 1, 0]]
+ for i in range(16):
+ for k in range(3):
+ x = random.gauss(dx[k] + xoffs, std)
+ z = random.gauss(15 - dist * i + dz[k], std)
+ name = 'sphere' + str(i) + str(k)
+ self.insertBody(name, 'sphere', [0.2], 5, pos=[x, 0.5, z], passSet=['sam'])
+ self.insertJoint('sample', name, 'fixed')
+
+
+class HapticTestSetupWithSpirals(XODEfinger):
+
+ def insertSampleStructure(self, std=0.05, xoffs=0.0, dist=1.0, **kwargs):
+ """create elongated spiral pattern"""
+ rg = 50
+ phi = [2.3 + sqrt(f) * pi * 10 / sqrt(rg) for f in range(rg)]
+ r = [sqrt(f) * 2.5 / sqrt(rg) for f in range(rg)]
+ for k in range(rg):
+ x = random.gauss(cos(phi[k]) * r[k], std)
+ z = random.gauss(5 + sin(phi[k]) * r[k] * 3, std)
+ name = 'sphere' + str(k)
+ self.insertBody(name, 'sphere', [0.2], 5, pos=[x, 0.5, z], passSet=['sam'])
+ self.insertJoint('sample', name, 'fixed')
+
+
+class HapticTestSetupWithSine(XODEfinger):
+
+ def insertSampleStructure(self, angle=0, std=0.05, xoffs=0.0, dist=1.0, **kwargs):
+ """create rotated sine pattern"""
+ rg = 50
+ z = [f * 10.0 / rg for f in range(rg)]
+ x = [sin(f * 2) * sin(f / 3) * 3.5 for f in z]
+ z = [f - 5 for f in z]
+ for i in range(rg):
+ r = sqrt(x[i] * x[i] + z[i] * z[i])
+ if r > 0:
+ phi = asin(x[i] / r)
+ if z[i] < 0: phi = pi - phi
+ phi += radians(angle)
+ x[i] = random.gauss(sin(phi) * r, std)
+ z[i] = random.gauss(cos(phi) * r, std)
+ name = 'sphere' + str(i)
+ self.insertBody(name, 'sphere', [0.2], 5, pos=[x[i], 0.5, z[i]], passSet=['sam'])
+ self.insertJoint('sample', name, 'fixed')
+
+class XODEJohnnie(XODEfile):
+
+ def __init__(self, name, **kwargs):
+ """Creates hand with fingertip and palm sensors -- palm up"""
+ XODEfile.__init__(self, name, **kwargs)
+ # create the hand and finger
+ self.insertBody('palm', 'box', [4.12, 3.0, 2], 30, pos=[0, 0, 0], passSet=['total'], mass=3.356)
+ self.insertBody('neck', 'cappedCylinder', [0.25, 5.6], 5, pos=[0, 2.8, 0], euler=[90, 0, 0], passSet=['total'], mass=0.1)
+ self.insertBody('head', 'box', [3.0, 1.2, 1.5], 30, pos=[0, 4.0, 0], passSet=['total'], mass=0.1)
+ self.insertBody('arm_left', 'cappedCylinder', [0.25, 7.5], 5, pos=[2.06, -2.89, 0], euler=[90, 0, 0], passSet=['total'], mass=2.473)
+ self.insertBody('arm_right', 'cappedCylinder', [0.25, 7.5], 5, pos=[-2.06, -2.89, 0], euler=[90, 0, 0], passSet=['total'], mass=2.473)
+ self.insertBody('hip', 'cappedCylinder', [0.25, 3.2], 5, pos=[0, -1.6, 0], euler=[90, 0, 0], passSet=['total'], mass=0.192)
+ self.insertBody('pelvis', 'cappedCylinder', [0.25, 2.4], 5, pos=[0, -3.2, 0], euler=[0, 90, 0], passSet=['total'], mass=1.0)
+ self.insertBody('pelLeft', 'cappedCylinder', [0.25, 0.8], 5, pos=[1.2, -3.6, 0], euler=[90, 0, 0], passSet=['total'], mass=2.567)
+ self.insertBody('pelRight', 'cappedCylinder', [0.25, 0.8], 5, pos=[-1.2, -3.6, 0], euler=[90, 0, 0], passSet=['total'], mass=2.567)
+ self.insertBody('tibiaLeft', 'cappedCylinder', [0.25, 4.4], 5, pos=[1.2, -6.2, 0], euler=[90, 0, 0], passSet=['total'], mass=5.024)
+ self.insertBody('tibiaRight', 'cappedCylinder', [0.25, 4.4], 5, pos=[-1.2, -6.2, 0], euler=[90, 0, 0], passSet=['total'], mass=5.024)
+ self.insertBody('sheenLeft', 'cappedCylinder', [0.25, 3.8], 5, pos=[1.2, -10.3, 0], euler=[90, 0, 0], passSet=['total'], mass=3.236)
+ self.insertBody('sheenRight', 'cappedCylinder', [0.25, 3.8], 5, pos=[-1.2, -10.3, 0], euler=[90, 0, 0], passSet=['total'], mass=3.236)
+ self.insertBody('footLeft', 'box', [2.2, 0.4, 2.6], 3, pos=[1.2, -12.2, 0.75], passSet=['total'], mass=1.801)
+ self.insertBody('footRight', 'box', [2.2, 0.4, 2.6], 3, pos=[-1.2, -12.2, 0.75], passSet=['total'], mass=1.801)
+ self.insertJoint('palm', 'neck', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(0, 0, 0))
+ self.insertJoint('neck', 'head', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(0, 2.8, 0))
+ self.insertJoint('palm', 'arm_left', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(2.06, 0.86, 0))
+ self.insertJoint('palm', 'arm_right', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(-2.06, 0.86, 0))
+ self.insertJoint('palm', 'hip', 'hinge', axis={'x':0, 'y':1, 'z':0, "HiStop":0.5, "LowStop":-0.5}, anchor=(0, -1.6, 0))
+ self.insertJoint('hip', 'pelvis', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(0, -3.2, 0))
+ self.insertJoint('pelvis', 'pelLeft', 'hinge', axis={'x':0, 'y':0, 'z':-1, "HiStop":0.5, "LowStop":0.0}, anchor=(1.2, -3.2, 0))
+ self.insertJoint('pelvis', 'pelRight', 'hinge', axis={'x':0, 'y':0, 'z':1, "HiStop":0.5, "LowStop":0.0}, anchor=(-1.2, -3.2, 0))
+ self.insertJoint('pelLeft', 'tibiaLeft', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(1.2, -4.0, 0))
+ self.insertJoint('pelRight', 'tibiaRight', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-1.2, -4.0, 0))
+ self.insertJoint('tibiaLeft', 'sheenLeft', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(1.2, -8.4, 0))
+ self.insertJoint('tibiaRight', 'sheenRight', 'hinge', axis={'x':-1, 'y':0, 'z':0, "HiStop":1.5, "LowStop":0.0}, anchor=(-1.2, -8.4, 0))
+ self.insertJoint('sheenLeft', 'footLeft', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":0.25, "LowStop":-0.25}, anchor=(1.2, -12.2, 0))
+ self.insertJoint('sheenRight', 'footRight', 'hinge', axis={'x':1, 'y':0, 'z':0, "HiStop":0.25, "LowStop":-0.25}, anchor=(-1.2, -12.2, 0))
+ self.centerOn('palm')
+ self.insertFloor(y= -12.7)
+ # add one group of haptic sensors
+ self._nSensorElements = 0
+ self.sensorElements = []
+ self.sensorGroupName = None
+
+class XODESLR(XODEfile):
+ def __init__(self, name, **kwargs):
+ """Creates hand with fingertip and palm sensors -- palm up"""
+ XODEfile.__init__(self, name, **kwargs)
+ # create the hand and finger
+ self.insertBody('body', 'box', [7.0, 16.0, 10.0], 30, pos=[0, 0, 2.0], passSet=['total'], mass=15.0, color=(0.5, 0.5, 0.4, 1.0))
+ #right arm
+ self.insertBody('shoulderUpRight', 'cappedCylinder', [0.5, 2.0], 5, pos=[2.5, 7.0, -3.5], euler=[0, 90, 0], passSet=['rightSh', 'total'], mass=0.25)
+ self.insertBody('shoulderLRRight', 'cappedCylinder', [0.5, 2.0], 5, pos=[3.5, 6.0, -3.5], euler=[90, 0, 0], passSet=['rightSh'], mass=0.25)
+ self.insertBody('shoulderPRRight', 'cappedCylinder', [0.5, 2.0], 5, pos=[3.5, 4.0, -3.5], euler=[90, 0, 0], passSet=['rightSh'], mass=0.25)
+ self.insertBody('armUpRight', 'cappedCylinder', [0.5, 2.0], 5, pos=[3.5, 2.0, -3.5], euler=[90, 0, 0], passSet=['rightAr', 'rightSh'], mass=0.25)
+ self.insertBody('armPRRight', 'cappedCylinder', [0.5, 2.0], 5, pos=[3.5, 0.0, -3.5], euler=[90, 0, 0], passSet=['rightAr'], mass=0.25)
+ self.insertBody('handUpRight', 'cappedCylinder', [0.5, 2.0], 5, pos=[3.5, -2.0, -3.5], euler=[90, 0, 0], passSet=['rightAr', 'rightHa'], mass=0.25)
+ #right hand
+ self.insertBody('palmRight', 'box', [1.5, 0.25, 0.5], 30, pos=[3.5, -3.0, -3.5], passSet=['rightHa'], mass=0.1, color=(0.6, 0.6, 0.3, 1.0))
+ self.insertBody('fingerRight1', 'box', [0.25, 1.0, 0.5], 30, pos=[4.0, -3.5, -3.5], passSet=['rightHa'], mass=0.1, color=(0.6, 0.6, 0.3, 1.0))
+ self.insertBody('fingerRight2', 'box', [0.25, 1.0, 0.5], 30, pos=[3.0, -3.5, -3.5], passSet=['rightHa'], mass=0.1, color=(0.6, 0.6, 0.3, 1.0))
+
+ #left arm
+ self.insertBody('shoulderUpLeft', 'cappedCylinder', [0.5, 2.0], 5, pos=[-2.5, 7.0, -3.5], euler=[0, 90, 0], passSet=['leftSh', 'total'], mass=0.25)
+ self.insertBody('shoulderLRLeft', 'cappedCylinder', [0.5, 2.0], 5, pos=[-3.5, 6.0, -3.5], euler=[90, 0, 0], passSet=['leftSh'], mass=0.25)
+ self.insertBody('shoulderPRLeft', 'cappedCylinder', [0.5, 2.0], 5, pos=[-3.5, 4.0, -3.5], euler=[90, 0, 0], passSet=['leftSh'], mass=0.25)
+ self.insertBody('armUpLeft', 'cappedCylinder', [0.5, 2.0], 5, pos=[-3.5, 2.0, -3.5], euler=[90, 0, 0], passSet=['leftAr', 'leftSh'], mass=0.25)
+ self.insertBody('armPRLeft', 'cappedCylinder', [0.5, 2.0], 5, pos=[-3.5, 0.0, -3.5], euler=[90, 0, 0], passSet=['leftAr'], mass=0.25)
+ self.insertBody('handUpLeft', 'cappedCylinder', [0.5, 2.0], 5, pos=[-3.5, -2.0, -3.5], euler=[90, 0, 0], passSet=['leftAr', 'leftHa'], mass=0.25)
+ #left hand
+ self.insertBody('palmLeft', 'box', [1.5, 0.25, 0.5], 30, pos=[-3.5, -3.0, -3.5], passSet=['leftHa'], mass=0.1, color=(0.6, 0.6, 0.3, 1.0))
+ self.insertBody('fingerLeft1', 'box', [0.25, 1.0, 0.5], 30, pos=[-4.0, -3.5, -3.5], passSet=['leftHa'], mass=0.1, color=(0.6, 0.6, 0.3, 1.0))
+ self.insertBody('fingerLeft2', 'box', [0.25, 1.0, 0.5], 30, pos=[-3.0, -3.5, -3.5], passSet=['leftHa'], mass=0.1, color=(0.6, 0.6, 0.3, 1.0))
+
+ #Joints right
+ self.insertJoint('body', 'shoulderUpRight', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(2.5, 7.0, -3.5))
+ self.insertJoint('shoulderUpRight', 'shoulderLRRight', 'hinge', axis={'x':0, 'y':0, 'z':1}, anchor=(3.5, 7.0, -3.5))
+ self.insertJoint('shoulderLRRight', 'shoulderPRRight', 'hinge', axis={'x':0, 'y':1, 'z':0}, anchor=(3.5, 5.0, -3.5))
+ self.insertJoint('shoulderPRRight', 'armUpRight', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(3.5, 3.0, -3.5))
+ self.insertJoint('armUpRight', 'armPRRight', 'hinge', axis={'x':0, 'y':1, 'z':0}, anchor=(3.5, 1.0, -3.5))
+ self.insertJoint('armPRRight', 'handUpRight', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(3.5, -1.0, -3.5))
+ self.insertJoint('handUpRight', 'palmRight', 'hinge', axis={'x':0, 'y':1, 'z':0}, anchor=(3.5, -3.0, -3.5))
+ self.insertJoint('palmRight', 'fingerRight1', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(4.0, -3.5, -3.5))
+ self.insertJoint('palmRight', 'fingerRight2', 'hinge', axis={'x':0, 'y':0, 'z':1}, anchor=(3.0, -3.0, -3.5))
+
+ #Joints left
+ self.insertJoint('body', 'shoulderUpLeft', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(-2.5, 7.0, -3.5))
+ self.insertJoint('shoulderUpLeft', 'shoulderLRLeft', 'hinge', axis={'x':0, 'y':0, 'z':1}, anchor=(-3.5, 7.0, -3.5))
+ self.insertJoint('shoulderLRLeft', 'shoulderPRLeft', 'hinge', axis={'x':0, 'y':1, 'z':0}, anchor=(-3.5, 5.0, -3.5))
+ self.insertJoint('shoulderPRLeft', 'armUpLeft', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(-3.5, 3.0, -3.5))
+ self.insertJoint('armUpLeft', 'armPRLeft', 'hinge', axis={'x':0, 'y':1, 'z':0}, anchor=(-3.5, 1.0, -3.5))
+ self.insertJoint('armPRLeft', 'handUpLeft', 'hinge', axis={'x':1, 'y':0, 'z':0}, anchor=(-3.5, -1.0, -3.5))
+ self.insertJoint('handUpLeft', 'palmLeft', 'hinge', axis={'x':0, 'y':1, 'z':0}, anchor=(-3.5, -3.0, -3.5))
+ self.insertJoint('palmLeft', 'fingerLeft1', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-4.0, -3.5, -3.5))
+ self.insertJoint('palmLeft', 'fingerLeft2', 'hinge', axis={'x':0, 'y':0, 'z':1}, anchor=(-3.0, -3.0, -3.5))
+
+ self.centerOn('body')
+ self.insertFloor(y= -8.0)
+ # add one group of haptic sensors
+ self._nSensorElements = 0
+ self.sensorElements = []
+ self.sensorGroupName = None
+
+class XODELSRTable(XODESLR): #XODESLR
+ def __init__(self, name, **kwargs):
+ XODESLR.__init__(self, name, **kwargs)
+ # create table
+ self.insertBody('plate', 'box', [15.0, 1.0, 8.0], 30, pos=[-12.5, 0.5, -14.0], passSet=['table'], mass=2.0, color=(0.4, 0.25, 0.0, 1.0))
+ self.insertBody('leg1', 'box', [0.5, 8.0, 0.5], 30, pos=[-19.5, -4.0, -17.5], passSet=['table'], mass=0.3, color=(0.6, 0.8, 0.8, 0.8))
+ self.insertJoint('plate', 'leg1', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-19.5, 0.0, -17.5))
+ self.insertBody('leg2', 'box', [0.5, 8.0, 0.5], 30, pos=[-5.5, -4.0, -17.5], passSet=['table'], mass=0.3, color=(0.6, 0.8, 0.8, 0.8))
+ self.insertJoint('plate', 'leg2', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-5.5, 0.0, -17.5))
+ self.insertBody('leg3', 'box', [0.5, 8.0, 0.5], 30, pos=[-5.5, -4.0, -10.5], passSet=['table'], mass=0.3, color=(0.6, 0.8, 0.8, 0.8))
+ self.insertJoint('plate', 'leg3', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-5.5, 0.0, -10.5))
+ self.insertBody('leg4', 'box', [0.5, 8.0, 0.5], 30, pos=[-19.5, -4.0, -10.5], passSet=['table'], mass=0.3, color=(0.6, 0.8, 0.8, 0.8))
+ self.insertJoint('plate', 'leg4', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-19.5, 0.0, -10.5))
+
+class XODELSRGlas(XODELSRTable): #XODESLR
+ def __init__(self, name, **kwargs):
+ XODELSRTable.__init__(self, name, **kwargs)
+ # create glass + coaster (necessary because cylinder collision has a bug)
+ self.insertBody('objectP00', 'cylinder', [0.2, 1], 30, pos=[-6.5, 1.51 , -11.0], passSet=['object'], mass=0.2, euler=[90, 0, 0], color=(0.6, 0.6, 0.8, 0.5))
+ self.insertBody('objectP01', 'box', [0.45, 0.02, 0.45], 30, pos=[-6.5, 1.01, -11.0], passSet=['object'], mass=0.01)
+ self.insertBody('objectP02', 'box', [0.45, 0.02, 0.45], 30, pos=[-6.5, 2.01, -11.0], passSet=['object'], mass=0.01)
+ self.insertJoint('objectP00', 'objectP01', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-6.5, 1.01, -11.0))
+ self.insertJoint('objectP00', 'objectP02', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(-6.5, 2.01, -11.0))
+
+class XODELSRPlate(XODELSRTable): #XODESLR
+ def __init__(self, name, **kwargs):
+ XODELSRTable.__init__(self, name, **kwargs)
+ # create plate
+ # plate ground
+ bX = 1.0 #width of plate floor
+ bY = 0.05 #height of plate floor
+ bZ = 1.0 #depth of plate floor
+ #plate sides
+ sX = 0.5 #width of plate side
+ sY = bY #height of plate side
+ sZ = 1.0 #depth of plate side
+ #position of plate
+ pX = -6.5
+ pY = 1.02
+ pZ = -11.0
+ #stuff
+ m = 0.05 #mass per part
+ c = (0.6, 0.6, 0.8, 0.95) #color of object
+ dif = sX / (2.0 * sqrt(5)) #
+
+ self.insertBody('objectP00', 'box', [bX, bY, bZ], 30, pos=[pX, pY, pZ], passSet=['object'], mass=m, color=c)
+ self.insertBody('objectP01', 'box', [sX, sY, sZ], 30, pos=[pX - bX * 0.5 - 2.0 * dif, pY + dif, pZ], passSet=['object'], mass=m, euler=[0, 0, 22.5], color=c)
+ self.insertJoint('objectP00', 'objectP01', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(pX - bX * 0.5, pY, pZ))
+
+ self.insertBody('objectP02', 'box', [sX, sY, sZ], 30, pos=[pX + bX * 0.5 + 2.0 * dif, pY + dif, pZ], passSet=['object'], mass=m, euler=[0, 0, -22.5], color=c)
+ self.insertJoint('objectP00', 'objectP02', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(pX + bX * 0.5, pY, pZ))
+
+ self.insertBody('objectP03', 'box', [sX, sY, sZ], 30, pos=[pX, pY + dif, pZ + bZ * 0.5 + 2.0 * dif], passSet=['object'], mass=m, euler=[0, 90, -22.5], color=c)
+ self.insertJoint('objectP00', 'objectP03', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(pX, pY, pZ + bZ * 0.5))
+
+ self.insertBody('objectP04', 'box', [sX, sY, sZ], 30, pos=[pX, pY + dif, pZ - bZ * 0.5 - 2.0 * dif], passSet=['object'], mass=m, euler=[0, 90, 22.5], color=c)
+ self.insertJoint('objectP00', 'objectP04', 'fixed', axis={'x':0, 'y':0, 'z':0}, anchor=(pX, pY, pZ - bZ * 0.5))
+
+if __name__ == '__main__' :
+
+ table = XODELSRPlate('../models/ccrlPlate')
+
+ #z = XODESLR('../models/slr')
+ #z = XODEhand('hand_mal_10')
+ #z = XODEhandflip('handflip')
+ #z = XODEhandflip('handflip')
+ #z.scaleModel(0.5)
+
+ table.writeXODE()
+
diff --git a/pybrain/rl/environments/ode/viewer.py b/pybrain/rl/environments/ode/viewer.py
index 4a958f782..dc46b63ff 100644
--- a/pybrain/rl/environments/ode/viewer.py
+++ b/pybrain/rl/environments/ode/viewer.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Martin Felder, felder@in.tum.de'
from OpenGL.GL import * #@UnusedWildImport
@@ -5,7 +7,7 @@
from OpenGL.GLUT import * #@UnusedWildImport
from math import acos, pi, sqrt
-from tools.mathhelpers import crossproduct, norm, dotproduct
+from .tools.mathhelpers import crossproduct, norm, dotproduct
import time
import Image #@UnresolvedImport
@@ -162,7 +164,7 @@ def draw_item(self, item):
if item['type'] in ['GeomBox', 'GeomSphere', 'GeomCylinder', 'GeomCCylinder']:
# set color of object (currently dark gray)
- if item.has_key('color'):
+ if 'color' in item:
glEnable (GL_BLEND)
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glColor4f(*(item['color']))
@@ -302,7 +304,7 @@ def _keyfunc (self, c, x, y):
""" keyboard call-back function. """
if c == 's':
self.setCaptureScreen(not self.getCaptureScreen())
- print("Screen Capture: " + (self.getCaptureScreen() and "on" or "off"))
+ print(("Screen Capture: " + (self.getCaptureScreen() and "on" or "off")))
if c in ['x', 'q']:
sys.exit()
if c == 'v':
@@ -360,7 +362,7 @@ def _screenshot(self, path_prefix='.', format='PNG'):
image = Image.fromstring("RGB", (self.width, self.height), data)
image = image.transpose(Image.FLIP_TOP_BOTTOM)
image.save(path, format)
- print('Image saved to %s' % (os.path.basename(path)))
+ print(('Image saved to %s' % (os.path.basename(path))))
else:
self.counter += 1
diff --git a/pybrain/rl/environments/serverinterface.py b/pybrain/rl/environments/serverinterface.py
index f861597f5..60b0eedb1 100644
--- a/pybrain/rl/environments/serverinterface.py
+++ b/pybrain/rl/environments/serverinterface.py
@@ -1,6 +1,6 @@
__author__ = 'Frank Sehnke, sehnke@in.tum.de'
-from environment import Environment
+from .environment import Environment
class GraphicalEnvironment(Environment):
""" Special type of environment that has graphical output and therefore needs a renderer.
diff --git a/pybrain/rl/environments/shipsteer/northwardtask.py b/pybrain/rl/environments/shipsteer/northwardtask.py
index 095691501..978b78a13 100644
--- a/pybrain/rl/environments/shipsteer/northwardtask.py
+++ b/pybrain/rl/environments/shipsteer/northwardtask.py
@@ -1,7 +1,7 @@
__author__ = 'Martin Felder, felder@in.tum.de'
from pybrain.rl.environments import EpisodicTask
-from shipsteer import ShipSteeringEnvironment
+from .shipsteer import ShipSteeringEnvironment
class GoNorthwardTask(EpisodicTask):
diff --git a/pybrain/rl/environments/shipsteer/viewer.py b/pybrain/rl/environments/shipsteer/viewer.py
index 070315269..9af6f993c 100644
--- a/pybrain/rl/environments/shipsteer/viewer.py
+++ b/pybrain/rl/environments/shipsteer/viewer.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Frank Sehnke, sehnke@in.tum.de'
#@PydevCodeAnalysisIgnore
@@ -73,7 +75,7 @@ def saveTo(self, filename, format="JPEG"):
image = Image.fromstring("RGB", (width, height), data)
image = image.transpose(Image.FLIP_TOP_BOTTOM)
image.save(filename, format)
- print('Saved image to ', filename)
+ print(('Saved image to ', filename))
return image
# the render method containing the Glut mainloop
diff --git a/pybrain/rl/environments/simple/renderer.py b/pybrain/rl/environments/simple/renderer.py
index c395383a0..9d9925e64 100644
--- a/pybrain/rl/environments/simple/renderer.py
+++ b/pybrain/rl/environments/simple/renderer.py
@@ -56,7 +56,7 @@ def drawPlot(self):
# draw function
xvalues = arange(self.min, self.max, 0.1)
- yvalues = map(self.f, xvalues)
+ yvalues = list(map(self.f, xvalues))
plot(xvalues, yvalues)
# draw exploration path
diff --git a/pybrain/rl/environments/simplerace/simplecontroller.py b/pybrain/rl/environments/simplerace/simplecontroller.py
index 8289d6703..fd2f96027 100644
--- a/pybrain/rl/environments/simplerace/simplecontroller.py
+++ b/pybrain/rl/environments/simplerace/simplecontroller.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Julian Togelius, julian@idsia.ch'
from scipy import array
@@ -26,6 +28,6 @@ def getAction(self):
steering = -1
else:
steering = 1
- print("speed", self.speed, "angle", self.angleToCurrentWP, "driving", driving, "steering", steering)
+ print(("speed", self.speed, "angle", self.angleToCurrentWP, "driving", driving, "steering", steering))
return array([driving, steering])
diff --git a/pybrain/rl/environments/simplerace/simpleracetask.py b/pybrain/rl/environments/simplerace/simpleracetask.py
index 80f4ce47b..78cb2b2e8 100644
--- a/pybrain/rl/environments/simplerace/simpleracetask.py
+++ b/pybrain/rl/environments/simplerace/simpleracetask.py
@@ -1,7 +1,7 @@
__author__ = 'Julian Togelius, julian@idsia.ch'
from pybrain.rl.environments import EpisodicTask
-from simpleracetcp import SimpleraceEnvironment
+from .simpleracetcp import SimpleraceEnvironment
class SimpleraceTask(EpisodicTask):
diff --git a/pybrain/rl/environments/simplerace/simpleracetcp.py b/pybrain/rl/environments/simplerace/simpleracetcp.py
index 615b495a3..215287f7f 100644
--- a/pybrain/rl/environments/simplerace/simpleracetcp.py
+++ b/pybrain/rl/environments/simplerace/simpleracetcp.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Julian Togelius, julian@idsia.ch'
from pybrain.rl.environments import Environment
@@ -70,7 +72,7 @@ def performAction(self, action):
self.serverIsReady = True
self.waitOne = True
elif (inputs[0] == "data"):
- inputs[2:20] = map(float, inputs[2:20])
+ inputs[2:20] = list(map(float, inputs[2:20]))
self.sensors = inputs[2:9]
currentWp = [inputs[18], inputs[19]]
# check that this is not the first step of an episode
@@ -79,7 +81,7 @@ def performAction(self, action):
if (currentWp[0] != self.lastStepCurrentWp[0]):
# check that we don't have a server side change of episode
if (currentWp[0] != self.lastStepNextWp[0]):
- print("%.3f %.3f %.3f %.3f " % (currentWp[0], currentWp[1], self.lastStepNextWp[0], self.lastStepNextWp[1]))
+ print(("%.3f %.3f %.3f %.3f " % (currentWp[0], currentWp[1], self.lastStepNextWp[0], self.lastStepNextWp[1])))
raise Exception("Unexpected episode change")
else:
# all is fine, increase score. but for who?
@@ -95,7 +97,7 @@ def performAction(self, action):
elif (len (inputs[0]) < 2):
print("impossible!")
else:
- print("incomprehensible and thus roundly ignored", data)
+ print(("incomprehensible and thus roundly ignored", data))
def reset(self):
self.step = 0
diff --git a/pybrain/rl/environments/twoplayergames/capturegame.py b/pybrain/rl/environments/twoplayergames/capturegame.py
index 03717e7e4..9dd9660f6 100644
--- a/pybrain/rl/environments/twoplayergames/capturegame.py
+++ b/pybrain/rl/environments/twoplayergames/capturegame.py
@@ -3,7 +3,7 @@
from random import choice
from scipy import zeros
-from twoplayergame import TwoPlayerGame
+from .twoplayergame import TwoPlayerGame
# TODO: undo operation
@@ -96,7 +96,7 @@ def doMove(self, c, pos):
def getSensors(self):
""" just a list of the board position states. """
- return map(lambda x: x[1], sorted(self.b.items()))
+ return [x[1] for x in sorted(self.b.items())]
def __str__(self):
s = ''
@@ -133,7 +133,7 @@ def _setStone(self, c, pos):
self.b[pos] = c
merge = False
self.groups[pos] = self.size * pos[0] + pos[1]
- freen = filter(lambda n: self.b[n] == self.EMPTY, self._neighbors(pos))
+ freen = [n for n in self._neighbors(pos) if self.b[n] == self.EMPTY]
self.liberties[self.groups[pos]] = set(freen)
for n in self._neighbors(pos):
if self.b[n] == -c:
@@ -146,7 +146,7 @@ def _setStone(self, c, pos):
self.liberties[newg].difference_update([pos])
else:
# merging 2 groups
- for p in self.groups.keys():
+ for p in list(self.groups.keys()):
if self.groups[p] == oldg:
self.groups[p] = newg
self.liberties[newg].update(self.liberties[oldg])
@@ -198,19 +198,19 @@ def getGroupSize(self, pos):
if self.b[pos] == self.EMPTY:
return None
g = self.groups[pos]
- return len(filter(lambda x: x == g, self.groups.values()))
+ return len([x for x in list(self.groups.values()) if x == g])
def getLegals(self, c):
""" return all the legal positions for a color """
- return filter(lambda p: self.b[p] == self.EMPTY, self._iterPos())
+ return [p for p in self._iterPos() if self.b[p] == self.EMPTY]
def getAcceptable(self, c):
""" return all legal positions for a color that don't commit suicide. """
- return filter(lambda p: not self._suicide(c, p), self.getLegals(c))
+ return [p for p in self.getLegals(c) if not self._suicide(c, p)]
def getKilling(self, c):
""" return all legal positions for a color that immediately kill the opponent. """
- return filter(lambda p: self._capture(c, p), self.getAcceptable(c))
+ return [p for p in self.getAcceptable(c) if self._capture(c, p)]
def randomBoard(self, nbmoves):
""" produce a random, undecided and legal capture-game board, after at most nbmoves.
diff --git a/pybrain/rl/environments/twoplayergames/capturegameplayers/clientwrapper.py b/pybrain/rl/environments/twoplayergames/capturegameplayers/clientwrapper.py
index c3801800d..24857083a 100644
--- a/pybrain/rl/environments/twoplayergames/capturegameplayers/clientwrapper.py
+++ b/pybrain/rl/environments/twoplayergames/capturegameplayers/clientwrapper.py
@@ -1,8 +1,10 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
import socket
-from captureplayer import CapturePlayer
+from .captureplayer import CapturePlayer
from pybrain.rl.environments.twoplayergames import CaptureGame
# TODO: allow partially forced random moves.
@@ -31,7 +33,7 @@ def __init__(self, game, color=CaptureGame.BLACK, player='AtariGreedy', **args):
#define player
self.theSocket.send(player + '-' + str(color) + '\n')
if self.verbose:
- print('Sending:', player + '-' + str(color))
+ print(('Sending:', player + '-' + str(color)))
accept = ""
while len (accept) < 2:
accept = self.theSocket.recv(1000)
@@ -56,17 +58,17 @@ def getAction(self):
# get the suggested move from the java player:
if self.verbose:
- print('Sending:', js)
+ print(('Sending:', js))
self.theSocket.send(js + '\n')
jr = ""
if self.verbose:
- print('Waiting for server',)
+ print(('Waiting for server',))
while len (jr) < 2:
jr = self.theSocket.recv(1000)
if self.verbose:
- print('.',)
+ print(('.',))
if self.verbose:
- print(" received.", jr)
+ print((" received.", jr))
chosen = eval(jr)
assert self.game.isLegal(self.color, chosen)
diff --git a/pybrain/rl/environments/twoplayergames/capturegameplayers/killing.py b/pybrain/rl/environments/twoplayergames/capturegameplayers/killing.py
index ce7f53d54..a468b7af9 100644
--- a/pybrain/rl/environments/twoplayergames/capturegameplayers/killing.py
+++ b/pybrain/rl/environments/twoplayergames/capturegameplayers/killing.py
@@ -2,7 +2,7 @@
from random import choice
-from nonsuicide import NonSuicidePlayer
+from .nonsuicide import NonSuicidePlayer
class KillingPlayer(NonSuicidePlayer):
diff --git a/pybrain/rl/environments/twoplayergames/capturegameplayers/moduledecision.py b/pybrain/rl/environments/twoplayergames/capturegameplayers/moduledecision.py
index 65b49e567..035e20e0b 100644
--- a/pybrain/rl/environments/twoplayergames/capturegameplayers/moduledecision.py
+++ b/pybrain/rl/environments/twoplayergames/capturegameplayers/moduledecision.py
@@ -1,10 +1,12 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
from scipy import zeros, ones
from pybrain.rl.environments.twoplayergames import CaptureGame
-from randomplayer import RandomCapturePlayer
+from .randomplayer import RandomCapturePlayer
from pybrain.utilities import drawGibbs
@@ -43,21 +45,21 @@ def _legalizeIt(self, a):
""" draw index from an array of values, filtering out illegal moves. """
if not min(a) >= 0:
print(a)
- print(min(a))
- print(self.module.params)
- print(self.module.inputbuffer)
- print(self.module.outputbuffer)
+ print((min(a)))
+ print((self.module.params))
+ print((self.module.inputbuffer))
+ print((self.module.outputbuffer))
raise Exception('Non-positive value in array?')
legals = self.game.getLegals(self.color)
vals = ones(len(a))*(-100)*(1+self.temperature)
for i in map(self._convertPosToIndex, legals):
vals[i] = a[i]
drawn = self._convertIndexToPos(drawGibbs(vals, self.temperature))
- assert drawn in legals
+ assert drawn in legals, (drawn, legals)
return drawn
def _convertIndexToPos(self, i):
- return (i/self.game.size, i%self.game.size)
+ return (i//self.game.size, i%self.game.size)
def _convertPosToIndex(self, p):
return p[0]*self.game.size+p[1]
diff --git a/pybrain/rl/environments/twoplayergames/capturegameplayers/nonsuicide.py b/pybrain/rl/environments/twoplayergames/capturegameplayers/nonsuicide.py
index 2cb6d7b06..46ea1ba79 100644
--- a/pybrain/rl/environments/twoplayergames/capturegameplayers/nonsuicide.py
+++ b/pybrain/rl/environments/twoplayergames/capturegameplayers/nonsuicide.py
@@ -2,7 +2,7 @@
from random import choice
-from randomplayer import RandomCapturePlayer
+from .randomplayer import RandomCapturePlayer
class NonSuicidePlayer(RandomCapturePlayer):
diff --git a/pybrain/rl/environments/twoplayergames/capturegameplayers/randomplayer.py b/pybrain/rl/environments/twoplayergames/capturegameplayers/randomplayer.py
index e4e293920..2d2c69733 100644
--- a/pybrain/rl/environments/twoplayergames/capturegameplayers/randomplayer.py
+++ b/pybrain/rl/environments/twoplayergames/capturegameplayers/randomplayer.py
@@ -2,7 +2,7 @@
from random import choice
-from captureplayer import CapturePlayer
+from .captureplayer import CapturePlayer
class RandomCapturePlayer(CapturePlayer):
diff --git a/pybrain/rl/environments/twoplayergames/gomoku.py b/pybrain/rl/environments/twoplayergames/gomoku.py
index 5c2e363d9..c9d6bdb5d 100644
--- a/pybrain/rl/environments/twoplayergames/gomoku.py
+++ b/pybrain/rl/environments/twoplayergames/gomoku.py
@@ -2,7 +2,7 @@
from scipy import zeros
-from twoplayergame import TwoPlayerGame
+from .twoplayergame import TwoPlayerGame
# TODO: factor out the similarities with the CaptureGame and Go.
@@ -95,7 +95,7 @@ def doMove(self, c, pos):
def getSensors(self):
""" just a list of the board position states. """
- return map(lambda x: x[1], sorted(self.b.items()))
+ return [x[1] for x in sorted(self.b.items())]
def __str__(self):
s = ''
@@ -133,11 +133,11 @@ def _setStone(self, c, pos):
def getLegals(self, c):
""" return all the legal positions for a color """
- return filter(lambda p: self.b[p] == self.EMPTY, self._iterPos())
+ return [p for p in self._iterPos() if self.b[p] == self.EMPTY]
def getKilling(self, c):
""" return all legal positions for a color that immediately kill the opponent. """
- return filter(lambda p: self._fiveRow(c, p), self.getLegals(c))
+ return [p for p in self.getLegals(c) if self._fiveRow(c, p)]
def playToTheEnd(self, p1, p2):
""" alternate playing moves between players until the game is over. """
diff --git a/pybrain/rl/environments/twoplayergames/gomokuplayers/killing.py b/pybrain/rl/environments/twoplayergames/gomokuplayers/killing.py
index af861152a..a07b2d68d 100644
--- a/pybrain/rl/environments/twoplayergames/gomokuplayers/killing.py
+++ b/pybrain/rl/environments/twoplayergames/gomokuplayers/killing.py
@@ -2,7 +2,7 @@
from random import choice
-from randomplayer import RandomGomokuPlayer
+from .randomplayer import RandomGomokuPlayer
class KillingGomokuPlayer(RandomGomokuPlayer):
diff --git a/pybrain/rl/environments/twoplayergames/gomokuplayers/moduledecision.py b/pybrain/rl/environments/twoplayergames/gomokuplayers/moduledecision.py
index 2859cb6ed..d6ad1454a 100644
--- a/pybrain/rl/environments/twoplayergames/gomokuplayers/moduledecision.py
+++ b/pybrain/rl/environments/twoplayergames/gomokuplayers/moduledecision.py
@@ -1,10 +1,12 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
from scipy import zeros, ones
from pybrain.rl.environments.twoplayergames import GomokuGame
-from randomplayer import RandomGomokuPlayer
+from .randomplayer import RandomGomokuPlayer
from pybrain.utilities import drawGibbs
@@ -43,10 +45,10 @@ def _legalizeIt(self, a):
""" draw index from an array of values, filtering out illegal moves. """
if not min(a) >= 0:
print(a)
- print(min(a))
- print(self.module.params)
- print(self.module.inputbuffer)
- print(self.module.outputbuffer)
+ print((min(a)))
+ print((self.module.params))
+ print((self.module.inputbuffer))
+ print((self.module.outputbuffer))
raise Exception('No positve value in array?')
legals = self.game.getLegals(self.color)
vals = ones(len(a))*(-100)*(1+self.temperature)
@@ -57,7 +59,7 @@ def _legalizeIt(self, a):
return drawn
def _convertIndexToPos(self, i):
- return (i/self.game.size[0], i%self.game.size[0])
+ return (i//self.game.size[0], i%self.game.size[0])
def _convertPosToIndex(self, p):
return p[0]*self.game.size[0]+p[1]
diff --git a/pybrain/rl/environments/twoplayergames/gomokuplayers/randomplayer.py b/pybrain/rl/environments/twoplayergames/gomokuplayers/randomplayer.py
index cf8fc0340..a5480a943 100644
--- a/pybrain/rl/environments/twoplayergames/gomokuplayers/randomplayer.py
+++ b/pybrain/rl/environments/twoplayergames/gomokuplayers/randomplayer.py
@@ -2,7 +2,7 @@
from random import choice
-from gomokuplayer import GomokuPlayer
+from .gomokuplayer import GomokuPlayer
class RandomGomokuPlayer(GomokuPlayer):
diff --git a/pybrain/rl/environments/twoplayergames/pente.py b/pybrain/rl/environments/twoplayergames/pente.py
index ecf76f27e..e02190a51 100644
--- a/pybrain/rl/environments/twoplayergames/pente.py
+++ b/pybrain/rl/environments/twoplayergames/pente.py
@@ -15,7 +15,7 @@ class PenteGame(GomokuGame):
def reset(self):
GomokuGame.reset(self)
self.pairsTaken = {self.BLACK: 0, self.WHITE: 0}
- center = (self.size[0] / 2, self.size[1] / 2)
+ center = (self.size[0] // 2, self.size[1] // 2)
self._setStone(-self.startcolor, center)
self.movesDone += 1
@@ -78,7 +78,7 @@ def _setStone(self, c, pos, tokill=None):
GomokuGame._setStone(self, c, pos)
for p in tokill:
self.b[p] = self.EMPTY
- self.pairsTaken[c] += len(tokill) / 2
+ self.pairsTaken[c] += len(tokill) // 2
def __str__(self):
s = GomokuGame.__str__(self)
diff --git a/pybrain/rl/environments/twoplayergames/tasks/handicaptask.py b/pybrain/rl/environments/twoplayergames/tasks/handicaptask.py
index 3e411a866..13591b655 100644
--- a/pybrain/rl/environments/twoplayergames/tasks/handicaptask.py
+++ b/pybrain/rl/environments/twoplayergames/tasks/handicaptask.py
@@ -1,6 +1,9 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
-from capturetask import CaptureGameTask
+
+from .capturetask import CaptureGameTask
from pybrain.rl.environments.twoplayergames.capturegameplayers.captureplayer import CapturePlayer
from pybrain.rl.environments.twoplayergames.capturegameplayers import ModuleDecidingPlayer
from pybrain.rl.environments.twoplayergames.capturegame import CaptureGame
@@ -120,8 +123,8 @@ def f(self, player):
h = HandicapCaptureTask(4, opponentStart=False)
p1 = RandomCapturePlayer(h.env)
p1 = KillingPlayer(h.env)
- print(h(p1))
- print(h.results)
- print(h.winProp(0))
- print(h.winProp(1))
+ print((h(p1)))
+ print((h.results))
+ print((h.winProp(0)))
+ print((h.winProp(1)))
diff --git a/pybrain/rl/environments/twoplayergames/tasks/pentetask.py b/pybrain/rl/environments/twoplayergames/tasks/pentetask.py
index 31bd2bb2d..daa5338c2 100644
--- a/pybrain/rl/environments/twoplayergames/tasks/pentetask.py
+++ b/pybrain/rl/environments/twoplayergames/tasks/pentetask.py
@@ -3,7 +3,7 @@
from pybrain.rl.environments.twoplayergames.pente import PenteGame
from pybrain.rl.environments.episodic import EpisodicTask
-from gomokutask import GomokuTask
+from .gomokutask import GomokuTask
from pybrain.rl.environments.twoplayergames.gomokuplayers import RandomGomokuPlayer
from inspect import isclass
diff --git a/pybrain/rl/environments/twoplayergames/tasks/relativegomokutask.py b/pybrain/rl/environments/twoplayergames/tasks/relativegomokutask.py
index 97d342181..bcf49d10b 100644
--- a/pybrain/rl/environments/twoplayergames/tasks/relativegomokutask.py
+++ b/pybrain/rl/environments/twoplayergames/tasks/relativegomokutask.py
@@ -1,6 +1,8 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
-from gomokutask import GomokuTask
+from .gomokutask import GomokuTask
from pybrain.rl.environments.twoplayergames.gomokuplayers import ModuleDecidingPlayer
from pybrain.rl.environments.twoplayergames import GomokuGame
from pybrain.rl.environments.twoplayergames.gomokuplayers.gomokuplayer import GomokuPlayer
@@ -107,7 +109,7 @@ def _oneGame(self, preset = None):
moves = self.env.movesDone
win = self.env.winner == self.player.color
if self.verbose:
- print('Preset:', preset, 'T:', self.temp, 'Win:', win, 'after', moves, 'moves.')
+ print(('Preset:', preset, 'T:', self.temp, 'Win:', win, 'after', moves, 'moves.'))
res = 1 - self.numMovesCoeff * (moves -self.minmoves)/(self.maxmoves-self.minmoves)
if win:
return res
@@ -120,11 +122,11 @@ def _oneGame(self, preset = None):
net1 = CaptureGameNetwork(hsize = 1)
net2 = CaptureGameNetwork(hsize = 1)
r = RelativeGomokuTask(7, maxGames = 10, useNetworks = True)
- print(r(net1, net2))
- print(r(net2, net1))
- print(r.env)
+ print((r(net1, net2)))
+ print((r(net2, net1)))
+ print((r.env))
r.maxGames = 50
- print(r(net1, net2))
- print(r(net2, net1))
- print(r.env)
+ print((r(net1, net2)))
+ print((r(net2, net1)))
+ print((r.env))
diff --git a/pybrain/rl/environments/twoplayergames/tasks/relativetask.py b/pybrain/rl/environments/twoplayergames/tasks/relativetask.py
index 9e3eedea2..eeb9239d8 100644
--- a/pybrain/rl/environments/twoplayergames/tasks/relativetask.py
+++ b/pybrain/rl/environments/twoplayergames/tasks/relativetask.py
@@ -1,6 +1,8 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
-from capturetask import CaptureGameTask
+from .capturetask import CaptureGameTask
from pybrain.rl.environments.twoplayergames.capturegameplayers import ModuleDecidingPlayer
from pybrain.rl.environments.twoplayergames import CaptureGame
from pybrain.rl.environments.twoplayergames.capturegameplayers.captureplayer import CapturePlayer
@@ -119,8 +121,8 @@ def _fixedStartingPos(self):
res = []
if self.size < 3:
return res
- for x in range(1, (self.size + 1) / 2):
- for y in range(x, (self.size + 1) / 2):
+ for x in range(1, (self.size + 1) // 2):
+ for y in range(x, (self.size + 1) // 2):
res.append((x, y))
return res
@@ -136,7 +138,7 @@ def _oneGame(self, preset=None):
moves = self.env.movesDone
win = self.env.winner == self.player.color
if self.verbose:
- print('Preset:', preset, 'T:', self.temp, 'Win:', win, 'after', moves, 'moves.')
+ print(('Preset:', preset, 'T:', self.temp, 'Win:', win, 'after', moves, 'moves.'))
res = 1 - self.numMovesCoeff * (moves - self.minmoves) / (self.maxmoves - self.minmoves)
if win:
return res
@@ -156,11 +158,11 @@ def _oneGame(self, preset=None):
r = RelativeCaptureTask(5, maxGames=40, useNetworks=True,
presetGamesProportion=0.5)
- print(r(net1, net2))
- print(r(net2, net1))
+ print((r(net1, net2)))
+ print((r(net2, net1)))
r.maxGames = 200
- print(r(net1, net2))
- print(r(net2, net1))
+ print((r(net1, net2)))
+ print((r(net2, net1)))
diff --git a/pybrain/rl/experiments/queued.py b/pybrain/rl/experiments/queued.py
index 2fc3eb7a6..1c743b3a9 100644
--- a/pybrain/rl/experiments/queued.py
+++ b/pybrain/rl/experiments/queued.py
@@ -1,6 +1,6 @@
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
-from episodic import EpisodicExperiment
+from .episodic import EpisodicExperiment
from scipy import arange
diff --git a/pybrain/rl/experiments/tournament.py b/pybrain/rl/experiments/tournament.py
index 059d9995f..d2983112b 100644
--- a/pybrain/rl/experiments/tournament.py
+++ b/pybrain/rl/experiments/tournament.py
@@ -104,14 +104,14 @@ def eloScore(self, startingscore=1500, k=32):
delta = k * (s - est)
elos[a1] += delta
elos[a2] -= delta
- for a, e in elos.items():
+ for a, e in list(elos.items()):
a.elo = e
return elos
def __str__(self):
s = 'Tournament results (' + str(self.rounds) + ' rounds, ' + str(self.numGames) + ' games):\n'
for p1, p2 in self._produceAllPairs():
- wins = len(filter(lambda x: x == p1, self.results[(p1, p2)]))
- losses = len(filter(lambda x: x == p2, self.results[(p1, p2)]))
+ wins = len([x for x in self.results[(p1, p2)] if x == p1])
+ losses = len([x for x in self.results[(p1, p2)] if x == p2])
s += ' ' * 3 + p1.name + ' won ' + str(wins) + ' times and lost ' + str(losses) + ' times against ' + p2.name + '\n'
return s
diff --git a/pybrain/rl/explorers/continuous/sde.py b/pybrain/rl/explorers/continuous/sde.py
index 7ff550b1a..e45373dca 100644
--- a/pybrain/rl/explorers/continuous/sde.py
+++ b/pybrain/rl/explorers/continuous/sde.py
@@ -67,9 +67,9 @@ def _backwardImplementation(self, outerr, inerr, outbuf, inbuf):
).reshape(len(outbuf), len(self.state))
idx = 0
- for j in xrange(len(outbuf)):
+ for j in range(len(outbuf)):
sigma_subst2 = dot(self.state ** 2, expln_params[j, :]**2)
- for i in xrange(len(self.state)):
+ for i in range(len(self.state)):
self._derivs[idx] = ((outbuf[j] - inbuf[j]) ** 2 - sigma_subst2) / sigma_subst2 * \
self.state[i] ** 2 * expln_params[j, i] * explnPrime_params[j, i]
# if self.autoalpha and sigma_subst2 != 0:
diff --git a/pybrain/rl/learners/directsearch/enac.py b/pybrain/rl/learners/directsearch/enac.py
index fcab0c29a..397bd9bdc 100644
--- a/pybrain/rl/learners/directsearch/enac.py
+++ b/pybrain/rl/learners/directsearch/enac.py
@@ -1,5 +1,6 @@
-__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
+from __future__ import print_function
+__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
from pybrain.rl.learners.directsearch.policygradient import PolicyGradientLearner
from scipy import ones, dot, ravel
@@ -20,7 +21,7 @@ def calculateGradient(self):
X = ones((self.dataset.getNumSequences(), self.loglh.getDimension('loglh') + 1), float)
# collect sufficient statistics
- print(self.dataset.getNumSequences())
+ print((self.dataset.getNumSequences()))
for n in range(self.dataset.getNumSequences()):
_state, _action, reward = self.dataset.getSequence(n)
seqidx = ravel(self.dataset['sequence_index'])
diff --git a/pybrain/rl/learners/directsearch/gpomdp.py b/pybrain/rl/learners/directsearch/gpomdp.py
index 6316bab6f..ede699ba1 100644
--- a/pybrain/rl/learners/directsearch/gpomdp.py
+++ b/pybrain/rl/learners/directsearch/gpomdp.py
@@ -1,6 +1,6 @@
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
-from policygradient import PolicyGradientLearner
+from .policygradient import PolicyGradientLearner
from scipy import zeros, mean
### NOT WORKING YET ###
diff --git a/pybrain/rl/learners/directsearch/rwr.py b/pybrain/rl/learners/directsearch/rwr.py
index 1445d13a8..6a712a9a3 100644
--- a/pybrain/rl/learners/directsearch/rwr.py
+++ b/pybrain/rl/learners/directsearch/rwr.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch and Daan Wiertra, daan@idsia.ch'
from scipy import zeros, array, mean, randn, exp, dot, argmax
@@ -113,8 +115,8 @@ def learn(self, batches):
self.initr0Avg = []
for b in range(batches):
if self.verbose:
- print
- print('Batch', b + 1)
+ print()
+ print(('Batch', b + 1))
self.reset()
self.learnOneBatch()
self.totalEpisodes += self.batchSize
@@ -126,7 +128,7 @@ def learn(self, batches):
rws += (sum(tmp) / float(len(tmp)))
self.greedyAvg.append(rws / self.greedyRuns)
if self.verbose:
- print('::', round(rws / self.greedyRuns, 5), '::')
+ print(('::', round(rws / self.greedyRuns, 5), '::'))
def learnOneBatch(self):
# collect a batch of runs as experience
@@ -172,8 +174,8 @@ def learnOneBatch(self):
avgR0 = mean(r0s)
avgReward /= self.batchSize
if self.verbose:
- print('***', round(avgLen, 3), '***', '(avg init exp. return:', round(avgR0, 5), ')',)
- print('avg reward', round(avgReward, 5), '(tau:', round(self.tau, 3), ')')
+ print(('***', round(avgLen, 3), '***', '(avg init exp. return:', round(avgR0, 5), ')',))
+ print(('avg reward', round(avgReward, 5), '(tau:', round(self.tau, 3), ')'))
print(lens)
# storage:
self.rewardAvg.append(avgReward)
diff --git a/pybrain/rl/learners/meta/levinsearch.py b/pybrain/rl/learners/meta/levinsearch.py
index cd2be3987..53d5611a5 100644
--- a/pybrain/rl/learners/meta/levinsearch.py
+++ b/pybrain/rl/learners/meta/levinsearch.py
@@ -30,7 +30,7 @@ def run(self, input, generator):
c = 0
while c <= maxLevin:
try:
- c, p = piter.next()
+ c, p = next(piter)
except StopIteration:
break
if c not in complexities:
diff --git a/pybrain/rl/learners/modelbased/leastsquares.py b/pybrain/rl/learners/modelbased/leastsquares.py
index 9600f3bf4..8d49da19d 100644
--- a/pybrain/rl/learners/modelbased/leastsquares.py
+++ b/pybrain/rl/learners/modelbased/leastsquares.py
@@ -32,7 +32,7 @@ def trueFeatureStats(T, R, fMap, discountFactor, stateProp=1, MAT_LIMIT=1e8):
numStates = len(T)
statMatrix = zeros((dim, dim))
statResidual = zeros(dim)
- ss = range(numStates)
+ ss = list(range(numStates))
repVersion = False
if stateProp < 1:
diff --git a/pybrain/rl/learners/valuebased/linearfa.py b/pybrain/rl/learners/valuebased/linearfa.py
index 377856958..ecdc83d22 100644
--- a/pybrain/rl/learners/valuebased/linearfa.py
+++ b/pybrain/rl/learners/valuebased/linearfa.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
""" RL with linear function approximation.
@@ -264,7 +266,7 @@ def testSingleStateFullDiscounted(self):
for x, l in r:
print(x)
for a in l:
- print(fListToString(a[0], 2) )
+ print((fListToString(a[0], 2) ))
for _, l in r:
self.assertAlmostEquals(min(l[0][0]), 1, places=0)
self.assertAlmostEquals(max(l[0][0]), 1, places=0)
@@ -281,7 +283,7 @@ def testSingleState(self):
for x, l in r:
print(x)
for a in l:
- print(fListToString(a[0], 2) )
+ print((fListToString(a[0], 2) ))
for _, l in r:
self.assertAlmostEquals(min(l[0][0]), max(l[0][0]), places=0)
self.assertAlmostEquals(min(l[1][0]), max(l[1][0]), places=0)
@@ -289,13 +291,13 @@ def testSingleState(self):
self.assertAlmostEquals(min(l[3][0]), max(l[3][0]), places=0)
def testSingleAction(self):
- r = self.runSequences(num_actions=1, r_states=map(array, [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]),
+ r = self.runSequences(num_actions=1, r_states=list(map(array, [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]])),
num_interactions=1000, lr=0.1, _lambda=0.5, gamma=0.5)
if self.verbose:
for x, l in r:
print(x)
for a in l:
- print(fListToString(a, 2) )
+ print((fListToString(a, 2) ))
for _, l in r:
self.assertAlmostEquals(min(l[0]), max(l[0]), places=0)
self.assertAlmostEquals(min(l[1]), max(l[1]), places=0)
@@ -309,7 +311,7 @@ def testSimple(self):
for x, l in r:
print(x)
for a in l:
- print(fListToString(a[0], 2) )
+ print((fListToString(a[0], 2) ))
for _, l in r:
self.assertAlmostEquals(min(l[0][0]), max(l[0][0]), places=0)
self.assertAlmostEquals(min(l[1][0]), max(l[1][0]), places=0)
@@ -326,7 +328,7 @@ def runSequences(self, num_actions=1, num_features=1, num_states=1,
state_seq = [choice(r_states) for _ in range(num_interactions)]
action_seq = [randint(0, num_actions - 1) for _ in range(num_interactions)]
rewards = [ones(num_interactions), rand(num_interactions), action_seq, [s[0] for s in state_seq]]
- datas = [zip(state_seq, action_seq, r) for r in rewards]
+ datas = [list(zip(state_seq, action_seq, r)) for r in rewards]
res = []
for algo in self.algos:
res.append((algo.__name__, []))
diff --git a/pybrain/structure/connections/full.py b/pybrain/structure/connections/full.py
index 4ee38112f..98ca3f0e1 100644
--- a/pybrain/structure/connections/full.py
+++ b/pybrain/structure/connections/full.py
@@ -26,4 +26,4 @@ def _backwardImplementation(self, outerr, inerr, inbuf):
def whichBuffers(self, paramIndex):
"""Return the index of the input module's output buffer and
the output module's input buffer for the given weight."""
- return paramIndex % self.inmod.outdim, paramIndex / self.inmod.outdim
+ return paramIndex % self.inmod.outdim, paramIndex // self.inmod.outdim
diff --git a/pybrain/structure/connections/permutation.py b/pybrain/structure/connections/permutation.py
index 17fd01b63..7dda24474 100644
--- a/pybrain/structure/connections/permutation.py
+++ b/pybrain/structure/connections/permutation.py
@@ -21,20 +21,20 @@ def __init__(self, inmod, outmod, permutation, blocksize, *args, **kwargs):
if len(permutation) * blocksize != self.indim:
raise ValueError(
"Permutation has wrong size: should be %i but is %i." %(
- (self.indim / blocksize), len(permutation)))
+ (self.indim // blocksize), len(permutation)))
self.permutation = array(permutation)
- self.invpermutation = permute(range(len(permutation)), permutation)
+ self.invpermutation = permute(list(range(len(permutation))), permutation)
self.blocksize = blocksize
def _forwardImplementation(self, inbuf, outbuf):
- inbuf = inbuf.reshape(self.indim / self.blocksize, self.blocksize)
+ inbuf = inbuf.reshape(self.indim // self.blocksize, self.blocksize)
inbuf = permute(inbuf, self.permutation)
inbuf.shape = self.indim,
outbuf += inbuf
def _backwardImplementation(self, outerr, inerr, inbuf):
- outerr = outerr.reshape(self.indim / self.blocksize, self.blocksize)
+ outerr = outerr.reshape(self.indim // self.blocksize, self.blocksize)
outerr = permute(outerr, self.invpermutation)
outerr.shape = self.indim,
inerr += outerr
diff --git a/pybrain/structure/modulemesh.py b/pybrain/structure/modulemesh.py
index 70cc22043..be15116af 100644
--- a/pybrain/structure/modulemesh.py
+++ b/pybrain/structure/modulemesh.py
@@ -2,6 +2,7 @@
from pybrain.utilities import iterCombinations, Named
from pybrain.structure.moduleslice import ModuleSlice
+from functools import reduce
class ModuleMesh(Named):
@@ -37,12 +38,12 @@ def viewOnFlatLayer(layer, dimensions, name = None):
assert max(dimensions) > 1, "At least one dimension needs to be larger than one."
def slicer():
nbunits = reduce(lambda x, y: x*y, dimensions, 1)
- insize = layer.indim / nbunits
- outsize = layer.outdim / nbunits
+ insize = layer.indim // nbunits
+ outsize = layer.outdim // nbunits
for index in range(nbunits):
yield ModuleSlice(layer, insize*index, insize*(index+1), outsize*index, outsize*(index+1))
c = slicer()
- return ModuleMesh(lambda: c.next(), dimensions, name)
+ return ModuleMesh(lambda: next(c), dimensions, name)
def __iter__(self):
for coord in iterCombinations(self.dims):
diff --git a/pybrain/structure/modules/evolinonetwork.py b/pybrain/structure/modules/evolinonetwork.py
index 13fe2d446..7caa2f877 100644
--- a/pybrain/structure/modules/evolinonetwork.py
+++ b/pybrain/structure/modules/evolinonetwork.py
@@ -100,7 +100,7 @@ def generate(self, length):
Use .reset() and .washout() before."""
generated_sequence = [] #empty(length)
- for _ in xrange(length):
+ for _ in range(length):
backprojection = self._getLastOutput()
backprojection *= self.backprojectionFactor
out = self._activateNetwork(backprojection)
@@ -209,7 +209,7 @@ def _getInputConnectionsOfLayer(self, layer):
"""Return a list of all input connections for the layer."""
connections = []
all_cons = list(self._network.recurrentConns)
- all_cons += sum(self._network.connections.values(), [])
+ all_cons += sum(list(self._network.connections.values()), [])
for c in all_cons:
if c.outmod is layer:
if not isinstance(c, FullConnection):
diff --git a/pybrain/structure/modules/gate.py b/pybrain/structure/modules/gate.py
index 39ac86e5a..99eceb941 100644
--- a/pybrain/structure/modules/gate.py
+++ b/pybrain/structure/modules/gate.py
@@ -61,12 +61,12 @@ def __init__(self, dim, name=None):
self.setArgs(dim=dim, name=self.name)
def _forwardImplementation(self, inbuf, outbuf):
- dim = self.indim / 2
+ dim = self.indim // 2
outbuf[:dim] += sigmoid(inbuf[:dim]) * inbuf[dim:]
outbuf[dim:] += (1 - sigmoid(inbuf[:dim])) * inbuf[dim:]
def _backwardImplementation(self, outerr, inerr, outbuf, inbuf):
- dim = self.indim / 2
+ dim = self.indim // 2
in0 = inbuf[:dim]
in1 = inbuf[dim:]
out0 = outerr[:dim]
diff --git a/pybrain/structure/modules/mdrnnlayer.py b/pybrain/structure/modules/mdrnnlayer.py
index 8df37a66d..1ddb467db 100644
--- a/pybrain/structure/modules/mdrnnlayer.py
+++ b/pybrain/structure/modules/mdrnnlayer.py
@@ -18,6 +18,7 @@
from pybrain.structure.modules.module import Module
from pybrain.structure.modules.neuronlayer import NeuronLayer
from pybrain.structure.parametercontainer import ParameterContainer
+from functools import reduce
class MdrnnLayer(NeuronLayer, ParameterContainer):
@@ -39,7 +40,7 @@ def predParams(self):
offset = self.num_in_params
rest = self.params[offset:]
return [rest[(i * self.num_rec_params):(i + 1) * self.num_rec_params]
- for i in xrange(self.timedim)]
+ for i in range(self.timedim)]
@property
def outParams(self):
@@ -79,7 +80,7 @@ def __init__(self, timedim, shape,
self.outsize = outsize
self.indim = reduce(operator.mul, shape, 1)
self.blocksize = reduce(operator.mul, blockshape, 1)
- self.sequenceLength = self.indim / self.blocksize
+ self.sequenceLength = self.indim // self.blocksize
self.outdim = self.sequenceLength * self.outsize
self.bufferlist = [('cellStates', self.sequenceLength * self.hiddendim)]
@@ -112,7 +113,7 @@ def __init__(self, timedim, shape,
self.hiddenlayer = MDLSTMLayer(self.hiddendim, self.timedim)
# Every point in the sequence has timedim predecessors.
- self.predlayers = [LinearLayer(self.outsize) for _ in xrange(timedim)]
+ self.predlayers = [LinearLayer(self.outsize) for _ in range(timedim)]
# We need a single layer to hold the input. We will swipe a connection
# over the corrects part of it, in order to feed the correct input in.
diff --git a/pybrain/structure/modules/mixturedensity.py b/pybrain/structure/modules/mixturedensity.py
index f2e07aea0..e7809f381 100644
--- a/pybrain/structure/modules/mixturedensity.py
+++ b/pybrain/structure/modules/mixturedensity.py
@@ -1,7 +1,7 @@
# $Id$
__author__ = 'Martin Felder'
-from neuronlayer import NeuronLayer
+from .neuronlayer import NeuronLayer
from pybrain.tools.functions import safeExp
diff --git a/pybrain/structure/modules/statedependentlayer.py b/pybrain/structure/modules/statedependentlayer.py
index dada78e25..920bb70d0 100644
--- a/pybrain/structure/modules/statedependentlayer.py
+++ b/pybrain/structure/modules/statedependentlayer.py
@@ -68,9 +68,9 @@ def _backwardImplementation(self, outerr, inerr, outbuf, inbuf):
).reshape(len(outbuf), len(self.state))
idx = 0
- for j in xrange(len(outbuf)):
+ for j in range(len(outbuf)):
sigma_subst2 = dot(self.state ** 2, expln_params[j, :]**2)
- for i in xrange(len(self.state)):
+ for i in range(len(self.state)):
self._derivs[idx] = ((outbuf[j] - inbuf[j]) ** 2 - sigma_subst2) / sigma_subst2 * \
self.state[i] ** 2 * expln_params[j, i] * explnPrime_params[j, i]
if self.autoalpha and sigma_subst2 != 0:
diff --git a/pybrain/structure/networks/borderswiping.py b/pybrain/structure/networks/borderswiping.py
index c0c74b396..ab3d4efce 100644
--- a/pybrain/structure/networks/borderswiping.py
+++ b/pybrain/structure/networks/borderswiping.py
@@ -70,11 +70,11 @@ def _buildBorderStructure(self, inmesh, hiddenmesh, outmesh):
hunits = []
if unit[dim] == 0:
for swipe in range(self.swipes):
- if (swipe/2**dim) % 2 == 0:
+ if (swipe//2**dim) % 2 == 0:
hunits.append(tuple(list(unit)+[swipe]))
if unit[dim] == maxval-1:
for swipe in range(self.swipes):
- if (swipe/2**dim) % 2 == 1:
+ if (swipe//2**dim) % 2 == 1:
hunits.append(tuple(list(unit)+[swipe]))
for hunit in hunits:
self.addConnection(SharedFullConnection(bconn, self['bias'], hiddenmesh[hunit]))
@@ -93,11 +93,11 @@ def _canonicForm(self, tup, dim):
def _extrapolateBorderAt(self, t, using):
""" maybe we can use weights that are similar to neighboring borderconnections
as initialization. """
- closest = reachable(decrementAny, [t], using.keys())
+ closest = reachable(decrementAny, [t], list(using.keys()))
if len(closest) > 0:
- params = zeros(using[closest.keys()[0]].paramdim)
+ params = zeros(using[list(closest.keys())[0]].paramdim)
normalize = 0.
- for c, dist in closest.items():
+ for c, dist in list(closest.items()):
params += using[c].params / dist
normalize += 1./dist
params /= normalize
diff --git a/pybrain/structure/networks/convolutional.py b/pybrain/structure/networks/convolutional.py
index e8f306fdb..f53af1dbc 100644
--- a/pybrain/structure/networks/convolutional.py
+++ b/pybrain/structure/networks/convolutional.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
from pybrain.structure.modules.linearlayer import LinearLayer
from pybrain.structure.modules.tanhlayer import TanhLayer
from pybrain.structure.moduleslice import ModuleSlice
@@ -54,7 +56,7 @@ def _buildStructure(self, inputdim, insize, inlayer, convSize, numFeatureMaps):
if __name__ == '__main__':
from scipy import array, ravel
- from custom.convboard import ConvolutionalBoardNetwork
+ from .custom.convboard import ConvolutionalBoardNetwork
from pybrain.rl.environments.twoplayergames.tasks import CaptureGameTask
N = ConvolutionalBoardNetwork(4, 3, 5)
@@ -65,11 +67,11 @@ def _buildStructure(self, inputdim, insize, inlayer, convSize, numFeatureMaps):
]
res = N.activate(ravel(array(input)))
res = res.reshape(4, 4)
- print(N['pad'].inputbuffer[0].reshape(6, 6, 2)[:, :, 0])
+ print((N['pad'].inputbuffer[0].reshape(6, 6, 2)[:, :, 0]))
print(res)
t = CaptureGameTask(4)
- print(t(N))
+ print((t(N)))
if False:
N = SimpleConvolutionalNetwork(4, 2, 5)
diff --git a/pybrain/structure/networks/custom/__init__.py b/pybrain/structure/networks/custom/__init__.py
index 026661d1c..038241c72 100644
--- a/pybrain/structure/networks/custom/__init__.py
+++ b/pybrain/structure/networks/custom/__init__.py
@@ -1 +1 @@
-from capturegame import CaptureGameNetwork
\ No newline at end of file
+from .capturegame import CaptureGameNetwork
\ No newline at end of file
diff --git a/pybrain/structure/networks/custom/convboard.py b/pybrain/structure/networks/custom/convboard.py
index 6111ebb5f..175aef0a4 100644
--- a/pybrain/structure/networks/custom/convboard.py
+++ b/pybrain/structure/networks/custom/convboard.py
@@ -22,7 +22,7 @@ def __init__(self, boardSize, convSize, numFeatureMaps, **args):
self.addInputModule(inlayer)
# we need some treatment of the border too - thus we pad the direct board input.
- x = convSize/2
+ x = convSize//2
insize = boardSize+2*x
if convSize % 2 == 0:
insize -= 1
@@ -36,11 +36,11 @@ def __init__(self, boardSize, convSize, numFeatureMaps, **args):
paddable = []
if convSize % 2 == 0:
- xs = range(x)+range(insize-x+1, insize)
+ xs = list(range(x))+list(range(insize-x+1, insize))
else:
- xs = range(x)+range(insize-x, insize)
- paddable.extend(crossproduct([range(insize), xs]))
- paddable.extend(crossproduct([xs, range(x, boardSize+x)]))
+ xs = list(range(x))+list(range(insize-x, insize))
+ paddable.extend(crossproduct([list(range(insize)), xs]))
+ paddable.extend(crossproduct([xs, list(range(x, boardSize+x))]))
for (i, j) in paddable:
self.addConnection(SharedFullConnection(biasConn, bias, paddedlayer,
diff --git a/pybrain/structure/networks/mdrnn.py b/pybrain/structure/networks/mdrnn.py
index 5ef22489d..92d5a84ca 100644
--- a/pybrain/structure/networks/mdrnn.py
+++ b/pybrain/structure/networks/mdrnn.py
@@ -7,6 +7,7 @@
import operator
import scipy
+from functools import reduce
try:
from arac.pybrainbridge import _FeedForwardNetwork #@UnresolvedImport
@@ -32,7 +33,7 @@ def __init__(self, timedim, shape,
self.blockshape = blockshape
self.indim = reduce(operator.mul, shape, 1)
self.blocksize = reduce(operator.mul, blockshape, 1)
- self.sequenceLength = self.indim / self.blocksize
+ self.sequenceLength = self.indim // self.blocksize
self.inlayerclass = inlayerclass
self.outlayerclass = outlayerclass
@@ -48,7 +49,7 @@ def _standardPermutation(self):
"""Return the permutation of input data that is suitable for this
network."""
# TODO: include blockpermute here
- return scipy.array(range(self.sequenceLength))
+ return scipy.array(list(range(self.sequenceLength)))
def _buildTopology(self):
inlayer = self.inlayerclass(self.indim)
@@ -62,7 +63,7 @@ def _buildTopology(self):
# Make a connection that permutes the input...
in_pc = PermutationConnection(inlayer, i, p, self.blocksize)
# .. and one that permutes it back.
- pinv = permute(range(len(p)), p)
+ pinv = permute(list(range(len(p))), p)
out_pc = PermutationConnection(i, outlayer, pinv, self.outsize)
self.addModule(i)
self.addConnection(in_pc)
@@ -93,8 +94,8 @@ def _permsForSwiping(self):
"""
# We use an identity permutation to generate the permutations from by
# slicing correctly.
- identity = scipy.array(range(self.sequenceLength))
- identity.shape = tuple(s / b for s, b in zip(self.shape, self.blockshape))
+ identity = scipy.array(list(range(self.sequenceLength)))
+ identity.shape = tuple(s // b for s, b in zip(self.shape, self.blockshape))
permutations = []
# Loop over all possible directions: from each corner to each corner
for direction in crossproduct([('+', '-')] * self.timedim):
diff --git a/pybrain/structure/networks/network.py b/pybrain/structure/networks/network.py
index f55dec156..d5c4d2fa9 100644
--- a/pybrain/structure/networks/network.py
+++ b/pybrain/structure/networks/network.py
@@ -1,5 +1,4 @@
-from __future__ import with_statement
-
+from __future__ import print_function
__author__ = 'Daan Wierstra and Tom Schaul'
@@ -184,13 +183,13 @@ def _topologicalSort(self):
if node not in graph:
# Zero incoming connections.
graph[node] = [0]
- for c in chain(*self.connections.values()):
+ for c in chain(*list(self.connections.values())):
graph[c.inmod].append(c.outmod)
# Update the count of incoming arcs in outnode.
graph[c.outmod][0] += 1
# Find all roots (nodes with zero incoming arcs).
- roots = [node for (node, nodeinfo) in graph.items() if nodeinfo[0] == 0]
+ roots = [node for (node, nodeinfo) in list(graph.items()) if nodeinfo[0] == 0]
# Make sure the ordering on all runs is the same.
roots.sort(key=lambda x: x.name)
@@ -244,8 +243,8 @@ def sortModules(self):
# TODO: make this a property; indim and outdim are invalid before
# .sortModules is called!
# Determine the input and output dimensions of the network.
- self.indim = sum(m.indim for m in self.inmodules)
- self.outdim = sum(m.outdim for m in self.outmodules)
+ self.indim = int(sum(m.indim for m in self.inmodules))
+ self.outdim = int(sum(m.outdim for m in self.outmodules))
self.indim = 0
for m in self.inmodules:
@@ -296,7 +295,7 @@ def convertToFastNetwork(self):
for m in net.modules:
cnet.addModule(m)
- for clist in net.connections.values():
+ for clist in list(net.connections.values()):
for c in clist:
cnet.addConnection(c)
if isinstance(net, RecurrentNetwork):
diff --git a/pybrain/structure/networks/neurondecomposable.py b/pybrain/structure/networks/neurondecomposable.py
index fe362f772..42fc0ed3d 100644
--- a/pybrain/structure/networks/neurondecomposable.py
+++ b/pybrain/structure/networks/neurondecomposable.py
@@ -101,7 +101,7 @@ def convertNormalNetwork(n):
res.addOutputModule(m)
for m in n.modules:
res.addModule(m)
- for c in chain(*n.connections.values()):
+ for c in chain(*list(n.connections.values())):
res.addConnection(c)
res.name = n.name
res.sortModules()
diff --git a/pybrain/structure/networks/swiping.py b/pybrain/structure/networks/swiping.py
index eba829b27..945a274a6 100644
--- a/pybrain/structure/networks/swiping.py
+++ b/pybrain/structure/networks/swiping.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
from pybrain.structure.networks.feedforward import FeedForwardNetwork
@@ -99,7 +101,7 @@ def _buildSwipingStructure(self, inmesh, hiddenmesh, outmesh):
# swipe directions are towards higher coordinates on dim D if the swipe%(2**D) = 0
# and towards lower coordinates otherwise.
previousunit = list(hunit)
- if (swipe / 2 ** dim) % 2 == 0:
+ if (swipe // 2 ** dim) % 2 == 0:
previousunit[dim] -= 1
dir = '+'
else:
@@ -124,12 +126,12 @@ def _printPredefined(self, dic=None, indent=0):
if dic == None:
dic = self.predefined
for k, val in sorted(dic.items()):
- print(' ' * indent, k,)
+ print((' ' * indent, k,))
if isinstance(val, dict):
print(':')
self._printPredefined(val, indent + 2)
elif isinstance(val, MotherConnection):
- print(val.params)
+ print((val.params))
else:
print(val)
diff --git a/pybrain/supervised/evolino/filter.py b/pybrain/supervised/evolino/filter.py
index 0a3bdeab8..127f44729 100644
--- a/pybrain/supervised/evolino/filter.py
+++ b/pybrain/supervised/evolino/filter.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Michael Isik'
from pybrain.supervised.evolino.gfilter import Filter, SimpleMutation
@@ -53,7 +55,7 @@ def _evaluateNet(self, net, dataset, wtRatio):
numSequences = dataset.getNumSequences()
washout_sequences = []
training_sequences = []
- for i in xrange(numSequences):
+ for i in range(numSequences):
sequence = dataset.getSequence(i)[1]
training_start = int(wtRatio * len(sequence))
washout_sequences.append(sequence[ : training_start ])
@@ -114,7 +116,7 @@ def apply(self, population):
net.setGenome(individual.getGenome())
fitness = self._evaluateNet(net, dataset, self.wtRatio)
if self.verbosity > 1:
- print("Calculated fitness for individual", id(individual), " is ", fitness)
+ print(("Calculated fitness for individual", id(individual), " is ", fitness))
# set the individual fitness
population.setIndividualFitness(individual, fitness)
@@ -222,7 +224,7 @@ def apply(self, population):
n = population.getIndividualsN()
if self.nParents is None:
- nKeep = n / 4
+ nKeep = n // 4
else:
nKeep = self.nParents
diff --git a/pybrain/supervised/evolino/gpopulation.py b/pybrain/supervised/evolino/gpopulation.py
index de4a3e44a..0b689bd38 100644
--- a/pybrain/supervised/evolino/gpopulation.py
+++ b/pybrain/supervised/evolino/gpopulation.py
@@ -95,7 +95,7 @@ def getIndividualFitness(self, individual):
def clearFitness(self):
""" Clears all stored fitness values """
- for (ind, _) in self._fitness.iteritems():
+ for (ind, _) in self._fitness.items():
self._fitness[ind] = -Infinity
# self._fitness.clear()
@@ -139,7 +139,7 @@ def removeWorstIndividuals(self, n):
def getSortedIndividualList(self):
""" Returns a sorted list of all individuals with descending fitness values. """
fitness = self._fitness
- return sorted(fitness.iterkeys(), key=lambda(k):-fitness[k])
+ return sorted(iter(fitness.keys()), key=lambda k:-fitness[k])
def getIndividualsN(self):
diff --git a/pybrain/supervised/evolino/networkwrapper.py b/pybrain/supervised/evolino/networkwrapper.py
index 96c62ef11..9a7071980 100644
--- a/pybrain/supervised/evolino/networkwrapper.py
+++ b/pybrain/supervised/evolino/networkwrapper.py
@@ -50,7 +50,7 @@ def _washout(self, input, target, first_idx=None, last_idx=None):
if first_idx is None: first_idx = 0
if last_idx is None: last_idx = len(target) - 1
raw_outputs = []
- for i in xrange(first_idx, last_idx + 1):
+ for i in range(first_idx, last_idx + 1):
backprojection = self._getLastOutput()
backprojection *= self.backprojectionFactor
full_inp = self._createFullInput(input[i], backprojection)
@@ -275,7 +275,7 @@ def _getRawOutput(self):
def _getInputConnectionsOfLayer(self, layer):
""" Returns a list of all input connections for the layer. """
connections = []
- for c in sum(self._network.connections.values(), []):
+ for c in sum(list(self._network.connections.values()), []):
if c.outmod is layer:
if not isinstance(c, FullConnection):
raise NotImplementedError("At the time there is only support for FullConnection")
@@ -526,7 +526,7 @@ def getFirstHiddenLayer(self):
def getConnections(self):
""" Returns a list of all connections. """
- return sum(self.network.connections.values(), [])
+ return sum(list(self.network.connections.values()), [])
def getInputLayer(self):
""" Returns the input layer. """
@@ -536,7 +536,7 @@ def getInputLayer(self):
def _getInputConnectionsOfLayer(self, layer):
""" Returns a list of all input connections for the layer. """
connections = []
- for c in sum(self.network.connections.values(), []):
+ for c in sum(list(self.network.connections.values()), []):
if c.outmod is layer:
if not isinstance(c, FullConnection):
raise NotImplementedError("At the time there is only support for FullConnection")
diff --git a/pybrain/supervised/evolino/population.py b/pybrain/supervised/evolino/population.py
index dc9a6c005..666022ed6 100644
--- a/pybrain/supervised/evolino/population.py
+++ b/pybrain/supervised/evolino/population.py
@@ -152,7 +152,7 @@ def __init__(self, chromosome, maxNIndividuals, valueInitializer=Randomization(-
def setArgs(self, **kwargs):
- for key, val in kwargs.iteritems():
+ for key, val in kwargs.items():
getattr(self, key)
setattr(self, key, val)
diff --git a/pybrain/supervised/knn/lsh/minhash.py b/pybrain/supervised/knn/lsh/minhash.py
index 08458fa35..3780e34cd 100644
--- a/pybrain/supervised/knn/lsh/minhash.py
+++ b/pybrain/supervised/knn/lsh/minhash.py
@@ -1,4 +1,4 @@
-from __future__ import division
+
"""Module that provides functionality for locality sensitive hashing in hamming
@@ -46,7 +46,7 @@ def jacardCoefficient(a, b):
a = a.astype(bool)
b = b.astype(bool)
- return (a == b).sum() / length
+ return float((a == b).sum()) / length
class MinHash(object):
@@ -75,7 +75,7 @@ def __init__(self, dim, nPermutations):
The number of buckets is dim * nPermutations."""
self.dim = dim
self.permutations = array([permutation(dim)
- for _ in xrange(nPermutations)])
+ for _ in range(nPermutations)])
self.buckets = defaultdict(lambda: [])
diff --git a/pybrain/supervised/knn/lsh/nearoptimal.py b/pybrain/supervised/knn/lsh/nearoptimal.py
index 241c47f35..ed59dc5de 100644
--- a/pybrain/supervised/knn/lsh/nearoptimal.py
+++ b/pybrain/supervised/knn/lsh/nearoptimal.py
@@ -1,4 +1,4 @@
-from __future__ import division
+
"""This module provides functionality for locality sensitive hashing in high
dimensional euclidean spaces.
@@ -17,7 +17,13 @@
from math import sqrt, log, ceil
from scipy import array, dot, random, ones
-from scipy import weave
+
+try:
+ # Python 2
+ from scipy import weave
+except ImportError:
+ # Python 3
+ pass
class MultiDimHash(object):
@@ -178,7 +184,8 @@ def knn(self, point, k):
candidates = self._findKnnCandidates(point)
- def sortKey((point_, satellite_)):
+ def sortKey(xxx_todo_changeme):
+ (point_, satellite_) = xxx_todo_changeme
distance = point - point_
return - dot(distance.T, distance)
diff --git a/pybrain/supervised/trainers/backprop.py b/pybrain/supervised/trainers/backprop.py
index 4426f6cfa..fa09fc9db 100644
--- a/pybrain/supervised/trainers/backprop.py
+++ b/pybrain/supervised/trainers/backprop.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Daan Wierstra and Tom Schaul'
from scipy import dot, argmax
@@ -6,6 +8,7 @@
from pybrain.supervised.trainers.trainer import Trainer
from pybrain.utilities import fListToString
from pybrain.auxiliary import GradientDescent
+from functools import reduce
class BackpropTrainer(Trainer):
@@ -67,7 +70,7 @@ def train(self):
self.module.resetDerivatives()
if self.verbose:
- print("Total error:", errors / ponderation)
+ print("Total error: {z: .12g}".format(z=errors / ponderation))
if self.batchlearning:
self.module._setParameters(self.descent(self.module.derivs))
self.epoch += 1
@@ -124,7 +127,7 @@ def _checkGradient(self, dataset=None, silent=False):
approxderiv = (righterror - lefterror) / (2 * e)
self.module.params[p] = storedoldval
numericalDerivs.append(approxderiv)
- r = zip(analyticalDerivs, numericalDerivs)
+ r = list(zip(analyticalDerivs, numericalDerivs))
res.append(r)
if not silent:
print(r)
@@ -150,11 +153,11 @@ def testOnData(self, dataset=None, verbose=False):
errors.append(e)
ponderatedErrors.append(e / i)
if verbose:
- print('All errors:', ponderatedErrors)
+ print(('All errors:', ponderatedErrors))
assert sum(importances) > 0
avgErr = sum(errors) / sum(importances)
if verbose:
- print('Average error:', avgErr)
+ print(('Average error:', avgErr))
print(('Max error:', max(ponderatedErrors), 'Median error:',
sorted(ponderatedErrors)[len(errors) / 2]))
return avgErr
@@ -252,6 +255,6 @@ def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
#self.trainingErrors.append(self.testOnData(trainingData))
self.ds = dataset
if verbose:
- print('train-errors:', fListToString(self.trainingErrors, 6))
- print('valid-errors:', fListToString(self.validationErrors, 6))
+ print(('train-errors:', fListToString(self.trainingErrors, 6)))
+ print(('valid-errors:', fListToString(self.validationErrors, 6)))
return self.trainingErrors[:bestepoch], self.validationErrors[:1 + bestepoch]
diff --git a/pybrain/supervised/trainers/evolino.py b/pybrain/supervised/trainers/evolino.py
index a3af86c1f..865c9de38 100644
--- a/pybrain/supervised/trainers/evolino.py
+++ b/pybrain/supervised/trainers/evolino.py
@@ -1,5 +1,6 @@
-__author__ = 'Michael Isik'
+from __future__ import print_function
+__author__ = 'Michael Isik'
from numpy import Infinity
@@ -130,11 +131,11 @@ def train(self):
filter.apply(self._population)
if self._max_fitness < self.evaluation.max_fitness:
- if self.verbosity: print("GAINED FITNESS: ", self._max_fitness, " -->" , self.evaluation.max_fitness, "\n")
+ if self.verbosity: print(("GAINED FITNESS: ", self._max_fitness, " -->" , self.evaluation.max_fitness, "\n"))
self._max_fitness = self.evaluation.max_fitness
self._max_fitness_epoch = self.totalepochs
else:
- if self.verbosity: print("DIDN'T GAIN FITNESS:", "best =", self._max_fitness, " current-best = ", self.evaluation.max_fitness, "\n")
+ if self.verbosity: print(("DIDN'T GAIN FITNESS:", "best =", self._max_fitness, " current-best = ", self.evaluation.max_fitness, "\n"))
def burstMutate(self):
self.burstMutation.apply(self._population)
diff --git a/pybrain/supervised/trainers/mixturedensity.py b/pybrain/supervised/trainers/mixturedensity.py
index 11ad7dbd7..85214d709 100644
--- a/pybrain/supervised/trainers/mixturedensity.py
+++ b/pybrain/supervised/trainers/mixturedensity.py
@@ -1,85 +1,85 @@
-# $Id$
-__author__ = 'Martin Felder'
-
-import numpy as np
-from pybrain.supervised.trainers import RPropMinusTrainer, BackpropTrainer
-from pybrain.structure.modules.mixturedensity import MixtureDensityLayer
-
-def gaussian(x, mean, stddev):
- """ return value of homogenous Gaussian at given vector point
- x: vector, mean: vector, stddev: scalar """
- tmp = -0.5 * sum(((x-mean)/stddev)**2)
- return np.exp(tmp) / (np.power(2.*np.pi, 0.5*len(x)) * stddev)
-
-
-class BackpropTrainerMix(BackpropTrainer):
- """ Trainer for mixture model network. See Bishop 2006, Eqn. 5.153-5.157.
- Due to PyBrain conventions it is more convenient (if not pretty) to treat the
- MixtureDensityLayer as having a linear transfer function, and calculate
- its derivative here."""
-
- def setData(self, dataset):
- # different output dimension check
- self.ds = dataset
- if dataset:
- assert dataset.indim == self.module.indim
- assert dataset.outdim == self.module.modulesSorted[-1].nDims
-
- def _calcDerivs(self, seq):
- """ calculate derivatives assuming we have a Network with a MixtureDensityLayer as output """
- assert isinstance(self.module.modulesSorted[-1], MixtureDensityLayer)
-
- self.module.reset()
- for time, sample in enumerate(seq):
- input = sample[0]
- self.module.inputbuffer[time] = input
- self.module.forward()
- error = 0
- nDims = self.module.modulesSorted[-1].nDims
- nGauss = self.module.modulesSorted[-1].nGaussians
- for time, sample in reversed(list(enumerate(seq))):
-
- # Should these three lines be inside this 'for' block
- # or outside? I moved them inside - Jack
- gamma = []
- means = []
- stddevs = []
-
- dummy, target = sample
- par = self.module.outputbuffer[time] # parameters for mixture
- # calculate error contributions from all Gaussians in the mixture
- for k in range(nGauss):
- coeff = par[k]
- stddevs.append(par[k+nGauss])
- idxm = 2*nGauss + k*nDims
- means.append(par[idxm:idxm+nDims])
- gamma.append(coeff * gaussian(target, means[-1], stddevs[-1]))
-
- # calculate error for this pattern, and posterior for target
- sumg = sum(gamma)
- error -= np.log(sumg)
- gamma = np.array(gamma)/sumg
-
- invvariance = 1./par[nGauss:2*nGauss]**2
- invstddev = 1./np.array(stddevs)
-
- # calculate gradient wrt. mixture coefficients
- grad_c = par[0:nGauss] - gamma
-
- # calculate gradient wrt. standard deviations
- grad_m = []
- grad_s = []
- for k in range(nGauss):
- delta = means[k]-target
- grad_m.append(gamma[k]*delta*invvariance[k])
- grad_s.append(-gamma[k]*(np.dot(delta,delta)*invvariance[k]*invstddev[k] - invstddev[k]))
-
- self.module.outputerror[time] = -np.r_[grad_c,grad_s,np.array(grad_m).flatten()]
- self.module.backward()
-
- return error, 1.0
-
-
-class RPropMinusTrainerMix(BackpropTrainerMix,RPropMinusTrainer):
- """ RProp trainer for mixture model network. See Bishop 2006, Eqn. 5.153-5.157. """
- dummy = 0
+# $Id$
+__author__ = 'Martin Felder'
+
+import numpy as np
+from pybrain.supervised.trainers import RPropMinusTrainer, BackpropTrainer
+from pybrain.structure.modules.mixturedensity import MixtureDensityLayer
+
+def gaussian(x, mean, stddev):
+ """ return value of homogenous Gaussian at given vector point
+ x: vector, mean: vector, stddev: scalar """
+ tmp = -0.5 * sum(((x-mean)/stddev)**2)
+ return np.exp(tmp) / (np.power(2.*np.pi, 0.5*len(x)) * stddev)
+
+
+class BackpropTrainerMix(BackpropTrainer):
+ """ Trainer for mixture model network. See Bishop 2006, Eqn. 5.153-5.157.
+ Due to PyBrain conventions it is more convenient (if not pretty) to treat the
+ MixtureDensityLayer as having a linear transfer function, and calculate
+ its derivative here."""
+
+ def setData(self, dataset):
+ # different output dimension check
+ self.ds = dataset
+ if dataset:
+ assert dataset.indim == self.module.indim
+ assert dataset.outdim == self.module.modulesSorted[-1].nDims
+
+ def _calcDerivs(self, seq):
+ """ calculate derivatives assuming we have a Network with a MixtureDensityLayer as output """
+ assert isinstance(self.module.modulesSorted[-1], MixtureDensityLayer)
+
+ self.module.reset()
+ for time, sample in enumerate(seq):
+ input = sample[0]
+ self.module.inputbuffer[time] = input
+ self.module.forward()
+ error = 0
+ nDims = self.module.modulesSorted[-1].nDims
+ nGauss = self.module.modulesSorted[-1].nGaussians
+ for time, sample in reversed(list(enumerate(seq))):
+
+ # Should these three lines be inside this 'for' block
+ # or outside? I moved them inside - Jack
+ gamma = []
+ means = []
+ stddevs = []
+
+ dummy, target = sample
+ par = self.module.outputbuffer[time] # parameters for mixture
+ # calculate error contributions from all Gaussians in the mixture
+ for k in range(nGauss):
+ coeff = par[k]
+ stddevs.append(par[k+nGauss])
+ idxm = 2*nGauss + k*nDims
+ means.append(par[idxm:idxm+nDims])
+ gamma.append(coeff * gaussian(target, means[-1], stddevs[-1]))
+
+ # calculate error for this pattern, and posterior for target
+ sumg = sum(gamma)
+ error -= np.log(sumg)
+ gamma = np.array(gamma)/sumg
+
+ invvariance = 1./par[nGauss:2*nGauss]**2
+ invstddev = 1./np.array(stddevs)
+
+ # calculate gradient wrt. mixture coefficients
+ grad_c = par[0:nGauss] - gamma
+
+ # calculate gradient wrt. standard deviations
+ grad_m = []
+ grad_s = []
+ for k in range(nGauss):
+ delta = means[k]-target
+ grad_m.append(gamma[k]*delta*invvariance[k])
+ grad_s.append(-gamma[k]*(np.dot(delta,delta)*invvariance[k]*invstddev[k] - invstddev[k]))
+
+ self.module.outputerror[time] = -np.r_[grad_c,grad_s,np.array(grad_m).flatten()]
+ self.module.backward()
+
+ return error, 1.0
+
+
+class RPropMinusTrainerMix(BackpropTrainerMix,RPropMinusTrainer):
+ """ RProp trainer for mixture model network. See Bishop 2006, Eqn. 5.153-5.157. """
+ dummy = 0
diff --git a/pybrain/supervised/trainers/rprop.py b/pybrain/supervised/trainers/rprop.py
index 4407ede21..885b0c8d4 100644
--- a/pybrain/supervised/trainers/rprop.py
+++ b/pybrain/supervised/trainers/rprop.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
# $Id$
__author__ = 'Martin Felder'
@@ -43,10 +45,10 @@ def train(self):
errors += e
ponderation += p
if self.verbose:
- print("epoch {epoch:6d} total error {error:12.5g} avg weight {weight:12.5g}".format(
+ print(("epoch {epoch:6d} total error {error:12.5g} avg weight {weight:12.5g}".format(
epoch=self.epoch,
error=errors / ponderation,
- weight=sqrt((self.module.params ** 2).mean())))
+ weight=sqrt((self.module.params ** 2).mean()))))
self.module._setParameters(self.descent(self.module.derivs - self.weightdecay * self.module.params))
self.epoch += 1
self.totalepochs += 1
diff --git a/pybrain/supervised/trainers/svmtrainer.py b/pybrain/supervised/trainers/svmtrainer.py
index bc11da5e6..78c8b0e2d 100644
--- a/pybrain/supervised/trainers/svmtrainer.py
+++ b/pybrain/supervised/trainers/svmtrainer.py
@@ -1,5 +1,6 @@
-__author__ = "Martin Felder, felder@in.tum.de"
+from __future__ import print_function
+__author__ = "Martin Felder, felder@in.tum.de"
try:
from svm import svm_model, svm_parameter, svm_problem, cross_validation #@UnresolvedImport
@@ -81,15 +82,15 @@ def setParams(self, **kwargs):
defined for the LIBSVM svm_model class, see their documentation.
:key searchlog: Save a list of coordinates and the achieved CV accuracy to this file."""
- if kwargs.has_key('weight'):
+ if 'weight' in kwargs:
self.params['nr_weight'] = len(kwargs['weight'])
- if kwargs.has_key('log2C'):
+ if 'log2C' in kwargs:
self.params['C'] = 2 ** kwargs['log2C']
kwargs.pop('log2C')
- if kwargs.has_key('log2g'):
+ if 'log2g' in kwargs:
self.params['gamma'] = 2 ** kwargs['log2g']
kwargs.pop('log2g')
- if kwargs.has_key('searchlog'):
+ if 'searchlog' in kwargs:
self.searchlog = kwargs['searchlog']
kwargs.pop('searchlog')
self.params.update(kwargs)
@@ -152,7 +153,7 @@ def __init__(self, problem, targets, cmin, cmax, cstep=None, crossval=5,
def setParams(self, **kwargs):
""" set parameters for SVM training """
- if kwargs.has_key('weight'):
+ if 'weight' in kwargs:
self.params['nr_weight'] = len(kwargs['weight'])
self.params.update(kwargs)
@@ -262,7 +263,7 @@ def cmp (x, y):
x = dbarr[:, 0]
dimy = len(x[x == x[0]])
dimx = x.size / dimy
- print('plotting: ', dimx, dimy)
+ print(('plotting: ', dimx, dimy))
x = x.reshape(dimx, dimy)
y = dbarr[:, 1]
y = y.reshape(dimx, dimy)
diff --git a/pybrain/tests/__init__.py b/pybrain/tests/__init__.py
index 4b3ebb924..bfaf02a14 100644
--- a/pybrain/tests/__init__.py
+++ b/pybrain/tests/__init__.py
@@ -1,3 +1,3 @@
-from helpers import gradientCheck, buildAppropriateDataset, xmlInvariance, \
+from .helpers import gradientCheck, buildAppropriateDataset, xmlInvariance, \
epsilonCheck
-from testsuites import runModuleTestSuite
\ No newline at end of file
+from .testsuites import runModuleTestSuite
\ No newline at end of file
diff --git a/pybrain/tests/helpers.py b/pybrain/tests/helpers.py
index 3e16e7217..b8277f04f 100644
--- a/pybrain/tests/helpers.py
+++ b/pybrain/tests/helpers.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
from os import unlink, getcwd
@@ -62,12 +64,12 @@ def gradientCheck(module, tolerance=0.0001, dataset=None):
print('Perfect gradient')
return True
else:
- print('Incorrect gradient', precision)
+ print(('Incorrect gradient', precision))
if isinstance(module, Network):
index = 0
for m in module._containerIterator():
if max(precision[index:index + m.paramdim]) > tolerance:
- print('Incorrect module:', m, res[-1][index:index + m.paramdim])
+ print(('Incorrect module:', m, res[-1][index:index + m.paramdim]))
index += m.paramdim
else:
print(res)
@@ -83,7 +85,7 @@ def netCompare(net1, net2, forwardpasses=1, verbose=False):
identical = False
if verbose:
print(net2)
- print('-' * 80)
+ print(('-' * 80))
print(net1)
outN = zeros(net2.outdim)
@@ -110,8 +112,8 @@ def netCompare(net1, net2, forwardpasses=1, verbose=False):
else:
identical = False
if verbose:
- print(net2.__class__)
- print(net1.__class__)
+ print((net2.__class__))
+ print((net1.__class__))
return identical
diff --git a/pybrain/tests/optimizationtest.py b/pybrain/tests/optimizationtest.py
index d0e8576d7..794ea183d 100644
--- a/pybrain/tests/optimizationtest.py
+++ b/pybrain/tests/optimizationtest.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#! /usr/bin/env python
""" This test script will test the set of optimization algorithms.
@@ -220,24 +222,24 @@ def testOnEvolvable(algo):
def testAll(tests, allalgos, tolerant=True):
countgood = 0
for i, algo in enumerate(sorted(allalgos)):
- print("%d, %s:" % (i + 1, algo.__name__))
- print(' ' * int(log10(i + 1) + 2),)
+ print(("%d, %s:" % (i + 1, algo.__name__)))
+ print((' ' * int(log10(i + 1) + 2),))
good = True
messages = []
for t in tests:
try:
res = t(algo)
- except Exception, e:
+ except Exception as e:
if not tolerant:
raise e
res = e
if res is True:
- print('.',)
+ print(('.',))
else:
good = False
messages.append(res)
- print('F',)
+ print(('F',))
if good:
countgood += 1
print('--- OK.')
@@ -245,25 +247,24 @@ def testAll(tests, allalgos, tolerant=True):
print('--- NOT OK.')
for m in messages:
if m is not None:
- print(' ' * int(log10(i + 1) + 2), '->', m)
- print
- print('Summary:', countgood, '/', len(allalgos), 'of test were passed.')
+ print((' ' * int(log10(i + 1) + 2), '->', m))
+ print()
+ print(('Summary:', countgood, '/', len(allalgos), 'of test were passed.'))
if __name__ == '__main__':
from pybrain.optimization import * #@UnusedWildImport
#from pybrain.optimization import CMAES #@UnusedImport
- allalgos = filter(lambda c: (isclass(c)
+ allalgos = [c for c in list(globals().values()) if (isclass(c)
and issubclass(c, bbo.BlackBoxOptimizer)
and not issubclass(c, mobj.MultiObjectiveGA)
- ),
- globals().values())
+ )]
- print('Optimization algorithms to be tested:', len(allalgos))
- print
+ print(('Optimization algorithms to be tested:', len(allalgos)))
+ print()
print('Note: this collection of tests may take quite some time.')
- print
+ print()
tests = [testInterface,
testContinuousInterface,
diff --git a/pybrain/tests/runtests.py b/pybrain/tests/runtests.py
index f49a7f9a0..51dd22574 100644
--- a/pybrain/tests/runtests.py
+++ b/pybrain/tests/runtests.py
@@ -74,7 +74,7 @@ def make_test_suite():
# Initialize the testsuite to add to
suite = TestSuite()
- optionflags = doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE
+ optionflags = doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE | doctest.IGNORE_EXCEPTION_DETAIL
for testdir in testdirs:
# All unittest modules have to start with 'test_' and have to be, of
diff --git a/pybrain/tests/testsuites.py b/pybrain/tests/testsuites.py
index 3241d8f7f..3091592f3 100644
--- a/pybrain/tests/testsuites.py
+++ b/pybrain/tests/testsuites.py
@@ -14,10 +14,10 @@ def runModuleTestSuite(module):
suite = TestSuite([TestLoader().loadTestsFromModule(module)])
# Add local doctests
- optionflags = ELLIPSIS | NORMALIZE_WHITESPACE | REPORT_ONLY_FIRST_FAILURE
+ optionflags = ELLIPSIS | NORMALIZE_WHITESPACE | REPORT_ONLY_FIRST_FAILURE | IGNORE_EXCEPTION_DETAIL
try:
- suite.addTest(DocTestSuite(module, optionflags=optionflags))
+ suite.addTest(DocTestSuite(module), optionflags=optionflags)
except ValueError:
# No tests have been found in that module.
pass
diff --git a/pybrain/tests/unittests/_test_equivalence_to_ctypes.py b/pybrain/tests/unittests/_test_equivalence_to_ctypes.py
index 13b8fd5f5..3fb4e022b 100644
--- a/pybrain/tests/unittests/_test_equivalence_to_ctypes.py
+++ b/pybrain/tests/unittests/_test_equivalence_to_ctypes.py
@@ -95,7 +95,6 @@
__author__ = 'Tom Schaul, tom@idsia.ch'
_dependencies = ['arac']
-
from pybrain.tests.helpers import buildAppropriateDataset, epsilonCheck
from pybrain.tests import runModuleTestSuite
@@ -114,13 +113,13 @@ def testEquivalence(net):
if net.name == '22':
h = net['hidden0']
ch = cnet['hidden0']
- print('ni', input, net.inputbuffer.T)
- print('ci', input, cnet.inputbuffer.T)
- print('hni', h.inputbuffer.T[0])
- print('hci', ch.inputbuffer.T[0])
- print('hnout', h.outputbuffer.T[0])
- print('hcout', ch.outputbuffer.T[0])
- print
+ print(('ni', input, net.inputbuffer.T))
+ print(('ci', input, cnet.inputbuffer.T))
+ print(('hni', h.inputbuffer.T[0]))
+ print(('hci', ch.inputbuffer.T[0]))
+ print(('hnout', h.outputbuffer.T[0]))
+ print(('hcout', ch.outputbuffer.T[0]))
+ print()
else:
for input, _ in ds:
@@ -129,10 +128,10 @@ def testEquivalence(net):
if epsilonCheck(sum(res - cres), 0.001):
return True
else:
- print('in-net', net.inputbuffer.T)
- print('in-arac', cnet.inputbuffer.T)
- print('out-net', net.outputbuffer.T)
- print('out-arac', cnet.outputbuffer.T)
+ print(('in-net', net.inputbuffer.T))
+ print(('in-arac', cnet.inputbuffer.T))
+ print(('out-net', net.outputbuffer.T))
+ print(('out-arac', cnet.outputbuffer.T))
return (res, cres)
diff --git a/pybrain/tests/unittests/datasets/test_datasets_datasets.py b/pybrain/tests/unittests/datasets/test_datasets_datasets.py
index 96e8a521b..c43df78e0 100644
--- a/pybrain/tests/unittests/datasets/test_datasets_datasets.py
+++ b/pybrain/tests/unittests/datasets/test_datasets_datasets.py
@@ -143,7 +143,7 @@
__author__ = 'Justin Bayer, bayerj@in.tum.de'
-from cStringIO import StringIO
+from io import StringIO
from pybrain.tests import runModuleTestSuite
@@ -160,12 +160,12 @@ def saveInvariant(dataset):
equal = True
for (k, v), (k_, v_) in zip(orig_array_data, rec_array_data):
if k != k_:
- print("Differing keys: %s <=> %s" % (dataset.dataset.keys(),
- rec_array_data.dataset.keys()))
+ print(("Differing keys: %s <=> %s" % (list(dataset.dataset.keys()),
+ list(rec_array_data.dataset.keys()))))
equal = False
break
if not (v == v_).all():
- print("Differing values for %s" % k)
+ print(("Differing values for %s" % k))
print(v)
print(v_)
equal = False
diff --git a/pybrain/tests/unittests/optimization/populationbased/test_pso_ring.py b/pybrain/tests/unittests/optimization/populationbased/test_pso_ring.py
index d73ea9c83..6b89f1551 100644
--- a/pybrain/tests/unittests/optimization/populationbased/test_pso_ring.py
+++ b/pybrain/tests/unittests/optimization/populationbased/test_pso_ring.py
@@ -2,11 +2,11 @@
>>> from pybrain.optimization.populationbased.pso import ring
- >>> ring(range(9))
+ >>> ring(list(range(9)))
{0: (1, 8), 1: (2, 0), 2: (3, 1), 3: (4, 2), 4: (5, 3), 5: (6, 4), 6: (7, 5), 7: (8, 6), 8: (0, 7)}
Disabled:
- lattice(range(9))
+ lattice(list(range(9)))
{0: (1, 2),
1: (0, 3),
2: (0, 3),
diff --git a/pybrain/tests/unittests/rl/environments/twoplayergames/test_capture_game.py b/pybrain/tests/unittests/rl/environments/twoplayergames/test_capture_game.py
index 5e7aa5588..2ecb230ca 100644
--- a/pybrain/tests/unittests/rl/environments/twoplayergames/test_capture_game.py
+++ b/pybrain/tests/unittests/rl/environments/twoplayergames/test_capture_game.py
@@ -42,13 +42,13 @@
. . . . .
Winner: White (*) (moves done:8)
+ >>> correct = {(0, 1): 5, (1, 2): 2, (2, 1): 10, (0, 2): 2, (2, 0): 10, (1, 0): 5, (1, 1): 5}
+ >>> c.groups == correct
+ True
- >>> c.groups
- {(0, 1): 5, (1, 2): 2, (2, 1): 10, (0, 2): 2, (2, 0): 10, (1, 0): 5, (1, 1): 5}
-
- >>> c.liberties
- {2: set([(0, 3), (1, 3), (2, 2)]), 5: set([(0, 0)]), 10: set([(3, 0), (3, 1), (2, 2)])}
-
+ >>> correct = {2: set([(0, 3), (1, 3), (2, 2)]), 5: set([(0, 0)]), 10: set([(3, 0), (3, 1), (2, 2)])}
+ >>> c.liberties == correct
+ True
"""
__author__ = 'Tom Schaul, tom@idsia.ch'
diff --git a/pybrain/tests/unittests/rl/environments/twoplayergames/test_pente.py b/pybrain/tests/unittests/rl/environments/twoplayergames/test_pente.py
index 59e34430c..e4edc0df6 100644
--- a/pybrain/tests/unittests/rl/environments/twoplayergames/test_pente.py
+++ b/pybrain/tests/unittests/rl/environments/twoplayergames/test_pente.py
@@ -1,7 +1,6 @@
"""
Initialize a game of Pente.
-
>>> from pybrain.rl.environments.twoplayergames.pente import PenteGame
>>> dim = 5
>>> c = PenteGame((dim, dim))
diff --git a/pybrain/tests/unittests/structure/modules/test_peephole_lstm.py b/pybrain/tests/unittests/structure/modules/test_peephole_lstm.py
index 380f2062c..d18ee0b9d 100644
--- a/pybrain/tests/unittests/structure/modules/test_peephole_lstm.py
+++ b/pybrain/tests/unittests/structure/modules/test_peephole_lstm.py
@@ -55,7 +55,6 @@
from pybrain.structure import LinearLayer, IdentityConnection, LSTMLayer, RecurrentNetwork
from pybrain.tests.helpers import epsilonCheck
-
def buildMinimalLSTMNetwork():
N = RecurrentNetwork('simpleLstmNet')
i = LinearLayer(4, name='i')
@@ -74,7 +73,7 @@ def predictOutcome(net, input, output):
if epsilonCheck(res - output):
return True
else:
- print('expected:', round(output, 7), '- got:', round(res, 7))
+ print(('expected:', round(output, 7), '- got:', round(res, 7)))
return False
diff --git a/pybrain/tests/unittests/structure/modules/test_peephole_mdlstm.py b/pybrain/tests/unittests/structure/modules/test_peephole_mdlstm.py
index 51f3a6490..e360f40d0 100644
--- a/pybrain/tests/unittests/structure/modules/test_peephole_mdlstm.py
+++ b/pybrain/tests/unittests/structure/modules/test_peephole_mdlstm.py
@@ -2,7 +2,7 @@
>>> from pybrain.tests.helpers import epsilonCheck
>>> from pybrain.tools.functions import tanh
>>> from pybrain.utilities import fListToString
- >>> from test_peephole_lstm import predictOutcome
+ >>> from .test_peephole_lstm import predictOutcome
>>> from scipy import arctanh
>>> from random import random
diff --git a/pybrain/tests/unittests/structure/modules/test_simple_lstm_network.py b/pybrain/tests/unittests/structure/modules/test_simple_lstm_network.py
index db6099b06..ddc722db2 100644
--- a/pybrain/tests/unittests/structure/modules/test_simple_lstm_network.py
+++ b/pybrain/tests/unittests/structure/modules/test_simple_lstm_network.py
@@ -1,7 +1,6 @@
"""
Build a simple lstm network with peepholes:
-
>>> n = buildSimpleLSTMNetwork(True)
>>> print(n)
simpleLstmNet
diff --git a/pybrain/tests/unittests/structure/modules/test_simple_mdlstm.py b/pybrain/tests/unittests/structure/modules/test_simple_mdlstm.py
index 73af48d16..9819c1535 100644
--- a/pybrain/tests/unittests/structure/modules/test_simple_mdlstm.py
+++ b/pybrain/tests/unittests/structure/modules/test_simple_mdlstm.py
@@ -1,7 +1,6 @@
"""
Build a simple mdlstm network with peepholes:
-
>>> n = buildSimpleMDLSTMNetwork(True)
>>> print(n)
simpleMDLstmNet
diff --git a/pybrain/tests/unittests/structure/networks/custom/test_capturegame_network.py b/pybrain/tests/unittests/structure/networks/custom/test_capturegame_network.py
index a48c51246..e2d2e0b6b 100644
--- a/pybrain/tests/unittests/structure/networks/custom/test_capturegame_network.py
+++ b/pybrain/tests/unittests/structure/networks/custom/test_capturegame_network.py
@@ -1,6 +1,5 @@
"""
Build a CaptureGameNetwork with LSTM cells
-
>>> from pybrain.structure.networks.custom import CaptureGameNetwork
>>> from pybrain import MDLSTMLayer
>>> size = 2
@@ -18,10 +17,10 @@
Check some of the connections dimensionalities
>>> c1 = n.connections[n['hidden(1, 0, 3)']][0]
>>> c2 = n.connections[n['hidden(0, 1, 2)']][-1]
- >>> print(c1.indim, c1.outdim)
- 1 1
- >>> print(c2.indim, c2.outdim)
- 1 1
+ >>> print((c1.indim, c1.outdim))
+ (1, 1)
+ >>> print((c2.indim, c2.outdim))
+ (1, 1)
>>> n.paramdim
21
diff --git a/pybrain/tests/unittests/structure/networks/custom/test_convolutional_nets.py b/pybrain/tests/unittests/structure/networks/custom/test_convolutional_nets.py
index 4a2eebfcd..4dfc755c5 100644
--- a/pybrain/tests/unittests/structure/networks/custom/test_convolutional_nets.py
+++ b/pybrain/tests/unittests/structure/networks/custom/test_convolutional_nets.py
@@ -1,7 +1,6 @@
"""
Let's build a convolutional network designed for board games:
-
>>> from pybrain.structure.networks.custom.convboard import ConvolutionalBoardNetwork
>>> from scipy import array, ravel, var
>>> N = ConvolutionalBoardNetwork(4, 3, 5)
@@ -41,8 +40,11 @@
On the output, all the values should be distinct, except for two in the middle above
because a cluster-size of 3x3 makes their input look identical.
- >>> res[0,1] - res[0,2]
- 0.0
+ The prior output was 0.0. Need to test this differently due to
+ print precision differences in 2 and 3.
+ >>> import sys
+ >>> res[0,1] - res[0,2] < sys.float_info.epsilon
+ True
>>> res[0,1] == res[0,3]
False
diff --git a/pybrain/tests/unittests/structure/networks/test_network_decomposition.py b/pybrain/tests/unittests/structure/networks/test_network_decomposition.py
index e425d7eaa..fe05f2bf3 100644
--- a/pybrain/tests/unittests/structure/networks/test_network_decomposition.py
+++ b/pybrain/tests/unittests/structure/networks/test_network_decomposition.py
@@ -1,7 +1,6 @@
"""
Build a decomposable network
-
>>> n = buildDecomposableNetwork()
Check if it was built correctly
diff --git a/pybrain/tests/unittests/structure/networks/test_network_sort.py b/pybrain/tests/unittests/structure/networks/test_network_sort.py
index 22021bfa0..ba211aa3a 100644
--- a/pybrain/tests/unittests/structure/networks/test_network_sort.py
+++ b/pybrain/tests/unittests/structure/networks/test_network_sort.py
@@ -42,7 +42,6 @@
True
Is it the same ordering than our reference?
-
>>> print(ord3)
[, , , , , , , , , ]
@@ -63,7 +62,7 @@ def buildSomeModules(number = 4):
def buildSomeConnections(modules):
""" add a connection from every second to every third module """
res = []
- for i in range(len(modules)/3-1):
+ for i in range(len(modules)//3-1):
res.append(FullConnection(modules[i*2], modules[i*3+1]))
return res
diff --git a/pybrain/tests/unittests/structure/networks/test_recurrent_network.py b/pybrain/tests/unittests/structure/networks/test_recurrent_network.py
index dd0e38b9b..396b971c5 100644
--- a/pybrain/tests/unittests/structure/networks/test_recurrent_network.py
+++ b/pybrain/tests/unittests/structure/networks/test_recurrent_network.py
@@ -1,7 +1,6 @@
"""
Build a simple recurrent network:
-
>>> n = buildRecurrentNetwork()
>>> print(n)
RecurrentNetwork
diff --git a/pybrain/tests/unittests/supervised/knn/lsh/test_minhash.py b/pybrain/tests/unittests/supervised/knn/lsh/test_minhash.py
index 7f8fff257..2845edc6f 100644
--- a/pybrain/tests/unittests/supervised/knn/lsh/test_minhash.py
+++ b/pybrain/tests/unittests/supervised/knn/lsh/test_minhash.py
@@ -49,8 +49,13 @@
Some "introspection" to check if everything went right
- >>> m.buckets
- defaultdict( at ...>, {(0, 0): [(array([...True], dtype=bool), 'red')]})
+ Changed this test to work with python 2 AND 3.
+ Old correct output: defaultdict( at ...>, {(0, 0): [(array([...True], dtype=bool), 'red')]})
+
+ >>> import pprint
+ >>> pprint.pprint(dict(m.buckets))
+ {(0, 0): [(array([ True, True, True, True, True], dtype=bool), 'red')]}
+
>>> m._hash(array([1, 1, 0, 0, 0]))
(0, 0)
diff --git a/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py b/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
index 4840b6bcb..be0975923 100644
--- a/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
+++ b/pybrain/tests/unittests/supervised/knn/lsh/test_nearoptimal.py
@@ -119,4 +119,4 @@
from pybrain.supervised.knn.lsh import nearoptimal
if __name__ == "__main__":
- runModuleTestSuite(__import__('__main__'))
+ runModuleTestSuite(__import__('__main__'))
diff --git a/pybrain/tests/unittests/tools/test_rlgluebridge.py b/pybrain/tests/unittests/tools/test_rlgluebridge.py
index ffb83064b..0ea3e1a59 100644
--- a/pybrain/tests/unittests/tools/test_rlgluebridge.py
+++ b/pybrain/tests/unittests/tools/test_rlgluebridge.py
@@ -116,11 +116,11 @@ class PseudoPybrainAgent(LearningAgent):
def __init__(self): pass
def integrateObservation(self, obs):
- print("I saw %s" % obs)
+ print(("I saw %s" % obs))
def getAction(self):
action = array([2.7, -1])
- print("I did %s" % action)
+ print(("I did %s" % action))
return action
def giveReward(self, r):
@@ -130,7 +130,7 @@ def giveReward(self, r):
negative
:type r: double
"""
- print("I was given %.2f" % float(r))
+ print(("I was given %.2f" % float(r)))
def newEpisode(self):
print("I got a new episode")
diff --git a/pybrain/tools/aptativeresampling.py b/pybrain/tools/aptativeresampling.py
index 19eca3b04..10dea295f 100644
--- a/pybrain/tools/aptativeresampling.py
+++ b/pybrain/tools/aptativeresampling.py
@@ -1,9 +1,10 @@
+from __future__ import print_function
+
__author__ = "Tom Schaul, tom@idsia.ch"
from scipy import median
-
class AdaptiveResampler(object):
""" A simplified version of the uncertainty handling method described in
Hansen, Niederberger, Guzzella and Koumoutsakos, 2009."""
@@ -53,14 +54,14 @@ def testnes():
fun2 = AdaptiveResampler(fun, 10)
l = XNES(fun, x0, maxEvaluations=1100, storeAllEvaluations=True)
res = l.learn()
- print(sum(res[0]**2) )
- pylab.plot(map(abs, l._allEvaluations))
+ print((sum(res[0]**2) ))
+ pylab.plot(list(map(abs, l._allEvaluations)))
l2 = XNES(fun2, x0, maxEvaluations=1100, storeAllEvaluations=True)
res = l2.learn()
- print(sum(res[0]**2) )
- print(fun2.resample_over)
- pylab.plot(map(abs,l2._allEvaluations))
+ print((sum(res[0]**2) ))
+ print((fun2.resample_over))
+ pylab.plot(list(map(abs,l2._allEvaluations)))
pylab.semilogy()
pylab.show()
diff --git a/pybrain/tools/customxml/__init__.py b/pybrain/tools/customxml/__init__.py
index 21da7bbda..c8a8d87ec 100644
--- a/pybrain/tools/customxml/__init__.py
+++ b/pybrain/tools/customxml/__init__.py
@@ -1,2 +1,2 @@
-from networkreader import NetworkReader
-from networkwriter import NetworkWriter
+from .networkreader import NetworkReader
+from .networkwriter import NetworkWriter
diff --git a/pybrain/tools/customxml/handling.py b/pybrain/tools/customxml/handling.py
index 2260dee08..237cbad2b 100644
--- a/pybrain/tools/customxml/handling.py
+++ b/pybrain/tools/customxml/handling.py
@@ -19,7 +19,7 @@ def __init__(self, filename, newfile):
if not newfile:
self.dom = parse(filename)
if self.dom.firstChild.nodeName != 'PyBrain':
- raise Exception, 'Not a correct PyBrain XML file'
+ raise Exception('Not a correct PyBrain XML file')
else:
domimpl = getDOMImplementation()
self.dom = domimpl.createDocument(None, 'PyBrain', None)
@@ -34,7 +34,7 @@ def readAttrDict(self, node, transform = None):
""" read a dictionnary of attributes
:key transform: optionally function transforming the attribute values on reading """
args = {}
- for name, val in node.attributes.items():
+ for name, val in list(node.attributes.items()):
name = str(name)
if transform != None:
args[name] = transform(val, name)
@@ -46,7 +46,7 @@ def writeAttrDict(self, node, adict, transform = None):
""" read a dictionnary of attributes
:key transform: optionally transform the attribute values on writing """
- for name, val in adict.items():
+ for name, val in list(adict.items()):
if val != None:
if transform != None:
node.setAttribute(name, transform(val, name))
@@ -74,7 +74,7 @@ def getChild(self, node, name):
def getChildrenOf(self, node):
""" get the element children """
- return filter(lambda x: x.nodeType == x.ELEMENT_NODE, node.childNodes)
+ return [x for x in node.childNodes if x.nodeType == x.ELEMENT_NODE]
def findNode(self, name, index = 0, root = None):
""" return the toplevel node with the provided name (if there are more, choose the
@@ -114,7 +114,7 @@ def writeMatrix(self, node, m, precision = 6):
def readDoubles(self, node):
dstrings = string.split(node.firstChild.data)
- return map(lambda s: float(s), dstrings)
+ return [float(s) for s in dstrings]
def readMatrix(self, node):
rows = []
diff --git a/pybrain/tools/customxml/networkreader.py b/pybrain/tools/customxml/networkreader.py
index 8f23be741..9462a5138 100644
--- a/pybrain/tools/customxml/networkreader.py
+++ b/pybrain/tools/customxml/networkreader.py
@@ -1,7 +1,9 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
-from handling import XMLHandling
+from .handling import XMLHandling
# those imports are necessary for the eval() commands to find the right classes
import pybrain #@UnusedImport
@@ -89,8 +91,8 @@ def readBuildable(self, node):
try:
m = eval(mclass)(**argdict)
except:
- print('Could not construct', mclass)
- print('with arguments:', argdict)
+ print(('Could not construct', mclass))
+ print(('with arguments:', argdict))
return None
m.name = node.getAttribute('name')
self.readParams(node, m)
@@ -109,8 +111,7 @@ def readArgs(self, node):
return res
def readParams(self, node, m):
- import string
pnode = self.getChild(node, 'Parameters')
if pnode:
- params = eval(string.strip(pnode.firstChild.data))
+ params = eval(pnode.firstChild.data.strip())
m._setParameters(params)
diff --git a/pybrain/tools/customxml/networkwriter.py b/pybrain/tools/customxml/networkwriter.py
index 37b42871d..c5ffc5272 100644
--- a/pybrain/tools/customxml/networkwriter.py
+++ b/pybrain/tools/customxml/networkwriter.py
@@ -2,7 +2,7 @@
from inspect import isclass
-from handling import XMLHandling
+from .handling import XMLHandling
from pybrain.structure.connections.shared import SharedConnection
from pybrain.structure.networks.network import Network
from pybrain.structure.networks.recurrent import RecurrentNetwork
@@ -96,7 +96,7 @@ def writeBuildable(self, rootnode, m):
def writeArgs(self, node, argdict):
""" write a dictionnary of arguments """
- for name, val in argdict.items():
+ for name, val in list(argdict.items()):
if val != None:
tmp = self.newChild(node, name)
if isclass(val):
diff --git a/pybrain/tools/datasets/mnist.py b/pybrain/tools/datasets/mnist.py
index 06c7bf52d..198d49e80 100644
--- a/pybrain/tools/datasets/mnist.py
+++ b/pybrain/tools/datasets/mnist.py
@@ -10,7 +10,7 @@ def labels(filename):
fp = file(filename)
magicnumber, length = struct.unpack('>ii', fp.read(8))
assert magicnumber in (2049, 2051), ("Not an MNIST file: %i" % magicnumber)
- for _ in xrange(length):
+ for _ in range(length):
label, = struct.unpack('B', fp.read(1))
yield label
@@ -21,7 +21,7 @@ def images(filename):
magicnumber, length, numrows, numcols = struct.unpack('>iiii', chunk)
assert magicnumber in (2049, 2051), ("Not an MNIST file: %i" % magicnumber)
imagesize = numrows * numcols
- for _ in xrange(length):
+ for _ in range(length):
imagestring = fp.read(imagesize)
image = struct.unpack('B' * imagesize, imagestring)
yield scipy.array(image)
@@ -42,7 +42,7 @@ def makeMnistDataSets(path):
test_images = images(test_image_file)
test_labels = (flaggedArrayByIndex(l, 10) for l in labels(test_label_file))
- for image, label in itertools.izip(test_images, test_labels):
+ for image, label in zip(test_images, test_labels):
test.addSample(image, label)
train = SupervisedDataSet(28 * 28, 10)
@@ -50,7 +50,7 @@ def makeMnistDataSets(path):
train_label_file = os.path.join(path, 'train-labels-idx1-ubyte')
train_images = images(train_image_file)
train_labels = (flaggedArrayByIndex(l, 10) for l in labels(train_label_file))
- for image, label in itertools.izip(train_images, train_labels):
+ for image, label in zip(train_images, train_labels):
train.addSample(image, label)
return train, test
diff --git a/pybrain/tools/datasettools.py b/pybrain/tools/datasettools.py
index 601fd14ee..1accb44b3 100644
--- a/pybrain/tools/datasettools.py
+++ b/pybrain/tools/datasettools.py
@@ -1,8 +1,9 @@
+from __future__ import print_function
+
# This tool converts a sequential data set into a number of equally sized windows,
# to be used for supervised training.
__author__ = "Martin Felder"
-
from numpy import r_, array, isfinite
from pybrain.datasets import SequentialDataSet
@@ -22,7 +23,7 @@ def convertSequenceToTimeWindows(DSseq, NewClass, winsize):
nsamples = 0
nseqs = 0
si = r_[DSseq['sequence_index'].flatten(), DSseq.endmarker['sequence_index']]
- for i in xrange(DSseq.getNumSequences()):
+ for i in range(DSseq.getNumSequences()):
# get one sequence as arrays
input = DSseq['input'][si[i]:si[i + 1], :]
target = DSseq['target'][si[i]:si[i + 1], :]
@@ -34,10 +35,10 @@ def convertSequenceToTimeWindows(DSseq, NewClass, winsize):
DSwin.addSample(inp_win.flatten(), tar_win.flatten())
nsamples += 1
##print("added sample %d from sequence %d: %d - %d" %( nsamples, nseqs, k-winsize, k-1))
- print("samples in original dataset: ", len(DSseq))
- print("window size * nsamples = ", winsize * nsamples)
- print("total data points in original data: ", len(DSseq) * DSseq.indim)
- print("total data points in windowed dataset: ", len(DSwin) * DSwin.indim)
+ print(("samples in original dataset: ", len(DSseq)))
+ print(("window size * nsamples = ", winsize * nsamples))
+ print(("total data points in original data: ", len(DSseq) * DSseq.indim))
+ print(("total data points in windowed dataset: ", len(DSwin) * DSwin.indim))
return DSwin
def windowSequenceEval(DS, winsz, result):
@@ -67,10 +68,10 @@ def windowSequenceEval(DS, winsz, result):
x.append(curr_x)
seq_res.append(100. * correct / (correct + wrong))
- print("sequence %d correct: %g12.2%%" % (i, seq_res[-1]))
+ print(("sequence %d correct: %g12.2%%" % (i, seq_res[-1])))
seq_res = array(seq_res)
- print("total fraction of correct sequences: ", 100. * float((seq_res >= 0.5).sum()) / seq_res.size)
+ print(("total fraction of correct sequences: ", 100. * float((seq_res >= 0.5).sum()) / seq_res.size))
class DataSetNormalizer(object):
diff --git a/pybrain/tools/example_tools.py b/pybrain/tools/example_tools.py
index fdfb0a33c..d7522bff7 100644
--- a/pybrain/tools/example_tools.py
+++ b/pybrain/tools/example_tools.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
#########################################################################
# Reinforcement Tools for printing, saving and loading for RL examples
#
@@ -6,7 +8,7 @@
# Author: Frank Sehnke, sehnke@in.tum.de
#########################################################################
-from cPickle import load, dump
+from pickle import load, dump
from scipy import array, sqrt
from pylab import errorbar, show
@@ -49,20 +51,20 @@ def printResults(self,resList, runs, updates):
if self.kind == "optimizer":
rLen = len(resList)
avReward = array(resList).sum()/rLen
- print("Parameters:", self.agent.learner._bestFound())
- print("Experiment:", runs,
+ print(("Parameters:", self.agent.learner._bestFound()))
+ print(("Experiment:", runs,
" Evaluation:", (updates+1)*self.batch*self.prnts,
" BestReward:", self.agent.learner.bestEvaluation,
- " AverageReward:", avReward)
+ " AverageReward:", avReward))
print()
self.rl.append(avReward)
else:
avReward = resList
#print("Parameters: ", self.agent.learner._bestFound())
- print(
+ print((
"Step: ", runs, "/", (updates+1)*self.batch*self.prnts,
#"Best: ", self.agent.learner.bestEvaluation,
- "Base: ", avReward)
+ "Base: ", avReward))
#print()
self.rl.append(avReward)
@@ -78,5 +80,5 @@ def showExps(self):
v = (d**2).sum(axis=0)
v = v/nEx
stand = sqrt(v)
- errorbar(array(range(len(self.rll[0])))*self.prnts*self.batch+self.prnts*self.batch,r,stand)
+ errorbar(array(list(range(len(self.rll[0]))))*self.prnts*self.batch+self.prnts*self.batch,r,stand)
show()
diff --git a/pybrain/tools/filehandling.py b/pybrain/tools/filehandling.py
index bd30e7429..2c6be9b1b 100644
--- a/pybrain/tools/filehandling.py
+++ b/pybrain/tools/filehandling.py
@@ -1,9 +1,10 @@
+from __future__ import print_function
+
__author__ = 'Tom Schaul, tom@idsia.ch'
import os
import pickle
-
def getAllFilesIn(dir, tag='', extension='.pickle'):
""" return a list of all filenames in the specified directory
(with the given tag and/or extension). """
@@ -45,8 +46,8 @@ def pickleDumpDict(name, d):
pickle.dump(d, f)
f.close()
return True
- except Exception, e:
- print('Error writing into', name, ':', str(e))
+ except Exception as e:
+ print(('Error writing into', name, ':', str(e)))
return False
@@ -56,15 +57,15 @@ def pickleReadDict(name):
f = open(name + '.pickle')
val = pickle.load(f)
f.close()
- except Exception, e:
- print('Nothing read from', name, ':', str(e))
+ except Exception as e:
+ print(('Nothing read from', name, ':', str(e)))
val = {}
return val
def addToDictFile(name, key, data, verbose=False):
if verbose:
- print('.',)
+ print(('.',))
d = pickleReadDict(name)
if key not in d:
d[key] = []
diff --git a/pybrain/tools/fisher.py b/pybrain/tools/fisher.py
index b4a5167e1..1169ad3b4 100644
--- a/pybrain/tools/fisher.py
+++ b/pybrain/tools/fisher.py
@@ -35,7 +35,7 @@ def calcInvFisher(sigma, invSigma=None, factorSigma=None):
invF = [mat(1 / (invSigma[-1, -1] + factorSigma[-1, -1] ** -2))]
invD = 1 / invSigma[-1, -1]
- for k in reversed(range(dim - 1)):
+ for k in reversed(list(range(dim - 1))):
v = invSigma[k + 1:, k]
w = invSigma[k, k]
wr = w + factorSigma[k, k] ** -2
diff --git a/pybrain/tools/functions.py b/pybrain/tools/functions.py
index f70128327..8afdb4016 100644
--- a/pybrain/tools/functions.py
+++ b/pybrain/tools/functions.py
@@ -22,7 +22,7 @@ def f(val):
else:
# linear function for x>=0
return val + 1.0
- return array(map(f, x)).reshape(shape)
+ return array(list(map(f, x))).reshape(shape)
def semilinearPrime(x):
@@ -43,7 +43,7 @@ def f(val):
else:
# linear function for x>=0
return 1.0
- return array(map(f, x)).reshape(shape)
+ return array(list(map(f, x))).reshape(shape)
def safeExp(x):
@@ -72,7 +72,7 @@ def ranking(R):
""" Produces a linear ranking of the values in R. """
l = sorted(list(enumerate(R)), cmp=lambda a, b: cmp(a[1], b[1]))
l = sorted(list(enumerate(l)), cmp=lambda a, b: cmp(a[1], b[1]))
- return array(map(lambda kv: kv[0], l))
+ return array([kv[0] for kv in l])
def expln(x):
@@ -86,7 +86,7 @@ def f(val):
# natural log function for x >= 0
return log(val + 1.0) + 1
try:
- result = array(map(f, x))
+ result = array(list(map(f, x)))
except TypeError:
result = array(f(x))
@@ -104,7 +104,7 @@ def f(val):
# linear function for x>=0
return 1.0 / (val + 1.0)
try:
- result = array(map(f, x))
+ result = array(list(map(f, x)))
except TypeError:
result = array(f(x))
diff --git a/pybrain/tools/gridsearch.py b/pybrain/tools/gridsearch.py
index 7afc3a613..7faa298ae 100644
--- a/pybrain/tools/gridsearch.py
+++ b/pybrain/tools/gridsearch.py
@@ -1,5 +1,6 @@
-__author__ = 'Michael Isik'
+from __future__ import print_function
+__author__ = 'Michael Isik'
from pybrain.tools.validation import CrossValidator
from numpy import linspace, append, ones, zeros, array, where, apply_along_axis
@@ -46,7 +47,7 @@ def setArgs(self, **kwargs):
""" :key **kwargs:
verbosity : set verbosity
"""
- for key, value in kwargs.items():
+ for key, value in list(kwargs.items()):
if key in ("verbose", "verbosity", "ver", "v"):
self._verbosity = value
@@ -70,12 +71,12 @@ def search(self):
perf = self._validate(params)
perfs[params] = perf
if self._verbosity > 0:
- print("validated:", params, " performance = ", perf)
+ print(("validated:", params, " performance = ", perf))
self._onStep()
- max_idx = array(perfs.values()).argmax()
- return perfs.keys()[max_idx]
+ max_idx = array(list(perfs.values())).argmax()
+ return list(perfs.keys())[max_idx]
def _validate(self, params):
""" Abstract validation method. Should validate the supplied metaparameters,
@@ -177,7 +178,7 @@ def setArgs(self, **kwargs):
""" :key **kwargs:
verbosity : set verbosity
"""
- for key, value in kwargs.items():
+ for key, value in list(kwargs.items()):
if key in ("verbose", "ver", "v"):
self._verbosity = value
@@ -195,9 +196,9 @@ def search(self):
max_idx = local_perf.argmax()
center = grid[max_idx]
if self._verbosity > 0:
- print
- print("Found maximum at:", center, " performance = ", local_perf[max_idx])
- print
+ print()
+ print(("Found maximum at:", center, " performance = ", local_perf[max_idx]))
+ print()
return center
@@ -207,7 +208,7 @@ def _validateWrapper(self, params):
"""
perf = self._validate(params)
if self._verbosity > 0:
- print("validated:", params, " performance = ", perf)
+ print(("validated:", params, " performance = ", perf))
self._performances[tuple(params)] = perf
return perf
@@ -279,7 +280,7 @@ def setArgs(self, **kwargs):
max_epochs: Maximum number of epochs for training
verbosity : set verbosity
"""
- for key, value in kwargs.items():
+ for key, value in list(kwargs.items()):
if key in ("folds", "nfolds"):
self._n_folds = int(value)
elif key in ("max_epochs"):
@@ -323,7 +324,7 @@ def __init__(self, trainer, dataset, min_params=[-5, -15], max_params=[15, 3], n
def setArgs(self, **kwargs):
""" See GridSearchCostGamma """
- for key, value in kwargs.items():
+ for key, value in list(kwargs.items()):
if key in ("folds", "nfolds"):
self._n_folds = int(value)
elif key in ("max_epochs"):
@@ -337,7 +338,7 @@ def _validate(self, params):
glob_idx = tuple(params)
perf = self._performances
- if not perf.has_key(glob_idx):
+ if glob_idx not in perf:
trainer = self._getTrainerForParams(params)
local_perf = CrossValidator(trainer, self._dataset, self._n_folds, **self._validator_kwargs).validate()
perf[glob_idx] = local_perf
diff --git a/pybrain/tools/ibp.py b/pybrain/tools/ibp.py
index 3f214178b..52985360f 100644
--- a/pybrain/tools/ibp.py
+++ b/pybrain/tools/ibp.py
@@ -20,7 +20,7 @@ def generateIBP(customers, alpha=10, reducedprop=1.):
This implementation uses a stick-breaking construction.
An additional parameter permits reducing the expected number of times a dish is tried. """
# max number of dishes is distributed according to Poisson(alpha*sum(1/i))
- _lambda = alpha * sum(1. / array(range(1, customers + 1)))
+ _lambda = alpha * sum(1. / array(list(range(1, customers + 1))))
alpha /= reducedprop
# we give it 2 standard deviations as cutoff
diff --git a/pybrain/tools/kwargsprocessor.py b/pybrain/tools/kwargsprocessor.py
index 1c5a0203f..0cb4601b9 100644
--- a/pybrain/tools/kwargsprocessor.py
+++ b/pybrain/tools/kwargsprocessor.py
@@ -1,5 +1,6 @@
-__author__ = 'Michael Isik'
+from __future__ import print_function
+__author__ = 'Michael Isik'
class KWArgDsc(object):
def __init__(self, name, **kwargs):
@@ -8,7 +9,7 @@ def __init__(self, name, **kwargs):
self.mandatory = False
keys = ['private', 'default', 'mandatory']
for key in keys:
- if kwargs.has_key(key):
+ if key in kwargs:
setattr(self, key, kwargs[key])
assert not (self.mandatory and self.hasDefault())
@@ -36,7 +37,7 @@ def add(self, name, **kwargs):
attrname = name
# set the objects attribute
- if self._obj_kwargs.has_key(name):
+ if name in self._obj_kwargs:
# set attribute supplied value
setattr(self._object, attrname, self._obj_kwargs[name])
elif kwargDsc.hasDefault():
@@ -66,20 +67,20 @@ def __str__(self):
return str(dict(self.__dict__))
c1 = C(m=1)
- print('c1 =', c1)
+ print(('c1 =', c1))
c2 = C(m=1, a=1, b=2)
- print('c2 =', c2)
+ print(('c2 =', c2))
c3 = C(m=1, simple="hallo", a=11, b=22, c=55)
- print('c3 =', c3)
+ print(('c3 =', c3))
- print("\nc3.b = ", c3.b)
+ print(("\nc3.b = ", c3.b))
try:
C() # will raise KeyError because mandatory keyword argument "m" is missing
- except KeyError, k:
+ except KeyError as k:
print(k)
diff --git a/pybrain/tools/mixtures/mogpuremax.py b/pybrain/tools/mixtures/mogpuremax.py
index 05ff1e461..9d2eac118 100644
--- a/pybrain/tools/mixtures/mogpuremax.py
+++ b/pybrain/tools/mixtures/mogpuremax.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Frank Sehnke, sehnke@in.tum.de'
@@ -103,7 +105,7 @@ def getSample(self):
if abs(n) > 5.0: n = 0.0
y = (20.0 + n ** 2 - 10.0 * cos(2.0 * 3.1416 * n)) / 55.0 + random.normal(0, 0.2) #one dimensional rastrigin
m.learn(x, y)
- print(m.alpha)
- print(m.mue)
- print(m.sigma)
+ print((m.alpha))
+ print((m.mue))
+ print((m.sigma))
diff --git a/pybrain/tools/networking/udpconnection.py b/pybrain/tools/networking/udpconnection.py
index 0b17ff4c6..4ab6c14b0 100644
--- a/pybrain/tools/networking/udpconnection.py
+++ b/pybrain/tools/networking/udpconnection.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
__author__ = 'Frank Sehnke, sehnke@in.tum.de'
#############################################################################################################
@@ -38,14 +40,14 @@ def __init__(self, ip="127.0.0.1", port="21560", buf="1024"):
self.cIP = []
self.addrList = []
self.UDPOutSockList = []
- print("listening on port", self.inPort)
+ print(("listening on port", self.inPort))
# Adding a client to the list
def addClient(self, cIP):
self.cIP.append(cIP)
self.addrList.append((cIP, self.outPort))
self.UDPOutSockList.append(socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
- print("client", cIP, "connected")
+ print(("client", cIP, "connected"))
self.clients += 1
# Listen for clients
@@ -76,7 +78,7 @@ def listen(self):
self.cIP = []
self.addrList = []
self.UDPOutSockList = []
- print("listening on port", self.inPort)
+ print(("listening on port", self.inPort))
# Sending the actual data too all clients
@@ -115,7 +117,7 @@ def listen(self, arrayList=None):
arrayList = eval(data)
return arrayList
except:
- print("Unsupported data format received from", self.outAddr, "!")
+ print(("Unsupported data format received from", self.outAddr, "!"))
return None
except:
diff --git a/pybrain/tools/nondominated.py b/pybrain/tools/nondominated.py
index bda2054f5..9f0fb7d53 100644
--- a/pybrain/tools/nondominated.py
+++ b/pybrain/tools/nondominated.py
@@ -11,14 +11,14 @@ def crowding_distance(individuals, fitnesses):
individuals = list(individuals)
# Infer the number of objectives by looking at the fitness of the first.
n_obj = len(fitnesses[individuals[0]])
- for i in xrange(n_obj):
+ for i in range(n_obj):
individuals.sort(key=lambda x: fitnesses[x][i])
# normalization between 0 and 1.
normalization = float(fitnesses[individuals[0]][i] - fitnesses[individuals[-1]][i])
# Make sure the boundary points are always selected.
distances[individuals[0]] = 1e100
distances[individuals[-1]] = 1e100
- tripled = zip(individuals, individuals[1:-1], individuals[2:])
+ tripled = list(zip(individuals, individuals[1:-1], individuals[2:]))
for pre, ind, post in tripled:
distances[ind] += (fitnesses[pre][i] - fitnesses[post][i]) / normalization
return distances
@@ -29,8 +29,8 @@ def _non_dominated_front_old(iterable, key=lambda x: x, allowequality=True):
other item in iterable."""
items = list(iterable)
keys = dict((i, key(i)) for i in items)
- dim = len(keys.values()[0])
- if any(dim != len(k) for k in keys.values()):
+ dim = len(list(keys.values())[0])
+ if any(dim != len(k) for k in list(keys.values())):
raise ValueError("Wrong tuple size.")
# Make a dictionary that holds the items another item dominates.
@@ -38,10 +38,10 @@ def _non_dominated_front_old(iterable, key=lambda x: x, allowequality=True):
for i in items:
for j in items:
if allowequality:
- if all(keys[i][k] < keys[j][k] for k in xrange(dim)):
+ if all(keys[i][k] < keys[j][k] for k in range(dim)):
dominations[i].append(j)
else:
- if all(keys[i][k] <= keys[j][k] for k in xrange(dim)):
+ if all(keys[i][k] <= keys[j][k] for k in range(dim)):
dominations[i].append(j)
dominates = lambda i, j: j in dominations[i]
@@ -69,18 +69,18 @@ def _non_dominated_front_fast(iterable, key=lambda x: x, allowequality=True):
"""
items = list(iterable)
keys = dict((i, key(i)) for i in items)
- dim = len(keys.values()[0])
+ dim = len(list(keys.values())[0])
dominations = {}
for i in items:
for j in items:
good = True
if allowequality:
- for k in xrange(dim):
+ for k in range(dim):
if keys[i][k] >= keys[j][k]:
good = False
break
else:
- for k in xrange(dim):
+ for k in range(dim):
if keys[i][k] > keys[j][k]:
good = False
break
@@ -105,9 +105,9 @@ def _non_dominated_front_merge(iterable, key=lambda x: x, allowequality=True):
items = list(iterable)
l = len(items)
if l > 20:
- part1 = list(_non_dominated_front_merge(items[:l / 2], key, allowequality))
- part2 = list(_non_dominated_front_merge(items[l / 2:], key, allowequality))
- if len(part1) >= l / 3 or len(part2) >= l / 3:
+ part1 = list(_non_dominated_front_merge(items[:l // 2], key, allowequality))
+ part2 = list(_non_dominated_front_merge(items[l // 2:], key, allowequality))
+ if len(part1) >= l // 3 or len(part2) >= l // 3:
return _non_dominated_front_fast(part1 + part2, key, allowequality)
else:
return _non_dominated_front_merge(part1 + part2, key, allowequality)
@@ -122,7 +122,7 @@ def _non_dominated_front_arr(iterable, key=lambda x: x, allowequality=True):
Faster version, based on boolean matrix manipulations.
"""
items = list(iterable)
- fits = map(key, items)
+ fits = list(map(key, items))
l = len(items)
x = array(fits)
a = tile(x, (l, 1, 1))
@@ -143,7 +143,7 @@ def _non_dominated_front_arr(iterable, key=lambda x: x, allowequality=True):
break
elif not ndom[ii, ij]:
res.remove(ij)
- return set(map(lambda i: items[i], res))
+ return set([items[i] for i in res])
def _non_dominated_front_merge_arr(iterable, key=lambda x: x, allowequality=True):
@@ -180,9 +180,9 @@ def _const_non_dominated_front_merge_arr(iterable, key=lambda x: x, allowequalit
items = list(iterable)
l = len(items)
if l > 100:
- part1 = list(_const_non_dominated_front_merge_arr(items[:l / 2], key, allowequality))
- part2 = list(_const_non_dominated_front_merge_arr(items[l / 2:], key, allowequality))
- if len(part1) >= l / 3 or len(part2) >= l / 3:
+ part1 = list(_const_non_dominated_front_merge_arr(items[:l // 2], key, allowequality))
+ part2 = list(_const_non_dominated_front_merge_arr(items[l // 2:], key, allowequality))
+ if len(part1) >= l // 3 or len(part2) >= l // 3:
return _const_non_dominated_front_arr(part1 + part2, key, allowequality)
else:
return _const_non_dominated_front_merge_arr(part1 + part2, key, allowequality)
@@ -197,7 +197,7 @@ def _const_non_dominated_front_arr(iterable, key=lambda x: x, allowequality=True
"""
items = list(iterable) # pop
- fits = map(key, items) # fitness
+ fits = list(map(key, items)) # fitness
x = array([fits[i][0] for i in range(len(fits))])
v = array([fits[i][1] for i in range(len(fits))])
@@ -236,7 +236,7 @@ def _const_non_dominated_front_arr(iterable, key=lambda x: x, allowequality=True
res.remove(ii)
break
- return set(map(lambda i: items[i], res))
+ return set([items[i] for i in res])
const_non_dominated_front = _const_non_dominated_front_merge_arr
@@ -260,14 +260,14 @@ def const_crowding_distance(individuals, fitnesses):
# Infer the number of objectives by looking at the fitness of the first.
n_obj = len(fitnesses[individuals[0]][0])
- for i in xrange(n_obj):
+ for i in range(n_obj):
individuals.sort(key=lambda x: fitnesses[x][0][i])
# normalization between 0 and 1.
normalization = float(fitnesses[individuals[0]][0][i] - fitnesses[individuals[-1]][0][i])
# Make sure the boundary points are always selected.
distances[individuals[0]] = 1e100
distances[individuals[-1]] = 1e100
- tripled = zip(individuals, individuals[1:-1], individuals[2:])
+ tripled = list(zip(individuals, individuals[1:-1], individuals[2:]))
for pre, ind, post in tripled:
distances[ind] += (fitnesses[pre][0][i] - fitnesses[post][0][i]) / normalization
return distances
@@ -280,7 +280,7 @@ def const_number_of_feasible_pop(iterable, key=lambda x: x, allowequality=True):
"""
items = list(iterable) # pop
- fits = map(key, items) # fitness
+ fits = list(map(key, items)) # fitness
v = list([fits[i][1] for i in range(len(fits))])
n = v.count(True)
diff --git a/pybrain/tools/plotting/__init__.py b/pybrain/tools/plotting/__init__.py
index ba5335db9..416557090 100644
--- a/pybrain/tools/plotting/__init__.py
+++ b/pybrain/tools/plotting/__init__.py
@@ -1,4 +1,4 @@
#from fitnesslandscapes import FitnessPlotter
-from multiline import MultilinePlotter
-from colormaps import ColorMap
-from fitnessprogression import plotFitnessProgession
\ No newline at end of file
+from .multiline import MultilinePlotter
+from .colormaps import ColorMap
+from .fitnessprogression import plotFitnessProgession
\ No newline at end of file
diff --git a/pybrain/tools/plotting/ciaoplot.py b/pybrain/tools/plotting/ciaoplot.py
index fbdd94066..9330167a3 100644
--- a/pybrain/tools/plotting/ciaoplot.py
+++ b/pybrain/tools/plotting/ciaoplot.py
@@ -2,7 +2,7 @@
from scipy import zeros, array, amin, amax, sqrt
-from colormaps import ColorMap
+from .colormaps import ColorMap
class CiaoPlot(ColorMap):
""" CIAO plot of coevolution performance with respect to the best
@@ -40,7 +40,7 @@ def __init__(self, evaluator, hof1, hof2, **args):
if __name__ == '__main__':
- x = array(range(100))
+ x = array(list(range(100)))
h1 = x * 4
h2 = x + 20 * sqrt(x)
def evo(x, y):
diff --git a/pybrain/tools/plotting/classification.py b/pybrain/tools/plotting/classification.py
index fe792535f..423448809 100644
--- a/pybrain/tools/plotting/classification.py
+++ b/pybrain/tools/plotting/classification.py
@@ -30,7 +30,7 @@ def plot_module_classification_sequence_performance(module, dataset, sequence_in
out = module.activate(sample[0])
outputs.append(out)
valid_output.append(out[sample[1].argmax()])
- plt.fill_between(range(len(valid_output)), 1, valid_output, facecolor='k', alpha=0.8)
+ plt.fill_between(list(range(len(valid_output))), 1, valid_output, facecolor='k', alpha=0.8)
plt.plot(outputs, linewidth=4, alpha=0.7)
plt.yticks(bounds)
@@ -84,7 +84,7 @@ def calculate_module_output_mean(module, inputs):
confidence_s = []
correct = 0
- for seq_i in xrange(num_sequences):
+ for seq_i in range(num_sequences):
seq = dataset.getSequence(seq_i)
outputs_mean = calculate_module_output_mean(module, seq[0])
actual.append(np.argmax(outputs_mean))
@@ -98,7 +98,7 @@ def calculate_module_output_mean(module, inputs):
plt.title('{}% Correct Classification (red dots mean bad classification)'.format(correct * 100 / num_sequences))
plt.xlabel('Sequence')
plt.ylabel('Class')
- plt.scatter(range(num_sequences), expected, s=s, c='r', linewidths=0)
- plt.scatter(range(num_sequences), actual, s=s, c='k')
- plt.scatter(confidence_x, range(module.outdim) * num_sequences, s=s*np.array(confidence_s), c='g', linewidths=0, alpha=0.66)
- plt.yticks(range(dataset.nClasses), dataset.class_labels)
+ plt.scatter(list(range(num_sequences)), expected, s=s, c='r', linewidths=0)
+ plt.scatter(list(range(num_sequences)), actual, s=s, c='k')
+ plt.scatter(confidence_x, list(range(module.outdim)) * num_sequences, s=s*np.array(confidence_s), c='g', linewidths=0, alpha=0.66)
+ plt.yticks(list(range(dataset.nClasses)), dataset.class_labels)
diff --git a/pybrain/tools/plotting/fitnessprogression.py b/pybrain/tools/plotting/fitnessprogression.py
index 7e7d6fed5..732ac7ebf 100644
--- a/pybrain/tools/plotting/fitnessprogression.py
+++ b/pybrain/tools/plotting/fitnessprogression.py
@@ -1,7 +1,10 @@
+from __future__ import print_function
+
""" a type of plots used so frequently that I think they merit their own utility """
__author__ = 'Tom Schaul, tom@idsia.ch'
+
import pylab
from pylab import xlabel, ylabel, legend, plot, semilogy
from scipy import array, zeros, power, log10
@@ -73,34 +76,34 @@ def relevantPart(l):
i += 1
nbRuns = len(flist)
- print(name, nbRuns, 'runs',)
+ print((name, nbRuns, 'runs',))
if targetcutoff != None:
if onlysuccessful:
# filter out unsuccessful runs
- flist = filter(isSuccessful, flist)
- print(',', len(flist), 'of which were successful.')
+ flist = list(filter(isSuccessful, flist))
+ print((',', len(flist), 'of which were successful.'))
else:
- print
+ print()
# cut off irrelevant part
- flist = map(relevantPart, flist)
+ flist = list(map(relevantPart, flist))
if len(flist) == 0:
continue
if averageOverEvaluations:
- worstPerf = max(map(max, flist))
+ worstPerf = max(list(map(max, flist)))
if semilog:
- yPlot = list(reversed(power(10, ((array(range(resolution + 1)) / float(resolution)) *
+ yPlot = list(reversed(power(10, ((array(list(range(resolution + 1))) / float(resolution)) *
(log10(worstPerf) - log10(targetcutoff)) + log10(targetcutoff)))))
else:
- yPlot = list(reversed((array(range(resolution + 1)) / float(resolution)) *
+ yPlot = list(reversed((array(list(range(resolution + 1))) / float(resolution)) *
(worstPerf - targetcutoff) + targetcutoff))
xPlot = avgFoundAfter(yPlot, flist, batchsize, useMedian=useMedian)
else:
- longestRun = max(map(len, flist))
- xPlot = array(range(longestRun)) * batchsize
+ longestRun = max(list(map(len, flist)))
+ xPlot = array(list(range(longestRun))) * batchsize
summed = zeros(longestRun)
for l in flist:
summed += paddedClipped(l, longestRun)
diff --git a/pybrain/tools/plotting/multiline.py b/pybrain/tools/plotting/multiline.py
index 3333952ff..e114defa3 100644
--- a/pybrain/tools/plotting/multiline.py
+++ b/pybrain/tools/plotting/multiline.py
@@ -147,7 +147,7 @@ def setLineStyle(self, id=None, **kwargs):
l.set(**kwargs)
# set as new default linestyle
- if kwargs.has_key('color'):
+ if 'color' in kwargs:
kwargs.popitem('color')
self.defaultLineStyle = kwargs
diff --git a/pybrain/tools/plotting/quickvariations.py b/pybrain/tools/plotting/quickvariations.py
index 79b4265e1..0ce2c8add 100644
--- a/pybrain/tools/plotting/quickvariations.py
+++ b/pybrain/tools/plotting/quickvariations.py
@@ -20,7 +20,7 @@ def plotVariations(datalist, titles, genFun, varyperplot=None, prePlotFun=None,
fdl = subDict(optionlists, varyperplot, True)
# title contains file and non-varying parameters
titadd1 = ''.join([k+'='+str(vs[0])[:min(5, len(str(vs[0])))]+' '
- for k,vs in odl.items()
+ for k,vs in list(odl.items())
if len(vs) == 1])
for x, tit in zip(datalist, titles):
for figdict in sorted(dictCombinations(fdl.copy())):
@@ -28,7 +28,7 @@ def plotVariations(datalist, titles, genFun, varyperplot=None, prePlotFun=None,
# it also contains the parameters that don't vary per figure
titadd2 = ''.join([k+'='+str(v)[:min(5, len(str(v)))]+' '
- for k,v in figdict.items()])
+ for k,v in list(figdict.items())])
pylab.title(tit+'\n'+titadd1+titadd2)
# code initializing the plot
@@ -38,7 +38,7 @@ def plotVariations(datalist, titles, genFun, varyperplot=None, prePlotFun=None,
for i, odict in enumerate(sorted(dictCombinations(odl.copy()))):
# concise labels
lab = ''.join([k[:3]+'='+str(v)[:min(5, len(str(v)))]+'-'
- for k,v in odict.items()
+ for k,v in list(odict.items())
if len(odl[k]) > 1])
if len(lab) > 0:
lab = lab[:-1] # remove trailing '-'
@@ -50,7 +50,7 @@ def plotVariations(datalist, titles, genFun, varyperplot=None, prePlotFun=None,
xs, ys = generated
else:
ys = generated
- xs = range(len(ys))
+ xs = list(range(len(ys)))
# the differentiator can slightly move the curves to be able to tell them apart if they overlap
if _differentiator != 0.0:
ys = generated+_differentiator*i
diff --git a/pybrain/tools/rankingfunctions.py b/pybrain/tools/rankingfunctions.py
index a46ba136e..ea9a923a8 100644
--- a/pybrain/tools/rankingfunctions.py
+++ b/pybrain/tools/rankingfunctions.py
@@ -15,7 +15,7 @@ def rankedFitness(R):
#l = sorted(list(enumerate(l)), cmp = lambda a,b: cmp(a[1],b[1]))
#return array(map(lambda (r, dummy): r, l))
res = zeros_like(R)
- l = zip(R, range(len(R)))
+ l = list(zip(R, list(range(len(R)))))
l.sort()
for i, (_, j) in enumerate(l):
res[j] = i
@@ -32,7 +32,7 @@ class RankingFunction(Named):
def __init__(self, **args):
self.setArgs(**args)
n = self.__class__.__name__
- for k, val in args.items():
+ for k, val in list(args.items()):
n += '-' + str(k) + '=' + str(val)
self.name = n
@@ -57,7 +57,7 @@ def __call__(self, R):
while randindex == i:
randindex = randint(0, len(R) - 1)
l.append(randindex)
- fits = map(lambda x: R[x], l)
+ fits = [R[x] for x in l]
res[argmax(fits)] += 1
return res
diff --git a/pybrain/tools/rlgluebridge.py b/pybrain/tools/rlgluebridge.py
index 18cb75e13..d4550d6d8 100644
--- a/pybrain/tools/rlgluebridge.py
+++ b/pybrain/tools/rlgluebridge.py
@@ -1,4 +1,4 @@
-from __future__ import division
+from __future__ import print_function
"""This module provides functionality to use pybrain with rlglue and to use it
for the rlcompetition.
@@ -153,7 +153,7 @@ def agent_freeze(self):
def agent_message(self, message):
# Originally thought to enable dynamic methods for agents, but this
# does not make a lot of sense in a dynamic language (and in OO?)
- print("Message:", message)
+ print(("Message:", message))
def _getAction(self):
"""
@@ -278,12 +278,12 @@ def run(self):
# and time.
try:
os.makedirs(self.benchmarkDir)
- except OSError, e:
+ except OSError as e:
if not "File exists" in str(e):
raise e
for name, agent_klass in self.agents:
- todo = xrange(self.loops)
+ todo = range(self.loops)
if not self.overwrite:
# If overwrite is set to false, we will only do the experiments
# that have not been done.
diff --git a/pybrain/tools/shortcuts.py b/pybrain/tools/shortcuts.py
index 3fcb2b88d..27edca2e5 100644
--- a/pybrain/tools/shortcuts.py
+++ b/pybrain/tools/shortcuts.py
@@ -44,7 +44,7 @@ def buildNetwork(*layers, **options):
'fast': False,
}
for key in options:
- if key not in opt.keys():
+ if key not in list(opt.keys()):
raise NetworkError('buildNetwork unknown option: %s' % key)
opt[key] = options[key]
@@ -135,8 +135,8 @@ def _buildNetwork(*layers, **options):
net = FeedForwardNetwork()
layerParts = iter(layers)
- firstPart = iter(layerParts.next())
- firstLayer = firstPart.next()
+ firstPart = iter(next(layerParts))
+ firstLayer = next(firstPart)
net.addInputModule(firstLayer)
prevLayer = firstLayer
diff --git a/pybrain/tools/svmdata.py b/pybrain/tools/svmdata.py
index abf21534c..b9761cb32 100644
--- a/pybrain/tools/svmdata.py
+++ b/pybrain/tools/svmdata.py
@@ -1,8 +1,9 @@
+from __future__ import print_function
+
__author__ = 'Michael Isik'
from pybrain.datasets import SupervisedDataSet
-
class SVMData(SupervisedDataSet):
""" Reads data files in LIBSVM/SVMlight format """
def __init__(self, filename=None):
@@ -109,12 +110,12 @@ def loadRawData(self, fname):
for line in open(fname, 'r').readlines():
if not line: break
targline = targetfile.readline()
- targline = map(int, targline.split())
+ targline = list(map(int, targline.split()))
for i, v in enumerate(targline):
if v:
label = i
break
- feat = map(float, line.split())
+ feat = list(map(float, line.split()))
x.append(feat)
y.append([float(label)])
self.nSamples += 1
@@ -155,6 +156,6 @@ def getClassHistogram(self):
d = SVMData()
d.clear()
d.loadSVMdata(r'M:\Data\Johan\svm\trials_scale.svm')
- print(d.getSample())
- print(d.getSample())
+ print((d.getSample()))
+ print((d.getSample()))
diff --git a/pybrain/tools/validation.py b/pybrain/tools/validation.py
index 87340e85d..8b98fe508 100644
--- a/pybrain/tools/validation.py
+++ b/pybrain/tools/validation.py
@@ -215,7 +215,7 @@ def _calculateModuleOutputSequential(cls, module, dataset):
outputs = []
for seq in dataset._provideSequences():
module.reset()
- for i in xrange(len(seq)):
+ for i in range(len(seq)):
output = module.activate(seq[i][0])
outputs.append(output.copy())
outputs = array(outputs)
@@ -275,7 +275,7 @@ def setArgs(self, **kwargs):
:key max_epochs: maximum number of epochs the trainer should train the module for.
:key verbosity: set verbosity level
"""
- for key, value in kwargs.items():
+ for key, value in list(kwargs.items()):
if key in ("verbose", "ver", "v"):
self._verbosity = value
elif key in ("max_epochs"):
@@ -300,7 +300,7 @@ def validate(self):
perf = 0.
for i in range(n_folds):
# determine train indices
- train_perms_idxs = range(n_folds)
+ train_perms_idxs = list(range(n_folds))
train_perms_idxs.pop(i)
temp_list = []
for train_perms_idx in train_perms_idxs:
@@ -370,7 +370,7 @@ def testOnSequenceData(module, dataset):
# one-of-many values
class_output = []
class_target = []
- for j in xrange(len(output)):
+ for j in range(len(output)):
# sum up the output values of one sequence
summed_output += output[j]
# print(j, output[j], " --> ", summed_output)
diff --git a/pybrain/unsupervised/trainers/deepbelief.py b/pybrain/unsupervised/trainers/deepbelief.py
index 95f8b5365..24125d1ba 100644
--- a/pybrain/unsupervised/trainers/deepbelief.py
+++ b/pybrain/unsupervised/trainers/deepbelief.py
@@ -49,7 +49,7 @@ def __init__(self, net, dataset, epochs=50,
def trainRbm(self, rbm, dataset):
trainer = self.trainerKlass(rbm, dataset, self.cfg)
- for _ in xrange(self.epochs):
+ for _ in range(self.epochs):
trainer.train()
return rbm
@@ -64,7 +64,7 @@ def iterRbms(self):
# with the corresponding layers.
biascons = self.net.connections[bias]
biascons.sort(key=lambda c: layers.index(c.outmod))
- modules = zip(layers, layers[1:], layercons, biascons)
+ modules = list(zip(layers, layers[1:], layercons, biascons))
for visible, hidden, layercon, biascon in modules:
rbm = Rbm.fromModules(visible, hidden, bias,
layercon, biascon)
@@ -80,7 +80,7 @@ def train(self):
bias = BiasUnit()
piecenet.addModule(bias)
# Add the first visible layer
- firstRbm = self.iterRbms().next()
+ firstRbm = next(self.iterRbms())
visible = copy.deepcopy(firstRbm.visible)
piecenet.addModule(visible)
# For saving the rbms and their inverses
@@ -90,7 +90,7 @@ def train(self):
self.net.sortModules()
# Train the first layer with an rbm trainer for `epoch` epochs.
trainer = self.trainerKlass(rbm, dataset, self.cfg)
- for _ in xrange(self.epochs):
+ for _ in range(self.epochs):
trainer.train()
self.invRbms.append(trainer.invRbm)
self.rbms.append(rbm)
diff --git a/pybrain/unsupervised/trainers/rbm.py b/pybrain/unsupervised/trainers/rbm.py
index 70bef58bd..74a2f18b1 100644
--- a/pybrain/unsupervised/trainers/rbm.py
+++ b/pybrain/unsupervised/trainers/rbm.py
@@ -56,7 +56,7 @@ def trainOnDataset(self, dataset):
zeros((self.rbm.visibleDim, self.rbm.hiddenDim)), \
zeros(self.rbm.hiddenDim), zeros(self.rbm.visibleDim)
- for t in xrange(cfg.maxIter):
+ for t in range(cfg.maxIter):
#print("*** Iteration %2d **************************************" % t)
params = self.rbm.params
diff --git a/pybrain/utilities.py b/pybrain/utilities.py
index 35495c667..7ebd6f6de 100644
--- a/pybrain/utilities.py
+++ b/pybrain/utilities.py
@@ -1,4 +1,4 @@
-from __future__ import with_statement
+from __future__ import print_function
__author__ = 'Tom Schaul, tom@idsia.ch; Justin Bayer, bayerj@in.tum.de'
@@ -14,6 +14,7 @@
from random import random, choice
from scipy import where, array, exp, zeros, size, mat, median
+from functools import reduce
# file extension for load/save protocol mapping
known_extensions = {
@@ -39,7 +40,7 @@ def drawIndex(probs, tolerant=False):
if tolerant:
probs /= sum(probs)
else:
- print(probs, 1 - sum(probs))
+ print((probs, 1 - sum(probs)))
raise ValueError()
r = random()
s = 0
@@ -47,7 +48,7 @@ def drawIndex(probs, tolerant=False):
s += p
if s > r:
return i
- return choice(range(len(probs)))
+ return choice(list(range(len(probs))))
def drawGibbs(vals, temperature=1.):
@@ -92,13 +93,13 @@ def setAllArgs(obj, argdict):
This function can be useful for quick initializations. """
xmlstore = isinstance(obj, XMLBuildable)
- for n in argdict.keys():
+ for n in list(argdict.keys()):
if hasattr(obj, n):
setattr(obj, n, argdict[n])
if xmlstore:
obj.argdict[n] = argdict[n]
else:
- print('Warning: parameter name', n, 'not found!')
+ print(('Warning: parameter name', n, 'not found!'))
if xmlstore:
if not hasattr(obj, '_unknown_argdict'):
obj._unknown_argdict = {}
@@ -233,7 +234,8 @@ def __repr__(self):
def fListToString(a_list, a_precision=3):
""" Returns a string representing a list of floats with a given precision """
- s_list = ", ".join(("%g" % round(x, a_precision)).ljust(a_precision+3)
+ from numpy import around
+ s_list = ", ".join(("%g" % around(x, a_precision)).ljust(a_precision+3)
for x in a_list)
return "[%s]" % s_list
@@ -253,7 +255,7 @@ def confidenceIntervalSize(stdev, nbsamples):
def trace(func):
def inner(*args, **kwargs):
- print("%s: %s, %s" % (func.__name__, args, kwargs))
+ print(("%s: %s, %s" % (func.__name__, args, kwargs)))
return func(*args, **kwargs)
return inner
@@ -290,7 +292,7 @@ def inner(*args, **kwargs):
args = tuple(args)
# Make a set for checking in the cache, since the order of
# .iteritems() is undefined
- kwargs_set = frozenset(kwargs.iteritems())
+ kwargs_set = frozenset(iter(kwargs.items()))
if (args, kwargs_set) in cache:
result = cache[args, kwargs_set]
else:
@@ -355,7 +357,7 @@ def int2gray(i):
def gray2int(g, size):
""" Transforms a Gray code back into an integer. """
res = 0
- for i in reversed(range(size)):
+ for i in reversed(list(range(size))):
gi = (g >> i) % 2
if i == size - 1:
bi = gi
@@ -444,7 +446,7 @@ def reachable(stepFunction, start, destinations, _alreadyseen=None):
deeper = reachable(stepFunction, new, ndestinations, _alreadyseen)
# adjust distances
- for k, val in deeper.items():
+ for k, val in list(deeper.items()):
res[k] = val + 1
return res
@@ -539,7 +541,7 @@ def permuteToBlocks2d(arr, blockheight, blockwidth):
_height, width = arr.shape
arr = arr.flatten()
new = zeros(size(arr))
- for i in xrange(size(arr)):
+ for i in range(size(arr)):
blockx = (i % width) / blockwidth
blocky = i / width / blockheight
blockoffset = blocky * width / blockwidth + blockx
@@ -589,7 +591,7 @@ def blockList2Matrix(l):
def blockCombine(l):
""" Produce a matrix from a list of lists of its components. """
- l = [map(mat, row) for row in l]
+ l = [list(map(mat, row)) for row in l]
hdims = [m.shape[1] for m in l[0]]
hs = sum(hdims)
vdims = [row[0].shape[0] for row in l]
@@ -612,7 +614,7 @@ def avgFoundAfter(decreasingTargetValues, listsOfActualValues, batchSize=1, useM
Returns an array. """
from scipy import sum
numLists = len(listsOfActualValues)
- longest = max(map(len, listsOfActualValues))
+ longest = max(list(map(len, listsOfActualValues)))
# gather a list of indices of first encounters
res = [[0] for _ in range(numLists)]
for tval in decreasingTargetValues:
@@ -642,7 +644,7 @@ def matchingDict(d, selection, require_existence=False):
""" Determines if the dictionary d conforms to the specified selection,
i.e. if a (key, x) is in the selection, then if key is in d as well it must be x
or contained in x (if x is a list). """
- for k, v in selection.items():
+ for k, v in list(selection.items()):
if k in d:
if isinstance(v, list):
if d[k] not in v:
@@ -659,7 +661,7 @@ def subDict(d, allowedkeys, flip=False):
""" Returns a new dictionary with a subset of the entries of d
that have on of the (dis-)allowed keys."""
res = {}
- for k, v in d.items():
+ for k, v in list(d.items()):
if (k in allowedkeys) ^ flip:
res[k] = v
return res
@@ -744,7 +746,7 @@ def uniqueArrays(vs):
resdic = {}
for v in vs:
resdic[xhash(v)] = v
- return resdic.values()
+ return list(resdic.values())
def seedit(seed=0):
From 42e8b02b1dcdf7ad031452d5a0053197d7a6217f Mon Sep 17 00:00:00 2001
From: Shane Bretz
Date: Sun, 30 Nov 2014 00:29:17 -0500
Subject: [PATCH 63/83] Update blackboxoptimization.py
---
docs/tutorials/blackboxoptimization.py | 2 ++
1 file changed, 2 insertions(+)
diff --git a/docs/tutorials/blackboxoptimization.py b/docs/tutorials/blackboxoptimization.py
index 7ebd31a1c..f1dcd1869 100644
--- a/docs/tutorials/blackboxoptimization.py
+++ b/docs/tutorials/blackboxoptimization.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
############################################################################
# PyBrain Tutorial "Black Box Optimization"
#
From 3b1fbeba7ba4fd33b1c0fcce6bfcc07d258d293e Mon Sep 17 00:00:00 2001
From: Shane Bretz
Date: Sun, 30 Nov 2014 00:30:04 -0500
Subject: [PATCH 64/83] Update fnn.py
---
docs/tutorials/fnn.py | 2 ++
1 file changed, 2 insertions(+)
diff --git a/docs/tutorials/fnn.py b/docs/tutorials/fnn.py
index 6f319fe44..cb826f938 100644
--- a/docs/tutorials/fnn.py
+++ b/docs/tutorials/fnn.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
############################################################################
# PyBrain Tutorial "Classification with Feed-Forward Neural Networks"
#
From deb82b134e05850a03feb182767ac9912f495216 Mon Sep 17 00:00:00 2001
From: Shane Bretz
Date: Sun, 30 Nov 2014 00:30:34 -0500
Subject: [PATCH 65/83] Update networks.py
---
docs/tutorials/networks.py | 2 ++
1 file changed, 2 insertions(+)
diff --git a/docs/tutorials/networks.py b/docs/tutorials/networks.py
index b521d41a0..9ad1a942a 100644
--- a/docs/tutorials/networks.py
+++ b/docs/tutorials/networks.py
@@ -1,3 +1,5 @@
+from __future__ import print_function
+
############################################################################
# PyBrain Tutorial "Networks, Modules, Connections"
#
From ccab74b1e9bd3ece8e56b77b6d5adab8e6cb1db7 Mon Sep 17 00:00:00 2001
From: Andrea Canciani
Date: Wed, 17 Dec 2014 14:38:25 +0100
Subject: [PATCH 66/83] Simplify check for convergence in backprop.py
---
pybrain/supervised/trainers/backprop.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/pybrain/supervised/trainers/backprop.py b/pybrain/supervised/trainers/backprop.py
index fa09fc9db..1790a061c 100644
--- a/pybrain/supervised/trainers/backprop.py
+++ b/pybrain/supervised/trainers/backprop.py
@@ -8,7 +8,6 @@
from pybrain.supervised.trainers.trainer import Trainer
from pybrain.utilities import fListToString
from pybrain.auxiliary import GradientDescent
-from functools import reduce
class BackpropTrainer(Trainer):
@@ -249,7 +248,8 @@ def trainUntilConvergence(self, dataset=None, maxEpochs=None, verbose=None,
if min(new) > max(old):
self.module.params[:] = bestweights
break
- elif reduce(lambda x, y: x + (y - round(new[-1], convergence_threshold)), [round(y, convergence_threshold) for y in new]) == 0:
+ lastnew = round(new[-1], convergence_threshold)
+ if sum(round(y, convergence_threshold) - lastnew for y in new) == 0:
self.module.params[:] = bestweights
break
#self.trainingErrors.append(self.testOnData(trainingData))
From 3bd33c134df6f52875ab6ae3d1c1f0f40ef05663 Mon Sep 17 00:00:00 2001
From: littledroplet
Date: Sun, 4 Jan 2015 11:04:25 -0800
Subject: [PATCH 67/83] consistenency of actions with maze.py
refer to https://github.com/pybrain/pybrain/blob/master/pybrain/rl/environments/mazes/maze.py#L45
line 45 allActions = [N, E, S, W]
---
docs/tutorials/rl.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/docs/tutorials/rl.py b/docs/tutorials/rl.py
index 278a266bb..cf6e62dc4 100644
--- a/docs/tutorials/rl.py
+++ b/docs/tutorials/rl.py
@@ -89,7 +89,7 @@
"""
The table needs the number of states and actions as parameters. The standard
-maze environment comes with the following 4 actions: north, south, east, west.
+maze environment comes with the following 4 actions: north, east, south, west.
Then, we initialize the table with 1 everywhere. This is not always necessary
but will help converge faster, because unvisited state-action pairs have a
From 2f02b8d9e4e9d6edbc135a355ab387048a00f1af Mon Sep 17 00:00:00 2001
From: Nihn
Date: Mon, 5 Jan 2015 19:34:48 +0100
Subject: [PATCH 68/83] Remove for loop from
datasets.supervised.SupervisedDataSet.splitWithProportion
Now splitWithProporion uses numpy array indicies with numpy.random.permutation instead of for loop, before this change on large datasets this method was very slow, now its finish almost instant.
---
pybrain/datasets/supervised.py | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/pybrain/datasets/supervised.py b/pybrain/datasets/supervised.py
index dfb184c04..0bb0752fd 100644
--- a/pybrain/datasets/supervised.py
+++ b/pybrain/datasets/supervised.py
@@ -2,6 +2,7 @@
__author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de'
+from numpy import random
from random import sample
from scipy import isscalar
@@ -104,16 +105,15 @@ def evaluateModuleMSE(self, module, averageOver = 1, **args):
def splitWithProportion(self, proportion = 0.5):
"""Produce two new datasets, the first one containing the fraction given
by `proportion` of the samples."""
- leftIndices = set(sample(list(range(len(self))), int(len(self)*proportion)))
- leftDs = self.copy()
- leftDs.clear()
- rightDs = leftDs.copy()
- index = 0
- for sp in self:
- if index in leftIndices:
- leftDs.addSample(*sp)
- else:
- rightDs.addSample(*sp)
- index += 1
+ indicies = random.permutation(len(self))
+ separator = int(len(self) * proportion)
+
+ leftIndicies = indicies[:separator]
+ rightIndicies = indicies[separator:]
+
+ leftDs = SupervisedDataSet(inp=self['input'][leftIndicies].copy(),
+ target=self['target'][leftIndicies].copy())
+ rightDs = SupervisedDataSet(inp=self['input'][rightIndicies].copy(),
+ target=self['target'][rightIndicies].copy())
return leftDs, rightDs
From 5c2c4d102fe843ed018e17350624e2e085347148 Mon Sep 17 00:00:00 2001
From: Ben Goodrich
Date: Thu, 8 Jan 2015 12:21:40 -0500
Subject: [PATCH 69/83] Fixed math errors in the simulation
---
pybrain/rl/environments/cartpole/cartpole.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/pybrain/rl/environments/cartpole/cartpole.py b/pybrain/rl/environments/cartpole/cartpole.py
index 99218e627..7a6957232 100644
--- a/pybrain/rl/environments/cartpole/cartpole.py
+++ b/pybrain/rl/environments/cartpole/cartpole.py
@@ -84,9 +84,9 @@ def _derivs(self, x, t):
mp = self.mp
mc = self.mc
l = self.l
- u_ = (self.g * sin_theta * (mc + mp) - (F + mp * l * theta ** 2 * sin_theta) * cos_theta) / (4 / 3 * l * (mc + mp) - mp * l * cos_theta ** 2)
+ u_ = (self.g * sin_theta * (mc + mp) - (F + mp * l * theta_ ** 2 * sin_theta) * cos_theta) / (4 / 3 * l * (mc + mp) - mp * l * cos_theta ** 2)
v = s_
- v_ = (F - mp * l * (u_ * cos_theta - (s_ ** 2 * sin_theta))) / (mc + mp)
+ v_ = (F - mp * l * (u_ * cos_theta - (theta_ ** 2 * sin_theta))) / (mc + mp)
return (u, u_, v, v_)
def getPoleAngles(self):
From 977acf5f362ffa3c71a944210fd6d9f7695df053 Mon Sep 17 00:00:00 2001
From: schaul
Date: Fri, 9 Jan 2015 16:27:10 +0000
Subject: [PATCH 70/83] version fix
---
setup.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/setup.py b/setup.py
index 4265a9f98..5a041c286 100644
--- a/setup.py
+++ b/setup.py
@@ -10,7 +10,7 @@
setup(
name="PyBrain",
- version="0.3.1",
+ version="0.3.3",
description="PyBrain is the Swiss army knife for neural networking.",
license="BSD",
keywords="Neural Networks Machine Learning",
From 53f2861b7cc9b900c9661db57bff2090e5c83fec Mon Sep 17 00:00:00 2001
From: AshleyChen
Date: Fri, 1 May 2015 02:44:41 -0700
Subject: [PATCH 71/83] added useful info for Windows users on tutorial
---
docs/html/index.html | 12 ++++++++++++
docs/html/tutorial/intro.html | 2 +-
2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/docs/html/index.html b/docs/html/index.html
index dc340c77d..38089c056 100644
--- a/docs/html/index.html
+++ b/docs/html/index.html
@@ -71,6 +71,18 @@ Installation.
+Possible difficulties for Windows users:
+To change environment variables, go to Control Panel > System and Security > System > Advanced System Settings. You should be on the "Advanced" tab in the System Properties pop-up window. Click "Environment Variables" and find PYTHONPATH under System Variables. Edit the variable called "Path" with the path the location of Pybrain in your files. To verify that this worked, go to the command line and type in 'python' and make sure you see the following:
+C:\Users\username>python
+Python 2.?.? (other information)
+Type "help", "copyright", or "license" for more information.
+>>>
+When downloading the ez_setup.py script put the file in the same folder as Python.
+To verify that the easy_install setup worked correctly after running the script you should see the following at the end of many lines:
+Processing dependencies for setuptools==0.8
+Finished processing dependencies for setuptools==0.8
+C:\Python27>
+If you are a novice or unfamiliar with Git, see this guide to Git.
Quickstart
diff --git a/docs/html/tutorial/intro.html b/docs/html/tutorial/intro.html
index 78f610fa7..6ce72f349 100644
--- a/docs/html/tutorial/intro.html
+++ b/docs/html/tutorial/intro.html
@@ -63,7 +63,7 @@
Navigation
hierarchical networks as well. Shortcuts exist for building the most common
network architectures, but in principle this system allows almost arbitrary
connectionist systems to be assembled, as long as they form a directed acyclic
-graph.
+graph.
The free parameters of the Network are adjusted by means of a Trainer,
which uses a Dataset to learn the optimum parameters from examples.
For reinforcement learning experiments, a simulation environment with an
From 24d39f92c3b5cfc988bb2fd88a417aad9dde3380 Mon Sep 17 00:00:00 2001
From: Theodor Nolte
Date: Sun, 17 May 2015 15:29:38 +0200
Subject: [PATCH 72/83] Fix incompatibility with Python 3 using //
Add python 2 and 3 compatibility by using the floor division operator '//'.
Cf. https://docs.python.org/2/whatsnew/2.2.html#pep-238-changing-the-division-operator
---
pybrain/supervised/trainers/backprop.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pybrain/supervised/trainers/backprop.py b/pybrain/supervised/trainers/backprop.py
index 1790a061c..b4c9f5fda 100644
--- a/pybrain/supervised/trainers/backprop.py
+++ b/pybrain/supervised/trainers/backprop.py
@@ -158,7 +158,7 @@ def testOnData(self, dataset=None, verbose=False):
if verbose:
print(('Average error:', avgErr))
print(('Max error:', max(ponderatedErrors), 'Median error:',
- sorted(ponderatedErrors)[len(errors) / 2]))
+ sorted(ponderatedErrors)[len(errors) // 2]))
return avgErr
def testOnClassData(self, dataset=None, verbose=False,
From cf6bf6ed7b9a681727091c4e16e2a2738e587e72 Mon Sep 17 00:00:00 2001
From: Lennart Wagner
Date: Thu, 16 Jul 2015 23:28:28 +0200
Subject: [PATCH 73/83] fix validation.CrossValidator.validate(), use test_ds
---
pybrain/tools/validation.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pybrain/tools/validation.py b/pybrain/tools/validation.py
index 8b98fe508..5fdb1f63e 100644
--- a/pybrain/tools/validation.py
+++ b/pybrain/tools/validation.py
@@ -328,7 +328,7 @@ def validate(self):
test_ds.setField("input" , inp[test_idxs])
test_ds.setField("target" , tar[test_idxs])
# perf += self.getPerformance( trainer.module, dataset )
- perf += self._calculatePerformance(trainer.module, dataset)
+ perf += self._calculatePerformance(trainer.module, test_ds)
perf /= n_folds
return perf
From 1926b6e14f792cee662ed99a1b81e89674df791c Mon Sep 17 00:00:00 2001
From: Ryan Lambert
Date: Tue, 18 Aug 2015 12:33:23 -0700
Subject: [PATCH 74/83] line 63 grammar
---
docs/html/api/datasets/classificationdataset.html | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/docs/html/api/datasets/classificationdataset.html b/docs/html/api/datasets/classificationdataset.html
index 95b6f572d..a3ab060ef 100644
--- a/docs/html/api/datasets/classificationdataset.html
+++ b/docs/html/api/datasets/classificationdataset.html
@@ -60,7 +60,7 @@ Navigation
__init__(inp, target=1, nb_classes=0, class_labels=None)
Initialize an empty dataset.
inp is used to specify the dimensionality of the input. While the
-number of targets is given by implicitly by the training samples, it can
+number of targets is given implicitly by the training samples, it can
also be set explicity by nb_classes. To give the classes names, supply
an iterable of strings as class_labels.
@@ -185,4 +185,4 @@ Navigation
Created using Sphinx 0.6.3.