├── FCLogger.py ├── FuzzyClassificator.py ├── FuzzyRoutines.py ├── LICENSE ├── PyBrainLearning.py ├── README.md ├── candidates.dat ├── ethalons.dat ├── network.xml ├── pybrain ├── __init__.py ├── auxiliary │ ├── __init__.py │ ├── gaussprocess.py │ ├── gradientdescent.py │ ├── importancemixing.py │ ├── kmeans.py │ └── pca.py ├── datasets │ ├── __init__.py │ ├── classification.py │ ├── dataset.py │ ├── importance.py │ ├── reinforcement.py │ ├── sequential.py │ ├── supervised.py │ └── unsupervised.py ├── optimization │ ├── __init__.py │ ├── distributionbased │ │ ├── __init__.py │ │ ├── cmaes.py │ │ ├── distributionbased.py │ │ ├── fem.py │ │ ├── nes.py │ │ ├── rank1.py │ │ ├── snes.py │ │ ├── ves.py │ │ └── xnes.py │ ├── finitedifference │ │ ├── __init__.py │ │ ├── fd.py │ │ ├── pgpe.py │ │ └── spsa.py │ ├── hillclimber.py │ ├── memetic │ │ ├── __init__.py │ │ ├── innerinversememetic.py │ │ ├── innermemetic.py │ │ ├── inversememetic.py │ │ └── memetic.py │ ├── neldermead.py │ ├── optimizer.py │ ├── populationbased │ │ ├── __init__.py │ │ ├── coevolution │ │ │ ├── __init__.py │ │ │ ├── coevolution.py │ │ │ ├── competitivecoevolution.py │ │ │ └── multipopulationcoevolution.py │ │ ├── es.py │ │ ├── evolution.py │ │ ├── ga.py │ │ ├── multiobjective │ │ │ ├── __init__.py │ │ │ ├── constnsga2.py │ │ │ └── nsga2.py │ │ └── pso.py │ └── randomsearch.py ├── rl │ ├── __init__.py │ ├── agents │ │ ├── __init__.py │ │ ├── agent.py │ │ ├── learning.py │ │ ├── linearfa.py │ │ ├── logging.py │ │ └── optimization.py │ ├── environments │ │ ├── __init__.py │ │ ├── cartpole │ │ │ ├── __init__.py │ │ │ ├── balancetask.py │ │ │ ├── cartpole.py │ │ │ ├── doublepole.py │ │ │ ├── fast_version │ │ │ │ ├── __init__.py │ │ │ │ ├── cartpolecompile.py │ │ │ │ └── cartpoleenv.py │ │ │ ├── nonmarkovdoublepole.py │ │ │ ├── nonmarkovpole.py │ │ │ └── renderer.py │ │ ├── classic │ │ │ ├── __init__.py │ │ │ ├── acrobot.py │ │ │ ├── mountaincar.py │ │ │ └── xor.py │ │ ├── environment.py │ │ ├── episodic.py │ │ ├── fitnessevaluator.py │ │ ├── flexcube │ │ │ ├── __init__.py │ │ │ ├── environment.py │ │ │ ├── masspoint.py │ │ │ ├── objects3d.py │ │ │ ├── sensors.py │ │ │ ├── tasks.py │ │ │ └── viewer.py │ │ ├── functions │ │ │ ├── __init__.py │ │ │ ├── bbob2010.py │ │ │ ├── function.py │ │ │ ├── lennardjones.py │ │ │ ├── multimodal.py │ │ │ ├── multiobjective.py │ │ │ ├── transformations.py │ │ │ ├── unbounded.py │ │ │ └── unimodal.py │ │ ├── graphical.py │ │ ├── mazes │ │ │ ├── __init__.py │ │ │ ├── maze.py │ │ │ ├── polarmaze.py │ │ │ └── tasks │ │ │ │ ├── __init__.py │ │ │ │ ├── cheesemaze.py │ │ │ │ ├── maze.py │ │ │ │ ├── maze4x3.py │ │ │ │ ├── maze89state.py │ │ │ │ ├── mdp.py │ │ │ │ ├── pomdp.py │ │ │ │ ├── shuttle.py │ │ │ │ ├── tiger.py │ │ │ │ └── tmaze.py │ │ ├── ode │ │ │ ├── __init__.py │ │ │ ├── actuators.py │ │ │ ├── environment.py │ │ │ ├── instances │ │ │ │ ├── __init__.py │ │ │ │ ├── acrobot.py │ │ │ │ ├── ccrl.py │ │ │ │ └── johnnie.py │ │ │ ├── models │ │ │ │ ├── acrobot.xode │ │ │ │ ├── acroside.xode │ │ │ │ ├── acrotop.xode │ │ │ │ ├── arm.xode │ │ │ │ ├── box-sphere.xode │ │ │ │ ├── ccrlGlas.xode │ │ │ │ ├── ccrlPlate.xode │ │ │ │ ├── ccrlTable.xode │ │ │ │ ├── crawler.xode │ │ │ │ ├── hand.xode │ │ │ │ ├── johnnie-heavyarms.xode │ │ │ │ ├── johnnie.xode │ │ │ │ ├── octacrawl.xode │ │ │ │ └── sphere-walker.xode │ │ │ ├── sensors.py │ │ │ ├── tasks │ │ │ │ ├── __init__.py │ │ │ │ ├── acrobot.py │ │ │ │ ├── ccrl.py │ │ │ │ └── johnnie.py │ │ │ ├── tools │ │ │ │ ├── __init__.py │ │ │ │ ├── configgrab.py │ │ │ │ ├── mathhelpers.py │ │ │ │ ├── xmltools.py │ │ │ │ └── xodetools.py │ │ │ └── viewer.py │ │ ├── renderer.py │ │ ├── serverinterface.py │ │ ├── shipsteer │ │ │ ├── __init__.py │ │ │ ├── northwardtask.py │ │ │ ├── shipsteer.py │ │ │ └── viewer.py │ │ ├── simple │ │ │ ├── __init__.py │ │ │ ├── environment.py │ │ │ ├── renderer.py │ │ │ └── tasks.py │ │ ├── simplerace │ │ │ ├── __init__.py │ │ │ ├── simplecontroller.py │ │ │ ├── simpleracetask.py │ │ │ └── simpleracetcp.py │ │ ├── task.py │ │ └── twoplayergames │ │ │ ├── __init__.py │ │ │ ├── capturegame.py │ │ │ ├── capturegameplayers │ │ │ ├── __init__.py │ │ │ ├── captureplayer.py │ │ │ ├── clientwrapper.py │ │ │ ├── killing.py │ │ │ ├── moduledecision.py │ │ │ ├── nonsuicide.py │ │ │ └── randomplayer.py │ │ │ ├── gomoku.py │ │ │ ├── gomokuplayers │ │ │ ├── __init__.py │ │ │ ├── gomokuplayer.py │ │ │ ├── killing.py │ │ │ ├── moduledecision.py │ │ │ └── randomplayer.py │ │ │ ├── pente.py │ │ │ ├── tasks │ │ │ ├── __init__.py │ │ │ ├── capturetask.py │ │ │ ├── gomokutask.py │ │ │ ├── handicaptask.py │ │ │ ├── pentetask.py │ │ │ ├── relativegomokutask.py │ │ │ └── relativetask.py │ │ │ └── twoplayergame.py │ ├── experiments │ │ ├── __init__.py │ │ ├── continuous.py │ │ ├── episodic.py │ │ ├── experiment.py │ │ ├── queued.py │ │ └── tournament.py │ ├── explorers │ │ ├── __init__.py │ │ ├── continuous │ │ │ ├── __init__.py │ │ │ ├── normal.py │ │ │ └── sde.py │ │ ├── discrete │ │ │ ├── __init__.py │ │ │ ├── boltzmann.py │ │ │ ├── discrete.py │ │ │ ├── discretesde.py │ │ │ └── egreedy.py │ │ └── explorer.py │ └── learners │ │ ├── __init__.py │ │ ├── directsearch │ │ ├── __init__.py │ │ ├── directsearch.py │ │ ├── enac.py │ │ ├── gpomdp.py │ │ ├── policygradient.py │ │ ├── reinforce.py │ │ └── rwr.py │ │ ├── learner.py │ │ ├── meta │ │ ├── __init__.py │ │ ├── levinsearch.py │ │ └── meta.py │ │ ├── modelbased │ │ ├── __init__.py │ │ ├── leastsquares.py │ │ └── policyiteration.py │ │ └── valuebased │ │ ├── __init__.py │ │ ├── interface.py │ │ ├── linearfa.py │ │ ├── nfq.py │ │ ├── q.py │ │ ├── qlambda.py │ │ ├── sarsa.py │ │ └── valuebased.py ├── structure │ ├── __init__.py │ ├── connections │ │ ├── __init__.py │ │ ├── connection.py │ │ ├── full.py │ │ ├── fullnotself.py │ │ ├── identity.py │ │ ├── linear.py │ │ ├── permutation.py │ │ ├── shared.py │ │ └── subsampling.py │ ├── evolvables │ │ ├── __init__.py │ │ ├── cheaplycopiable.py │ │ ├── evolvable.py │ │ ├── maskedmodule.py │ │ ├── maskedparameters.py │ │ └── topology.py │ ├── modulemesh.py │ ├── modules │ │ ├── __init__.py │ │ ├── biasunit.py │ │ ├── evolinonetwork.py │ │ ├── gate.py │ │ ├── gaussianlayer.py │ │ ├── kohonen.py │ │ ├── linearlayer.py │ │ ├── lstm.py │ │ ├── mdlstm.py │ │ ├── mdrnnlayer.py │ │ ├── module.py │ │ ├── neuronlayer.py │ │ ├── relulayer.py │ │ ├── samplelayer.py │ │ ├── sigmoidlayer.py │ │ ├── softmax.py │ │ ├── softsign.py │ │ ├── statedependentlayer.py │ │ ├── svmunit.py │ │ ├── table.py │ │ └── tanhlayer.py │ ├── moduleslice.py │ ├── networks │ │ ├── __init__.py │ │ ├── bidirectional.py │ │ ├── borderswiping.py │ │ ├── convolutional.py │ │ ├── custom │ │ │ ├── __init__.py │ │ │ ├── capturegame.py │ │ │ └── convboard.py │ │ ├── feedforward.py │ │ ├── mdrnn.py │ │ ├── multidimensional.py │ │ ├── network.py │ │ ├── neurondecomposable.py │ │ ├── rbm.py │ │ ├── recurrent.py │ │ └── swiping.py │ └── parametercontainer.py ├── supervised │ ├── __init__.py │ ├── evolino │ │ ├── __init__.py │ │ ├── filter.py │ │ ├── gfilter.py │ │ ├── gindividual.py │ │ ├── gpopulation.py │ │ ├── individual.py │ │ ├── networkwrapper.py │ │ ├── population.py │ │ └── variate.py │ ├── knn │ │ ├── __init__.py │ │ └── lsh │ │ │ ├── __init__.py │ │ │ ├── minhash.py │ │ │ └── nearoptimal.py │ └── trainers │ │ ├── __init__.py │ │ ├── backprop.py │ │ ├── evolino.py │ │ ├── rprop.py │ │ ├── svmtrainer.py │ │ └── trainer.py ├── tests │ ├── __init__.py │ ├── auxiliary.py │ ├── helpers.py │ ├── optimizationtest.py │ ├── runtests.py │ ├── testsuites.py │ └── unittests │ │ ├── __init__.py │ │ ├── _test_equivalence_to_ctypes.py │ │ ├── auxiliary │ │ ├── __init__.py │ │ └── test_pca.py │ │ ├── datasets │ │ ├── __init__.py │ │ └── test_datasets_datasets.py │ │ ├── optimization │ │ ├── __init__.py │ │ └── populationbased │ │ │ ├── __init__.py │ │ │ └── test_pso_ring.py │ │ ├── rl │ │ ├── __init__.py │ │ └── environments │ │ │ ├── __init__.py │ │ │ └── twoplayergames │ │ │ ├── __init__.py │ │ │ ├── test_capture_game.py │ │ │ └── test_pente.py │ │ ├── structure │ │ ├── __init__.py │ │ ├── connections │ │ │ ├── __init__.py │ │ │ ├── test_shared_connections.py │ │ │ ├── test_sliced_connections.py │ │ │ └── test_subsampling_connection.py │ │ ├── modules │ │ │ ├── __init__.py │ │ │ ├── test_peephole_lstm.py │ │ │ ├── test_peephole_mdlstm.py │ │ │ ├── test_samplelayer.py │ │ │ ├── test_simple_lstm_network.py │ │ │ └── test_simple_mdlstm.py │ │ └── networks │ │ │ ├── __init__.py │ │ │ ├── _test_mdrnn.py │ │ │ ├── _test_rbm.py │ │ │ ├── custom │ │ │ ├── __init__.py │ │ │ ├── test_capturegame_network.py │ │ │ └── test_convolutional_nets.py │ │ │ ├── test_bidirectional_network.py │ │ │ ├── test_borderswipingnetwork.py │ │ │ ├── test_cyclic_network.py │ │ │ ├── test_nested_ffn_and_rnn.py │ │ │ ├── test_nested_network.py │ │ │ ├── test_network_decomposition.py │ │ │ ├── test_network_forward_backward.py │ │ │ ├── test_network_sort.py │ │ │ ├── test_no_gravity_network.py │ │ │ ├── test_recurrent_network.py │ │ │ └── test_swiping_network.py │ │ ├── supervised │ │ ├── __init__.py │ │ ├── knn │ │ │ ├── __init__.py │ │ │ └── lsh │ │ │ │ ├── __init__.py │ │ │ │ ├── test_minhash.py │ │ │ │ └── test_nearoptimal.py │ │ └── trainers │ │ │ ├── __init__.py │ │ │ ├── test_backprop.py │ │ │ ├── test_evolino.py │ │ │ └── test_rprop.py │ │ ├── test_utilities.py │ │ ├── test_utilities_dictionaries.py │ │ ├── test_utilities_flood.py │ │ ├── test_utilities_foundafter.py │ │ ├── test_utilities_reachable.py │ │ ├── tools │ │ ├── __init__.py │ │ ├── test_ibp_leftordered.py │ │ └── test_rlgluebridge.py │ │ └── unsupervised │ │ └── __init__.py ├── tools │ ├── __init__.py │ ├── aptativeresampling.py │ ├── benchmark.py │ ├── customxml │ │ ├── __init__.py │ │ ├── handling.py │ │ ├── networkreader.py │ │ └── networkwriter.py │ ├── datasets │ │ ├── __init__.py │ │ └── mnist.py │ ├── datasettools.py │ ├── example_tools.py │ ├── filehandling.py │ ├── fisher.py │ ├── functions.py │ ├── gridsearch.py │ ├── ibp.py │ ├── kwargsprocessor.py │ ├── mixtures │ │ ├── __init__.py │ │ ├── mixtureofgaussian.py │ │ └── mogpuremax.py │ ├── networking │ │ ├── __init__.py │ │ └── udpconnection.py │ ├── neuralnets.py │ ├── nondominated.py │ ├── plotting │ │ ├── __init__.py │ │ ├── ciaoplot.py │ │ ├── colormaps.py │ │ ├── fitnesslandscapes.py │ │ ├── fitnessprogression.py │ │ ├── multiline.py │ │ └── quickvariations.py │ ├── rankingfunctions.py │ ├── rlgluebridge.py │ ├── shortcuts.py │ ├── svmdata.py │ └── validation.py ├── unsupervised │ ├── __init__.py │ └── trainers │ │ ├── __init__.py │ │ ├── deepbelief.py │ │ └── rbm.py └── utilities.py └── report.txt /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Open DevOps Community 4 | Project Lead, main ideas, author of fuzzy measuring scales: Timur Gilmullin, tim55667757@gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. -------------------------------------------------------------------------------- /candidates.dat: -------------------------------------------------------------------------------- 1 | input1 input2 input3 2 | 0.12 0.32 Min 3 | 0.32 0.35 Low 4 | 0.54 0.57 Med 5 | 0.65 0.68 High 6 | 0.76 0.79 Max 7 | -------------------------------------------------------------------------------- /ethalons.dat: -------------------------------------------------------------------------------- 1 | input1 input2 input3 1st_class_output 2nd_class_output 2 | 0.1 0.2 Min Min Max 3 | 0.2 0.3 Low Min Max 4 | 0.3 0.4 Med Min Max 5 | 0.4 0.5 Med Max Min 6 | 0.5 0.6 High Max Min 7 | 0.6 0.7 Max Max Min 8 | -------------------------------------------------------------------------------- /pybrain/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.structure.__init__ import * 2 | -------------------------------------------------------------------------------- /pybrain/auxiliary/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.auxiliary.gradientdescent import GradientDescent 2 | from pybrain.auxiliary.gaussprocess import GaussianProcess 3 | from pybrain.auxiliary.importancemixing import importanceMixing -------------------------------------------------------------------------------- /pybrain/auxiliary/importancemixing.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from random import uniform 4 | from scipy import array 5 | from scipy.stats.distributions import norm 6 | 7 | 8 | def importanceMixing(oldpoints, oldpdf, newpdf, newdistr, forcedRefresh = 0.01): 9 | """ Implements importance mixing. Given a set of points, an old and a new pdf-function for them 10 | and a generator function for new points, it produces a list of indices of the old points to be reused and a list of new points. 11 | Parameter (optional): forced refresh rate. 12 | """ 13 | reuseindices = [] 14 | batch = len(oldpoints) 15 | for i, sample in enumerate(oldpoints): 16 | r = uniform(0, 1) 17 | if r < (1-forcedRefresh) * newpdf(sample) / oldpdf(sample): 18 | reuseindices.append(i) 19 | # never use only old samples 20 | if batch - len(reuseindices) <= max(1, batch * forcedRefresh): 21 | break 22 | newpoints = [] 23 | # add the remaining ones 24 | while len(reuseindices)+len(newpoints) < batch: 25 | r = uniform(0, 1) 26 | sample = newdistr() 27 | if r < forcedRefresh: 28 | newpoints.append(sample) 29 | else: 30 | if r < 1 - oldpdf(sample)/newpdf(sample): 31 | newpoints.append(sample) 32 | return reuseindices, newpoints 33 | 34 | 35 | def testImportanceMixing(popsize = 5000, forcedRefresh = 0.0): 36 | import pylab 37 | distr1 = norm() 38 | distr2 = norm(loc = 1.5) 39 | p1 = distr1.rvs(popsize) 40 | inds, np = importanceMixing(p1, distr1.pdf, distr2.pdf, lambda: distr2.rvs()[0], forcedRefresh) 41 | reuse = [p1[i] for i in inds] 42 | p2 = reuse + np 43 | p2b = distr2.rvs(popsize) 44 | pylab.hist(array([p2, p2b]).T, 45 | 20, normed=1, histtype='bar') 46 | pylab.show() 47 | 48 | 49 | if __name__ == '__main__': 50 | testImportanceMixing() -------------------------------------------------------------------------------- /pybrain/auxiliary/kmeans.py: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # k-means++ 3 | # 4 | # this is a k-means clustering algorithm that selects its 5 | # initial cluster centers in a smart way to speed up convergence. 6 | # see: Arthur, D. and Vassilvitskii, S. "k-means++: the advantages 7 | # of careful seeding". ACM-SIAM symposium on Discrete algorithms. 2007 8 | # 9 | # Implementation from Yong Sun's website 10 | # http://blogs.sun.com/yongsun/entry/k_means_and_k_means 11 | ####################################################################### 12 | 13 | __author__ = "Thomas Rueckstiess, ruecksti@in.tum.de" 14 | 15 | from scipy.cluster.vq import kmeans2 16 | from scipy import random, array 17 | from scipy.linalg import norm 18 | 19 | def kinit(X, k): 20 | 'init k seeds according to kmeans++' 21 | n = X.shape[0] 22 | 23 | 'choose the 1st seed randomly, and store D(x)^2 in D[]' 24 | centers = [X[random.randint(n)]] 25 | D = [norm(x - centers[0]) ** 2 for x in X] 26 | 27 | for _ in range(k - 1): 28 | bestDsum = bestIdx = -1 29 | 30 | for i in range(n): 31 | 'Dsum = sum_{x in X} min(D(x)^2,||x-xi||^2)' 32 | Dsum = reduce(lambda x, y:x + y, 33 | (min(D[j], norm(X[j] - X[i]) ** 2) for j in xrange(n))) 34 | 35 | if bestDsum < 0 or Dsum < bestDsum: 36 | bestDsum, bestIdx = Dsum, i 37 | 38 | centers.append (X[bestIdx]) 39 | D = [min(D[i], norm(X[i] - X[bestIdx]) ** 2) for i in xrange(n)] 40 | 41 | return array (centers) 42 | 43 | def kmeanspp(Y, k): 44 | return kmeans2(Y, kinit(Y, k), minit='points') 45 | 46 | -------------------------------------------------------------------------------- /pybrain/datasets/__init__.py: -------------------------------------------------------------------------------- 1 | # $Id$ 2 | from pybrain.datasets.sequential import SequentialDataSet 3 | from pybrain.datasets.supervised import SupervisedDataSet 4 | from pybrain.datasets.unsupervised import UnsupervisedDataSet 5 | from pybrain.datasets.importance import ImportanceDataSet 6 | from pybrain.datasets.reinforcement import ReinforcementDataSet 7 | from pybrain.datasets.classification import ClassificationDataSet, SequenceClassificationDataSet -------------------------------------------------------------------------------- /pybrain/datasets/importance.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from scipy import ones, dot 4 | 5 | from pybrain.datasets.sequential import SequentialDataSet 6 | from pybrain.utilities import fListToString 7 | 8 | 9 | # CHECKME: does this provide for importance-datasets in the non-sequential case 10 | # maybe there should be a second class - or another structure! 11 | 12 | 13 | class ImportanceDataSet(SequentialDataSet): 14 | """ Allows setting an importance value for each of the targets of a sample. """ 15 | 16 | def __init__(self, indim, targetdim): 17 | SequentialDataSet.__init__(self, indim, targetdim) 18 | self.addField('importance', targetdim) 19 | self.link.append('importance') 20 | 21 | def addSample(self, inp, target, importance=None): 22 | """ adds a new sample consisting of input, target and importance. 23 | 24 | :arg inp: the input of the sample 25 | :arg target: the target of the sample 26 | :key importance: the importance of the sample. If left None, the 27 | importance will be set to 1.0 28 | """ 29 | if importance == None: 30 | importance = ones(len(target)) 31 | self.appendLinked(inp, target, importance) 32 | 33 | def _evaluateSequence(self, f, seq, verbose = False): 34 | """ return the importance-ponderated MSE over one sequence. """ 35 | totalError = 0 36 | ponderation = 0. 37 | for input, target, importance in seq: 38 | res = f(input) 39 | e = 0.5 * dot(importance.flatten(), ((target-res).flatten()**2)) 40 | totalError += e 41 | ponderation += sum(importance) 42 | if verbose: 43 | print( 'out: ', fListToString(list(res))) 44 | print( 'correct: ', fListToString(target)) 45 | print( 'importance:', fListToString(importance)) 46 | print( 'error: % .8f' % e) 47 | return totalError, ponderation 48 | 49 | -------------------------------------------------------------------------------- /pybrain/datasets/unsupervised.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = 'Justin S Bayer, bayer.justin@googlemail.com' 4 | __version__ = '$Id$' 5 | 6 | 7 | from pybrain.datasets.dataset import DataSet 8 | 9 | 10 | class UnsupervisedDataSet(DataSet): 11 | """UnsupervisedDataSets have a single field 'sample'.""" 12 | 13 | def __init__(self, dim): 14 | """Initialize an empty unsupervised dataset. 15 | 16 | Pass `dim` to specify the dimensionality of the samples.""" 17 | super(UnsupervisedDataSet, self).__init__() 18 | self.addField('sample', dim) 19 | self.linkFields(['sample']) 20 | self.dim = dim 21 | 22 | # reset the index marker 23 | self.index = 0 24 | 25 | def __reduce__(self): 26 | _, _, state, _, _ = super(UnsupervisedDataSet, self).__reduce__() 27 | creator = self.__class__ 28 | args = (self.dim,) 29 | return creator, args, state, iter([]), iter({}) 30 | 31 | def addSample(self, sample): 32 | self.appendLinked(sample) 33 | 34 | def getSample(self, index): 35 | return self.getLinked(index) 36 | -------------------------------------------------------------------------------- /pybrain/optimization/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.optimization.hillclimber import HillClimber, StochasticHillClimber 2 | from pybrain.optimization.randomsearch import RandomSearch, WeightGuessing, WeightMaskGuessing 3 | from pybrain.optimization.neldermead import NelderMead 4 | from pybrain.optimization.populationbased.__init__ import * 5 | from pybrain.optimization.finitedifference.__init__ import * 6 | from pybrain.optimization.distributionbased.__init__ import * 7 | from pybrain.optimization.memetic.__init__ import * -------------------------------------------------------------------------------- /pybrain/optimization/distributionbased/__init__.py: -------------------------------------------------------------------------------- 1 | from cmaes import CMAES 2 | from fem import FEM 3 | from nes import ExactNES, OriginalNES 4 | from ves import VanillaGradientEvolutionStrategies 5 | from xnes import XNES 6 | from snes import SNES 7 | -------------------------------------------------------------------------------- /pybrain/optimization/distributionbased/distributionbased.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.optimization.optimizer import ContinuousOptimizer 4 | 5 | 6 | class DistributionBasedOptimizer(ContinuousOptimizer): 7 | """ The parent class for all optimization algorithms that are based on 8 | iteratively updating a search distribution. 9 | 10 | Provides a number of potentially useful methods that could be used by subclasses. """ 11 | 12 | online = False 13 | batchSize = 100 14 | 15 | # distribution types 16 | GAUSSIAN = 1 17 | CAUCHY = 2 18 | GENERALIZEDGAUSSIAN = 3 19 | STUDENTT = 4 20 | 21 | distributionType = GAUSSIAN 22 | 23 | storeAllDistributions = False 24 | 25 | def _updateDistribution(self, dparamDeltas): 26 | """ Update the parameters of the current distribution, directly. """ 27 | 28 | def _generateSample(self): 29 | """ Generate 1 sample from the current distribution. """ 30 | 31 | def _generateConformingBatch(self): 32 | """ Generate a batch of samples that conforms to the current distribution. 33 | If importance mixing is enabled, this can reuse old samples. """ 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /pybrain/optimization/finitedifference/__init__.py: -------------------------------------------------------------------------------- 1 | from fd import FiniteDifferences 2 | from spsa import SimpleSPSA 3 | from pgpe import PGPE -------------------------------------------------------------------------------- /pybrain/optimization/finitedifference/fd.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de, Tom Schaul' 2 | 3 | from scipy import ones, zeros, dot, ravel, random 4 | from scipy.linalg import pinv 5 | 6 | from pybrain.auxiliary import GradientDescent 7 | from pybrain.optimization.optimizer import ContinuousOptimizer 8 | 9 | 10 | class FiniteDifferences(ContinuousOptimizer): 11 | """ Basic finite difference method. """ 12 | 13 | epsilon = 1.0 14 | gamma = 0.999 15 | batchSize = 10 16 | 17 | # gradient descent parameters 18 | learningRate = 0.1 19 | learningRateDecay = None 20 | momentum = 0.0 21 | rprop = False 22 | 23 | def _setInitEvaluable(self, evaluable): 24 | ContinuousOptimizer._setInitEvaluable(self, evaluable) 25 | self.current = self._initEvaluable 26 | self.gd = GradientDescent() 27 | self.gd.alpha = self.learningRate 28 | if self.learningRateDecay is not None: 29 | self.gd.alphadecay = self.learningRateDecay 30 | self.gd.momentum = self.momentum 31 | self.gd.rprop = self.rprop 32 | self.gd.init(self._initEvaluable) 33 | 34 | def perturbation(self): 35 | """ produce a parameter perturbation """ 36 | deltas = random.uniform(-self.epsilon, self.epsilon, self.numParameters) 37 | # reduce epsilon by factor gamma 38 | self.epsilon *= self.gamma 39 | return deltas 40 | 41 | def _learnStep(self): 42 | """ calls the gradient calculation function and executes a step in direction 43 | of the gradient, scaled with a small learning rate alpha. """ 44 | 45 | # initialize matrix D and vector R 46 | D = ones((self.batchSize, self.numParameters)) 47 | R = zeros((self.batchSize, 1)) 48 | 49 | # calculate the gradient with pseudo inverse 50 | for i in range(self.batchSize): 51 | deltas = self.perturbation() 52 | x = self.current + deltas 53 | D[i, :] = deltas 54 | R[i, :] = self._oneEvaluation(x) 55 | beta = dot(pinv(D), R) 56 | gradient = ravel(beta) 57 | 58 | # update the weights 59 | self.current = self.gd(gradient) 60 | 61 | -------------------------------------------------------------------------------- /pybrain/optimization/memetic/__init__.py: -------------------------------------------------------------------------------- 1 | from inversememetic import InverseMemeticSearch 2 | from innerinversememetic import InnerInverseMemeticSearch 3 | from memetic import MemeticSearch 4 | from innermemetic import InnerMemeticSearch -------------------------------------------------------------------------------- /pybrain/optimization/memetic/innerinversememetic.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from innermemetic import InnerMemeticSearch 4 | from inversememetic import InverseMemeticSearch 5 | 6 | class InnerInverseMemeticSearch(InnerMemeticSearch, InverseMemeticSearch): 7 | """ inverse of inner memetic search""" 8 | 9 | def _learnStep(self): 10 | self.switchMutations() 11 | InnerMemeticSearch._learnStep(self) 12 | self.switchMutations() -------------------------------------------------------------------------------- /pybrain/optimization/memetic/innermemetic.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from memetic import MemeticSearch 4 | from pybrain.optimization.populationbased.es import ES 5 | 6 | 7 | class InnerMemeticSearch(ES, MemeticSearch): 8 | """ Population-based memetic search """ 9 | 10 | mu = 5 11 | lambada = 5 12 | 13 | def _learnStep(self): 14 | self.switchMutations() 15 | ES._learnStep(self) 16 | self.switchMutations() 17 | 18 | @property 19 | def batchSize(self): 20 | if self.evaluatorIsNoisy: 21 | return (self.mu + self.lambada)*self.localSteps 22 | else: 23 | return self.lambada*self.localSteps 24 | -------------------------------------------------------------------------------- /pybrain/optimization/memetic/inversememetic.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from memetic import MemeticSearch 4 | 5 | 6 | class InverseMemeticSearch(MemeticSearch): 7 | """ Interleaving local search with topology search (inverse of memetic search) """ 8 | 9 | def _learnStep(self): 10 | self.switchMutations() 11 | MemeticSearch._learnStep(self) 12 | self.switchMutations() 13 | -------------------------------------------------------------------------------- /pybrain/optimization/neldermead.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from scipy.optimize import fmin 4 | 5 | from pybrain.optimization.optimizer import ContinuousOptimizer 6 | 7 | 8 | class DesiredFoundException(Exception): 9 | """ The desired target has been found. """ 10 | 11 | 12 | class NelderMead(ContinuousOptimizer): 13 | """Do the optimization using a simple wrapper for scipy's fmin.""" 14 | 15 | # acceptable relative error in the evaluator for convergence. 16 | stopPrecision = 1e-6 17 | 18 | mustMinimize = True 19 | 20 | 21 | def _callback(self, *_): 22 | if self._stoppingCriterion(): 23 | raise DesiredFoundException() 24 | 25 | def _learnStep(self): 26 | try: 27 | fmin(func = self._oneEvaluation, 28 | x0 = self.bestEvaluable, 29 | callback = self._callback, 30 | ftol = self.stopPrecision, 31 | maxfun = self.maxEvaluations-self.numEvaluations-1, 32 | disp = self.verbose) 33 | except DesiredFoundException: 34 | pass 35 | # the algorithm has finished: no point in doing more steps. 36 | self.maxLearningSteps = self.numLearningSteps 37 | -------------------------------------------------------------------------------- /pybrain/optimization/populationbased/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.optimization.populationbased.es import ES 2 | from pybrain.optimization.populationbased.ga import GA 3 | from pybrain.optimization.populationbased.pso import ParticleSwarmOptimizer 4 | from pybrain.optimization.populationbased.multiobjective.__init__ import * 5 | #from pybrain.optimization.populationbased.coevolution.__init__ import * -------------------------------------------------------------------------------- /pybrain/optimization/populationbased/coevolution/__init__.py: -------------------------------------------------------------------------------- 1 | from coevolution import Coevolution 2 | from competitivecoevolution import CompetitiveCoevolution 3 | from multipopulationcoevolution import MultiPopulationCoevolution -------------------------------------------------------------------------------- /pybrain/optimization/populationbased/evolution.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.utilities import abstractMethod 4 | from pybrain.optimization.optimizer import BlackBoxOptimizer 5 | 6 | 7 | class Evolution(BlackBoxOptimizer): 8 | """ Base class for evolutionary algorithms, seen as function optimizers. """ 9 | 10 | populationSize = 10 11 | 12 | storeAllPopulations = False 13 | 14 | mustMaximize = True 15 | 16 | def _additionalInit(self): 17 | self.currentpop = [] 18 | self.fitnesses = [] 19 | self._allGenerations = [] 20 | self.initPopulation() 21 | 22 | def _learnStep(self): 23 | """ do one generation step """ 24 | self.fitnesses = [self._oneEvaluation(indiv) for indiv in self.currentpop] 25 | if self.storeAllPopulations: 26 | self._allGenerations.append((self.currentpop, self.fitnesses)) 27 | self.produceOffspring() 28 | 29 | def initPopulation(self): 30 | """ initialize the population """ 31 | abstractMethod() 32 | 33 | def produceOffspring(self): 34 | """ generate the new generation of offspring, given the current population, and their fitnesses """ 35 | abstractMethod() 36 | 37 | @property 38 | def batchSize(self): 39 | return self.populationSize -------------------------------------------------------------------------------- /pybrain/optimization/populationbased/multiobjective/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.optimization.populationbased.multiobjective.nsga2 import MultiObjectiveGA 2 | """ added by JPQ """ 3 | from pybrain.optimization.populationbased.multiobjective.constnsga2 import ConstMultiObjectiveGA 4 | # --- -------------------------------------------------------------------------------- /pybrain/optimization/randomsearch.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.optimization.optimizer import BlackBoxOptimizer, TopologyOptimizer 4 | 5 | 6 | class RandomSearch(BlackBoxOptimizer): 7 | """ Every point is chosen randomly, independently of all previous ones. """ 8 | 9 | def _additionalInit(self): 10 | self._oneEvaluation(self._initEvaluable) 11 | 12 | def _learnStep(self): 13 | new = self._initEvaluable.copy() 14 | new.randomize() 15 | self._oneEvaluation(new) 16 | 17 | 18 | class WeightGuessing(RandomSearch): 19 | """ Just an Alias. """ 20 | 21 | 22 | class WeightMaskGuessing(WeightGuessing, TopologyOptimizer): 23 | """ random search, with a random mask that disables weights """ 24 | -------------------------------------------------------------------------------- /pybrain/rl/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/rl/__init__.py -------------------------------------------------------------------------------- /pybrain/rl/agents/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.agents.learning import LearningAgent 2 | from pybrain.rl.agents.optimization import OptimizationAgent -------------------------------------------------------------------------------- /pybrain/rl/agents/agent.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.utilities import abstractMethod, Named 4 | 5 | class Agent(Named): 6 | """ An agent is an entity capable of producing actions, based on previous observations. 7 | Generally it will also learn from experience. It can interact directly with a Task. 8 | """ 9 | 10 | def integrateObservation(self, obs): 11 | """ Integrate the current observation of the environment. 12 | :arg obs: The last observation returned from the environment 13 | :type obs: by default, this is assumed to be a numpy array of doubles 14 | """ 15 | pass 16 | 17 | def getAction(self): 18 | """ Return a chosen action. 19 | :rtype: by default, this is assumed to ba a numpy array of doubles. 20 | :note: This method is abstract and needs to be implemented. 21 | """ 22 | abstractMethod() 23 | 24 | def giveReward(self, r): 25 | """ Reward or punish the agent. 26 | :key r: reward, if C{r} is positive, punishment if C{r} is negative 27 | :type r: double 28 | """ 29 | pass 30 | 31 | def newEpisode(self): 32 | """ Inform the agent that a new episode has started. """ 33 | pass 34 | -------------------------------------------------------------------------------- /pybrain/rl/agents/optimization.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.rl.agents.agent import Agent 4 | 5 | class OptimizationAgent(Agent): 6 | """ A simple wrapper to allow optimizers to conform to the RL interface. 7 | Works only in conjunction with EpisodicExperiment. 8 | """ 9 | def __init__(self, module, learner): 10 | self.module = module 11 | self.learner = learner 12 | 13 | -------------------------------------------------------------------------------- /pybrain/rl/environments/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.environments.environment import Environment 2 | from pybrain.rl.environments.task import Task 3 | from pybrain.rl.environments.episodic import EpisodicTask -------------------------------------------------------------------------------- /pybrain/rl/environments/cartpole/__init__.py: -------------------------------------------------------------------------------- 1 | # try importing the external dependencies 2 | try: 3 | from matplotlib.mlab import rk4 4 | except ImportError: 5 | raise ImportError('This environment needs the matplotlib library installed.') 6 | 7 | from pybrain.rl.environments.cartpole.cartpole import CartPoleEnvironment, CartPoleLinEnvironment 8 | from pybrain.rl.environments.cartpole.renderer import CartPoleRenderer 9 | from pybrain.rl.environments.cartpole.balancetask import BalanceTask, EasyBalanceTask, DiscreteBalanceTask, DiscreteNoHelpTask, JustBalanceTask, LinearizedBalanceTask, DiscretePOMDPTask 10 | from pybrain.rl.environments.cartpole.doublepole import DoublePoleEnvironment 11 | from pybrain.rl.environments.cartpole.nonmarkovpole import NonMarkovPoleEnvironment 12 | from pybrain.rl.environments.cartpole.nonmarkovdoublepole import NonMarkovDoublePoleEnvironment -------------------------------------------------------------------------------- /pybrain/rl/environments/cartpole/doublepole.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from cartpole import CartPoleEnvironment 4 | from pybrain.rl.environments import Environment 5 | 6 | 7 | class DoublePoleEnvironment(Environment): 8 | """ two poles to be balanced from the same cart. """ 9 | 10 | indim = 1 11 | ooutdim = 6 12 | 13 | def __init__(self): 14 | self.p1 = CartPoleEnvironment() 15 | self.p2 = CartPoleEnvironment() 16 | self.p2.l = 0.05 17 | self.p2.mp = 0.01 18 | self.reset() 19 | 20 | def getSensors(self): 21 | """ returns the state one step (dt) ahead in the future. stores the state in 22 | self.sensors because it is needed for the next calculation. The sensor return 23 | vector has 6 elements: theta1, theta1', theta2, theta2', s, s' 24 | (s being the distance from the origin). 25 | """ 26 | s1 = self.p1.getSensors() 27 | s2 = self.p2.getSensors() 28 | self.sensors = (s1[0], s1[1], s2[0], s2[1], s2[2], s2[3]) 29 | return self.sensors 30 | 31 | def reset(self): 32 | """ re-initializes the environment, setting the cart back in a random position. 33 | """ 34 | self.p1.reset() 35 | self.p2.reset() 36 | # put cart in the same place: 37 | self.p2.sensors = (self.p2.sensors[0], self.p2.sensors[1], self.p1.sensors[2], self.p1.sensors[3]) 38 | self.getSensors() 39 | 40 | def performAction(self, action): 41 | """ stores the desired action for the next runge-kutta step. 42 | """ 43 | self.p1.performAction(action) 44 | self.p2.performAction(action) 45 | 46 | def getCartPosition(self): 47 | """ auxiliary access to just the cart position, to be used by BalanceTask """ 48 | return self.sensors[4] 49 | 50 | def getPoleAngles(self): 51 | """ auxiliary access to just the pole angle(s), to be used by BalanceTask """ 52 | return [self.sensors[0], self.sensors[2]] 53 | 54 | -------------------------------------------------------------------------------- /pybrain/rl/environments/cartpole/fast_version/__init__.py: -------------------------------------------------------------------------------- 1 | try: 2 | import cartpolewrap 3 | except ImportError: 4 | raise ImportError('This task needs to be compiled. Please use the script: cartpolecompile.py') 5 | 6 | from pybrain.rl.environments.cartpole.fast_version.cartpoleenv import FastCartPoleTask -------------------------------------------------------------------------------- /pybrain/rl/environments/cartpole/fast_version/cartpolecompile.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | 4 | from distutils.core import setup 5 | from Pyrex.Distutils.extension import Extension 6 | from Pyrex.Distutils import build_ext 7 | import platform, sys, os 8 | 9 | # Mac users need an environment variable set: 10 | if platform.system() == 'Darwin': 11 | # get mac os version 12 | version = platform.mac_ver() 13 | # don't care about sub-versions like 10.5.2 14 | baseversion = version[0].rsplit('.', 1)[0] 15 | os.environ["MACOSX_DEPLOYMENT_TARGET"] = baseversion 16 | 17 | sys.argv.append('build_ext') 18 | sys.argv.append('--inplace') 19 | 20 | if platform.system() == 'Windows': 21 | sys.argv.append('-c') 22 | sys.argv.append('mingw32') 23 | 24 | setup(ext_modules=[Extension('cartpolewrap', ['cartpolewrap.pyx', 'cartpole.cpp'], 25 | pyrex_cplus=[True], 26 | )], 27 | cmdclass={'build_ext': build_ext}) 28 | 29 | -------------------------------------------------------------------------------- /pybrain/rl/environments/cartpole/nonmarkovdoublepole.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from doublepole import DoublePoleEnvironment 4 | from nonmarkovpole import NonMarkovPoleEnvironment 5 | 6 | 7 | class NonMarkovDoublePoleEnvironment(DoublePoleEnvironment, NonMarkovPoleEnvironment): 8 | """ DoublePoleEnvironment which does not give access to the derivatives. """ 9 | 10 | outdim = 3 11 | 12 | def getSensors(self): 13 | """ returns the state one step (dt) ahead in the future. stores the state in 14 | self.sensors because it is needed for the next calculation. The sensor return 15 | vector has 3 elements: theta1, theta2, s 16 | (s being the distance from the origin). 17 | """ 18 | tmp = DoublePoleEnvironment.getSensors(self) 19 | return (tmp[0], tmp[2], tmp[4]) 20 | 21 | def getPoleAngles(self): 22 | """ auxiliary access to just the pole angle(s), to be used by BalanceTask """ 23 | return [self.sensors[0], self.sensors[1]] 24 | 25 | def getCartPosition(self): 26 | """ auxiliary access to just the cart position, to be used by BalanceTask """ 27 | return self.sensors[2] 28 | 29 | 30 | -------------------------------------------------------------------------------- /pybrain/rl/environments/cartpole/nonmarkovpole.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from cartpole import CartPoleEnvironment 4 | 5 | 6 | class NonMarkovPoleEnvironment(CartPoleEnvironment): 7 | """ CartPoleEnvironment which does not give access to the derivatives. """ 8 | 9 | outdim = 2 10 | 11 | def getSensors(self): 12 | """ returns the state one step (dt) ahead in the future. stores the state in 13 | self.sensors because it is needed for the next calculation. The sensor return 14 | vector has 2 elements: theta, s 15 | (s being the distance from the origin). 16 | """ 17 | tmp = CartPoleEnvironment.getSensors(self) 18 | return (tmp[0], tmp[2]) 19 | 20 | def getCartPosition(self): 21 | """ auxiliary access to just the cart position, to be used by BalanceTask """ 22 | return self.sensors[1] 23 | 24 | 25 | -------------------------------------------------------------------------------- /pybrain/rl/environments/classic/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/rl/environments/classic/__init__.py -------------------------------------------------------------------------------- /pybrain/rl/environments/environment.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.utilities import abstractMethod 4 | 5 | 6 | class Environment(object): 7 | """ The general interface for whatever we would like to model, learn about, 8 | predict, or simply interact in. We can perform actions, and access 9 | (partial) observations. 10 | """ 11 | 12 | # the number of action values the environment accepts 13 | indim = 0 14 | 15 | # the number of sensor values the environment produces 16 | outdim = 0 17 | 18 | # discrete state space 19 | discreteStates = False 20 | 21 | # discrete action space 22 | discreteActions = False 23 | 24 | # number of possible actions for discrete action space 25 | numActions = None 26 | 27 | def getSensors(self): 28 | """ the currently visible state of the world (the observation may be 29 | stochastic - repeated calls returning different values) 30 | 31 | :rtype: by default, this is assumed to be a numpy array of doubles 32 | :note: This function is abstract and has to be implemented. 33 | """ 34 | abstractMethod() 35 | 36 | def performAction(self, action): 37 | """ perform an action on the world that changes it's internal state (maybe 38 | stochastically). 39 | :key action: an action that should be executed in the Environment. 40 | :type action: by default, this is assumed to be a numpy array of doubles 41 | :note: This function is abstract and has to be implemented. 42 | """ 43 | abstractMethod() 44 | 45 | def reset(self): 46 | """ Most environments will implement this optional method that allows for 47 | reinitialization. 48 | """ 49 | 50 | 51 | -------------------------------------------------------------------------------- /pybrain/rl/environments/fitnessevaluator.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.utilities import abstractMethod 4 | 5 | 6 | class FitnessEvaluator(object): 7 | """ The superclass of all evaluators that produce a single output value, 8 | given an arbitrary input. 9 | """ 10 | 11 | # what would be the desired fitness? 12 | desiredValue = None 13 | 14 | # what is the desirable direction? 15 | toBeMinimized = False 16 | 17 | def f(self, x): 18 | """ The function itself, to be defined by subclasses """ 19 | abstractMethod() 20 | 21 | def __call__(self, x): 22 | """ All FitnessEvaluators are callable. 23 | 24 | :return: float """ 25 | return self.f(x) 26 | -------------------------------------------------------------------------------- /pybrain/rl/environments/flexcube/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.environments.flexcube.environment import FlexCubeEnvironment 2 | from pybrain.rl.environments.flexcube.tasks import * 3 | -------------------------------------------------------------------------------- /pybrain/rl/environments/flexcube/masspoint.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Frank Sehnke, sehnke@in.tum.de' 2 | 3 | 4 | class MArray: 5 | def __init__(self): 6 | self.field = {} 7 | def get(self, i): 8 | return self.field[i] 9 | def set(self, i, val): 10 | self.field[i] = val 11 | 12 | class MassPoint: 13 | def __init__(self): 14 | self.pos = [0.0, 0.0, 0.0] 15 | self.vel = [0.0, 0.0, 0.0] 16 | def setPos(self, pos): 17 | self.pos = pos 18 | def setVel(self, vel): 19 | self.vel = vel 20 | 21 | -------------------------------------------------------------------------------- /pybrain/rl/environments/functions/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.environments.functions.function import FunctionEnvironment 2 | from pybrain.rl.environments.functions.unimodal import SchwefelFunction, SphereFunction, TabletFunction, DiffPowFunction, \ 3 | CigarFunction, ElliFunction, RosenbrockFunction 4 | from pybrain.rl.environments.functions.multimodal import RastriginFunction, AckleyFunction, GriewankFunction, Schwefel_2_13Function, \ 5 | WeierstrassFunction, FunnelFunction 6 | from pybrain.rl.environments.functions.unbounded import ParabRFunction, SharpRFunction, LinearFunction 7 | from pybrain.rl.environments.functions.transformations import oppositeFunction, RotateFunction, TranslateFunction -------------------------------------------------------------------------------- /pybrain/rl/environments/functions/unbounded.py: -------------------------------------------------------------------------------- 1 | """ The functions implemented here are standard benchmarks from literature. """ 2 | 3 | __author__ = 'Tom Schaul, tom@idsia.ch' 4 | 5 | from math import sqrt 6 | 7 | from pybrain.rl.environments.functions.function import FunctionEnvironment 8 | 9 | 10 | class UnboundedFunctionEnvironment(FunctionEnvironment): 11 | """ a function that does not have a minimum """ 12 | desiredValue = -1e3 13 | toBeMinimized = True 14 | 15 | 16 | class LinearFunction(UnboundedFunctionEnvironment): 17 | def f(self, x): 18 | return sum(x) 19 | 20 | 21 | class ParabRFunction(UnboundedFunctionEnvironment): 22 | def f(self, x): 23 | return -x[0] + 100 * sum(x[1:]**2) 24 | 25 | 26 | class SharpRFunction(UnboundedFunctionEnvironment): 27 | def f(self, x): 28 | return -x[0] + 100*sqrt(sum(x[1:]**2)) 29 | 30 | -------------------------------------------------------------------------------- /pybrain/rl/environments/graphical.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from environment import Environment 4 | 5 | class GraphicalEnvironment(Environment): 6 | """ Special type of environment that has graphical output and therefore needs a renderer. 7 | """ 8 | 9 | def __init__(self): 10 | self.renderer = None 11 | 12 | def setRenderer(self, renderer): 13 | """ set the renderer, which is an object of or inherited from class Renderer. 14 | 15 | :key renderer: The renderer that should display the Environment 16 | :type renderer: L{Renderer} 17 | :see: Renderer 18 | """ 19 | self.renderer = renderer 20 | 21 | def getRenderer(self): 22 | """ returns the current renderer. 23 | 24 | :return: the current renderer 25 | :rtype: L{Renderer} 26 | """ 27 | return self.renderer 28 | 29 | def hasRenderer(self): 30 | """ tells you whether or not a Renderer has been set previously 31 | 32 | :return: True if a renderer was set, False otherwise 33 | :rtype: Boolean 34 | """ 35 | return (self.getRenderer() != None) 36 | -------------------------------------------------------------------------------- /pybrain/rl/environments/mazes/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.environments.mazes.maze import Maze 2 | from pybrain.rl.environments.mazes.polarmaze import PolarMaze 3 | from pybrain.rl.environments.mazes.tasks.__init__ import * -------------------------------------------------------------------------------- /pybrain/rl/environments/mazes/polarmaze.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from scipy import zeros 4 | from random import choice, random 5 | 6 | from maze import Maze 7 | 8 | 9 | class PolarMaze(Maze): 10 | """ Mazes with the emphasis on Perseus: allow him to turn, go forward or backward. 11 | Thus there are 4 states per position. 12 | """ 13 | 14 | actions = 5 15 | 16 | Stay = 0 17 | Forward = 1 18 | TurnAround = 2 19 | TurnLeft = 3 20 | TurnRight = 4 21 | 22 | allActions = [Stay, Forward, TurnAround, TurnLeft, TurnRight] 23 | 24 | def reset(self): 25 | Maze.reset(self) 26 | self.perseusDir = choice(range(4)) 27 | 28 | def performAction(self, action): 29 | if self.stochAction > 0: 30 | if random() < self.stochAction: 31 | action = choice(range(len(PolarMaze.allActions))) 32 | act = PolarMaze.allActions[action] 33 | self.bang = False 34 | if act == self.Forward: 35 | tmp = self._moveInDir(self.perseus, Maze.allActions[self.perseusDir]) 36 | if self.mazeTable[tmp] == False: 37 | self.perseus = tmp 38 | else: 39 | self.bang = True 40 | elif act == self.TurnLeft: 41 | self.perseusDir = (self.perseusDir + 1) % 4 42 | elif act == self.TurnRight: 43 | self.perseusDir = (self.perseusDir - 1) % 4 44 | elif act == self.TurnAround: 45 | self.perseusDir = (self.perseusDir + 2) % 4 46 | 47 | def getSensors(self): 48 | obs = Maze.getSensors(self) 49 | res = zeros(4) 50 | res[:4 - self.perseusDir] = obs[self.perseusDir:] 51 | res[4 - self.perseusDir:] = obs[:self.perseusDir] 52 | return res 53 | 54 | def __str__(self): 55 | return Maze.__str__(self) + '(dir:' + str(self.perseusDir) + ')' 56 | -------------------------------------------------------------------------------- /pybrain/rl/environments/mazes/tasks/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.environments.mazes.tiger import TigerTask 2 | from pybrain.rl.environments.mazes.maze import TrivialMaze, MazeTask 3 | from pybrain.rl.environments.mazes.cheesemaze import CheeseMaze 4 | from pybrain.rl.environments.mazes.tmaze import TMaze 5 | from pybrain.rl.environments.mazes.maze4x3 import FourByThreeMaze 6 | from pybrain.rl.environments.mazes.maze89state import EightyNineStateMaze 7 | from pybrain.rl.environments.mazes.shuttle import ShuttleDocking 8 | from pybrain.rl.environments.mazes.mdp import MDPMazeTask 9 | -------------------------------------------------------------------------------- /pybrain/rl/environments/mazes/tasks/cheesemaze.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from scipy import zeros, array 4 | 5 | from maze import MazeTask 6 | 7 | 8 | class CheeseMaze(MazeTask): 9 | """ 10 | ####### 11 | # # 12 | # # # # 13 | # #*# # 14 | ####### 15 | """ 16 | 17 | observations = 7 18 | discount = 0.95 19 | 20 | topology = array([[1] * 7, 21 | [1, 0, 1, 0, 1, 0, 1], 22 | [1, 0, 1, 0, 1, 0, 1], 23 | [1, 0, 0, 0, 0, 0, 1], 24 | [1] * 7]) 25 | goal = (1, 3) 26 | 27 | def getObservation(self): 28 | """ observations are encoded in a 1-n encoding of possible wall combinations. """ 29 | res = zeros(7) 30 | obs = self.env.getSensors() 31 | if self.env.perseus == self.env.goal: 32 | res[6] = 1 33 | elif sum(obs) == 3: 34 | res[0] = 1 35 | elif sum(obs) == 1: 36 | res[5] = 1 37 | elif obs[0] == obs[1]: 38 | res[1] = 1 39 | elif obs[0] == obs[3]: 40 | res[2] = 1 41 | elif obs[0] == obs[2]: 42 | if obs[0] == 1: 43 | res[3] = 1 44 | else: 45 | res[4] = 1 46 | return res 47 | -------------------------------------------------------------------------------- /pybrain/rl/environments/mazes/tasks/maze.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from scipy import array 4 | 5 | from pomdp import POMDPTask 6 | from pybrain.rl.environments.mazes import Maze 7 | from pybrain.rl.environments.task import Task 8 | 9 | 10 | class MazeTask(POMDPTask): 11 | """ a task corresponding to a maze environment """ 12 | 13 | bangPenalty = 0 14 | defaultPenalty = 0 15 | finalReward = 1 16 | 17 | topology = None 18 | goal = None 19 | initPos = None 20 | mazeclass = Maze 21 | 22 | stochObs = 0 23 | stochAction = 0 24 | 25 | @property 26 | def noisy(self): 27 | return self.stochObs > 0 28 | 29 | def __init__(self, **args): 30 | self.setArgs(**args) 31 | Task.__init__(self, self.mazeclass(self.topology, self.goal, initPos=self.initPos, 32 | stochObs=self.stochObs, stochAction=self.stochAction)) 33 | self.minReward = min(self.bangPenalty, self.defaultPenalty) 34 | self.reset() 35 | 36 | def getReward(self): 37 | if self.env.perseus == self.env.goal: 38 | return self.finalReward 39 | elif self.env.bang: 40 | return self.bangPenalty 41 | else: 42 | return self.defaultPenalty 43 | 44 | def isFinished(self): 45 | return self.env.perseus == self.env.goal or POMDPTask.isFinished(self) 46 | 47 | def __str__(self): 48 | return str(self.env) 49 | 50 | 51 | class TrivialMaze(MazeTask): 52 | """ 53 | ##### 54 | #. *# 55 | ##### 56 | """ 57 | discount = 0.8 58 | initPos = [(1, 1)] 59 | topology = array([[1] * 5, 60 | [1, 0, 0, 0, 1], 61 | [1] * 5, ]) 62 | goal = (1, 3) 63 | 64 | -------------------------------------------------------------------------------- /pybrain/rl/environments/mazes/tasks/maze4x3.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from random import random, choice 4 | from scipy import array, zeros 5 | 6 | from maze import MazeTask 7 | 8 | 9 | class FourByThreeMaze(MazeTask): 10 | """ 11 | ###### 12 | # *# 13 | # # -# 14 | # # 15 | ###### 16 | 17 | The '-' spot if absorbing, and giving negative reward. 18 | """ 19 | 20 | discount = 0.95 21 | 22 | defaultPenalty = -0.04 23 | bangPenalty = -0.04 24 | minReward = -1 25 | 26 | stochAction = 0.1 27 | 28 | initPos = [(1, 1), (1, 2), (1, 3), (2, 1), (3, 1), (3, 2), (2, 3), (3, 3), (1, 4)] 29 | topology = array([[1] * 6, 30 | [1, 0, 0, 0, 0, 1], 31 | [1, 0, 1, 0, 0, 1], 32 | [1, 0, 0, 0, 0, 1], 33 | [1] * 6, ]) 34 | goal = (3, 4) 35 | 36 | def reset(self): 37 | MazeTask.reset(self) 38 | self.bad = False 39 | 40 | def performAction(self, action): 41 | poss = [] 42 | for a in range(self.actions): 43 | if action - a % 4 != 2: 44 | poss.append(a) 45 | if random() < self.stochAction * len(poss): 46 | MazeTask.performAction(self, choice(poss)) 47 | else: 48 | MazeTask.performAction(self, action) 49 | 50 | def getReward(self): 51 | if self.bad: 52 | return self.minReward 53 | else: 54 | return MazeTask.getReward(self) 55 | 56 | def getObservation(self): 57 | """only walls on w, E, both or neither are observed. """ 58 | res = zeros(4) 59 | all = self.env.getSensors() 60 | res[0] = all[3] 61 | res[1] = all[1] 62 | res[2] = all[3] and all[1] 63 | res[3] = not all[3] and not all[1] 64 | return res 65 | -------------------------------------------------------------------------------- /pybrain/rl/environments/mazes/tasks/maze89state.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from scipy import array 4 | 5 | from maze import MazeTask 6 | from pybrain.rl.environments.mazes import PolarMaze 7 | 8 | 9 | class EightyNineStateMaze(MazeTask): 10 | """ 11 | ######### 12 | ## ## 13 | # # # # 14 | ## # # ## 15 | # # # *# 16 | ## ## 17 | ######### 18 | """ 19 | 20 | mazeclass = PolarMaze 21 | topology = array([[1]*9, 22 | [1, 1, 0, 0, 0, 0, 0, 1, 1], 23 | [1, 0, 0, 1, 0, 1, 0, 0, 1], 24 | [1, 1, 0, 1, 0, 1, 0, 1, 1], 25 | [1, 0, 0, 1, 0, 1, 0, 0, 1], 26 | [1, 1, 0, 0, 0, 0, 0, 1, 1], 27 | [1]*9]) 28 | goal = (2, 7) 29 | stochAction = 0.1 30 | stochObs = 0.1 31 | -------------------------------------------------------------------------------- /pybrain/rl/environments/mazes/tasks/mdp.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from pybrain.rl.environments import Task 4 | from scipy import array 5 | 6 | class MDPMazeTask(Task): 7 | """ This is a MDP task for the MazeEnvironment. The state is fully observable, 8 | giving the agent the current position of perseus. Reward is given on reaching 9 | the goal, otherwise no reward. """ 10 | 11 | def getReward(self): 12 | """ compute and return the current reward (i.e. corresponding to the last action performed) """ 13 | if self.env.goal == self.env.perseus: 14 | self.env.reset() 15 | reward = 1. 16 | else: 17 | reward = 0. 18 | return reward 19 | 20 | def performAction(self, action): 21 | """ The action vector is stripped and the only element is cast to integer and given 22 | to the super class. 23 | """ 24 | Task.performAction(self, int(action[0])) 25 | 26 | 27 | def getObservation(self): 28 | """ The agent receives its position in the maze, to make this a fully observable 29 | MDP problem. 30 | """ 31 | obs = array([self.env.perseus[0] * self.env.mazeTable.shape[0] + self.env.perseus[1]]) 32 | return obs 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /pybrain/rl/environments/mazes/tasks/pomdp.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from scipy import ndarray 4 | 5 | from pybrain.rl.environments import EpisodicTask 6 | from pybrain.utilities import Named, drawIndex 7 | 8 | 9 | class POMDPTask(EpisodicTask, Named): 10 | """ Partially observable episodic MDP (with discrete actions) 11 | Has actions that can be performed, and observations in every state. 12 | By default, the observation is a vector, and the actions are integers. 13 | """ 14 | # number of observations 15 | observations = 4 16 | 17 | # number of possible actions 18 | actions = 4 19 | 20 | # maximal number of steps before the episode is stopped 21 | maxSteps = None 22 | 23 | # the lower bound on the reward value 24 | minReward = 0 25 | 26 | def __init__(self, **args): 27 | self.setArgs(**args) 28 | self.steps = 0 29 | 30 | @property 31 | def indim(self): 32 | return self.actions 33 | 34 | @property 35 | def outdim(self): 36 | return self.observations 37 | 38 | def reset(self): 39 | self.steps = 0 40 | EpisodicTask.reset(self) 41 | 42 | def isFinished(self): 43 | if self.maxSteps != None: 44 | return self.steps >= self.maxSteps 45 | return False 46 | 47 | def performAction(self, action): 48 | """ POMDP tasks, as they have discrete actions, can me used by providing either an index, 49 | or an array with a 1-in-n coding (which can be stochastic). """ 50 | if type(action) == ndarray: 51 | action = drawIndex(action, tolerant = True) 52 | self.steps += 1 53 | EpisodicTask.performAction(self, action) -------------------------------------------------------------------------------- /pybrain/rl/environments/mazes/tasks/tiger.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from random import choice, random 4 | from scipy import array 5 | 6 | from pomdp import POMDPTask 7 | 8 | 9 | class TigerTask(POMDPTask): 10 | """ two doors, behind one is a tiger - we can listen or open. """ 11 | 12 | observations = 2 13 | actions = 3 14 | minReward = -100 15 | discount = 0.75 16 | 17 | # allowed actions: 18 | Listen = 0 19 | OpenLeft = 1 20 | OpenRight = 2 21 | 22 | # stochsticity on the observation 23 | stochObs = 0.15 24 | 25 | def reset(self): 26 | self.steps = 0 27 | self.cumreward = 0 28 | self.tigerLeft = choice([True, False]) 29 | self.done = False 30 | self.nextReward = -1 31 | 32 | def performAction(self, action): 33 | self.steps += 1 34 | if action != self.Listen: 35 | self.done = True 36 | if ((action == self.OpenLeft and self.tigerLeft) 37 | or (action == self.OpenRight and not self.tigerLeft)): 38 | self.nextReward = -100 39 | else: 40 | self.nextReward = 10 41 | 42 | def getObservation(self): 43 | """ do we think we heard something on the left or on the right? """ 44 | if self.tigerLeft: 45 | obs = array([1, 0]) 46 | else: 47 | obs = array([0, 1]) 48 | if random() < self.stochObs: 49 | obs = 1 - obs 50 | return obs 51 | 52 | def isFinished(self): 53 | return self.done or POMDPTask.isFinished(self) 54 | 55 | def getReward(self): 56 | return self.nextReward 57 | -------------------------------------------------------------------------------- /pybrain/rl/environments/mazes/tasks/tmaze.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from scipy import array, zeros 4 | from random import choice 5 | 6 | from maze import MazeTask 7 | 8 | 9 | class TMaze(MazeTask): 10 | """ 11 | ############# 12 | ###########*# 13 | #. # 14 | ########### # 15 | ############# 16 | 17 | 1-in-n encoding for observations. 18 | """ 19 | 20 | discount = 0.98 21 | observations = 4 22 | 23 | finalReward = 4 24 | bangPenalty = -0.1 25 | 26 | length = 10 27 | 28 | def __init__(self, **args): 29 | self.initPos = [(2, 1)] 30 | self.setArgs(**args) 31 | columns = [[1] * 5] 32 | for dummy in range(self.length): 33 | columns.append([1, 1, 0, 1, 1]) 34 | columns.append([1, 0, 0, 0, 1]) 35 | columns.append([1] * 5) 36 | self.topology = array(columns).T 37 | MazeTask.__init__(self, **args) 38 | 39 | def reset(self): 40 | MazeTask.reset(self) 41 | goUp = choice([True, False]) 42 | self.specialObs = goUp 43 | if goUp: 44 | self.env.goal = (3, self.length + 1) 45 | else: 46 | self.env.goal = (1, self.length + 1) 47 | 48 | def getObservation(self): 49 | res = zeros(4) 50 | if self.env.perseus == self.env.initPos[0]: 51 | if self.specialObs: 52 | res[0] = 1 53 | else: 54 | res[1] = 1 55 | elif self.env.perseus[1] == self.length + 1: 56 | res[2] = 1 57 | else: 58 | res[3] = 1 59 | return res 60 | 61 | def getReward(self): 62 | if self.env.perseus[1] == self.length + 1: 63 | if abs(self.env.perseus[0] - self.env.goal[0]) == 2: 64 | # bad choice taken 65 | self.env.perseus = self.env.goal 66 | return self.bangPenalty 67 | return MazeTask.getReward(self) 68 | -------------------------------------------------------------------------------- /pybrain/rl/environments/ode/__init__.py: -------------------------------------------------------------------------------- 1 | try: 2 | import ode, xode.parser, xode.body, xode.geom #@Reimport 3 | except ImportError: 4 | raise ImportError('This environment requires the py-ode package to be installed on your system.') 5 | 6 | from pybrain.rl.environments.ode.environment import ODEEnvironment 7 | from pybrain.rl.environments.ode.sensors import * 8 | from pybrain.rl.environments.ode.actuators import * 9 | from pybrain.rl.environments.ode.instances import * -------------------------------------------------------------------------------- /pybrain/rl/environments/ode/instances/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.environments.ode.instances.johnnie import JohnnieEnvironment 2 | from pybrain.rl.environments.ode.instances.ccrl import CCRLEnvironment 3 | from pybrain.rl.environments.ode.instances.acrobot import AcrobotEnvironment 4 | -------------------------------------------------------------------------------- /pybrain/rl/environments/ode/instances/acrobot.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Frank Sehnke, sehnke@in.tum.de' 2 | 3 | from pybrain.rl.environments.ode import ODEEnvironment, sensors, actuators 4 | import imp 5 | from scipy import array 6 | 7 | class AcrobotEnvironment(ODEEnvironment): 8 | def __init__(self, renderer=True, realtime=True, ip="127.0.0.1", port="21590", buf='16384'): 9 | ODEEnvironment.__init__(self, renderer, realtime, ip, port, buf) 10 | # load model file 11 | self.loadXODE(imp.find_module('pybrain')[1] + "/rl/environments/ode/models/acrobot.xode") 12 | 13 | # standard sensors and actuators 14 | self.addSensor(sensors.JointSensor()) 15 | self.addSensor(sensors.JointVelocitySensor()) 16 | self.addActuator(actuators.JointActuator()) 17 | 18 | #set act- and obsLength, the min/max angles and the relative max touques of the joints 19 | self.actLen = self.indim 20 | self.obsLen = len(self.getSensors()) 21 | 22 | self.stepsPerAction = 1 23 | 24 | if __name__ == '__main__' : 25 | w = AcrobotEnvironment() 26 | while True: 27 | w.step() 28 | if w.stepCounter == 1000: w.reset() 29 | 30 | -------------------------------------------------------------------------------- /pybrain/rl/environments/ode/instances/johnnie.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Frank Sehnke, sehnke@in.tum.de' 2 | 3 | from pybrain.rl.environments.ode import ODEEnvironment, sensors, actuators 4 | import imp 5 | from scipy import array 6 | 7 | class JohnnieEnvironment(ODEEnvironment): 8 | def __init__(self, renderer=True, realtime=False, ip="127.0.0.1", port="21590", buf='16384'): 9 | ODEEnvironment.__init__(self, renderer, realtime, ip, port, buf) 10 | # load model file 11 | self.loadXODE(imp.find_module('pybrain')[1] + "/rl/environments/ode/models/johnnie.xode") 12 | 13 | # standard sensors and actuators 14 | self.addSensor(sensors.JointSensor()) 15 | self.addSensor(sensors.JointVelocitySensor()) 16 | self.addActuator(actuators.JointActuator()) 17 | 18 | #set act- and obsLength, the min/max angles and the relative max touques of the joints 19 | self.actLen = self.indim 20 | self.obsLen = len(self.getSensors()) 21 | #ArmLeft, ArmRight, Hip, PevelLeft, PevelRight, TibiaLeft, TibiaRight, KneeLeft, KneeRight, FootLeft, FootRight 22 | self.tourqueList = array([0.2, 0.2, 0.2, 0.5, 0.5, 2.0, 2.0, 2.0, 2.0, 0.5, 0.5],) 23 | self.cHighList = array([1.0, 1.0, 0.5, 0.5, 0.5, 1.5, 1.5, 1.5, 1.5, 0.25, 0.25],) 24 | self.cLowList = array([-0.5, -0.5, -0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.25, -0.25],) 25 | 26 | self.stepsPerAction = 1 27 | 28 | if __name__ == '__main__' : 29 | w = JohnnieEnvironment() 30 | while True: 31 | w.step() 32 | if w.stepCounter == 1000: w.reset() 33 | 34 | -------------------------------------------------------------------------------- /pybrain/rl/environments/ode/models/box-sphere.xode: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 19 | 20 | 21 | 22 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 40 | 41 | 42 | 43 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 60 | 61 | 62 | 63 | 64 | 71 | -------------------------------------------------------------------------------- /pybrain/rl/environments/ode/tasks/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.environments.ode.tasks.johnnie import * 2 | from pybrain.rl.environments.ode.tasks.ccrl import * 3 | from pybrain.rl.environments.ode.tasks.acrobot import * 4 | -------------------------------------------------------------------------------- /pybrain/rl/environments/ode/tasks/acrobot.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from pybrain.rl.environments import EpisodicTask 4 | from scipy import pi 5 | 6 | class GradualRewardTask(EpisodicTask): 7 | ''' task gives more reward, the higher the bar is.''' 8 | def __init__(self, environment): 9 | EpisodicTask.__init__(self, environment) 10 | self.maxPower = 0.5 11 | self.reward_history = [] 12 | self.count = 0 13 | # normalize to (-1, 1) 14 | self.sensor_limits = [(-pi, pi), (-20, 20)] 15 | #self.actor_limits = [(-1, 1)] 16 | self.actor_limits = None 17 | 18 | def isFinished(self): 19 | if self.count > 1000: 20 | self.count = 0 21 | self.reward_history.append(self.getTotalReward()) 22 | return True 23 | else: 24 | self.count += 1 25 | return False 26 | 27 | def getReward(self): 28 | # calculate reward and return reward 29 | jointSense = self.env.getSensorByName('JointSensor') 30 | veloSense = self.env.getSensorByName('JointVelocitySensor') 31 | 32 | j = jointSense[0] 33 | v = veloSense[0] 34 | 35 | reward = (abs(j)) ** 2 - 0.2 * abs(v) 36 | # time.sleep(0.001) 37 | return reward 38 | 39 | def performAction(self, action): 40 | EpisodicTask.performAction(self, action*self.maxPower) 41 | -------------------------------------------------------------------------------- /pybrain/rl/environments/ode/tools/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /pybrain/rl/environments/ode/tools/configgrab.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | import string 4 | 5 | class ConfigGrabber: 6 | def __init__(self, filename, sectionId="", delim=("[", "]")): 7 | # filename: name of the config file to be parsed 8 | # sectionId: start looking for parameters only after this string has 9 | # been encountered in the file 10 | # delim: tuple of delimiters to identify tags 11 | self.filename = filename 12 | self.sectionId = string.strip(sectionId) 13 | self.delim = delim 14 | 15 | def getValue(self, name): 16 | file = open(self.filename, "r") 17 | flag = -1 18 | output = [] 19 | # ignore sectionId if not set 20 | if self.sectionId == "": 21 | self.sectionId = string.strip(file.readline()) 22 | file.seek(0) 23 | 24 | for line in file: 25 | if flag == -1 and string.strip(line) == self.sectionId: 26 | flag = 0 27 | if flag > -1: 28 | if line[0] != self.delim[0]: 29 | if flag == 1: output.append(string.strip(line)) 30 | else: 31 | if line == self.delim[0] + name + self.delim[1] + "\n": flag = 1 32 | else: flag = 0 33 | file.close() 34 | #if len(output)==0: print("Attention: Config for ", name, "not found") 35 | return output 36 | 37 | -------------------------------------------------------------------------------- /pybrain/rl/environments/ode/tools/mathhelpers.py: -------------------------------------------------------------------------------- 1 | # TODO - redundant file, and functionality, as we use scipy! 2 | 3 | import math 4 | 5 | def dotproduct(p, q): 6 | return p[0] * q[0] + p[1] * q[1] + p[2] * q[2] 7 | 8 | def norm(p): 9 | sum = 0 10 | for i in p: 11 | sum += i ** 2 12 | return math.sqrt(sum) 13 | 14 | def crossproduct(p, q): 15 | return (p[1] * q[2] - p[2] * q[1], 16 | p[2] * q[0] - p[0] * q[2], 17 | p[0] * q[1] - p[1] * q[0]) 18 | 19 | -------------------------------------------------------------------------------- /pybrain/rl/environments/renderer.py: -------------------------------------------------------------------------------- 1 | # obsolete - should be deleted if there are no objections. 2 | 3 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 4 | 5 | from pybrain.utilities import abstractMethod 6 | import threading 7 | 8 | class Renderer(threading.Thread): 9 | """ The general interface for a class displays what is happening in an environment. 10 | The renderer is executed as concurrent thread. Start the renderer with the function 11 | start() inherited from Thread, and check with isAlive(), if the thread is running. 12 | """ 13 | 14 | def __init__(self): 15 | """ initializes some variables and parent init functions """ 16 | threading.Thread.__init__(self) 17 | 18 | def updateData(self): 19 | """ overwrite this class to update whatever data the renderer needs to display the current 20 | state of the world. """ 21 | abstractMethod() 22 | 23 | def _render(self): 24 | """ Here, the render methods are called. This function has to be implemented by subclasses. """ 25 | abstractMethod() 26 | 27 | def start(self): 28 | """ wrapper for Thread.start(). only calls start if thread has not been started before. """ 29 | if not self.isAlive(): 30 | threading.Thread.start(self) 31 | 32 | def run(self): 33 | """ Don't call this function on its own. Use start() instead. """ 34 | self._render() 35 | 36 | def stop(self): 37 | """ stop signal requested. stop current thread. 38 | @note: only if possible. OpenGL glutMainLoop is not stoppable. 39 | """ 40 | pass 41 | 42 | -------------------------------------------------------------------------------- /pybrain/rl/environments/serverinterface.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Frank Sehnke, sehnke@in.tum.de' 2 | 3 | from environment import Environment 4 | 5 | class GraphicalEnvironment(Environment): 6 | """ Special type of environment that has graphical output and therefore needs a renderer. 7 | """ 8 | 9 | def __init__(self): 10 | self.renderInterface = None 11 | 12 | def setRenderInterface(self, renderer): 13 | """ set the renderer, which is an object of or inherited from class Renderer. 14 | 15 | :arg renderer: The renderer that should display the Environment 16 | :type renderer: L{Renderer} 17 | .. seealso:: :class:`Renderer` 18 | """ 19 | self.renderInterface = renderer 20 | 21 | def getRenderInterface(self): 22 | """ returns the current renderer. 23 | 24 | :return: the current renderer 25 | :rtype: L{Renderer} 26 | """ 27 | return self.renderInterface 28 | 29 | def hasRenderInterface(self): 30 | """ tells you, if a Renderer has been set previously or not 31 | 32 | :return: True if a renderer was set, False otherwise 33 | :rtype: Boolean 34 | """ 35 | return (self.getRenderInterface() != None) 36 | -------------------------------------------------------------------------------- /pybrain/rl/environments/shipsteer/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.environments.shipsteer.shipsteer import ShipSteeringEnvironment 2 | from pybrain.rl.environments.shipsteer.northwardtask import GoNorthwardTask -------------------------------------------------------------------------------- /pybrain/rl/environments/shipsteer/northwardtask.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Martin Felder, felder@in.tum.de' 2 | 3 | from pybrain.rl.environments import EpisodicTask 4 | from shipsteer import ShipSteeringEnvironment 5 | 6 | 7 | class GoNorthwardTask(EpisodicTask): 8 | 9 | """ The task of balancing some pole(s) on a cart """ 10 | def __init__(self, env=None, maxsteps=1000): 11 | """ 12 | :key env: (optional) an instance of a ShipSteeringEnvironment (or a subclass thereof) 13 | :key maxsteps: maximal number of steps (default: 1000) 14 | """ 15 | if env == None: 16 | env = ShipSteeringEnvironment(render=False) 17 | EpisodicTask.__init__(self, env) 18 | self.N = maxsteps 19 | self.t = 0 20 | 21 | # scale sensors 22 | # [h, hdot, v] 23 | self.sensor_limits = [(-180.0, +180.0), (-180.0, +180.0), (-10.0, +40.0)] 24 | 25 | # actions: thrust, rudder 26 | self.actor_limits = [(-1.0, +2.0), (-90.0, +90.0)] 27 | # scale reward over episode, such that max. return = 100 28 | self.rewardscale = 100. / maxsteps / self.sensor_limits[2][1] 29 | 30 | def reset(self): 31 | EpisodicTask.reset(self) 32 | self.t = 0 33 | 34 | def performAction(self, action): 35 | self.t += 1 36 | EpisodicTask.performAction(self, action) 37 | 38 | def isFinished(self): 39 | if self.t >= self.N: 40 | # maximal timesteps 41 | return True 42 | return False 43 | 44 | def getReward(self): 45 | if abs(self.env.getHeading()) < 5.: 46 | return self.env.getSpeed() * self.rewardscale 47 | else: 48 | return 0 49 | 50 | def setMaxLength(self, n): 51 | self.N = n 52 | 53 | -------------------------------------------------------------------------------- /pybrain/rl/environments/simple/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.environments.simple.environment import SimpleEnvironment 2 | from pybrain.rl.environments.simple.tasks import MinimizeTask -------------------------------------------------------------------------------- /pybrain/rl/environments/simple/environment.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from scipy import random, zeros 4 | from pybrain.rl.environments import Environment 5 | 6 | 7 | class SimpleEnvironment(Environment): 8 | def __init__(self, dim=1): 9 | Environment.__init__(self) 10 | self.dim = dim 11 | self.indim = dim 12 | self.outdim = dim 13 | self.noise = None 14 | self.reset() 15 | 16 | def setNoise(self, variance): 17 | self.noise = variance 18 | 19 | def getSensors(self): 20 | if not self.updated: 21 | self.update() 22 | return self.state 23 | 24 | def performAction(self, action): 25 | self.updated = False 26 | self.action = action 27 | 28 | def update(self): 29 | self.state = [s + 0.1 * a for s, a in zip(self.state, self.action)] 30 | if self.noise: 31 | self.state += random.normal(0, self.noise, self.dim) 32 | 33 | def f(self, x): 34 | return [v ** 2 for v in x] 35 | 36 | def reset(self): 37 | self.state = random.uniform(2, 2, self.dim) 38 | 39 | self.action = zeros(self.dim, float) 40 | self.updated = True 41 | 42 | 43 | -------------------------------------------------------------------------------- /pybrain/rl/environments/simple/tasks.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from pybrain.rl.environments import EpisodicTask 4 | 5 | 6 | class MinimizeTask(EpisodicTask): 7 | def __init__(self, environment): 8 | EpisodicTask.__init__(self, environment) 9 | self.N = 15 10 | self.t = 0 11 | self.state = [0.0] * environment.dim 12 | self.action = [0.0] * environment.dim 13 | 14 | def reset(self): 15 | EpisodicTask.reset(self) 16 | self.t = 0 17 | 18 | def isFinished(self): 19 | if self.t >= self.N: 20 | self.t = 0 21 | return True 22 | else: 23 | self.t += 1 24 | return False 25 | 26 | def getObservation(self): 27 | self.state = EpisodicTask.getObservation(self) 28 | return self.state 29 | 30 | def performAction(self, action): 31 | EpisodicTask.performAction(self, action) 32 | self.action = action 33 | 34 | def getReward(self): 35 | # sleep(0.01) 36 | # print(self.state, self.action) 37 | reward = self.env.f([s + 0.1 * a for s, a in zip(self.state, self.action)]) 38 | return - sum(reward) 39 | 40 | -------------------------------------------------------------------------------- /pybrain/rl/environments/simplerace/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/rl/environments/simplerace/__init__.py -------------------------------------------------------------------------------- /pybrain/rl/environments/simplerace/simplecontroller.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Julian Togelius, julian@idsia.ch' 2 | 3 | from scipy import array 4 | 5 | from pybrain.rl.agents.agent import Agent 6 | 7 | 8 | class SimpleController(Agent): 9 | 10 | def integrateObservation(self, obs): 11 | self.speed = obs[0] 12 | self.angleToCurrentWP = obs[1] 13 | self.distanceToCurrentWP = obs[2] 14 | self.angleToNextWP = obs[3] 15 | self.distanceToNextWP = obs[4] 16 | self.angleToOtherVehicle = obs[5] 17 | self.distanceToOtherVehicle = obs[6] 18 | 19 | 20 | def getAction(self): 21 | if self.speed < 10: 22 | driving = 1 23 | else: 24 | driving = 0 25 | if self.angleToCurrentWP > 0: 26 | steering = -1 27 | else: 28 | steering = 1 29 | print("speed", self.speed, "angle", self.angleToCurrentWP, "driving", driving, "steering", steering) 30 | return array([driving, steering]) 31 | 32 | -------------------------------------------------------------------------------- /pybrain/rl/environments/simplerace/simpleracetask.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Julian Togelius, julian@idsia.ch' 2 | 3 | from pybrain.rl.environments import EpisodicTask 4 | from simpleracetcp import SimpleraceEnvironment 5 | 6 | class SimpleraceTask(EpisodicTask): 7 | 8 | def getTotalReward(self): 9 | #score handled by environment? 10 | return self.environment.firstCarScore 11 | 12 | def getReward(self): 13 | return 0 14 | 15 | def isFinished(self): 16 | #this task can't really fail, a bad policy will just get a low score 17 | return False 18 | 19 | def setMaxLength(self, n): 20 | # I don't think this can be done 21 | pass 22 | 23 | def reset(self): 24 | EpisodicTask.reset(self) 25 | self.t = 0 26 | 27 | def performAction(self, action): 28 | self.t += 1 29 | EpisodicTask.performAction(self, action) 30 | 31 | def __init__(self): 32 | self.environment = SimpleraceEnvironment() 33 | EpisodicTask.__init__(self, self.environment) 34 | self.t = 0 35 | 36 | -------------------------------------------------------------------------------- /pybrain/rl/environments/twoplayergames/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.environments.twoplayergames.capturegame import CaptureGame 2 | from pybrain.rl.environments.twoplayergames.gomoku import GomokuGame 3 | from pybrain.rl.environments.twoplayergames.tasks.__init__ import * -------------------------------------------------------------------------------- /pybrain/rl/environments/twoplayergames/capturegameplayers/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.environments.twoplayergames.capturegameplayers.randomplayer import RandomCapturePlayer 2 | from pybrain.rl.environments.twoplayergames.capturegameplayers.killing import KillingPlayer 3 | from pybrain.rl.environments.twoplayergames.capturegameplayers.nonsuicide import NonSuicidePlayer 4 | from pybrain.rl.environments.twoplayergames.capturegameplayers.moduledecision import ModuleDecidingPlayer 5 | from pybrain.rl.environments.twoplayergames.capturegameplayers.clientwrapper import ClientCapturePlayer -------------------------------------------------------------------------------- /pybrain/rl/environments/twoplayergames/capturegameplayers/captureplayer.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.rl.agents.agent import Agent 4 | from pybrain.rl.environments.twoplayergames import CaptureGame 5 | 6 | 7 | class CapturePlayer(Agent): 8 | """ a class of agent that can play the capture game, i.e. provides actions in the format: 9 | (playerid, position) 10 | playerid is self.color, by convention. 11 | It generally also has access to the game object. """ 12 | def __init__(self, game, color = CaptureGame.BLACK, **args): 13 | self.game = game 14 | self.color = color 15 | self.setArgs(**args) 16 | 17 | 18 | -------------------------------------------------------------------------------- /pybrain/rl/environments/twoplayergames/capturegameplayers/killing.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from random import choice 4 | 5 | from nonsuicide import NonSuicidePlayer 6 | 7 | 8 | class KillingPlayer(NonSuicidePlayer): 9 | """ do random moves, but always instant-kill if possible, 10 | and never suicide, if avoidable. """ 11 | def getAction(self): 12 | p = self.game.getKilling(self.color) 13 | if len(p) > 0: 14 | return [self.color, choice(p)] 15 | else: 16 | return NonSuicidePlayer.getAction(self) -------------------------------------------------------------------------------- /pybrain/rl/environments/twoplayergames/capturegameplayers/nonsuicide.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from random import choice 4 | 5 | from randomplayer import RandomCapturePlayer 6 | 7 | 8 | class NonSuicidePlayer(RandomCapturePlayer): 9 | """ do random non-suicide moves in the capture game """ 10 | def getAction(self): 11 | p = self.game.getAcceptable(self.color) 12 | if len(p) > 0: 13 | return [self.color, choice(p)] 14 | else: 15 | return RandomCapturePlayer.getAction(self) -------------------------------------------------------------------------------- /pybrain/rl/environments/twoplayergames/capturegameplayers/randomplayer.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from random import choice 4 | 5 | from captureplayer import CapturePlayer 6 | 7 | 8 | class RandomCapturePlayer(CapturePlayer): 9 | """ do random moves in the capture game""" 10 | 11 | def getAction(self): 12 | return [self.color, choice(self.game.getLegals(self.color))] -------------------------------------------------------------------------------- /pybrain/rl/environments/twoplayergames/gomokuplayers/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.environments.twoplayergames.gomokuplayers.randomplayer import RandomGomokuPlayer 2 | from pybrain.rl.environments.twoplayergames.gomokuplayers.killing import KillingGomokuPlayer 3 | from pybrain.rl.environments.twoplayergames.gomokuplayers.moduledecision import ModuleDecidingPlayer -------------------------------------------------------------------------------- /pybrain/rl/environments/twoplayergames/gomokuplayers/gomokuplayer.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | 4 | from pybrain.rl.agents.agent import Agent 5 | from pybrain.rl.environments.twoplayergames import GomokuGame 6 | 7 | 8 | class GomokuPlayer(Agent): 9 | """ a class of agent that can play Go-Moku, i.e. provides actions in the format: 10 | (playerid, position) 11 | playerid is self.color, by convention. 12 | It generally also has access to the game object. """ 13 | def __init__(self, game, color = GomokuGame.BLACK, **args): 14 | self.game = game 15 | self.color = color 16 | self.setArgs(**args) 17 | 18 | -------------------------------------------------------------------------------- /pybrain/rl/environments/twoplayergames/gomokuplayers/killing.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from random import choice 4 | 5 | from randomplayer import RandomGomokuPlayer 6 | 7 | 8 | class KillingGomokuPlayer(RandomGomokuPlayer): 9 | """ do random moves, but always instant-kill if possible. """ 10 | def getAction(self): 11 | p = self.game.getKilling(self.color) 12 | if len(p) > 0: 13 | return [self.color, choice(p)] 14 | else: 15 | return RandomGomokuPlayer.getAction(self) -------------------------------------------------------------------------------- /pybrain/rl/environments/twoplayergames/gomokuplayers/randomplayer.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from random import choice 4 | 5 | from gomokuplayer import GomokuPlayer 6 | 7 | 8 | class RandomGomokuPlayer(GomokuPlayer): 9 | """ do random moves in Go-Moku""" 10 | 11 | def getAction(self): 12 | return [self.color, choice(self.game.getLegals(self.color))] -------------------------------------------------------------------------------- /pybrain/rl/environments/twoplayergames/tasks/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.environments.twoplayergames.tasks.capturetask import CaptureGameTask 2 | from pybrain.rl.environments.twoplayergames.tasks.handicaptask import HandicapCaptureTask 3 | from pybrain.rl.environments.twoplayergames.tasks.relativetask import RelativeCaptureTask 4 | from pybrain.rl.environments.twoplayergames.tasks.gomokutask import GomokuTask 5 | from pybrain.rl.environments.twoplayergames.tasks.relativegomokutask import RelativeGomokuTask 6 | from pybrain.rl.environments.twoplayergames.tasks.pentetask import PenteTask 7 | -------------------------------------------------------------------------------- /pybrain/rl/environments/twoplayergames/tasks/pentetask.py: -------------------------------------------------------------------------------- 1 | 2 | __author__ = 'Tom Schaul, tom@idsia.ch' 3 | 4 | from pybrain.rl.environments.twoplayergames.pente import PenteGame 5 | from pybrain.rl.environments.episodic import EpisodicTask 6 | from gomokutask import GomokuTask 7 | from pybrain.rl.environments.twoplayergames.gomokuplayers import RandomGomokuPlayer 8 | from inspect import isclass 9 | 10 | 11 | class PenteTask(GomokuTask): 12 | """ The task of winning the maximal number of Gomoku games against a fixed opponent. """ 13 | 14 | def __init__(self, size, opponent = None, **args): 15 | EpisodicTask.__init__(self, PenteGame((size, size))) 16 | self.setArgs(**args) 17 | if opponent == None: 18 | opponent = RandomGomokuPlayer(self.env) 19 | elif isclass(opponent): 20 | # assume the agent can be initialized without arguments then. 21 | opponent = opponent(self.env) 22 | if not self.opponentStart: 23 | opponent.color = PenteGame.WHITE 24 | self.opponent = opponent 25 | self.minmoves = 9 26 | self.maxmoves = self.env.size[0] * self.env.size[1] 27 | self.reset() 28 | 29 | -------------------------------------------------------------------------------- /pybrain/rl/environments/twoplayergames/twoplayergame.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.utilities import abstractMethod 4 | from pybrain.rl.environments import Environment 5 | 6 | 7 | class CompetitiveEnvironment(Environment): 8 | """ an environment in which multiple agents interact, competitively. 9 | This class is only for conceptual grouping, it only constrains the format of action input. 10 | """ 11 | 12 | def performAction(self, action): 13 | """ perform an action on the world that changes it's internal state (maybe stochastically) 14 | 15 | :key action: an action that should be executed in the Environment, by an agent. 16 | :type action: tuple: (agentID, action value) 17 | :note: This function is abstract and has to be implemented. 18 | """ 19 | abstractMethod() 20 | 21 | 22 | class TwoPlayerGame(CompetitiveEnvironment): 23 | """ a game between 2 players, alternating turns. Outcome can be one winner or a draw. """ 24 | 25 | DRAW = 'draw' 26 | 27 | def reset(self): 28 | self.winner = None 29 | self.lastplayer = None 30 | 31 | def performAction(self, action): 32 | self.lastplayer = action[0] 33 | self.doMove(*action) 34 | 35 | def doMove(self, player, action): 36 | """ the core method to be implemented bu all TwoPlayerGames: 37 | what happens when a player performs an action. """ 38 | abstractMethod() 39 | 40 | def isLegal(self, player, action): 41 | """ is this a legal move? By default, everything is allowed. """ 42 | return True 43 | 44 | def gameOver(self): 45 | """ is the game over? """ 46 | return self.winner != None 47 | 48 | def getWinner(self): 49 | """ returns the id of the winner, 'draw' if it's a draw, and None if the game is undecided. """ 50 | return self.winner 51 | -------------------------------------------------------------------------------- /pybrain/rl/experiments/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.experiments.experiment import Experiment 2 | from pybrain.rl.experiments.episodic import EpisodicExperiment 3 | from pybrain.rl.experiments.continuous import ContinuousExperiment -------------------------------------------------------------------------------- /pybrain/rl/experiments/continuous.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from pybrain.rl.experiments.experiment import Experiment 4 | 5 | 6 | class ContinuousExperiment(Experiment): 7 | """ The extension of Experiment to handle continuous tasks. """ 8 | 9 | def doInteractionsAndLearn(self, number = 1): 10 | """ Execute a number of steps while learning continuously. 11 | no reset is performed, such that consecutive calls to 12 | this function can be made. 13 | """ 14 | for _ in range(number): 15 | self._oneInteraction() 16 | self.agent.learn() 17 | return self.stepid 18 | -------------------------------------------------------------------------------- /pybrain/rl/experiments/episodic.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | 4 | from pybrain.rl.experiments.experiment import Experiment 5 | from pybrain.rl.agents.optimization import OptimizationAgent 6 | 7 | 8 | class EpisodicExperiment(Experiment): 9 | """ The extension of Experiment to handle episodic tasks. """ 10 | 11 | doOptimization = False 12 | 13 | def __init__(self, task, agent): 14 | if isinstance(agent, OptimizationAgent): 15 | self.doOptimization = True 16 | self.optimizer = agent.learner 17 | self.optimizer.setEvaluator(task, agent.module) 18 | self.optimizer.maxEvaluations = self.optimizer.numEvaluations 19 | else: 20 | Experiment.__init__(self, task, agent) 21 | 22 | def _oneInteraction(self): 23 | """ Do an interaction between the Task and the Agent. """ 24 | if self.doOptimization: 25 | raise Exception('When using a black-box learning algorithm, only full episodes can be done.') 26 | else: 27 | return Experiment._oneInteraction(self) 28 | 29 | def doEpisodes(self, number = 1): 30 | """ Do one episode, and return the rewards of each step as a list. """ 31 | if self.doOptimization: 32 | self.optimizer.maxEvaluations += number 33 | self.optimizer.learn() 34 | else: 35 | all_rewards = [] 36 | for dummy in range(number): 37 | self.agent.newEpisode() 38 | rewards = [] 39 | self.stepid = 0 40 | self.task.reset() 41 | while not self.task.isFinished(): 42 | r = self._oneInteraction() 43 | rewards.append(r) 44 | all_rewards.append(rewards) 45 | 46 | return all_rewards 47 | -------------------------------------------------------------------------------- /pybrain/rl/experiments/experiment.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | 4 | class Experiment(object): 5 | """ An experiment matches up a task with an agent and handles their interactions. 6 | """ 7 | 8 | def __init__(self, task, agent): 9 | self.task = task 10 | self.agent = agent 11 | self.stepid = 0 12 | 13 | def doInteractions(self, number = 1): 14 | """ The default implementation directly maps the methods of the agent and the task. 15 | Returns the number of interactions done. 16 | """ 17 | for _ in range(number): 18 | self._oneInteraction() 19 | return self.stepid 20 | 21 | def _oneInteraction(self): 22 | """ Give the observation to the agent, takes its resulting action and returns 23 | it to the task. Then gives the reward to the agent again and returns it. 24 | """ 25 | self.stepid += 1 26 | self.agent.integrateObservation(self.task.getObservation()) 27 | self.task.performAction(self.agent.getAction()) 28 | reward = self.task.getReward() 29 | self.agent.giveReward(reward) 30 | return reward 31 | -------------------------------------------------------------------------------- /pybrain/rl/experiments/queued.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from episodic import EpisodicExperiment 4 | from scipy import arange 5 | 6 | 7 | class QueuedExperiment(EpisodicExperiment): 8 | """ This experiment type runs n episodes at the beginning, followed by a learning step. 9 | From then on it removes the oldest episode, learns a new one, and executes another 10 | training step with the n current episodes. This way, learning happens after each 11 | episode, and each episode is considered n times for learning until discarded. """ 12 | 13 | def run(self, queuelength, learningcycles=-1): 14 | # fill the queue with given number of episodes 15 | self._fillQueue(queuelength) 16 | 17 | # start the queue loop 18 | if learningcycles == -1: 19 | while True: 20 | # indefinite learning 21 | self._stepQueueLoop() 22 | else: 23 | for _ in arange(learningcycles): 24 | # learn the given number of times 25 | self._stepQueueLoop() 26 | 27 | 28 | def _fillQueue(self, queuelength): 29 | # reset agent (empty queue) 30 | self.agent.reset() 31 | # fill queue with first n episodes 32 | self.doEpisodes(queuelength) 33 | 34 | 35 | def _stepQueueLoop(self): 36 | # let agent learn with full queue 37 | self.agent.learn() 38 | # remove oldest episode 39 | self.agent.history.removeSequence(0) 40 | # execute one new episode 41 | self.doEpisodes(1) 42 | 43 | -------------------------------------------------------------------------------- /pybrain/rl/explorers/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.explorers.discrete.__init__ import * 2 | from pybrain.rl.explorers.continuous.__init__ import * -------------------------------------------------------------------------------- /pybrain/rl/explorers/continuous/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.explorers.continuous.normal import NormalExplorer -------------------------------------------------------------------------------- /pybrain/rl/explorers/continuous/normal.py: -------------------------------------------------------------------------------- 1 | __author__ = "Thomas Rueckstiess, ruecksti@in.tum.de" 2 | 3 | from scipy import random 4 | 5 | from pybrain.rl.explorers.explorer import Explorer 6 | from pybrain.tools.functions import expln, explnPrime 7 | from pybrain.structure.parametercontainer import ParameterContainer 8 | 9 | 10 | class NormalExplorer(Explorer, ParameterContainer): 11 | """ A continuous explorer, that perturbs the resulting action with 12 | additive, normally distributed random noise. The exploration 13 | has parameter(s) sigma, which are related to the distribution's 14 | standard deviation. In order to allow for negative values of sigma, 15 | the real std. derivation is a transformation of sigma according 16 | to the expln() function (see pybrain.tools.functions). 17 | """ 18 | 19 | def __init__(self, dim, sigma=0.): 20 | Explorer.__init__(self, dim, dim) 21 | self.dim = dim 22 | 23 | # initialize parameters to sigma 24 | ParameterContainer.__init__(self, dim, stdParams=0) 25 | self.sigma = [sigma] * dim 26 | 27 | def _setSigma(self, sigma): 28 | """ Wrapper method to set the sigmas (the parameters of the module) to a 29 | certain value. 30 | """ 31 | assert len(sigma) == self.dim 32 | self._params *= 0 33 | self._params += sigma 34 | 35 | def _getSigma(self): 36 | return self.params 37 | 38 | sigma = property(_getSigma, _setSigma) 39 | 40 | def _forwardImplementation(self, inbuf, outbuf): 41 | outbuf[:] = random.normal(inbuf, expln(self.sigma)) 42 | 43 | def _backwardImplementation(self, outerr, inerr, outbuf, inbuf): 44 | expln_sigma = expln(self.sigma) 45 | self._derivs += ((outbuf - inbuf) ** 2 - expln_sigma ** 2) / expln_sigma * explnPrime(self.sigma) 46 | inerr[:] = (outbuf - inbuf) 47 | 48 | # auto-alpha 49 | # inerr /= expln_sigma**2 50 | # self._derivs /= expln_sigma**2 51 | 52 | 53 | -------------------------------------------------------------------------------- /pybrain/rl/explorers/discrete/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.explorers.discrete.boltzmann import BoltzmannExplorer 2 | from pybrain.rl.explorers.discrete.egreedy import EpsilonGreedyExplorer 3 | from pybrain.rl.explorers.discrete.discretesde import DiscreteStateDependentExplorer -------------------------------------------------------------------------------- /pybrain/rl/explorers/discrete/boltzmann.py: -------------------------------------------------------------------------------- 1 | __author__ = "Thomas Rueckstiess, ruecksti@in.tum.de" 2 | 3 | from scipy import array 4 | 5 | from pybrain.rl.explorers.discrete.discrete import DiscreteExplorer 6 | from pybrain.utilities import drawGibbs 7 | 8 | class BoltzmannExplorer(DiscreteExplorer): 9 | """ A discrete explorer, that executes the actions with probability 10 | that depends on their action values. The boltzmann explorer has 11 | a parameter tau (the temperature). for high tau, the actions are 12 | nearly equiprobable. for tau close to 0, this action selection 13 | becomes greedy. 14 | """ 15 | 16 | def __init__(self, tau = 2., decay = 0.9995): 17 | DiscreteExplorer.__init__(self) 18 | self.tau = tau 19 | self.decay = decay 20 | self._state = None 21 | 22 | def activate(self, state, action): 23 | """ The super class ignores the state and simply passes the 24 | action through the module. implement _forwardImplementation() 25 | in subclasses. 26 | """ 27 | self._state = state 28 | return DiscreteExplorer.activate(self, state, action) 29 | 30 | 31 | def _forwardImplementation(self, inbuf, outbuf): 32 | """ Draws a random number between 0 and 1. If the number is less 33 | than epsilon, a random action is chosen. If it is equal or 34 | larger than epsilon, the greedy action is returned. 35 | """ 36 | assert self.module 37 | 38 | values = self.module.getActionValues(self._state) 39 | action = drawGibbs(values, self.tau) 40 | 41 | self.tau *= self.decay 42 | 43 | outbuf[:] = array([action]) 44 | -------------------------------------------------------------------------------- /pybrain/rl/explorers/discrete/discrete.py: -------------------------------------------------------------------------------- 1 | __author__ = "Thomas Rueckstiess, ruecksti@in.tum.de" 2 | 3 | from pybrain.rl.explorers.explorer import Explorer 4 | # from pybrain.rl.learners.valuebased.interface import ActionValueInterface 5 | 6 | class DiscreteExplorer(Explorer): 7 | """ Discrete explorers choose one of the available actions from the 8 | set of actions. In order to know which actions are available and 9 | which action to choose, discrete explorers need access to the 10 | module (which has to of class ActionValueTable). 11 | """ 12 | 13 | _module = None 14 | 15 | def __init__(self): 16 | Explorer.__init__(self, 1, 1) 17 | 18 | def _setModule(self, module): 19 | """ Tells the explorer the module (which has to be ActionValueTable). """ 20 | # removed: cause for circular import 21 | # assert isinstance(module, ActionValueInterface) 22 | self._module = module 23 | 24 | def _getModule(self): 25 | return self._module 26 | 27 | module = property(_getModule, _setModule) -------------------------------------------------------------------------------- /pybrain/rl/explorers/discrete/egreedy.py: -------------------------------------------------------------------------------- 1 | __author__ = "Thomas Rueckstiess, ruecksti@in.tum.de" 2 | 3 | from scipy import random, array 4 | 5 | from pybrain.rl.explorers.discrete.discrete import DiscreteExplorer 6 | 7 | class EpsilonGreedyExplorer(DiscreteExplorer): 8 | """ A discrete explorer, that executes the original policy in most cases, 9 | but sometimes returns a random action (uniformly drawn) instead. The 10 | randomness is controlled by a parameter 0 <= epsilon <= 1. The closer 11 | epsilon gets to 0, the more greedy (and less explorative) the agent 12 | behaves. 13 | """ 14 | 15 | def __init__(self, epsilon = 0.3, decay = 0.9999): 16 | DiscreteExplorer.__init__(self) 17 | self.epsilon = epsilon 18 | self.decay = decay 19 | 20 | def _forwardImplementation(self, inbuf, outbuf): 21 | """ Draws a random number between 0 and 1. If the number is less 22 | than epsilon, a random action is chosen. If it is equal or 23 | larger than epsilon, the greedy action is returned. 24 | """ 25 | assert self.module 26 | 27 | if random.random() < self.epsilon: 28 | outbuf[:] = array([random.randint(self.module.numActions)]) 29 | else: 30 | outbuf[:] = inbuf 31 | 32 | self.epsilon *= self.decay 33 | 34 | 35 | -------------------------------------------------------------------------------- /pybrain/rl/explorers/explorer.py: -------------------------------------------------------------------------------- 1 | __author__ = "Thomas Rueckstiess, ruecksti@in.tum.de" 2 | 3 | 4 | from pybrain.structure.modules.module import Module 5 | 6 | 7 | class Explorer(Module): 8 | """ An Explorer object is used in Agents, receives the current state 9 | and action (from the controller Module) and returns an explorative 10 | action that is executed instead the given action. 11 | 12 | Continous explorer will produce continous action states, discrete 13 | once discrete actions accordingly. 14 | 15 | Explorer action episodic? 16 | =============================== ========= ========= 17 | NormalExplorer continous no 18 | StateDependentExplorer continous yes 19 | BoltzmannExplorer discrete no 20 | EpsilonGreedyExplorer discrete no 21 | DiscreteStateDependentExplorer discrete yes 22 | 23 | 24 | Explorer has to be added to the learner before adding the learner 25 | to the LearningAgent. 26 | 27 | For Example:: 28 | 29 | controller = ActionValueNetwork(2, 100) 30 | learner = SARSA() 31 | learner.explorer = NormalExplorer(1, 0.1) 32 | self.learning_agent = LearningAgent(controller, learner) 33 | """ 34 | 35 | def activate(self, state, action): 36 | """ The super class commonly ignores the state and simply passes the 37 | action through the module. implement _forwardImplementation() 38 | in subclasses. 39 | """ 40 | return Module.activate(self, action) 41 | 42 | 43 | def newEpisode(self): 44 | """ Inform the explorer about the start of a new episode. """ 45 | pass 46 | -------------------------------------------------------------------------------- /pybrain/rl/learners/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.learners.directsearch.__init__ import * 2 | from pybrain.rl.learners.valuebased.__init__ import * 3 | from pybrain.rl.learners.modelbased.__init__ import * 4 | -------------------------------------------------------------------------------- /pybrain/rl/learners/directsearch/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.learners.directsearch.rwr import RWR 2 | from pybrain.rl.learners.directsearch.enac import ENAC 3 | from pybrain.rl.learners.directsearch.reinforce import Reinforce 4 | 5 | # TODO: also black-box optimizers -- but this leads to circular imports... 6 | # from pybrain.optimization.__init__ import * 7 | -------------------------------------------------------------------------------- /pybrain/rl/learners/directsearch/directsearch.py: -------------------------------------------------------------------------------- 1 | # The code for all black-box optimization algorithms is located in 2 | # the pybrain/optimization directory (to avoid duplicating files). 3 | # Those algorithms can perfectly be used on (episodic) RL tasks anyway. 4 | # 5 | # See also examples/optimization/optimizers_for_rl.py 6 | 7 | __author__ = 'Tom Schaul and Thomas Rueckstiess, tom@idsia.ch, ruecksti@in.tum.de' 8 | 9 | from pybrain.rl.learners.learner import Learner, EpisodicLearner 10 | 11 | 12 | class DirectSearchLearner(Learner): 13 | """ The class of learners that (in contrast to value-based learners) 14 | searches directly in policy space. 15 | """ 16 | -------------------------------------------------------------------------------- /pybrain/rl/learners/directsearch/enac.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | 4 | from pybrain.rl.learners.directsearch.policygradient import PolicyGradientLearner 5 | from scipy import ones, dot, ravel 6 | from scipy.linalg import pinv 7 | 8 | 9 | class ENAC(PolicyGradientLearner): 10 | """ Episodic Natural Actor-Critic. See J. Peters "Natural Actor-Critic", 2005. 11 | Estimates natural gradient with regression of log likelihoods to rewards. 12 | """ 13 | 14 | def calculateGradient(self): 15 | # normalize rewards 16 | # self.dataset.data['reward'] /= max(ravel(abs(self.dataset.data['reward']))) 17 | 18 | # initialize variables 19 | R = ones((self.dataset.getNumSequences(), 1), float) 20 | X = ones((self.dataset.getNumSequences(), self.loglh.getDimension('loglh') + 1), float) 21 | 22 | # collect sufficient statistics 23 | print(self.dataset.getNumSequences()) 24 | for n in range(self.dataset.getNumSequences()): 25 | _state, _action, reward = self.dataset.getSequence(n) 26 | seqidx = ravel(self.dataset['sequence_index']) 27 | if n == self.dataset.getNumSequences() - 1: 28 | # last sequence until end of dataset 29 | loglh = self.loglh['loglh'][seqidx[n]:, :] 30 | else: 31 | loglh = self.loglh['loglh'][seqidx[n]:seqidx[n + 1], :] 32 | 33 | X[n, :-1] = sum(loglh, 0) 34 | R[n, 0] = sum(reward, 0) 35 | 36 | # linear regression 37 | beta = dot(pinv(X), R) 38 | return beta[:-1] 39 | 40 | -------------------------------------------------------------------------------- /pybrain/rl/learners/directsearch/gpomdp.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from policygradient import PolicyGradientLearner 4 | from scipy import zeros, mean 5 | 6 | ### NOT WORKING YET ### 7 | 8 | class GPOMDP(PolicyGradientLearner): 9 | def __init__(self): 10 | PolicyGradientLearner.__init__(self) 11 | 12 | def calculateGradient(self): 13 | 14 | # normalize rewards 15 | # self.ds.data['reward'] /= max(ravel(abs(self.ds.data['reward']))) 16 | 17 | g = zeros((self.ds.getNumSequences(), self.ds.getDimension('loglh')), float) 18 | 19 | # get maximal length 20 | maxlen = max([self.ds.getSequenceLength(n) for n in range(self.ds.getNumSequences())]) 21 | baselines = zeros((maxlen, self.ds.getDimension('loglh')), float) 22 | seqcount = zeros((maxlen, 1)) 23 | 24 | # calculcate individual baseline for each timestep and episode 25 | for seq in range(self.ds.getNumSequences()): 26 | _, _, rewards, loglhs = self.ds.getSequence(seq) 27 | for t in range(len(rewards)): 28 | baselines[t, :] += mean(sum(loglhs[:t + 1, :], 0) ** 2 * rewards[t, :], 0) / mean(sum(loglhs[:t + 1, :], 0) ** 2, 0) 29 | seqcount[t, :] += 1 30 | 31 | baselines = baselines / seqcount 32 | # print(baselines) 33 | for seq in range(self.ds.getNumSequences()): 34 | _, _, rewards, loglhs = self.ds.getSequence(seq) 35 | for t in range(len(rewards)): 36 | g[seq, :] += sum(loglhs[:t + 1, :], 0) * (rewards[t, :] - baselines[t]) 37 | 38 | gradient = mean(g, 0) 39 | return gradient 40 | -------------------------------------------------------------------------------- /pybrain/rl/learners/directsearch/reinforce.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from pybrain.rl.learners.directsearch.policygradient import PolicyGradientLearner 4 | from scipy import mean, ravel, array 5 | 6 | 7 | class Reinforce(PolicyGradientLearner): 8 | """ Reinforce is a gradient estimator technique by Williams (see 9 | "Simple Statistical Gradient-Following Algorithms for 10 | Connectionist Reinforcement Learning"). It uses optimal 11 | baselines and calculates the gradient with the log likelihoods 12 | of the taken actions. """ 13 | 14 | def calculateGradient(self): 15 | # normalize rewards 16 | # self.ds.data['reward'] /= max(ravel(abs(self.ds.data['reward']))) 17 | 18 | # initialize variables 19 | returns = self.dataset.getSumOverSequences('reward') 20 | seqidx = ravel(self.dataset['sequence_index']) 21 | 22 | # sum of sequences up to n-1 23 | loglhs = [sum(self.loglh['loglh'][seqidx[n]:seqidx[n + 1], :]) for n in range(self.dataset.getNumSequences() - 1)] 24 | # append sum of last sequence as well 25 | loglhs.append(sum(self.loglh['loglh'][seqidx[-1]:, :])) 26 | loglhs = array(loglhs) 27 | 28 | baselines = mean(loglhs ** 2 * returns, 0) / mean(loglhs ** 2, 0) 29 | # TODO: why gradient negative? 30 | gradient = -mean(loglhs * (returns - baselines), 0) 31 | 32 | return gradient 33 | 34 | -------------------------------------------------------------------------------- /pybrain/rl/learners/meta/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/rl/learners/meta/__init__.py -------------------------------------------------------------------------------- /pybrain/rl/learners/meta/levinsearch.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | 4 | def timeBoundExecution(algo, maxtime): 5 | """ wrap the algo, to stop execution after it has used all its allocated time """ 6 | # TODO 7 | return algo 8 | 9 | 10 | class LevinSeach: 11 | """ a.k.a. Universal Search 12 | 13 | Note: don't run this, it's a bit slow... but it will solve all your problems! """ 14 | 15 | def stoppingCriterion(self, val): 16 | return val == True 17 | 18 | def run(self, input, generator): 19 | complexities = {} 20 | 21 | # an iterator over all valid programs, by increasing complexity, and in 22 | # lexicographical order, together with its code's complexity. 23 | piter = generator.orderedEnumeration() 24 | 25 | maxLevin = 1 26 | # every phase goes through all programs of a certain Levin-complexity. 27 | while True: 28 | 29 | # generate all programs that might be needed in this phase. 30 | c = 0 31 | while c <= maxLevin: 32 | try: 33 | c, p = piter.next() 34 | except StopIteration: 35 | break 36 | if c not in complexities: 37 | complexities[c] = [p] 38 | else: 39 | complexities[c].append(p) 40 | 41 | # execute all programs, but with a set time-limit: 42 | # every phase the total time used doubles (= 2**maxLevin) 43 | for c in range(maxLevin): 44 | for p in complexities[c]: 45 | boundP = timeBoundExecution(p, 2**(maxLevin-c)/maxLevin) 46 | res = boundP.run(input) 47 | if self.stoppingCriterion(res): 48 | return res 49 | 50 | maxLevin += 1 -------------------------------------------------------------------------------- /pybrain/rl/learners/meta/meta.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | 4 | from pybrain.rl.learners.learner import Learner 5 | 6 | 7 | class MetaLearner(Learner): 8 | """ Learners that make use of other Learners, or learn how to learn. """ -------------------------------------------------------------------------------- /pybrain/rl/learners/modelbased/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.learners.modelbased.policyiteration import trueValues, trueQValues, policyIteration 2 | from pybrain.rl.learners.modelbased.leastsquares import LSTD_values, LSTD_Qvalues, LSPI_policy, LSTD_PI_policy -------------------------------------------------------------------------------- /pybrain/rl/learners/valuebased/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.rl.learners.valuebased.interface import ActionValueTable, ActionValueNetwork 2 | from pybrain.rl.learners.valuebased.nfq import NFQ 3 | from pybrain.rl.learners.valuebased.q import Q 4 | from pybrain.rl.learners.valuebased.qlambda import QLambda 5 | from pybrain.rl.learners.valuebased.sarsa import SARSA -------------------------------------------------------------------------------- /pybrain/rl/learners/valuebased/nfq.py: -------------------------------------------------------------------------------- 1 | from scipy import r_ 2 | 3 | from pybrain.rl.learners.valuebased.valuebased import ValueBasedLearner 4 | from pybrain.datasets import SupervisedDataSet 5 | from pybrain.supervised.trainers.rprop import RPropMinusTrainer 6 | from pybrain.supervised.trainers import BackpropTrainer 7 | from pybrain.utilities import one_to_n 8 | 9 | 10 | class NFQ(ValueBasedLearner): 11 | """ Neuro-fitted Q-learning""" 12 | 13 | def __init__(self, maxEpochs=20): 14 | ValueBasedLearner.__init__(self) 15 | self.gamma = 0.9 16 | self.maxEpochs = maxEpochs 17 | 18 | def learn(self): 19 | # convert reinforcement dataset to NFQ supervised dataset 20 | supervised = SupervisedDataSet(self.module.network.indim, 1) 21 | 22 | for seq in self.dataset: 23 | lastexperience = None 24 | for state, action, reward in seq: 25 | if not lastexperience: 26 | # delay each experience in sequence by one 27 | lastexperience = (state, action, reward) 28 | continue 29 | 30 | # use experience from last timestep to do Q update 31 | (state_, action_, reward_) = lastexperience 32 | 33 | Q = self.module.getValue(state_, action_[0]) 34 | 35 | inp = r_[state_, one_to_n(action_[0], self.module.numActions)] 36 | tgt = Q + 0.5*(reward_ + self.gamma * max(self.module.getActionValues(state)) - Q) 37 | supervised.addSample(inp, tgt) 38 | 39 | # update last experience with current one 40 | lastexperience = (state, action, reward) 41 | 42 | # train module with backprop/rprop on dataset 43 | trainer = RPropMinusTrainer(self.module.network, dataset=supervised, batchlearning=True, verbose=False) 44 | trainer.trainUntilConvergence(maxEpochs=self.maxEpochs) 45 | 46 | # alternative: backprop, was not as stable as rprop 47 | # trainer = BackpropTrainer(self.module.network, dataset=supervised, learningrate=0.005, batchlearning=True, verbose=True) 48 | # trainer.trainUntilConvergence(maxEpochs=self.maxEpochs) 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /pybrain/rl/learners/valuebased/qlambda.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from pybrain.rl.learners.valuebased.valuebased import ValueBasedLearner 4 | 5 | 6 | class QLambda(ValueBasedLearner): 7 | """ Q-lambda is a variation of Q-learning that uses an eligibility trace. """ 8 | 9 | offPolicy = True 10 | batchMode = False 11 | 12 | def __init__(self, alpha=0.5, gamma=0.99, qlambda=0.9): 13 | ValueBasedLearner.__init__(self) 14 | 15 | self.alpha = alpha 16 | self.gamma = gamma 17 | self.qlambda = qlambda 18 | 19 | self.laststate = None 20 | self.lastaction = None 21 | 22 | 23 | def learn(self): 24 | states = self.dataset['state'] 25 | actions = self.dataset['action'] 26 | rewards = self.dataset['reward'] 27 | 28 | for i in range(states.shape[0] - 1, 0, -1): 29 | lbda = self.qlambda ** (states.shape[0] - 1 - i) 30 | # if eligibility trace gets too long, break 31 | if lbda < 0.0001: 32 | break 33 | 34 | state = int(states[i]) 35 | laststate = int(states[i - 1]) 36 | # action = int(actions[i]) 37 | lastaction = int(actions[i - 1]) 38 | lastreward = int(rewards[i - 1]) 39 | 40 | qvalue = self.module.getValue(laststate, lastaction) 41 | maxnext = self.module.getValue(state, self.module.getMaxAction(state)) 42 | self.module.updateValue(laststate, lastaction, qvalue + self.alpha * lbda * (lastreward + self.gamma * maxnext - qvalue)) 43 | -------------------------------------------------------------------------------- /pybrain/rl/learners/valuebased/sarsa.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from pybrain.rl.learners.valuebased.valuebased import ValueBasedLearner 4 | 5 | 6 | class SARSA(ValueBasedLearner): 7 | """ State-Action-Reward-State-Action (SARSA) algorithm. 8 | 9 | In batchMode, the algorithm goes through all the samples in the 10 | history and performs an update on each of them. if batchMode is 11 | False, only the last data sample is considered. The user himself 12 | has to make sure to keep the dataset consistent with the agent's 13 | history.""" 14 | 15 | offPolicy = False 16 | batchMode = True 17 | 18 | def __init__(self, alpha=0.5, gamma=0.99): 19 | ValueBasedLearner.__init__(self) 20 | 21 | self.alpha = alpha 22 | self.gamma = gamma 23 | 24 | self.laststate = None 25 | self.lastaction = None 26 | 27 | def learn(self): 28 | if self.batchMode: 29 | samples = self.dataset 30 | else: 31 | samples = [[self.dataset.getSample()]] 32 | 33 | for seq in samples: 34 | # information from the previous episode (sequence) 35 | # should not influence the training on this episode 36 | self.laststate = None 37 | self.lastaction = None 38 | self.lastreward = None 39 | for state, action, reward in seq: 40 | 41 | state = int(state) 42 | action = int(action) 43 | 44 | # first learning call has no last state: skip 45 | if self.laststate == None: 46 | self.lastaction = action 47 | self.laststate = state 48 | self.lastreward = reward 49 | continue 50 | 51 | qvalue = self.module.getValue(self.laststate, self.lastaction) 52 | qnext = self.module.getValue(state, action) 53 | self.module.updateValue(self.laststate, self.lastaction, qvalue + self.alpha * (self.lastreward + self.gamma * qnext - qvalue)) 54 | 55 | # move state to oldstate 56 | self.laststate = state 57 | self.lastaction = action 58 | self.lastreward = reward 59 | 60 | -------------------------------------------------------------------------------- /pybrain/rl/learners/valuebased/valuebased.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from pybrain.rl.learners.learner import ExploringLearner, DataSetLearner, EpisodicLearner 4 | from pybrain.rl.explorers.discrete.egreedy import EpsilonGreedyExplorer 5 | 6 | 7 | class ValueBasedLearner(ExploringLearner, DataSetLearner, EpisodicLearner): 8 | """ An RL algorithm based on estimating a value-function.""" 9 | 10 | #: Does the algorithm work on-policy or off-policy? 11 | offPolicy = False 12 | 13 | #: Does the algorithm run in batch mode or online? 14 | batchMode = True 15 | 16 | _module = None 17 | _explorer = None 18 | 19 | def __init__(self): 20 | """ Create a default explorer for discrete learning tasks. """ 21 | self.explorer = EpsilonGreedyExplorer() 22 | 23 | def _setModule(self, module): 24 | """ Set module and tell explorer about the module. """ 25 | if self.explorer: 26 | self.explorer.module = module 27 | self._module = module 28 | 29 | def _getModule(self): 30 | """ Return the internal module. """ 31 | return self._module 32 | 33 | module = property(_getModule, _setModule) 34 | 35 | def _setExplorer(self, explorer): 36 | """ Set explorer and tell it the module, if already available. """ 37 | self._explorer = explorer 38 | if self.module: 39 | self._explorer.module = self.module 40 | 41 | def _getExplorer(self): 42 | """ Return the internal explorer. """ 43 | return self._explorer 44 | 45 | explorer = property(_getExplorer, _setExplorer) 46 | 47 | 48 | -------------------------------------------------------------------------------- /pybrain/structure/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.structure.connections.__init__ import * 2 | from pybrain.structure.modules.__init__ import * 3 | from pybrain.structure.networks.__init__ import * 4 | from pybrain.structure.modulemesh import ModuleMesh -------------------------------------------------------------------------------- /pybrain/structure/connections/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.structure.connections.full import FullConnection 2 | from pybrain.structure.connections.identity import IdentityConnection 3 | from pybrain.structure.connections.shared import SharedFullConnection, MotherConnection, SharedConnection 4 | from pybrain.structure.connections.linear import LinearConnection 5 | from pybrain.structure.connections.fullnotself import FullNotSelfConnection -------------------------------------------------------------------------------- /pybrain/structure/connections/full.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from scipy import reshape, dot, outer 4 | 5 | from pybrain.structure.connections.connection import Connection 6 | from pybrain.structure.parametercontainer import ParameterContainer 7 | 8 | 9 | class FullConnection(Connection, ParameterContainer): 10 | """Connection which fully connects every element from the first module's 11 | output buffer to the second module's input buffer in a matrix multiplicative 12 | manner.""" 13 | 14 | def __init__(self, *args, **kwargs): 15 | Connection.__init__(self, *args, **kwargs) 16 | ParameterContainer.__init__(self, self.indim*self.outdim) 17 | 18 | def _forwardImplementation(self, inbuf, outbuf): 19 | outbuf += dot(reshape(self.params, (self.outdim, self.indim)), inbuf) 20 | 21 | def _backwardImplementation(self, outerr, inerr, inbuf): 22 | inerr += dot(reshape(self.params, (self.outdim, self.indim)).T, outerr) 23 | ds = self.derivs 24 | ds += outer(inbuf, outerr).T.flatten() 25 | 26 | def whichBuffers(self, paramIndex): 27 | """Return the index of the input module's output buffer and 28 | the output module's input buffer for the given weight.""" 29 | return paramIndex % self.inmod.outdim, paramIndex / self.inmod.outdim 30 | -------------------------------------------------------------------------------- /pybrain/structure/connections/fullnotself.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from scipy import reshape, dot, outer, eye 4 | from pybrain.structure.connections import FullConnection 5 | 6 | 7 | class FullNotSelfConnection(FullConnection): 8 | """Connection which connects every element from the first module's 9 | output buffer to the second module's input buffer in a matrix multiplicative 10 | manner, EXCEPT the corresponding elements with the same index of each buffer 11 | (the diagonal of the parameter matrix is 0). Asserts that in and out dimensions 12 | are equal. """ 13 | #:TODO: the values on the diagonal are counted as parameters but not used! FIX! 14 | 15 | def __init__(self, *args, **kwargs): 16 | FullConnection.__init__(self, *args, **kwargs) 17 | assert self.indim == self.outdim, \ 18 | "Indim (%i) does not equal outdim (%i)" % ( 19 | self.indim, self.outdim) 20 | 21 | def _forwardImplementation(self, inbuf, outbuf): 22 | p = reshape(self.params, (self.outdim, self.indim)) * (1-eye(self.outdim)) 23 | outbuf += dot(p, inbuf) 24 | 25 | def _backwardImplementation(self, outerr, inerr, inbuf): 26 | p = reshape(self.params, (self.outdim, self.indim)) * (1-eye(self.outdim)) 27 | inerr += dot(p.T, outerr) 28 | ds = self.derivs 29 | ds += outer(inbuf, outerr).T.flatten() 30 | -------------------------------------------------------------------------------- /pybrain/structure/connections/identity.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.structure.connections.connection import Connection 4 | 5 | 6 | class IdentityConnection(Connection): 7 | """Connection which connects the i'th element from the first module's output 8 | buffer to the i'th element of the second module's input buffer.""" 9 | 10 | def __init__(self, *args, **kwargs): 11 | Connection.__init__(self, *args, **kwargs) 12 | assert self.indim == self.outdim, \ 13 | "Indim (%i) does not equal outdim (%i)" % ( 14 | self.indim, self.outdim) 15 | 16 | def _forwardImplementation(self, inbuf, outbuf): 17 | outbuf += inbuf 18 | 19 | def _backwardImplementation(self, outerr, inerr, inbuf): 20 | inerr += outerr -------------------------------------------------------------------------------- /pybrain/structure/connections/linear.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Justin S Bayer, bayer.justin@googlemail.com' 2 | 3 | 4 | from pybrain.structure.connections.connection import Connection 5 | from pybrain.structure.parametercontainer import ParameterContainer 6 | 7 | 8 | class LinearConnection(Connection, ParameterContainer): 9 | """Connection that just forwards by multiplying the output of the inmodule 10 | with a parameter and adds it to the input of the outmodule.""" 11 | 12 | def __init__(self, inmod, outmod, name=None, 13 | inSliceFrom=0, inSliceTo=None, outSliceFrom=0, outSliceTo=None): 14 | if inSliceTo is None: 15 | inSliceTo = inmod.outdim 16 | size = inSliceTo - inSliceFrom 17 | Connection.__init__(self, inmod, outmod, name, 18 | inSliceFrom, inSliceTo, outSliceFrom, outSliceTo) 19 | ParameterContainer.__init__(self, size) 20 | 21 | def _forwardImplementation(self, inbuf, outbuf): 22 | outbuf += inbuf * self.params 23 | 24 | def _backwardImplementation(self, outerr, inerr, inbuf): 25 | #CHECKME: not setting derivatives -- this means the multiplicative weight is never updated! 26 | inerr += outerr * self.params 27 | -------------------------------------------------------------------------------- /pybrain/structure/connections/permutation.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -_*- 2 | 3 | __author__ = 'Justin Bayer, bayer.justin@googlemail.com' 4 | __version__ = '$Id$' 5 | 6 | 7 | from scipy import array 8 | 9 | from pybrain.structure.connections.connection import Connection 10 | from pybrain.utilities import permute 11 | 12 | 13 | class PermutationConnection(Connection): 14 | """Connection that permutes the input by a given permutation.""" 15 | 16 | def __init__(self, inmod, outmod, permutation, blocksize, *args, **kwargs): 17 | Connection.__init__(self, inmod, outmod, *args, **kwargs) 18 | if self.indim != self.outdim: 19 | raise ValueError("Indim (%i) does not equal outdim (%i)" % ( 20 | self.indim, self.outdim)) 21 | if len(permutation) * blocksize != self.indim: 22 | raise ValueError( 23 | "Permutation has wrong size: should be %i but is %i." %( 24 | (self.indim / blocksize), len(permutation))) 25 | 26 | self.permutation = array(permutation) 27 | self.invpermutation = permute(range(len(permutation)), permutation) 28 | self.blocksize = blocksize 29 | 30 | def _forwardImplementation(self, inbuf, outbuf): 31 | inbuf = inbuf.reshape(self.indim / self.blocksize, self.blocksize) 32 | inbuf = permute(inbuf, self.permutation) 33 | inbuf.shape = self.indim, 34 | outbuf += inbuf 35 | 36 | def _backwardImplementation(self, outerr, inerr, inbuf): 37 | outerr = outerr.reshape(self.indim / self.blocksize, self.blocksize) 38 | outerr = permute(outerr, self.invpermutation) 39 | outerr.shape = self.indim, 40 | inerr += outerr 41 | -------------------------------------------------------------------------------- /pybrain/structure/connections/subsampling.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.structure.connections.connection import Connection 4 | from pybrain.structure.parametercontainer import ParameterContainer 5 | from scipy import average 6 | 7 | #:TODO: backward pass 8 | 9 | class SubsamplingConnection(Connection, ParameterContainer): 10 | """Connection that just averages all the inputs before forwarding.""" 11 | 12 | def __init__(self, inmod, outmod, name=None, 13 | inSliceFrom=0, inSliceTo=None, outSliceFrom=0, outSliceTo=None): 14 | if outSliceTo is None: 15 | outSliceTo = outmod.indim 16 | size = outSliceTo - outSliceFrom 17 | Connection.__init__(self, inmod, outmod, name, 18 | inSliceFrom, inSliceTo, outSliceFrom, outSliceTo) 19 | ParameterContainer.__init__(self, size) 20 | 21 | def _forwardImplementation(self, inbuf, outbuf): 22 | outbuf += average(inbuf) * self.params 23 | 24 | def _backwardImplementation(self, outerr, inerr, inbuf): 25 | raise NotImplementedError() 26 | 27 | -------------------------------------------------------------------------------- /pybrain/structure/evolvables/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/structure/evolvables/__init__.py -------------------------------------------------------------------------------- /pybrain/structure/evolvables/evolvable.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | import copy 4 | 5 | from pybrain.utilities import abstractMethod, Named 6 | 7 | 8 | class Evolvable(Named): 9 | """ The interface for all Evolvables, i.e. which implement mutation, randomize and copy operators. """ 10 | 11 | def mutate(self, **args): 12 | """ Vary some properties of the underlying module, so that it's behavior 13 | changes, (but not too abruptly). """ 14 | abstractMethod() 15 | 16 | def copy(self): 17 | """ By default, returns a full deep-copy - subclasses should implement something faster, if appropriate. """ 18 | return copy.deepcopy(self) 19 | 20 | def randomize(self): 21 | """ Sets all variable parameters to random values. """ 22 | abstractMethod() 23 | 24 | def newSimilarInstance(self): 25 | """ Generates a new Evolvable of the same kind.""" 26 | res = self.copy() 27 | res.randomize() 28 | return res 29 | -------------------------------------------------------------------------------- /pybrain/structure/evolvables/maskedmodule.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.structure.evolvables.maskedparameters import MaskedParameters 4 | from pybrain.structure.modules.module import Module 5 | 6 | 7 | class MaskedModule(MaskedParameters, Module): 8 | """ an extension of masked-parameters, that wraps a module, and forwards the functionality. """ 9 | 10 | def reset(self): 11 | return self.pcontainer.reset() 12 | 13 | def _resetBuffers(self): 14 | return self.pcontainer._resetBuffers() 15 | 16 | def activate(self, *args, **kwargs): 17 | return self.pcontainer.activate(*args, **kwargs) 18 | 19 | def backActivate(self, *args, **kwargs): 20 | return self.pcontainer.backActivate(*args, **kwargs) 21 | 22 | def forward(self, *args, **kwargs): 23 | return self.pcontainer.forward(*args, **kwargs) 24 | 25 | def backward(self, *args, **kwargs): 26 | return self.pcontainer.backward(*args, **kwargs) 27 | 28 | def activateOnDataset(self, *args, **kwargs): 29 | return self.pcontainer.activateOnDataset(*args, **kwargs) 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /pybrain/structure/evolvables/topology.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.utilities import abstractMethod 4 | from pybrain.structure.evolvables.evolvable import Evolvable 5 | from pybrain.structure.parametercontainer import ParameterContainer 6 | 7 | 8 | class TopologyEvolvable(ParameterContainer): 9 | """ An evolvable object, with higher-level mutations, 10 | that change the topology (in the broadest sense). 11 | It contains an instance of ParameterContainer. """ 12 | 13 | pcontainer = None 14 | 15 | def __init__(self, pcontainer, **args): 16 | self.setArgs(**args) 17 | self.pcontainer = pcontainer 18 | 19 | @property 20 | def params(self): 21 | return self.pcontainer.params 22 | 23 | def _setParameters(self, x): 24 | self.pcontainer._setParameters(x) 25 | 26 | def topologyMutate(self): 27 | abstractMethod() 28 | 29 | def newSimilarInstance(self): 30 | """ generate a new Evolvable with the same topology """ 31 | res = self.copy() 32 | res.randomize() 33 | return res 34 | 35 | def copy(self): 36 | """ copy everything, except the pcontainer """ 37 | # CHECKME: is this correct, or might it be misleading? 38 | tmp = self.pcontainer 39 | self.pcontainer = None 40 | cp = Evolvable.copy(self) 41 | cp.pcontainer = tmp 42 | self.pcontainer = tmp 43 | return cp -------------------------------------------------------------------------------- /pybrain/structure/modules/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.structure.modules.biasunit import BiasUnit 2 | from pybrain.structure.modules.gate import GateLayer, DoubleGateLayer, MultiplicationLayer, SwitchLayer 3 | from pybrain.structure.modules.gaussianlayer import GaussianLayer 4 | from pybrain.structure.modules.linearlayer import LinearLayer 5 | from pybrain.structure.modules.lstm import LSTMLayer 6 | from pybrain.structure.modules.mdlstm import MDLSTMLayer 7 | from pybrain.structure.modules.mdrnnlayer import MdrnnLayer 8 | from pybrain.structure.modules.sigmoidlayer import SigmoidLayer 9 | from pybrain.structure.modules.softmax import SoftmaxLayer, PartialSoftmaxLayer 10 | from pybrain.structure.modules.statedependentlayer import StateDependentLayer 11 | from pybrain.structure.modules.tanhlayer import TanhLayer 12 | from pybrain.structure.modules.kohonen import KohonenMap 13 | from pybrain.structure.modules.table import Table 14 | from pybrain.structure.modules.module import Module 15 | from pybrain.structure.modules.relulayer import ReluLayer 16 | -------------------------------------------------------------------------------- /pybrain/structure/modules/biasunit.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.structure.modules.neuronlayer import NeuronLayer 4 | from pybrain.structure.modules.module import Module 5 | 6 | 7 | class BiasUnit(NeuronLayer): 8 | """A simple bias unit with a single constant output.""" 9 | 10 | dim = 1 11 | 12 | def __init__(self, name=None): 13 | Module.__init__(self, 0, 1, name = name) 14 | 15 | def _forwardImplementation(self, inbuf, outbuf): 16 | outbuf[:] = 1 -------------------------------------------------------------------------------- /pybrain/structure/modules/gaussianlayer.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from scipy import random 4 | from pybrain.structure.modules.neuronlayer import NeuronLayer 5 | from pybrain.tools.functions import expln, explnPrime 6 | from pybrain.structure.parametercontainer import ParameterContainer 7 | 8 | 9 | class GaussianLayer(NeuronLayer, ParameterContainer): 10 | """ A layer implementing a gaussian interpretation of the input. The mean is 11 | the input, the sigmas are stored in the module parameters.""" 12 | 13 | def __init__(self, dim, name=None): 14 | NeuronLayer.__init__(self, dim, name) 15 | # initialize sigmas to 0 16 | ParameterContainer.__init__(self, dim, stdParams = 0) 17 | # if autoalpha is set to True, alpha_sigma = alpha_mu = alpha*sigma^2 18 | self.autoalpha = False 19 | self.enabled = True 20 | 21 | def setSigma(self, sigma): 22 | """Wrapper method to set the sigmas (the parameters of the module) to a 23 | certain value. """ 24 | assert len(sigma) == self.indim 25 | self._params *= 0 26 | self._params += sigma 27 | 28 | def _forwardImplementation(self, inbuf, outbuf): 29 | if not self.enabled: 30 | outbuf[:] = inbuf 31 | else: 32 | outbuf[:] = random.normal(inbuf, expln(self.params)) 33 | 34 | def _backwardImplementation(self, outerr, inerr, outbuf, inbuf): 35 | expln_params = expln(self.params) 36 | self._derivs += ((outbuf - inbuf)**2 - expln_params**2) / expln_params * explnPrime(self.params) 37 | inerr[:] = (outbuf - inbuf) 38 | 39 | if not self.autoalpha: 40 | inerr /= expln_params**2 41 | self._derivs /= expln_params**2 42 | -------------------------------------------------------------------------------- /pybrain/structure/modules/linearlayer.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.structure.modules.neuronlayer import NeuronLayer 4 | 5 | 6 | class LinearLayer(NeuronLayer): 7 | """ The simplest kind of module, not doing any transformation. """ 8 | 9 | def _forwardImplementation(self, inbuf, outbuf): 10 | outbuf[:] = inbuf 11 | 12 | def _backwardImplementation(self, outerr, inerr, outbuf, inbuf): 13 | inerr[:] = outerr -------------------------------------------------------------------------------- /pybrain/structure/modules/neuronlayer.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.structure.modules.module import Module 4 | 5 | 6 | class NeuronLayer(Module): 7 | """Module conceptually representing a layer of units """ 8 | 9 | # Number of neurons 10 | dim = 0 11 | 12 | def __init__(self, dim, name=None): 13 | """Create a layer with dim number of units.""" 14 | Module.__init__(self, dim, dim, name=name) 15 | self.setArgs(dim=dim) 16 | 17 | def whichNeuron(self, inputIndex=None, outputIndex=None): 18 | """Determine which neuron a position in the input/output buffer 19 | corresponds to. """ 20 | if inputIndex is not None: 21 | return inputIndex 22 | if outputIndex is not None: 23 | return outputIndex -------------------------------------------------------------------------------- /pybrain/structure/modules/relulayer.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.structure.modules.neuronlayer import NeuronLayer 4 | 5 | class ReluLayer(NeuronLayer): 6 | """ Layer of rectified linear units (relu). """ 7 | 8 | def _forwardImplementation(self, inbuf, outbuf): 9 | outbuf[:] = inbuf * (inbuf > 0) 10 | 11 | def _backwardImplementation(self, outerr, inerr, outbuf, inbuf): 12 | inerr[:] = outerr * (inbuf > 0) -------------------------------------------------------------------------------- /pybrain/structure/modules/samplelayer.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | __author__ = ('Christian Osendorfer, osendorf@in.tum.de; ' 5 | 'Justin S Bayer, bayerj@in.tum.de') 6 | 7 | 8 | from scipy import random 9 | 10 | from pybrain.structure.modules.neuronlayer import NeuronLayer 11 | 12 | 13 | class SampleLayer(NeuronLayer): 14 | """Baseclass for all layers that have stochastic output depending on the 15 | incoming weight.""" 16 | 17 | 18 | class BernoulliLayer(SampleLayer): 19 | 20 | def _forwardImplementation(self, inbuf, outbuf): 21 | outbuf[:] = inbuf <= random.random(inbuf.shape) 22 | -------------------------------------------------------------------------------- /pybrain/structure/modules/sigmoidlayer.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.structure.modules.neuronlayer import NeuronLayer 4 | from pybrain.tools.functions import sigmoid 5 | 6 | 7 | class SigmoidLayer(NeuronLayer): 8 | """Layer implementing the sigmoid squashing function.""" 9 | 10 | def _forwardImplementation(self, inbuf, outbuf): 11 | outbuf[:] = sigmoid(inbuf) 12 | 13 | def _backwardImplementation(self, outerr, inerr, outbuf, inbuf): 14 | inerr[:] = outbuf * (1 - outbuf) * outerr 15 | 16 | -------------------------------------------------------------------------------- /pybrain/structure/modules/softmax.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | 4 | import scipy 5 | 6 | from pybrain.structure.modules.neuronlayer import NeuronLayer 7 | from pybrain.tools.functions import safeExp 8 | 9 | 10 | class SoftmaxLayer(NeuronLayer): 11 | """ A layer implementing a softmax distribution over the input.""" 12 | 13 | # TODO: collapsing option? 14 | # CHECKME: temperature parameter? 15 | 16 | def _forwardImplementation(self, inbuf, outbuf): 17 | outbuf[:] = safeExp(inbuf) 18 | outbuf /= sum(outbuf) 19 | 20 | def _backwardImplementation(self, outerr, inerr, outbuf, inbuf): 21 | inerr[:] = outerr 22 | 23 | 24 | class PartialSoftmaxLayer(NeuronLayer): 25 | """Layer implementing a softmax distribution over slices of the input.""" 26 | 27 | def __init__(self, size, slicelength): 28 | super(PartialSoftmaxLayer, self).__init__(size) 29 | self.slicelength = slicelength 30 | 31 | def _forwardImplementation(self, inbuf, outbuf): 32 | outbuf[:] = safeExp(inbuf) 33 | outbuf.shape = scipy.size(outbuf) / self.slicelength, self.slicelength 34 | s = outbuf.sum(axis=1) 35 | outbuf = (outbuf.T / s).T.flatten() 36 | 37 | def _backwardImplementation(self, outerr, inerr, outbuf, inbuf): 38 | inerr[:] = outerr 39 | -------------------------------------------------------------------------------- /pybrain/structure/modules/softsign.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | from pybrain.structure.modules.neuronlayer import NeuronLayer 4 | 5 | class SoftSignLayer(NeuronLayer): 6 | """ softsign activation function as described in X. Glorot and Y. 7 | Bengio. Understanding the difficulty of training deep feedforward neural 8 | networks. In Proceedings of the 13th International Workshop on 9 | Artificial Intelligence and Statistics, 2010. """ 10 | 11 | def _forwardImplementation(self, inbuf, outbuf): 12 | outbuf[:] = inbuf / (1 + abs(inbuf)) 13 | 14 | def _backwardImplementation(self, outerr, inerr, outbuf, inbuf): 15 | inerr[:] = (1 - abs(outbuf))**2 * outerr 16 | -------------------------------------------------------------------------------- /pybrain/structure/modules/table.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Thomas Rueckstiess, ruecksti@in.tum.de' 2 | 3 | 4 | from pybrain.structure.modules.module import Module 5 | from pybrain.structure.parametercontainer import ParameterContainer 6 | 7 | class Table(Module, ParameterContainer): 8 | """ implements a simple 2D table with dimensions rows x columns, 9 | which is basically a wrapper for a numpy array. 10 | """ 11 | 12 | def __init__(self, numRows, numColumns, name=None): 13 | """ initialize with the number of rows and columns. the table 14 | values are all set to zero. 15 | """ 16 | Module.__init__(self, 2, 1, name) 17 | ParameterContainer.__init__(self, numRows*numColumns) 18 | 19 | self.numRows = numRows 20 | self.numColumns = numColumns 21 | 22 | def _forwardImplementation(self, inbuf, outbuf): 23 | """ takes two coordinates, row and column, and returns the 24 | value in the table. 25 | """ 26 | outbuf[0] = self.params.reshape(self.numRows, self.numColumns)[inbuf[0], inbuf[1]] 27 | 28 | def updateValue(self, row, column, value): 29 | """ set the value at a certain location in the table. """ 30 | self.params.reshape(self.numRows, self.numColumns)[row, column] = value 31 | 32 | def getValue(self, row, column): 33 | """ return the value at a certain location in the table. """ 34 | return self.params.reshape(self.numRows, self.numColumns)[row, column] 35 | 36 | -------------------------------------------------------------------------------- /pybrain/structure/modules/tanhlayer.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from scipy import tanh 4 | 5 | from pybrain.structure.modules.neuronlayer import NeuronLayer 6 | 7 | 8 | class TanhLayer(NeuronLayer): 9 | """ A layer implementing the tanh squashing function. """ 10 | 11 | def _forwardImplementation(self, inbuf, outbuf): 12 | outbuf[:] = tanh(inbuf) 13 | 14 | def _backwardImplementation(self, outerr, inerr, outbuf, inbuf): 15 | inerr[:] = (1 - outbuf**2) * outerr 16 | -------------------------------------------------------------------------------- /pybrain/structure/moduleslice.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.utilities import Named 4 | 5 | 6 | class ModuleSlice(Named): 7 | """ A wrapper for using a particular input-output slice of a module's buffers. 8 | The constructors of connections between ModuleSlices need to ensure a correct use 9 | (i.e) do the slicing on the base module directly. """ 10 | 11 | def __init__(self, base, inSliceFrom = 0, inSliceTo = None, outSliceFrom = 0, outSliceTo = None): 12 | """ :key base: the base module that is sliced """ 13 | if isinstance(base, ModuleSlice): 14 | # tolerantly handle the case of a slice of another slice 15 | self.base = base.base 16 | self.inOffset = inSliceFrom + base.inSliceFrom 17 | self.outOffset = outSliceFrom + base.outSliceFrom 18 | if inSliceTo == None: 19 | inSliceTo = self.base.indim + base.inSliceFrom 20 | if outSliceTo == None: 21 | outSliceTo = self.base.outdim + base.outSliceFrom 22 | self.name = base.base.name 23 | else: 24 | self.base = base 25 | self.inOffset = inSliceFrom 26 | self.outOffset = outSliceFrom 27 | if inSliceTo == None: 28 | inSliceTo = self.base.indim 29 | if outSliceTo == None: 30 | outSliceTo = self.base.outdim 31 | self.name = base.name 32 | assert self.inOffset >= 0 and self.outOffset >= 0 33 | self.indim = inSliceTo - inSliceFrom 34 | self.outdim = outSliceTo - outSliceFrom 35 | self.name += ('-slice:('+str(self.inOffset)+','+str(self.indim+self.inOffset)+')(' 36 | +str(self.outOffset)+','+str(self.outdim+self.outOffset)+')') 37 | # some slicing is required 38 | assert self.indim+self.outdim < base.indim+base.outdim 39 | -------------------------------------------------------------------------------- /pybrain/structure/networks/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.structure.networks.swiping import SwipingNetwork 2 | from pybrain.structure.networks.borderswiping import BorderSwipingNetwork 3 | from pybrain.structure.networks.neurondecomposable import NeuronDecomposableNetwork 4 | from pybrain.structure.networks.feedforward import FeedForwardNetwork 5 | from pybrain.structure.networks.recurrent import RecurrentNetwork 6 | from pybrain.structure.networks.network import Network 7 | from pybrain.structure.networks.bidirectional import BidirectionalNetwork 8 | -------------------------------------------------------------------------------- /pybrain/structure/networks/custom/__init__.py: -------------------------------------------------------------------------------- 1 | from capturegame import CaptureGameNetwork -------------------------------------------------------------------------------- /pybrain/supervised/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.supervised.trainers.__init__ import * -------------------------------------------------------------------------------- /pybrain/supervised/evolino/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/supervised/evolino/__init__.py -------------------------------------------------------------------------------- /pybrain/supervised/evolino/gindividual.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Michael Isik' 2 | 3 | 4 | class Individual(object): 5 | """ Simple abstract template for a minimal individual """ 6 | def getGenome(self): 7 | """ Should return a reference to the genome. 8 | """ 9 | raise NotImplementedError() 10 | 11 | def copy(self): 12 | """ Should return a full copy of the individual 13 | """ 14 | raise NotImplementedError() 15 | 16 | 17 | -------------------------------------------------------------------------------- /pybrain/supervised/evolino/individual.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Michael Isik' 2 | 3 | 4 | from pybrain.supervised.evolino.gindividual import Individual 5 | from copy import copy, deepcopy 6 | 7 | 8 | class EvolinoIndividual(Individual): 9 | """ Individual of the Evolino framework, that consists of a list of 10 | sub-individuals. The genomes of the sub-individuals are used as 11 | the cromosomes for the main individual's genome. 12 | The genome of an individual encodes the RNN's connection weights. 13 | """ 14 | def __init__(self, sub_individuals): 15 | """ :key sub_individuals: sequence (e.g. list) of sub-individuals 16 | """ 17 | self._sub_individuals = list(sub_individuals) 18 | 19 | def getGenome(self): 20 | """ Returns the genome created by concatenating the chromosomes supplied 21 | by the sub-individuals. 22 | """ 23 | genome = [] 24 | for sub_individual in self._sub_individuals: 25 | genome.append(deepcopy(sub_individual.getGenome())) 26 | return genome 27 | 28 | def getSubIndividuals(self): 29 | """ Returns a shallow copy of the list of sub-individuals """ 30 | return copy(self._sub_individuals) 31 | 32 | 33 | 34 | class EvolinoSubIndividual(Individual): 35 | """ The sub-individual class of evolino 36 | """ 37 | _next_id = 0 38 | def __init__(self, genome): 39 | """ :key genome: Any kind of nested iteratable container containing 40 | floats as leafs 41 | """ 42 | self.setGenome(genome) 43 | self.id = EvolinoSubIndividual._next_id 44 | EvolinoSubIndividual._next_id += 1 45 | 46 | def getGenome(self): 47 | """ Returns the genome. """ 48 | return self._genome 49 | 50 | def setGenome(self, genome): 51 | """ Sets the genome. """ 52 | self._genome = genome 53 | 54 | def copy(self): 55 | """ Returns a complete copy of the individual. """ 56 | return copy(self) 57 | 58 | def __copy__(self): 59 | """ Returns a complete copy of the individual. """ 60 | return EvolinoSubIndividual(deepcopy(self._genome)) 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /pybrain/supervised/evolino/variate.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Michael Isik' 2 | 3 | from random import uniform, random, gauss 4 | from numpy import tan, pi 5 | 6 | 7 | class UniformVariate: 8 | def __init__(self, min_val=0., max_val=1.): 9 | """ Initializes the uniform variate with a min and a max value. 10 | """ 11 | self._min_val = min_val 12 | self._max_val = max_val 13 | 14 | def getSample(self, min_val=None, max_val=None): 15 | """ Returns a random value between min_val and max_val. 16 | """ 17 | if min_val is None: min_val = self._min_val 18 | if max_val is None: max_val = self._max_val 19 | return uniform(min_val, max_val) 20 | 21 | class CauchyVariate: 22 | def __init__(self, x0=0., alpha=1.): 23 | """ :key x0: Median and mode of the Cauchy distribution 24 | :key alpha: scale 25 | """ 26 | self.x0 = x0 27 | self.alpha = alpha 28 | 29 | def getSample(self, x0=None, alpha=None): 30 | if x0 is None: x0 = self.x0 31 | if alpha is None: alpha = self.alpha 32 | uniform_variate = random() 33 | cauchy_variate = x0 + alpha * tan(pi * (uniform_variate - 0.5)) 34 | return cauchy_variate 35 | 36 | 37 | class GaussianVariate: 38 | def __init__(self, x0=0., alpha=1.): 39 | """ :key x0: Mean 40 | :key alpha: standard deviation 41 | """ 42 | self.x0 = x0 43 | self.alpha = alpha 44 | 45 | def getSample(self, x0=None, alpha=None): 46 | if x0 is None: x0 = self.x0 47 | if alpha is None: alpha = self.alpha 48 | return gauss(x0, alpha) 49 | 50 | 51 | -------------------------------------------------------------------------------- /pybrain/supervised/knn/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/supervised/knn/__init__.py -------------------------------------------------------------------------------- /pybrain/supervised/knn/lsh/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/supervised/knn/lsh/__init__.py -------------------------------------------------------------------------------- /pybrain/supervised/trainers/__init__.py: -------------------------------------------------------------------------------- 1 | from pybrain.supervised.trainers.trainer import Trainer 2 | from pybrain.supervised.trainers.backprop import BackpropTrainer 3 | from pybrain.supervised.trainers.rprop import RPropMinusTrainer 4 | -------------------------------------------------------------------------------- /pybrain/supervised/trainers/trainer.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | __version__ = '$Id$' 3 | 4 | from pybrain.utilities import Named, abstractMethod 5 | 6 | 7 | class Trainer(Named): 8 | """ A trainer determines how to change the adaptive parameters of a module. 9 | It requires access to a DataSet object (which provides input-target tuples). """ 10 | # e.g. bptt, rtrl, svm 11 | 12 | ds = None 13 | module = None 14 | 15 | def __init__(self, module): 16 | self.module = module 17 | 18 | def setData(self, dataset): 19 | """Associate the given dataset with the trainer.""" 20 | self.ds = dataset 21 | if dataset: 22 | assert dataset.indim == self.module.indim 23 | assert dataset.outdim == self.module.outdim 24 | 25 | def trainOnDataset(self, dataset, *args, **kwargs): 26 | """Set the dataset and train. 27 | 28 | Additional arguments are passed on to the train method.""" 29 | self.setData(dataset) 30 | self.trainEpochs(*args, **kwargs) 31 | 32 | def trainEpochs(self, epochs=1, *args, **kwargs): 33 | """Train on the current dataset for the given number of `epochs`. 34 | 35 | Additional arguments are passed on to the train method.""" 36 | for dummy in range(epochs): 37 | self.train(*args, **kwargs) 38 | 39 | def train(self): 40 | """Train on the current dataset, for a single epoch.""" 41 | abstractMethod() 42 | 43 | 44 | -------------------------------------------------------------------------------- /pybrain/tests/__init__.py: -------------------------------------------------------------------------------- 1 | from helpers import gradientCheck, buildAppropriateDataset, xmlInvariance, \ 2 | epsilonCheck 3 | from testsuites import runModuleTestSuite -------------------------------------------------------------------------------- /pybrain/tests/auxiliary.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = 'Justin Bayer, bayer.justin@googlemail.com' 4 | __version__ = '$Id$' 5 | 6 | 7 | """"This module is a place to hold functionality that _has_ to be outside of a 8 | test module but is required by it.""" 9 | -------------------------------------------------------------------------------- /pybrain/tests/testsuites.py: -------------------------------------------------------------------------------- 1 | """Module that contains several utilities for testing.""" 2 | 3 | __author__ = 'Justin Bayer, bayer.justin@googlemail.com' 4 | __version__ = '$Id$' 5 | 6 | 7 | from doctest import DocTestSuite, ELLIPSIS, REPORT_ONLY_FIRST_FAILURE, \ 8 | NORMALIZE_WHITESPACE 9 | from unittest import TestSuite, TestLoader, TextTestRunner 10 | 11 | 12 | def runModuleTestSuite(module): 13 | """Runs a test suite for all local tests.""" 14 | suite = TestSuite([TestLoader().loadTestsFromModule(module)]) 15 | 16 | # Add local doctests 17 | optionflags = ELLIPSIS | NORMALIZE_WHITESPACE | REPORT_ONLY_FIRST_FAILURE 18 | 19 | try: 20 | suite.addTest(DocTestSuite(module, optionflags=optionflags)) 21 | except ValueError: 22 | # No tests have been found in that module. 23 | pass 24 | 25 | TextTestRunner().run(suite) 26 | 27 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/auxiliary/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/auxiliary/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/datasets/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/datasets/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/optimization/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/optimization/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/optimization/populationbased/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/optimization/populationbased/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/optimization/populationbased/test_pso_ring.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | 4 | >>> from pybrain.optimization.populationbased.pso import ring 5 | >>> ring(range(9)) 6 | {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)} 7 | 8 | Disabled: 9 | lattice(range(9)) 10 | {0: (1, 2), 11 | 1: (0, 3), 12 | 2: (0, 3), 13 | 3: (1, 2) 14 | 4: 15 | 5: 16 | 6: 17 | 7: 18 | 8: 19 | 9:} 20 | 21 | """ 22 | 23 | __author__ = ('Justin Bayer, bayer.justin@googlemail.com;' 24 | 'Julian Togelius, julian@idsia.ch') 25 | 26 | from pybrain.tests import runModuleTestSuite 27 | 28 | if __name__ == '__main__': 29 | runModuleTestSuite(__import__('__main__')) 30 | 31 | 32 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/rl/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/rl/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/rl/environments/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/rl/environments/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/rl/environments/twoplayergames/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/rl/environments/twoplayergames/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/rl/environments/twoplayergames/test_capture_game.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Initialize a capturegame 4 | >>> from pybrain.rl.environments.twoplayergames import CaptureGame 5 | >>> c = CaptureGame(5) 6 | >>> print(c) 7 | . . . . . 8 | . . . . . 9 | . . . . . 10 | . . . . . 11 | . . . . . 12 | 13 | 14 | Do some moves to produce a situation 15 | >>> c.performAction([1, (1,0)]) 16 | >>> c.performAction([1, (0,1)]) 17 | >>> c.performAction([1, (1,1)]) 18 | >>> c.performAction([-1, (2,0)]) 19 | >>> c.performAction([-1, (0,2)]) 20 | >>> c.performAction([-1, (1,2)]) 21 | >>> c.performAction([-1, (2,1)]) 22 | 23 | Now it is almost decided, white has a killing move! 24 | >>> c.getKilling(-1) 25 | [(0, 0)] 26 | 27 | >>> c.getWinner() 28 | 29 | Do it! 30 | >>> c.performAction([-1, (0,0)]) 31 | 32 | White wins. 33 | >>> c.getWinner() 34 | -1 35 | 36 | Check if all the values are right: 37 | >>> print(c) 38 | x X O . . 39 | X X O . . 40 | O O . . . 41 | . . . . . 42 | . . . . . 43 | Winner: White (*) (moves done:8) 44 | 45 | 46 | >>> c.groups 47 | {(0, 1): 5, (1, 2): 2, (2, 1): 10, (0, 2): 2, (2, 0): 10, (1, 0): 5, (1, 1): 5} 48 | 49 | >>> c.liberties 50 | {2: set([(0, 3), (1, 3), (2, 2)]), 5: set([(0, 0)]), 10: set([(3, 0), (3, 1), (2, 2)])} 51 | 52 | """ 53 | 54 | __author__ = 'Tom Schaul, tom@idsia.ch' 55 | 56 | from pybrain.tests import runModuleTestSuite 57 | 58 | if __name__ == '__main__': 59 | runModuleTestSuite(__import__('__main__')) 60 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/rl/environments/twoplayergames/test_pente.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Initialize a game of Pente. 4 | 5 | >>> from pybrain.rl.environments.twoplayergames.pente import PenteGame 6 | >>> dim = 5 7 | >>> c = PenteGame((dim, dim)) 8 | >>> print(c) 9 | _ _ _ _ _ 10 | _ _ _ _ _ 11 | _ _ * _ _ 12 | _ _ _ _ _ 13 | _ _ _ _ _ 14 | Black captured:0, white captured:0. 15 | 16 | 17 | Do some moves to produce a situation 18 | >>> c.performAction([1, (0,1)]) 19 | >>> c.performAction([-1, (1,0)]) 20 | >>> c.performAction([1, (1,1)]) 21 | >>> c.performAction([-1, (1,2)]) 22 | >>> c.performAction([1, (2,0)]) 23 | >>> c.performAction([-1, (2,1)]) 24 | >>> c.performAction([1, (0,2)]) 25 | 26 | Show the updated board: 27 | >>> print(c) 28 | _ # # _ _ 29 | * # * _ _ 30 | # * * _ _ 31 | _ _ _ _ _ 32 | _ _ _ _ _ 33 | Black captured:0, white captured:0. 34 | 35 | 36 | Do some captures: 37 | >>> c.performAction([-1, (0,3)]) 38 | >>> c.performAction([1, (3,2)]) 39 | >>> c.performAction([-1, (0,0)]) 40 | 41 | Stepping between black stones is not deadly though: 42 | >>> c.performAction([1, (2,3)]) 43 | >>> c.performAction([-1, (2,2)]) 44 | >>> print(c) 45 | * _ _ * _ 46 | * # _ _ _ 47 | # * * # _ 48 | _ _ # _ _ 49 | _ _ _ _ _ 50 | Black captured:1, white captured:1. 51 | 52 | Fast forward to the end: 53 | >>> c.performAction([-1, (0,4)]) 54 | >>> c.performAction([-1, (3,1)]) 55 | >>> c.performAction([-1, (4,0)]) 56 | 57 | Now it is almost decided, white has a killing move! 58 | >>> c.getKilling(-1) 59 | [(1, 3)] 60 | 61 | >>> c.getWinner() 62 | 63 | Do it! 64 | >>> c.performAction([-1, (1,3)]) 65 | 66 | White wins. 67 | >>> c.getWinner() 68 | -1 69 | 70 | Check if all the values are right: 71 | >>> print(c) 72 | * _ _ * * 73 | * # _ x _ 74 | # * * # _ 75 | _ * # _ _ 76 | * _ _ _ _ 77 | Winner: White (*) (moves done:17) 78 | Black captured:1, white captured:1. 79 | 80 | 81 | """ 82 | 83 | __author__ = 'Tom Schaul, tom@idsia.ch' 84 | 85 | from pybrain.tests import runModuleTestSuite 86 | 87 | if __name__ == '__main__': 88 | runModuleTestSuite(__import__('__main__')) 89 | 90 | 91 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/structure/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/connections/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/structure/connections/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/connections/test_sliced_connections.py: -------------------------------------------------------------------------------- 1 | """ 2 | >>> from scipy import array 3 | >>> from pybrain.tests import epsilonCheck 4 | 5 | Trying to build a network with shared connections: 6 | 7 | >>> from random import random 8 | >>> n = buildSlicedNetwork() 9 | >>> n.params[:] = array((2, 2)) 10 | 11 | The transformation of the first input to the second output is identical to the transformation of the 12 | second towards the first: 13 | 14 | >>> r1, r2 = 2.5, 3.2 15 | >>> v1, v2 = n.activate([r1, r2]) 16 | >>> epsilonCheck(6.4 - v1) 17 | True 18 | >>> epsilonCheck(5 - v2) 19 | True 20 | 21 | 22 | """ 23 | 24 | __author__ = 'Tom Schaul, tom@idsia.ch' 25 | 26 | 27 | from pybrain.structure.networks.feedforward import FeedForwardNetwork 28 | from pybrain import LinearLayer, FullConnection 29 | from pybrain.tests import runModuleTestSuite 30 | 31 | 32 | def buildSlicedNetwork(): 33 | """ build a network with shared connections. Two hidden modules are 34 | symmetrically linked, but to a different input neuron than the 35 | output neuron. The weights are random. """ 36 | N = FeedForwardNetwork('sliced') 37 | a = LinearLayer(2, name = 'a') 38 | b = LinearLayer(2, name = 'b') 39 | N.addInputModule(a) 40 | N.addOutputModule(b) 41 | 42 | N.addConnection(FullConnection(a, b, inSliceTo=1, outSliceFrom=1)) 43 | N.addConnection(FullConnection(a, b, inSliceFrom=1, outSliceTo=1)) 44 | N.sortModules() 45 | return N 46 | 47 | 48 | if __name__ == "__main__": 49 | runModuleTestSuite(__import__('__main__')) 50 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/connections/test_subsampling_connection.py: -------------------------------------------------------------------------------- 1 | """ 2 | >>> from pybrain.tests import epsilonCheck 3 | >>> n = buildSubsamplingNetwork() 4 | 5 | All those inputs will be averaged in two blocks (first 4 and last 2), 6 | so they should produce the same outputs. 7 | 8 | >>> x1 = n.activate([3,0,0,0,0,2])[0] 9 | >>> x2 = n.activate([0,0,0,3,2,0])[0] 10 | >>> x3 = n.activate([1,1,-2,3,1,1])[0] 11 | 12 | >>> epsilonCheck(x1 - x2) 13 | True 14 | >>> epsilonCheck(x1 - x3) 15 | True 16 | 17 | 18 | """ 19 | 20 | __author__ = 'Tom Schaul, tom@idsia.ch' 21 | 22 | 23 | from pybrain.structure.connections.subsampling import SubsamplingConnection 24 | from pybrain.structure.networks.feedforward import FeedForwardNetwork 25 | from pybrain import LinearLayer 26 | from pybrain.tests import runModuleTestSuite 27 | 28 | 29 | def buildSubsamplingNetwork(): 30 | """ Builds a network with subsampling connections. """ 31 | n = FeedForwardNetwork() 32 | n.addInputModule(LinearLayer(6, 'in')) 33 | n.addOutputModule(LinearLayer(1, 'out')) 34 | n.addConnection(SubsamplingConnection(n['in'], n['out'], inSliceTo=4)) 35 | n.addConnection(SubsamplingConnection(n['in'], n['out'], inSliceFrom=4)) 36 | n.sortModules() 37 | return n 38 | 39 | 40 | if __name__ == "__main__": 41 | runModuleTestSuite(__import__('__main__')) 42 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/modules/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/structure/modules/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/modules/test_samplelayer.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | >>> from pybrain.structure.modules.samplelayer import BernoulliLayer 4 | >>> from scipy import random, array, empty 5 | 6 | Set the random seed so we can predict the random variables. 7 | 8 | >>> random.seed(0) 9 | 10 | Create a layer. 11 | 12 | >>> layer = BernoulliLayer(3) 13 | >>> input = array((0.8, 0.5, 0.2)) 14 | >>> output = empty((3,)) 15 | 16 | Now test some forwards: 17 | 18 | >>> layer._forwardImplementation(input, output) 19 | >>> output 20 | array([ 0., 1., 1.]) 21 | 22 | >>> layer._forwardImplementation(input, output) 23 | >>> output 24 | array([ 0., 0., 1.]) 25 | 26 | >>> layer._forwardImplementation(input, output) 27 | >>> output 28 | array([ 0., 1., 1.]) 29 | 30 | """ 31 | 32 | __author__ = 'Justin Bayer, bayerj@in.tum.de' 33 | 34 | from pybrain.tests import runModuleTestSuite 35 | 36 | if __name__ == "__main__": 37 | runModuleTestSuite(__import__('__main__')) 38 | 39 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/modules/test_simple_mdlstm.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Build a simple mdlstm network with peepholes: 4 | 5 | >>> n = buildSimpleMDLSTMNetwork(True) 6 | >>> print(n) 7 | simpleMDLstmNet 8 | Modules: 9 | [, , , ] 10 | Connections: 11 | [ 'MDlstm'>, 'MDlstm'>, 'o'>] 12 | Recurrent Connections: 13 | [ 'MDlstm'>, 'MDlstm'>] 14 | 15 | Check its gradient: 16 | 17 | >>> from pybrain.tests import gradientCheck 18 | >>> gradientCheck(n) 19 | Perfect gradient 20 | True 21 | 22 | Try writing it to an xml file, reread it and determine if it looks the same: 23 | 24 | >>> from pybrain.tests import xmlInvariance 25 | >>> xmlInvariance(n) 26 | Same representation 27 | Same function 28 | Same class 29 | 30 | """ 31 | 32 | __author__ = 'Tom Schaul, tom@idsia.ch' 33 | 34 | from pybrain.structure.networks.recurrent import RecurrentNetwork 35 | from pybrain import LinearLayer, FullConnection, MDLSTMLayer, BiasUnit, IdentityConnection 36 | from pybrain.tests import runModuleTestSuite 37 | 38 | 39 | def buildSimpleMDLSTMNetwork(peepholes = False): 40 | N = RecurrentNetwork('simpleMDLstmNet') 41 | i = LinearLayer(1, name = 'i') 42 | dim = 1 43 | h = MDLSTMLayer(dim, peepholes = peepholes, name = 'MDlstm') 44 | o = LinearLayer(1, name = 'o') 45 | b = BiasUnit('bias') 46 | N.addModule(b) 47 | N.addOutputModule(o) 48 | N.addInputModule(i) 49 | N.addModule(h) 50 | N.addConnection(FullConnection(i, h, outSliceTo = 4*dim, name = 'f1')) 51 | N.addConnection(FullConnection(b, h, outSliceTo = 4*dim, name = 'f2')) 52 | N.addRecurrentConnection(FullConnection(h, h, inSliceTo = dim, outSliceTo = 4*dim, name = 'r1')) 53 | N.addRecurrentConnection(IdentityConnection(h, h, inSliceFrom = dim, outSliceFrom = 4*dim, name = 'rstate')) 54 | N.addConnection(FullConnection(h, o, inSliceTo = dim, name = 'f3')) 55 | N.sortModules() 56 | return N 57 | 58 | 59 | if __name__ == "__main__": 60 | runModuleTestSuite(__import__('__main__')) 61 | 62 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/structure/networks/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/_test_mdrnn.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | >>> m = _MultiDirectionalMdrnn(2, (4, 4), 1, 1, (2, 2)) 4 | >>> m._permsForSwiping()[0] 5 | array([0, 1, 2, 3]) 6 | >>> m._permsForSwiping()[1] 7 | array([1, 0, 3, 2]) 8 | >>> m._permsForSwiping()[2] 9 | array([2, 3, 0, 1]) 10 | >>> m._permsForSwiping()[3] 11 | array([3, 2, 1, 0]) 12 | 13 | >>> m = _Mdrnn(2, (4, 4), 1, 1, (2, 2)) 14 | >>> m._permsForSwiping() 15 | [array([0, 1, 2, 3])] 16 | """ 17 | 18 | 19 | from pybrain.structure.networks.mdrnn import _MultiDirectionalMdrnn, _Mdrnn #@UnusedImport 20 | from pybrain.tests import runModuleTestSuite 21 | 22 | 23 | if __name__ == "__main__": 24 | runModuleTestSuite(__import__('__main__')) 25 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/_test_rbm.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | >>> import scipy 4 | 5 | >>> from pybrain.structure.networks.rbm import Rbm 6 | >>> rbm = Rbm.fromDims(3, 2, 7 | ... weights=scipy.array((0, 1, 2, 3, 4, 5))) 8 | ... 9 | >>> scipy.size(rbm.params) 10 | 8 11 | >>> rbmi = Rbm.invert() 12 | >>> rbmi.connections[rbmi['visible']][0].params 13 | array([ 0., 3., 1., 4., 2., 5.]) 14 | 15 | """ 16 | 17 | __author__ = 'Justin S Bayer, bayer.justin@googlemail.com' 18 | __version__ = '$Id$' 19 | 20 | 21 | from pybrain.tests import runModuleTestSuite 22 | 23 | 24 | if __name__ == "__main__": 25 | runModuleTestSuite(__import__('__main__')) 26 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/custom/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/structure/networks/custom/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/custom/test_capturegame_network.py: -------------------------------------------------------------------------------- 1 | """ 2 | Build a CaptureGameNetwork with LSTM cells 3 | 4 | >>> from pybrain.structure.networks.custom import CaptureGameNetwork 5 | >>> from pybrain import MDLSTMLayer 6 | >>> size = 2 7 | >>> n = CaptureGameNetwork(size = size, componentclass = MDLSTMLayer, hsize = 1, peepholes = False) 8 | 9 | Check it's string representation 10 | >>> print(n) 11 | CaptureGameNetwork-s2-h1-MDLSTMLayer--... 12 | Modules: 13 | [, , , ... , ] 14 | Connections: 15 | [>> c1 = n.connections[n['hidden(1, 0, 3)']][0] 20 | >>> c2 = n.connections[n['hidden(0, 1, 2)']][-1] 21 | >>> print(c1.indim, c1.outdim) 22 | 1 1 23 | >>> print(c2.indim, c2.outdim) 24 | 1 1 25 | >>> n.paramdim 26 | 21 27 | 28 | Try writing it to an xml file, reread it and determine if it looks the same: 29 | 30 | >>> from pybrain.tests import xmlInvariance 31 | >>> xmlInvariance(n) 32 | Same representation 33 | Same function 34 | Same class 35 | 36 | Check its gradient: 37 | 38 | >>> from pybrain.tests import gradientCheck 39 | >>> gradientCheck(n) 40 | Perfect gradient 41 | True 42 | 43 | """ 44 | 45 | __author__ = 'Tom Schaul, tom@idsia.ch' 46 | 47 | from pybrain.tests import runModuleTestSuite 48 | 49 | if __name__ == '__main__': 50 | runModuleTestSuite(__import__('__main__')) 51 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/custom/test_convolutional_nets.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Let's build a convolutional network designed for board games: 4 | 5 | >>> from pybrain.structure.networks.custom.convboard import ConvolutionalBoardNetwork 6 | >>> from scipy import array, ravel, var 7 | >>> N = ConvolutionalBoardNetwork(4, 3, 5) 8 | >>> print(N.paramdim) 9 | 97 10 | 11 | This is what a typical input would look like (on a 4x4 board) 12 | 13 | >>> input = [[[0,0],[0,0],[0,0],[0,0]],\ 14 | [[0,0],[0,0],[0,0],[0,0]],\ 15 | [[0,0],[1,1],[0,0],[0,1]],\ 16 | [[0,0],[1,0],[1,1],[0,1]],\ 17 | ] 18 | 19 | We let the network process the input: 20 | 21 | >>> res = N.activate(ravel(array(input))) 22 | >>> res = res.reshape(4,4) 23 | >>> inp = N['pad'].inputbuffer[0].reshape(6,6,2)[:,:,0] 24 | 25 | The input of the first features (e.g. white stone presence) is in the middle, like we set it: 26 | 27 | >>> print(inp[1:5,1:5]) 28 | [[ 0. 0. 0. 0.] 29 | [ 0. 0. 0. 0.] 30 | [ 0. 1. 0. 0.] 31 | [ 0. 1. 1. 0.]] 32 | 33 | The rest of that array has been padded with an arbitrary, but identical bias weight: 34 | 35 | >>> var(inp[0,:]) < 1e-20 36 | True 37 | 38 | >>> inp[0,0] != 0.0 39 | True 40 | 41 | On the output, all the values should be distinct, except for two in the middle above 42 | because a cluster-size of 3x3 makes their input look identical. 43 | 44 | >>> res[0,1] - res[0,2] 45 | 0.0 46 | 47 | >>> res[0,1] == res[0,3] 48 | False 49 | 50 | >>> res[1,1] == res[0,0] 51 | False 52 | 53 | >>> res[0,2] == res[3,2] 54 | False 55 | 56 | Now let's use the network, and play a game with it: 57 | 58 | >>> from pybrain.rl.environments.twoplayergames import CaptureGameTask 59 | >>> t = CaptureGameTask(4) 60 | >>> tmp = t(N) 61 | 62 | """ 63 | 64 | __author__ = 'Tom Schaul, tom@idsia.ch' 65 | 66 | from pybrain.tests import runModuleTestSuite 67 | 68 | if __name__ == '__main__': 69 | runModuleTestSuite(__import__('__main__')) 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/test_bidirectional_network.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Build a bi-directional Network for sequences (each sample a single value) of length 20: 4 | 5 | >>> n = BidirectionalNetwork(seqlen=20, inputsize=1, hiddensize=5, symmetric=False) 6 | 7 | It should have 2x1x5 + 2x1x5 + 2x5x5 = 70 weights 8 | 9 | >>> n.paramdim 10 | 70 11 | 12 | Now let's build a symmetric network: 13 | 14 | >>> n = BidirectionalNetwork(seqlen=12, inputsize=2, hiddensize=3, symmetric=True) 15 | >>> n.indim 16 | 24 17 | 18 | It should have 1x2x3 + 1x1x3 + 1x3x3 = 18 weights 19 | 20 | >>> n.paramdim 21 | 18 22 | 23 | A forward pass: 24 | 25 | >>> from numpy import ones 26 | >>> r = n.activate(ones(24)) 27 | >>> len(r) 28 | 12 29 | 30 | The result should be symmetric (although the weights are random) 31 | 32 | >>> r[0]-r[-1] 33 | 0.0 34 | 35 | Check its gradient: 36 | 37 | >>> from pybrain.tests import gradientCheck 38 | >>> gradientCheck(n) 39 | Perfect gradient 40 | True 41 | 42 | 43 | """ 44 | 45 | __author__ = 'Tom Schaul, tom@idsia.ch' 46 | 47 | 48 | from pybrain.structure.networks import BidirectionalNetwork #@UnusedImport 49 | from pybrain.tests import runModuleTestSuite 50 | 51 | 52 | if __name__ == "__main__": 53 | runModuleTestSuite(__import__('__main__')) 54 | 55 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/test_cyclic_network.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Trying to build a cyclic network (should fail): 4 | 5 | >>> buildCyclicNetwork(False) 6 | Traceback (most recent call last): 7 | ... 8 | NetworkConstructionException: Loop in network graph. 9 | 10 | If one connection is recurrent, it should work: 11 | 12 | >>> buildCyclicNetwork(True) 13 | 14 | 15 | """ 16 | 17 | __author__ = 'Tom Schaul, tom@idsia.ch' 18 | 19 | from pybrain import FeedForwardNetwork, RecurrentNetwork, LinearLayer, \ 20 | FullConnection 21 | from pybrain.tests import runModuleTestSuite 22 | 23 | 24 | def buildCyclicNetwork(recurrent): 25 | """ build a cyclic network with 4 modules 26 | 27 | :key recurrent: make one of the connections recurrent """ 28 | Network = RecurrentNetwork if recurrent else FeedForwardNetwork 29 | N = Network('cyc') 30 | a = LinearLayer(1, name='a') 31 | b = LinearLayer(2, name='b') 32 | c = LinearLayer(3, name='c') 33 | d = LinearLayer(4, name='d') 34 | N.addInputModule(a) 35 | N.addModule(b) 36 | N.addModule(d) 37 | N.addOutputModule(c) 38 | N.addConnection(FullConnection(a, b)) 39 | N.addConnection(FullConnection(b, c)) 40 | N.addConnection(FullConnection(c, d)) 41 | if recurrent: 42 | N.addRecurrentConnection(FullConnection(d, a)) 43 | else: 44 | N.addConnection(FullConnection(d, a)) 45 | N.sortModules() 46 | return N 47 | 48 | 49 | if __name__ == "__main__": 50 | runModuleTestSuite(__import__('__main__')) 51 | 52 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/test_nested_ffn_and_rnn.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Build a mixed nested network: 4 | 5 | >>> n = buildMixedNestedNetwork() 6 | >>> inner = n['inner'] 7 | 8 | Some specific tests: 9 | The feed-forward part should have its buffers increased in size, and 10 | keep the correct offset. 11 | 12 | >>> len(inner.outputbuffer) 13 | 1 14 | 15 | >>> o1 = n.activate([1]) 16 | >>> o2 = n.activate([2]) 17 | >>> o2 = n.activate([3]) 18 | >>> (o1 == o2).any() 19 | False 20 | 21 | >>> n.offset 22 | 3 23 | >>> inner.offset 24 | 3 25 | >>> len(inner.outputbuffer) 26 | 4 27 | 28 | Verify everything is still fine after reset 29 | >>> n.reset() 30 | 31 | >>> n.offset 32 | 0 33 | >>> inner.offset 34 | 0 35 | 36 | 37 | 38 | Check its gradient: 39 | 40 | >>> from pybrain.tests import gradientCheck 41 | >>> gradientCheck(n) 42 | Perfect gradient 43 | True 44 | 45 | Try writing it to an xml file, reread it and determine if it looks the same: 46 | 47 | >>> from pybrain.tests import xmlInvariance 48 | >>> xmlInvariance(n) 49 | Same representation 50 | Same function 51 | Same class 52 | 53 | 54 | """ 55 | 56 | __author__ = 'Tom Schaul, tom@idsia.ch' 57 | 58 | from pybrain.structure import RecurrentNetwork 59 | from pybrain import LinearLayer, FullConnection 60 | from pybrain.tools.shortcuts import buildNetwork 61 | from pybrain.tests import runModuleTestSuite 62 | 63 | 64 | def buildMixedNestedNetwork(): 65 | """ build a nested network with the inner one being a ffn and the outer one being recurrent. """ 66 | N = RecurrentNetwork('outer') 67 | a = LinearLayer(1, name='a') 68 | b = LinearLayer(2, name='b') 69 | c = buildNetwork(2, 3, 1) 70 | c.name = 'inner' 71 | N.addInputModule(a) 72 | N.addModule(c) 73 | N.addOutputModule(b) 74 | N.addConnection(FullConnection(a, b)) 75 | N.addConnection(FullConnection(b, c)) 76 | N.addRecurrentConnection(FullConnection(c, c)) 77 | N.sortModules() 78 | return N 79 | 80 | 81 | if __name__ == "__main__": 82 | runModuleTestSuite(__import__('__main__')) 83 | 84 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/test_nested_network.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Build a nested network: 4 | 5 | >>> n = buildNestedNetwork() 6 | 7 | Check its gradient: 8 | 9 | >>> from pybrain.tests import gradientCheck 10 | >>> gradientCheck(n) 11 | Perfect gradient 12 | True 13 | 14 | Try writing it to an xml file, reread it and determine if it looks the same: 15 | 16 | >>> from pybrain.tests import xmlInvariance 17 | >>> xmlInvariance(n) 18 | Same representation 19 | Same function 20 | Same class 21 | 22 | """ 23 | 24 | __author__ = 'Tom Schaul, tom@idsia.ch' 25 | 26 | from pybrain.structure import FeedForwardNetwork 27 | from pybrain import LinearLayer, FullConnection 28 | from pybrain.tools.shortcuts import buildNetwork 29 | from pybrain.tests import runModuleTestSuite 30 | 31 | 32 | def buildNestedNetwork(): 33 | """ build a nested network. """ 34 | N = FeedForwardNetwork('outer') 35 | a = LinearLayer(1, name='a') 36 | b = LinearLayer(2, name='b') 37 | c = buildNetwork(2, 3, 1) 38 | c.name = 'inner' 39 | N.addInputModule(a) 40 | N.addModule(c) 41 | N.addOutputModule(b) 42 | N.addConnection(FullConnection(a, b)) 43 | N.addConnection(FullConnection(b, c)) 44 | N.sortModules() 45 | return N 46 | 47 | 48 | if __name__ == "__main__": 49 | runModuleTestSuite(__import__('__main__')) 50 | 51 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/test_network_decomposition.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Build a decomposable network 4 | 5 | >>> n = buildDecomposableNetwork() 6 | 7 | Check if it was built correctly 8 | >>> print(n.paramdim) 9 | 12 10 | >>> tmp = n.getDecomposition() 11 | >>> tmp[2] 12 | array([ 1., 1., 1., 1.]) 13 | 14 | Let's keep the output value for later 15 | >>> act = n.activate([-1.2,0.5]) 16 | 17 | 18 | Now, change the values for the first neuron 19 | >>> tmp[0] *= 0 20 | 21 | The network has not changed yet 22 | >>> n.getDecomposition()[0] 23 | array([ 1., 1., 1., 1.]) 24 | 25 | Now it has: 26 | >>> n.setDecomposition(tmp) 27 | >>> n.getDecomposition()[0] 28 | array([ 0., 0., 0., 0.]) 29 | 30 | The new output value should be 2/3 of the original one, with one neuron disabled. 31 | 32 | >>> act2 = n.activate([-1.2,0.5]) 33 | >>> (act2 * 3 / 2 - act)[0] 34 | 0.0 35 | 36 | Check its gradient: 37 | 38 | >>> from pybrain.tests import gradientCheck 39 | >>> gradientCheck(n) 40 | Perfect gradient 41 | True 42 | 43 | Try writing it to an xml file, reread it and determine if it looks the same: 44 | 45 | >>> from pybrain.tests import xmlInvariance 46 | >>> xmlInvariance(n) 47 | Same representation 48 | Same function 49 | Same class 50 | 51 | """ 52 | 53 | __author__ = 'Tom Schaul, tom@idsia.ch' 54 | 55 | from scipy import ones 56 | 57 | from pybrain.structure.networks import NeuronDecomposableNetwork 58 | from pybrain.tools.shortcuts import buildNetwork 59 | from pybrain.tests import runModuleTestSuite 60 | 61 | 62 | def buildDecomposableNetwork(): 63 | """ three hidden neurons, with 2 in- and 2 outconnections each. """ 64 | n = buildNetwork(2, 3, 2, bias = False) 65 | ndc = NeuronDecomposableNetwork.convertNormalNetwork(n) 66 | # set all the weights to 1 67 | ndc._setParameters(ones(12)) 68 | return ndc 69 | 70 | if __name__ == "__main__": 71 | runModuleTestSuite(__import__('__main__')) 72 | 73 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/test_network_forward_backward.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Test the forward and backward passes through a linear network. 4 | 5 | >>> from scipy import array 6 | >>> from pybrain import LinearLayer 7 | >>> from pybrain.tools.shortcuts import buildNetwork 8 | >>> n = buildNetwork(2, 4, 3, bias = False, hiddenclass = LinearLayer, recurrent=True) 9 | 10 | 11 | The forward passes (2 timesteps), by two different but equivalent methods 12 | >>> input = array([1,2]) 13 | >>> n.inputbuffer[0] = input 14 | >>> n.forward() 15 | >>> tmp = n.activate(input * 2) 16 | 17 | The backward passes, also by two different but equivalent methods 18 | >>> outerr = array([-0.1, 0, 1]) 19 | >>> n.outputerror[1] = outerr * 3 20 | >>> n.backward() 21 | >>> tmp = n.backActivate(outerr) 22 | 23 | Verify that the inputs and outputs are proportional 24 | >>> sum(n.outputbuffer[1]/n.outputbuffer[0]) 25 | 6.0 26 | >>> abs((n.inputerror[1]/n.inputerror[0])[1] - 3.0) < 0.0001 27 | True 28 | 29 | """ 30 | 31 | __author__ = 'Tom Schaul, tom@idsia.ch' 32 | 33 | from pybrain.tests import runModuleTestSuite 34 | 35 | if __name__ == "__main__": 36 | runModuleTestSuite(__import__('__main__')) 37 | 38 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/test_network_sort.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Determine if the modules in a Network are always sorted in the same way, even if the connections 4 | don't constrain a particular order. 5 | 6 | Build a number of modules and connections, to be used for all constructions 7 | 8 | >>> from pybrain.structure.networks.network import Network 9 | >>> mods = buildSomeModules(10) 10 | >>> conns = buildSomeConnections(mods) 11 | 12 | Construct a network, normally, and sort it 13 | >>> n = Network() 14 | >>> for m in mods: 15 | ... n.addModule(m) 16 | ... 17 | >>> for c in conns: 18 | ... n.addConnection(c) 19 | ... 20 | >>> n.sortModules() 21 | >>> ord = str(n.modulesSorted) 22 | 23 | Is the order the same, if we sort it again? 24 | 25 | >>> n.sortModules() 26 | >>> ord2 = str(n.modulesSorted) 27 | >>> ord == ord2 28 | True 29 | 30 | What if we construct it in a different order? 31 | 32 | >>> n = Network() 33 | >>> for m in reversed(mods): 34 | ... n.addModule(m) 35 | ... 36 | >>> for c in reversed(conns): 37 | ... n.addConnection(c) 38 | ... 39 | >>> n.sortModules() 40 | >>> ord3 = str(n.modulesSorted) 41 | >>> ord == ord3 42 | True 43 | 44 | Is it the same ordering than our reference? 45 | 46 | >>> print(ord3) 47 | [, , , , , , , , , ] 48 | 49 | """ 50 | 51 | __author__ = 'Tom Schaul, tom@idsia.ch' 52 | 53 | from pybrain import LinearLayer, FullConnection 54 | from pybrain.tests import runModuleTestSuite 55 | 56 | 57 | def buildSomeModules(number = 4): 58 | res = [] 59 | for i in range(number): 60 | res.append(LinearLayer(1, 'l'+str(i))) 61 | return res 62 | 63 | def buildSomeConnections(modules): 64 | """ add a connection from every second to every third module """ 65 | res = [] 66 | for i in range(len(modules)/3-1): 67 | res.append(FullConnection(modules[i*2], modules[i*3+1])) 68 | return res 69 | 70 | 71 | if __name__ == "__main__": 72 | runModuleTestSuite(__import__('__main__')) 73 | 74 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/test_no_gravity_network.py: -------------------------------------------------------------------------------- 1 | """ 2 | The library should be able to handle networks without any weight: 3 | 4 | >>> n1= buildNonGravityNet(False) 5 | >>> n1.paramdim 6 | 0 7 | 8 | >>> n1.activate([0.2,0.4])[0] 9 | 1.289... 10 | >>> n1.activate([0.2,0.4])[0] 11 | 1.289... 12 | 13 | Now let's verify the recurrent one as well: 14 | 15 | >>> n2= buildNonGravityNet(True) 16 | >>> n2.paramdim 17 | 0 18 | 19 | >>> n2.activate([0.2,0.4])[0] 20 | 1.289... 21 | >>> n2.activate([0.2,0.4])[0] 22 | 3.478... 23 | 24 | """ 25 | 26 | from pybrain.structure import RecurrentNetwork, FeedForwardNetwork, IdentityConnection, LinearLayer, SigmoidLayer 27 | from pybrain.tests.testsuites import runModuleTestSuite 28 | 29 | def buildNonGravityNet(recurrent = False): 30 | if recurrent: 31 | net = RecurrentNetwork() 32 | else: 33 | net = FeedForwardNetwork() 34 | l1 = LinearLayer(2) 35 | l2 = LinearLayer(3) 36 | s1 = SigmoidLayer(2) 37 | l3 = LinearLayer(1) 38 | net.addInputModule(l1) 39 | net.addModule(l2) 40 | net.addModule(s1) 41 | net.addOutputModule(l3) 42 | net.addConnection(IdentityConnection(l1, l2, outSliceFrom = 1)) 43 | net.addConnection(IdentityConnection(l1, l2, outSliceTo = 2)) 44 | net.addConnection(IdentityConnection(l2, l3, inSliceFrom = 2)) 45 | net.addConnection(IdentityConnection(l2, l3, inSliceTo = 1)) 46 | net.addConnection(IdentityConnection(l1, s1)) 47 | net.addConnection(IdentityConnection(l2, s1, inSliceFrom = 1)) 48 | net.addConnection(IdentityConnection(s1, l3, inSliceFrom = 1)) 49 | if recurrent: 50 | net.addRecurrentConnection(IdentityConnection(s1, l1)) 51 | net.addRecurrentConnection(IdentityConnection(l2, l2, inSliceFrom = 1, outSliceTo = 2)) 52 | net.sortModules() 53 | return net 54 | 55 | if __name__ == "__main__": 56 | runModuleTestSuite(__import__('__main__')) 57 | 58 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/test_recurrent_network.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Build a simple recurrent network: 4 | 5 | >>> n = buildRecurrentNetwork() 6 | >>> print(n) 7 | RecurrentNetwork 8 | Modules: 9 | [, , ] 10 | ... 11 | Recurrent Connections: 12 | [ 'hidden0'>] 13 | 14 | Check its gradient: 15 | 16 | >>> from pybrain.tests import gradientCheck 17 | >>> gradientCheck(n) 18 | Perfect gradient 19 | True 20 | 21 | Try writing it to an xml file, reread it and determine if it looks the same: 22 | 23 | >>> from pybrain.tests import xmlInvariance 24 | >>> xmlInvariance(n) 25 | Same representation 26 | Same function 27 | Same class 28 | 29 | Set all the weights to one, and the recurrent one to 0.5, and then do some checks. 30 | >>> n.params[:] = [1,1,0.5] 31 | >>> n.reset() 32 | >>> n.activate(4)[0] 33 | 4.0 34 | >>> n.activate(-1)[0] 35 | 1.0 36 | >>> n.activate(0)[0] 37 | 0.5 38 | >>> n.reset() 39 | >>> n.activate(0)[0] 40 | 0.0 41 | 42 | """ 43 | __author__ = 'Tom Schaul, tom@idsia.ch' 44 | 45 | from scipy import ones #@UnusedImport 46 | from pybrain import FullConnection 47 | from pybrain.tools.shortcuts import buildNetwork 48 | from pybrain.structure import LinearLayer 49 | from pybrain.tests import runModuleTestSuite 50 | 51 | 52 | def buildRecurrentNetwork(): 53 | N = buildNetwork(1, 1, 1, recurrent=True, bias=False, hiddenclass=LinearLayer, outputbias=False) 54 | h = N['hidden0'] 55 | N.addRecurrentConnection(FullConnection(h, h)) 56 | N.sortModules() 57 | N.name = 'RecurrentNetwork' 58 | return N 59 | 60 | if __name__ == "__main__": 61 | runModuleTestSuite(__import__('__main__')) 62 | 63 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/structure/networks/test_swiping_network.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Build a 2-dimensional BorderSwipingNetwork: 4 | 5 | >>> n = buildSwipingNetwork(2) 6 | 7 | Check its gradient: 8 | 9 | >>> from pybrain.tests import gradientCheck 10 | >>> gradientCheck(n) 11 | Perfect gradient 12 | True 13 | 14 | Try writing it to an xml file, reread it and determine if it looks the same: 15 | 16 | >>> from pybrain.tests import xmlInvariance 17 | >>> xmlInvariance(n) 18 | Same representation 19 | Same function 20 | Same class 21 | 22 | """ 23 | 24 | __author__ = 'Tom Schaul, tom@idsia.ch' 25 | 26 | 27 | from pybrain import ModuleMesh, LinearLayer 28 | from pybrain.structure.networks import BorderSwipingNetwork 29 | from pybrain.tests import runModuleTestSuite 30 | 31 | 32 | def buildSwipingNetwork(dimensions = 3): 33 | d = tuple([2] * dimensions) 34 | inmesh = ModuleMesh.constructWithLayers(LinearLayer, 1, d, 'in') 35 | hmesh = ModuleMesh.constructWithLayers(LinearLayer, 1, tuple(list(d)+[2**len(d)]), 'h') 36 | outmesh = ModuleMesh.constructWithLayers(LinearLayer, 1, d, 'out') 37 | return BorderSwipingNetwork(inmesh, hmesh, outmesh) 38 | 39 | 40 | 41 | if __name__ == "__main__": 42 | runModuleTestSuite(__import__('__main__')) 43 | 44 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/supervised/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/supervised/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/supervised/knn/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/supervised/knn/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/supervised/knn/lsh/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/supervised/knn/lsh/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/supervised/knn/lsh/test_minhash.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | Internal Tests: 5 | 6 | >>> from scipy import array 7 | >>> from pybrain.supervised.knn.lsh.minhash import arrayPermutation 8 | >>> permutation = array([4, 3, 2, 1, 0]) 9 | >>> permute = arrayPermutation(permutation) 10 | >>> permute(array([5, 2, 3, 1, 4])) 11 | array([4, 1, 3, 2, 5]) 12 | 13 | >>> from pybrain.supervised.knn.lsh.minhash import jacardCoefficient 14 | >>> a = array([0, 0, 0, 1]) 15 | >>> b = array([1, 1, 1, 1]) 16 | >>> c = array([1, 1, 0, 1]) 17 | >>> d = array([0, 0, 1, 1]) 18 | >>> jacardCoefficient(a, b) 19 | 0.25 20 | >>> jacardCoefficient(b, c) 21 | 0.75 22 | >>> jacardCoefficient(a, a) 23 | 1.0 24 | >>> jacardCoefficient(a, d) 25 | 0.75 26 | 27 | 28 | Example Usage: 29 | 30 | >>> from pybrain.supervised.knn.lsh.minhash import MinHash 31 | 32 | We need to specify the length of the inputs and how many permutations should be 33 | used: 34 | 35 | >>> m = MinHash(5, 1) 36 | 37 | The permutation is initialized randomly 38 | 39 | >>> m.permutations 40 | array([...]) 41 | 42 | But for the tests, we will justify it to our means: 43 | 44 | >>> m.permutations = array([[0, 1, 2, 4, 3], [0, 1, 2, 3, 4]]) 45 | 46 | So let's put in some values that will hash to the same bucket 47 | 48 | >>> m.put(array([1, 1, 1, 1, 1]), 'red') 49 | 50 | Some "introspection" to check if everything went right 51 | 52 | >>> m.buckets 53 | defaultdict( at ...>, {(0, 0): [(array([...True], dtype=bool), 'red')]}) 54 | >>> m._hash(array([1, 1, 0, 0, 0])) 55 | (0, 0) 56 | 57 | Put another one in 58 | 59 | >>> m.put(array([1, 1, 1, 1, 0]), 'red') 60 | 61 | An check if this one is favored above the other 62 | 63 | >>> m.knn(array([1, 1, 0, 0, 0]), 1) 64 | [(array([... True, False], dtype=bool), 'red')] 65 | >>> m.knn(array([1, 1, 0, 0, 0]), 1) 66 | [(array([... True, False], dtype=bool), 'red')] 67 | 68 | 69 | 70 | Let's make a hash that returns nothing 71 | 72 | >>> m.knn(array([0, 0, 0, 0, 0]), 1) 73 | [] 74 | 75 | """ 76 | 77 | from pybrain.tests import runModuleTestSuite 78 | 79 | if __name__ == "__main__": 80 | runModuleTestSuite(__import__('__main__')) 81 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/supervised/trainers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/supervised/trainers/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/supervised/trainers/test_backprop.py: -------------------------------------------------------------------------------- 1 | """ 2 | >>> from pybrain.datasets.supervised import SupervisedDataSet 3 | >>> from pybrain.supervised.trainers import BackpropTrainer 4 | >>> from pybrain import FeedForwardNetwork 5 | >>> from pybrain.structure import LinearLayer, SigmoidLayer, FullConnection 6 | >>> from random import randrange 7 | >>> dataset = SupervisedDataSet(6, 2) 8 | >>> for i in range(1000): 9 | ... state = [randrange(0, 15), 10 | ... randrange(-70, 50), 11 | ... randrange(-70, 50), 12 | ... randrange(-70, 50), 13 | ... randrange(-70, 50), 14 | ... float(randrange(1, 5))/20.] 15 | ... action = [float(randrange(-1, 1))/10.0, 16 | ... randrange(0, 1)] 17 | ... dataset.addSample(state, action) 18 | >>> 19 | >>> net = FeedForwardNetwork() 20 | >>> 21 | >>> net.addInputModule(LinearLayer(6, name='in')) 22 | >>> net.addModule(SigmoidLayer(40, name='hidden_0')) 23 | >>> net.addModule(SigmoidLayer(16, name='hidden_1')) 24 | >>> net.addOutputModule(LinearLayer(2, name='out')) 25 | >>> 26 | >>> net.addConnection(FullConnection(net['in'], net['hidden_0'])) 27 | >>> net.addConnection(FullConnection(net['hidden_0'], net['hidden_1'])) 28 | >>> net.addConnection(FullConnection(net['hidden_1'], net['out'])) 29 | >>> 30 | >>> net.sortModules() 31 | >>> 32 | >>> trainer = BackpropTrainer(net, 33 | ... dataset=dataset, 34 | ... learningrate=0.01, 35 | ... lrdecay=1, 36 | ... momentum=0.5, 37 | ... verbose=False, 38 | ... weightdecay=0, 39 | ... batchlearning=False) 40 | >>> 41 | >>> trainingErrors, validationErrors = trainer.trainUntilConvergence( 42 | ... dataset=dataset, 43 | ... maxEpochs=10) 44 | """ 45 | 46 | 47 | 48 | __author__ = 'Steffen Kampmann, steffen.kampmann@gmail.com' 49 | 50 | from pybrain.tests import runModuleTestSuite 51 | 52 | if __name__ == "__main__": 53 | runModuleTestSuite(__import__('__main__')) 54 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/supervised/trainers/test_evolino.py: -------------------------------------------------------------------------------- 1 | """ 2 | >>> import numpy 3 | >>> from pybrain.datasets.sequential import SequentialDataSet 4 | >>> from pybrain.structure.modules.evolinonetwork import EvolinoNetwork 5 | >>> from pybrain.supervised.trainers.evolino import EvolinoTrainer 6 | 7 | >>> dataset = SequentialDataSet(0,1) 8 | >>> dataset.newSequence() 9 | >>> for x in numpy.arange( 0 , 30 , 0.2 ): 10 | ... dataset.addSample([], [x]) 11 | ... 12 | 13 | 14 | Tests for Construction of Module and Trainer 15 | -------------------------------------------- 16 | >>> net = EvolinoNetwork( dataset.outdim, 7 ) 17 | >>> trainer = EvolinoTrainer( 18 | ... net, 19 | ... dataset=dataset, 20 | ... subPopulationSize = 5, 21 | ... nParents = 2, 22 | ... nCombinations = 1, 23 | ... initialWeightRange = ( -0.01 , 0.01 ), 24 | ... backprojectionFactor = 0.001, 25 | ... mutationAlpha = 0.001, 26 | ... nBurstMutationEpochs = numpy.Infinity, 27 | ... wtRatio = 1./3., 28 | ... verbosity = 0) 29 | 30 | 31 | Tests for Training and Applying 32 | ------------------------------- 33 | >>> trainer.trainEpochs( 1 ) 34 | >>> trainer.evaluation.max_fitness > -100 35 | True 36 | 37 | >>> sequence = dataset.getField('target') 38 | >>> net.extrapolate(sequence, len(sequence))[-1][0]>55 39 | True 40 | """ 41 | 42 | 43 | 44 | __author__ = 'Michael Isik, isikmichael@gmx.net' 45 | 46 | from pybrain.tests import runModuleTestSuite 47 | 48 | if __name__ == "__main__": 49 | runModuleTestSuite(__import__('__main__')) 50 | 51 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/supervised/trainers/test_rprop.py: -------------------------------------------------------------------------------- 1 | """ 2 | >>> from pybrain.tools.shortcuts import buildNetwork 3 | >>> from pybrain.supervised.trainers import BackpropTrainer, RPropMinusTrainer 4 | >>> from pybrain.datasets import SupervisedDataSet, ImportanceDataSet 5 | >>> from scipy import random, array 6 | 7 | Initialize random number generator 8 | 9 | >>> random.seed(42) 10 | 11 | Create an XOR-dataset and a recurrent network 12 | 13 | >>> ds = ImportanceDataSet(2,2) 14 | >>> ds.addSample([0,0],[0, 1], [1,0]) 15 | >>> ds.addSample([0,1],[1, 10], [1,0]) 16 | >>> ds.addSample([1,0],[1, -1], [1,0]) 17 | >>> ds.addSample([1,1],[0, 0], [1,0]) 18 | >>> n = buildNetwork(ds.indim, 4, ds.outdim, recurrent=True) 19 | 20 | Create and test backprop trainer 21 | 22 | >>> t = BackpropTrainer(n, learningrate = 0.01, momentum = 0.99, verbose = True) 23 | >>> t.trainOnDataset(ds, 4) 24 | Total error: 2.44696473875 25 | Total error: 1.97570498879 26 | Total error: 1.23940309483 27 | Total error: 0.546129967878 28 | >>> abs(n.params[10:15] - array([ -0.53868206, -0.54185834, 0.26726394, -1.90008234, -1.12114946])).round(5) 29 | array([ 0., 0., 0., 0., 0.]) 30 | 31 | Now the same for RPROP 32 | 33 | >>> t = RPropMinusTrainer(n, verbose = True) 34 | >>> t.trainOnDataset(ds, 4) 35 | epoch 0 total error 0.16818 avg weight 0.92638 36 | epoch 1 total error 0.15007 avg weight 0.92202 37 | epoch 2 total error 0.15572 avg weight 0.92684 38 | epoch 3 total error 0.13036 avg weight 0.92604 39 | >>> abs(n.params[5:10] - array([ -0.19241111, 1.43404022, 0.23062397, -0.40105413, 0.62100109])).round(5) 40 | array([ 0., 0., 0., 0., 0.]) 41 | 42 | """ 43 | 44 | __author__ = 'Martin Felder, felder@in.tum.de' 45 | 46 | 47 | from pybrain.tests import runModuleTestSuite 48 | 49 | if __name__ == "__main__": 50 | runModuleTestSuite(__import__('__main__')) 51 | 52 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/test_utilities.py: -------------------------------------------------------------------------------- 1 | """ 2 | >>> from pybrain.utilities import memoize 3 | >>> call_count = 0 4 | >>> @memoize 5 | ... def longComp(): 6 | ... global call_count 7 | ... call_count += 1 8 | ... return 'result' 9 | >>> longComp() 10 | 'result' 11 | >>> call_count 12 | 1 13 | >>> longComp() 14 | 'result' 15 | >>> call_count 16 | 1 17 | 18 | 19 | Tests for Serializable 20 | ====================== 21 | 22 | >>> from cStringIO import StringIO 23 | >>> s = StringIO() 24 | >>> p = P() 25 | >>> p.x = 2 26 | >>> p.saveToFileLike(s) 27 | >>> s.seek(0) 28 | >>> q = P.loadFromFileLike(s) 29 | >>> q.x 30 | 2 31 | 32 | 33 | 34 | Tests for permute 35 | ================= 36 | 37 | >>> from pybrain.utilities import permute 38 | >>> permute(array((0, 1, 2)), [2, 1, 0]) 39 | array([2, 1, 0]) 40 | >>> permute(array(((0, 0, 0), (1, 1, 1), (2, 2, 2))), (2, 0, 1)) 41 | array([[2, 2, 2], 42 | [0, 0, 0], 43 | [1, 1, 1]]) 44 | 45 | 46 | Tests for permuteToBlocks 47 | ========================= 48 | 49 | >>> from pybrain.utilities import permuteToBlocks 50 | >>> arr = array([[0, 1, 2, 3], [4, 5 ,6 ,7], [8, 9, 10, 11], [12, 13,14, 15]]) 51 | >>> permuteToBlocks(arr, (2, 2)) 52 | array([ 0., 1., 4., 5., 2., 3., 6., 7., 8., 9., 12., 53 | 13., 10., 11., 14., 15.]) 54 | >>> arr = array(range(32)).reshape(2, 4, 4) 55 | >>> permuteToBlocks(arr, (2, 2, 2)).astype('int8').tolist() 56 | [0, 1, 4, 5, 16, 17, 20, 21, 2, 3, 6, 7, 18, 19, 22, 23, 8, 9, 12, 13, 24, 25, 28, 29, 10, 11, 14, 15, 26, 27, 30, 31] 57 | 58 | 59 | """ 60 | 61 | 62 | from scipy import array #@UnusedImport 63 | from pybrain.utilities import Serializable 64 | from pybrain.tests import runModuleTestSuite 65 | 66 | 67 | class P(Serializable): 68 | 69 | def __getstate__(self): 70 | return {'x': self.x} 71 | 72 | def __setstate__(self, dct): 73 | self.x = dct['x'] 74 | 75 | 76 | if __name__ == "__main__": 77 | runModuleTestSuite(__import__('__main__')) 78 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/test_utilities_dictionaries.py: -------------------------------------------------------------------------------- 1 | """ 2 | >>> from pybrain.utilities import dictCombinations, subDict, matchingDict 3 | >>> d1 = {'ones':[1,1,1,'one'], 2:[2,4,6,8], 4:4, 8:['eight']} 4 | >>> d2 = {1:1, 2:2, 4:4, 8:8} 5 | 6 | subDict produces a sub-dictionary, by removing some keys. 7 | 8 | >>> d3 = subDict(d1, ['ones', 2, 4]) 9 | >>> print(sorted(d3.items())) 10 | [(2, [2, 4, 6, 8]), (4, 4), ('ones', [1, 1, 1, 'one'])] 11 | 12 | We can also flip the selection, and limit the keys to the ones NOT in the list: 13 | >>> d4 = subDict(d1, [8], flip=True) 14 | >>> d4 == d3 15 | True 16 | 17 | 18 | matchingDict determines whether the values of a dictionary match the selection. 19 | No selection always works: 20 | 21 | >>> matchingDict(d2, {}) 22 | True 23 | 24 | Not all elements must be present: 25 | 26 | >>> matchingDict(d2, {3:3}) 27 | True 28 | 29 | But those that are in both must fit (here 8 is wrong) 30 | >>> matchingDict(d2, d1) 31 | False 32 | 33 | Without the 8 key: 34 | >>> matchingDict(d2, d3) 35 | True 36 | 37 | dictCombinations will produce all the combinations of the elements in lists 38 | with their keys, not allowing for identical items, 39 | but dealing with non-lists, and any types of keys and values. 40 | 41 | >>> for x in dictCombinations(d1): print(sorted(x.items())) 42 | [(2, 2), (4, 4), (8, 'eight'), ('ones', 1)] 43 | [(2, 4), (4, 4), (8, 'eight'), ('ones', 1)] 44 | [(2, 6), (4, 4), (8, 'eight'), ('ones', 1)] 45 | [(2, 8), (4, 4), (8, 'eight'), ('ones', 1)] 46 | [(2, 2), (4, 4), (8, 'eight'), ('ones', 'one')] 47 | [(2, 4), (4, 4), (8, 'eight'), ('ones', 'one')] 48 | [(2, 6), (4, 4), (8, 'eight'), ('ones', 'one')] 49 | [(2, 8), (4, 4), (8, 'eight'), ('ones', 'one')] 50 | 51 | 52 | """ 53 | 54 | __author__ = 'Tom Schaul, tom@idsia.ch' 55 | 56 | from pybrain.tests import runModuleTestSuite 57 | 58 | if __name__ == '__main__': 59 | runModuleTestSuite(__import__('__main__')) 60 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/test_utilities_flood.py: -------------------------------------------------------------------------------- 1 | """ 2 | >>> from pybrain.utilities import flood 3 | 4 | The reachable-search can only get to 3 of the points. 5 | 6 | >>> sorted(flood(step, range(10), [2])) 7 | [2, 4, 5, 7, 8] 8 | 9 | Early stopping with relevance argument: 10 | 11 | >>> sorted(flood(step, range(100), [2], relevant=[5])) 12 | [2, 4, 5] 13 | 14 | If the initial point must be included for it to work: 15 | 16 | >>> sorted(flood(step, range(10), [-1])) 17 | [] 18 | 19 | """ 20 | 21 | __author__ = 'Tom Schaul, tom@idsia.ch' 22 | 23 | from pybrain.tests import runModuleTestSuite 24 | 25 | def step(x): 26 | """ A neighbor of x is either 2*x or x+3""" 27 | return [x+3, 2*x] 28 | 29 | if __name__ == '__main__': 30 | runModuleTestSuite(__import__('__main__')) 31 | 32 | 33 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/test_utilities_foundafter.py: -------------------------------------------------------------------------------- 1 | """ 2 | >>> from pybrain.utilities import avgFoundAfter 3 | 4 | A sequence of decreasing target values 5 | >>> dess = [20,10,3,1,0] 6 | 7 | A list of sequences of encountered values 8 | >>> ls = [[11,11,11,11,11,11,11,11,1,1,1,10,1,0],\ 9 | [11,9,7,5,2,0.5,-2],\ 10 | [2,2,2,2,2,0,2,2,0,2,2,2,-1]] 11 | 12 | Average index where each value is encountered. 13 | >>> avgFoundAfter(dess, ls) 14 | array([ 0., 3., 4., 6., 8.]) 15 | 16 | If a value is not always encountered, the length of the longest sequence is used: 17 | >>> avgFoundAfter([10,0], [[20],[20,1,1,1,-2]]) 18 | array([ 3. , 4.5]) 19 | 20 | 21 | """ 22 | 23 | __author__ = 'Tom Schaul, tom@idsia.ch' 24 | 25 | from pybrain.tests import runModuleTestSuite 26 | 27 | if __name__ == '__main__': 28 | runModuleTestSuite(__import__('__main__')) 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/test_utilities_reachable.py: -------------------------------------------------------------------------------- 1 | """ 2 | >>> from pybrain.utilities import reachable, decrementAny, flood 3 | 4 | The reachable-search can only get to 3 of the points. 5 | 6 | >>> dests = [(1,3), (2,2), (3,2), (3,1), (1,0), (0,2), (2,0), (0,1)] 7 | >>> sorted(reachable(decrementAny, [(3,3)], dests).items()) 8 | [((1, 3), 2), ((2, 2), 2), ((3, 2), 1)] 9 | 10 | >>> d2 = map(lambda i: (i,), range(10)) 11 | >>> reachable(decrementAny, [(12,), (29,)], d2) 12 | {(9,): 3} 13 | 14 | """ 15 | 16 | __author__ = 'Tom Schaul, tom@idsia.ch' 17 | 18 | from pybrain.tests import runModuleTestSuite 19 | 20 | if __name__ == '__main__': 21 | runModuleTestSuite(__import__('__main__')) 22 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/tools/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/tools/__init__.py -------------------------------------------------------------------------------- /pybrain/tests/unittests/tools/test_ibp_leftordered.py: -------------------------------------------------------------------------------- 1 | """ 2 | >>> from pybrain.tools.ibp import leftordered 3 | >>> from scipy import rand, array 4 | 5 | Build a random binary matrix 6 | 7 | >>> M = array(rand(10,20)<0.4, dtype=bool) 8 | >>> L = leftordered(M) 9 | 10 | Reordering rows gives the same result 11 | 12 | >>> M2 = M[:, ::-1] 13 | >>> sum(sum(L == leftordered(M2))) == 200 14 | True 15 | 16 | Reordering columns does not 17 | >>> M3 = M[::-1, :] 18 | >>> sum(sum(L == leftordered(M3))) < 200 19 | True 20 | 21 | """ 22 | 23 | __author__ = 'Tom Schaul, tom@idsia.ch' 24 | 25 | from pybrain.tests import runModuleTestSuite 26 | 27 | if __name__ == '__main__': 28 | runModuleTestSuite(__import__('__main__')) 29 | -------------------------------------------------------------------------------- /pybrain/tests/unittests/unsupervised/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tests/unittests/unsupervised/__init__.py -------------------------------------------------------------------------------- /pybrain/tools/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tools/__init__.py -------------------------------------------------------------------------------- /pybrain/tools/benchmark.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Justin Bayer, bayerj@in.tum.de' 2 | 3 | 4 | from pybrain.datasets.dataset import DataSet 5 | 6 | 7 | class BenchmarkDataSet(DataSet): 8 | 9 | def __init__(self): 10 | super(BenchmarkDataSet, self).__init__() 11 | self.addField('Average Reward', 1) 12 | self.addField('Episode Length', 1) 13 | self.linkFields(['Average Reward', 'Episode Length']) 14 | 15 | def _initialValues(self): 16 | return tuple(), dict() 17 | -------------------------------------------------------------------------------- /pybrain/tools/customxml/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tools/customxml/__init__.py -------------------------------------------------------------------------------- /pybrain/tools/datasets/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tools/datasets/__init__.py -------------------------------------------------------------------------------- /pybrain/tools/datasets/mnist.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | import os 3 | import scipy 4 | import struct 5 | 6 | from pybrain.datasets import SupervisedDataSet 7 | 8 | 9 | def labels(filename): 10 | fp = file(filename) 11 | magicnumber, length = struct.unpack('>ii', fp.read(8)) 12 | assert magicnumber in (2049, 2051), ("Not an MNIST file: %i" % magicnumber) 13 | for _ in xrange(length): 14 | label, = struct.unpack('B', fp.read(1)) 15 | yield label 16 | 17 | 18 | def images(filename): 19 | fp = file(filename,'rb') 20 | chunk = fp.read(16) 21 | magicnumber, length, numrows, numcols = struct.unpack('>iiii', chunk) 22 | assert magicnumber in (2049, 2051), ("Not an MNIST file: %i" % magicnumber) 23 | imagesize = numrows * numcols 24 | for _ in xrange(length): 25 | imagestring = fp.read(imagesize) 26 | image = struct.unpack('B' * imagesize, imagestring) 27 | yield scipy.array(image) 28 | 29 | 30 | def flaggedArrayByIndex(idx, length): 31 | arr = scipy.zeros(length) 32 | arr[idx] = 1. 33 | return arr 34 | 35 | 36 | def makeMnistDataSets(path): 37 | """Return a pair consisting of two datasets, the first being the training 38 | and the second being the test dataset.""" 39 | test = SupervisedDataSet(28 * 28, 10) 40 | test_image_file = os.path.join(path, 't10k-images-idx3-ubyte') 41 | test_label_file = os.path.join(path, 't10k-labels-idx1-ubyte') 42 | test_images = images(test_image_file) 43 | test_labels = (flaggedArrayByIndex(l, 10) for l in labels(test_label_file)) 44 | 45 | for image, label in itertools.izip(test_images, test_labels): 46 | test.addSample(image, label) 47 | 48 | train = SupervisedDataSet(28 * 28, 10) 49 | train_image_file = os.path.join(path, 'train-images-idx3-ubyte') 50 | train_label_file = os.path.join(path, 'train-labels-idx1-ubyte') 51 | train_images = images(train_image_file) 52 | train_labels = (flaggedArrayByIndex(l, 10) for l in labels(train_label_file)) 53 | for image, label in itertools.izip(train_images, train_labels): 54 | train.addSample(image, label) 55 | 56 | return train, test 57 | -------------------------------------------------------------------------------- /pybrain/tools/filehandling.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | import os 4 | import pickle 5 | 6 | 7 | def getAllFilesIn(dir, tag='', extension='.pickle'): 8 | """ return a list of all filenames in the specified directory 9 | (with the given tag and/or extension). """ 10 | allfiles = os.listdir(dir) 11 | res = [] 12 | for f in allfiles: 13 | if f[-len(extension):] == extension and f[:len(tag)] == tag: 14 | res.append(f[:-len(extension)]) 15 | return res 16 | 17 | 18 | def selectSome(strings, requiredsubstrings=[], requireAll=True): 19 | """ Filter the list of strings to only contain those that have at least 20 | one of the required substrings. """ 21 | if len(requiredsubstrings) == 0: 22 | return strings 23 | res = [] 24 | for s in strings: 25 | if requireAll: 26 | bad = False 27 | for rs in requiredsubstrings: 28 | if s.find(rs) < 0: 29 | bad = True 30 | break 31 | if not bad: 32 | res.append(s) 33 | else: 34 | for rs in requiredsubstrings: 35 | if s.find(rs) >= 0: 36 | res.append(s) 37 | break 38 | return res 39 | 40 | 41 | def pickleDumpDict(name, d): 42 | """ pickle-dump a variable into a file """ 43 | try: 44 | f = open(name + '.pickle', 'w') 45 | pickle.dump(d, f) 46 | f.close() 47 | return True 48 | except Exception, e: 49 | print('Error writing into', name, ':', str(e)) 50 | return False 51 | 52 | 53 | def pickleReadDict(name): 54 | """ pickle-read a (default: dictionnary) variable from a file """ 55 | try: 56 | f = open(name + '.pickle') 57 | val = pickle.load(f) 58 | f.close() 59 | except Exception, e: 60 | print('Nothing read from', name, ':', str(e)) 61 | val = {} 62 | return val 63 | 64 | 65 | def addToDictFile(name, key, data, verbose=False): 66 | if verbose: 67 | print('.',) 68 | d = pickleReadDict(name) 69 | if key not in d: 70 | d[key] = [] 71 | d[key].append(data) 72 | pickleDumpDict(name, d) 73 | if verbose: 74 | print(':') 75 | 76 | 77 | -------------------------------------------------------------------------------- /pybrain/tools/fisher.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pybrain.utilities import blockCombine 4 | from scipy import mat, dot, outer 5 | from scipy.linalg import inv, cholesky 6 | 7 | 8 | 9 | def calcFisherInformation(sigma, invSigma=None, factorSigma=None): 10 | """ Compute the exact Fisher Information Matrix of a Gaussian distribution, 11 | given its covariance matrix. 12 | Returns a list of the diagonal blocks. """ 13 | if invSigma == None: 14 | invSigma = inv(sigma) 15 | if factorSigma == None: 16 | factorSigma = cholesky(sigma) 17 | dim = sigma.shape[0] 18 | fim = [invSigma] 19 | for k in range(dim): 20 | D = invSigma[k:, k:].copy() 21 | D[0, 0] += factorSigma[k, k] ** -2 22 | fim.append(D) 23 | return fim 24 | 25 | 26 | 27 | def calcInvFisher(sigma, invSigma=None, factorSigma=None): 28 | """ Efficiently compute the exact inverse of the FIM of a Gaussian. 29 | Returns a list of the diagonal blocks. """ 30 | if invSigma == None: 31 | invSigma = inv(sigma) 32 | if factorSigma == None: 33 | factorSigma = cholesky(sigma) 34 | dim = sigma.shape[0] 35 | 36 | invF = [mat(1 / (invSigma[-1, -1] + factorSigma[-1, -1] ** -2))] 37 | invD = 1 / invSigma[-1, -1] 38 | for k in reversed(range(dim - 1)): 39 | v = invSigma[k + 1:, k] 40 | w = invSigma[k, k] 41 | wr = w + factorSigma[k, k] ** -2 42 | u = dot(invD, v) 43 | s = dot(v, u) 44 | q = 1 / (w - s) 45 | qr = 1 / (wr - s) 46 | t = -(1 + q * s) / w 47 | tr = -(1 + qr * s) / wr 48 | invF.append(blockCombine([[qr, tr * u], [mat(tr * u).T, invD + qr * outer(u, u)]])) 49 | invD = blockCombine([[q , t * u], [mat(t * u).T, invD + q * outer(u, u)]]) 50 | 51 | invF.append(sigma) 52 | invF.reverse() 53 | return invF 54 | 55 | -------------------------------------------------------------------------------- /pybrain/tools/mixtures/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tools/mixtures/__init__.py -------------------------------------------------------------------------------- /pybrain/tools/networking/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/tools/networking/__init__.py -------------------------------------------------------------------------------- /pybrain/tools/plotting/__init__.py: -------------------------------------------------------------------------------- 1 | #from fitnesslandscapes import FitnessPlotter 2 | from multiline import MultilinePlotter 3 | from colormaps import ColorMap 4 | from fitnessprogression import plotFitnessProgession -------------------------------------------------------------------------------- /pybrain/tools/plotting/ciaoplot.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from scipy import zeros, array, amin, amax, sqrt 4 | 5 | from colormaps import ColorMap 6 | 7 | class CiaoPlot(ColorMap): 8 | """ CIAO plot of coevolution performance with respect to the best 9 | individuals from previous generations (Hall of Fame). 10 | Requires 2 populations. """ 11 | 12 | @staticmethod 13 | def generateData(evaluator, hof1, hof2, symmetric=True): 14 | assert len(hof1) == len(hof2) 15 | gens = len(hof1) 16 | res = zeros((gens, gens)) 17 | for g1, ind1 in enumerate(hof1): 18 | for g2, ind2 in enumerate(hof2[:g1 + 1]): 19 | res[g1, g2] = evaluator(ind1, ind2) 20 | if symmetric: 21 | res[g2, g1] = res[g1, g2] 22 | elif g1 == g2: 23 | # TODO: chack this! 24 | res[g1, g2] += evaluator(ind2, ind1) 25 | else: 26 | res[g2, g1] = evaluator(ind2, ind1) 27 | return res 28 | 29 | 30 | def __init__(self, evaluator, hof1, hof2, **args): 31 | if 'symmetric' in args: 32 | M = CiaoPlot.generateData(evaluator, hof1, hof2, symmetric=args['symmetric']) 33 | del args['symmetric'] 34 | else: 35 | M = CiaoPlot.generateData(evaluator, hof1, hof2) 36 | M *= 1 / (amin(M) - amax(M)) 37 | M -= amin(M) 38 | self.relData = M 39 | ColorMap.__init__(self, M, minvalue=0, maxvalue=1, **args) 40 | 41 | 42 | if __name__ == '__main__': 43 | x = array(range(100)) 44 | h1 = x * 4 45 | h2 = x + 20 * sqrt(x) 46 | def evo(x, y): 47 | return x - y 48 | from pylab import cm 49 | p = CiaoPlot(evo, h1, h2, cmap=cm.hot).show() 50 | -------------------------------------------------------------------------------- /pybrain/tools/plotting/colormaps.py: -------------------------------------------------------------------------------- 1 | __author__ = 'Tom Schaul, tom@idsia.ch' 2 | 3 | from pylab import figure, savefig, imshow, axes, axis, cm, show 4 | from scipy import array, amin, amax 5 | 6 | 7 | class ColorMap: 8 | def __init__(self, mat, cmap=None, pixelspervalue=20, minvalue=None, maxvalue=None): 9 | """ Make a colormap image of a matrix 10 | 11 | :key mat: the matrix to be used for the colormap. 12 | """ 13 | if minvalue == None: 14 | minvalue = amin(mat) 15 | if maxvalue == None: 16 | maxvalue = amax(mat) 17 | if not cmap: 18 | cmap = cm.hot 19 | 20 | figsize = (array(mat.shape) / 100. * pixelspervalue)[::-1] 21 | self.fig = figure(figsize=figsize) 22 | axes([0, 0, 1, 1]) # Make the plot occupy the whole canvas 23 | axis('off') 24 | self.fig.set_size_inches(figsize) 25 | imshow(mat, cmap=cmap, clim=(minvalue, maxvalue), interpolation='nearest') 26 | 27 | def show(self): 28 | """ have the image popup """ 29 | show() 30 | 31 | def save(self, filename): 32 | """ save colormap to file""" 33 | savefig(filename, fig=self.fig, facecolor='black', edgecolor='black') 34 | 35 | -------------------------------------------------------------------------------- /pybrain/unsupervised/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/unsupervised/__init__.py -------------------------------------------------------------------------------- /pybrain/unsupervised/trainers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tim55667757/FuzzyClassificator/f556447eaec39e8309623d8f2536a1277b4cfb7c/pybrain/unsupervised/trainers/__init__.py -------------------------------------------------------------------------------- /report.txt: -------------------------------------------------------------------------------- 1 | Neuronet: C:\work\projects\FuzzyClassificator\network.xml 2 | 3 | FuzzyScale = {Min, Low, Med, High, Max} 4 | Min = 5 | Low = 6 | Med = 7 | High = 8 | Max = 9 | 10 | Classification results for candidates vectors: 11 | 12 | Input: ['0.12', '0.32', 'Min'] Output: ['Min', 'Max'] 13 | Input: ['0.32', '0.35', 'Low'] Output: ['Low', 'High'] 14 | Input: ['0.54', '0.57', 'Med'] Output: ['Max', 'Min'] 15 | Input: ['0.65', '0.68', 'High'] Output: ['Max', 'Min'] 16 | Input: ['0.76', '0.79', 'Max'] Output: ['Max', 'Min'] 17 | --------------------------------------------------------------------------------