source: framspy/frams-test.py @ 1267

Last change on this file since 1267 was 1195, checked in by Maciej Komosinski, 22 months ago

Cosmetic

File size: 7.2 KB
Line 
1"""Simple examples of using the "frams" module to communicate directly with the Framsticks library (dll/so).
2
3For an introduction to Framsticks, its usage and scripting, see https://www.youtube.com/playlist?list=PLkPlXm7pOPatTl3_Gecx8ZaCVGeH4UV1L
4For a list of available classes, objects, methods and fields, see http://www.framsticks.com/files/classdoc/
5For a number of examples of scripting, see the "scripts" directory in the Framsticks distribution."""
6
7import sys
8import json
9import frams
10
11frams.init(*(sys.argv[1:]))  # pass whatever args we have, init() is the right place to deal with different scenarios:
12# frams.init() - should try to figure out everything (and might fail)
13# frams.init('path/to/lib') - load the library from the specified directory and configure Framsticks path as "data" inside this directory
14# frams.init('path/to/lib','-d/tmp/workdir/data') - as above, but set the working (writable) directory somewhere else (see also -D)
15# frams.init('path/to/lib','-Lframs-objects-alt.dll') - use specified library location and non-default file name
16
17print('Available objects:', dir(frams))
18print()
19
20
21def extValueDetails(v):
22        """A helper function to display basic information about a variable of type ExtValue."""
23        return '\t"' + str(v) + '"    frams type=' + str(v._type()) + '    frams class=' + str(v._class()) + '    python type=' + str(type(v._value()))
24
25
26dic_as_string = '[100,2.2,"abc",[null,[],{}],XYZ[9,8,7]]'
27print("We have the following string:\n\t'%s'" % dic_as_string)
28print("Looks like a serialized dictionary, let's ask Framsticks String.deserialize() to do its job.")
29v = frams.String.deserialize(dic_as_string)
30print("Framsticks String.deserialize() returned\n\t", type(v))
31print("More specifically, it is:")
32print(extValueDetails(v))
33print("Even though it is ExtValue (Framsticks' Vector), it supports iteration like a python vector, so let's inspect its elements:")
34for e in v:
35        print(extValueDetails(e))
36
37print("\nNow let's play with the Framsticks simulator. Let's create a Genotype object and set fields in its custom 'data' dictionary.")
38g = frams.GenePools[0].add('X')
39g.name = "Snakis Py"
40g.data['custom'] = 123.456
41g.data['a'] = 'b'  # implicit conversion, looks like python dictionary but still converts '3' and '4' to ExtValue
42dic = frams.Dictionary.new()  # let's create a Dictionary object from Framsticks
43dic.set('1', '2')  # calling set() from Framsticks Dictionary
44dic['3'] = '4'  # implicit conversion, looks like python dictionary but still converts '3' and '4' to ExtValue
45g.data['d'] = dic
46print(extValueDetails(g))
47
48print("Let's add a few mutants and display their data:")
49for more in range(5):
50        frams.GenePools[0].add(frams.GenMan.mutate(g.geno))
51
52for g in frams.GenePools[0]:
53        print("\t%d. name='%s'\tgenotype='%s'\tdata=%s" % (g.index._value(), str(g.name), str(g.genotype), str(g.data)))
54
55print("Let's now change some property of the simulation. Current water level is", frams.World.wrldwat)
56frams.World.wrldwat = 0.5
57print("Now water level is", frams.World.wrldwat)
58frams.World.wrldwat = frams.World.wrldwat._value() + 0.7
59print("Now water level is", frams.World.wrldwat)
60
61initial_genotype = 'X(X,RX(X[T],X[G]))'  # simple body with touch and gyroscope sensors
62print("Let's perform a few simulation steps of the initial genotype:", initial_genotype)
63frams.ExpProperties.initialgen = initial_genotype
64frams.ExpProperties.p_mut = 0  # no mutation (the selection procedure will clone our initial genotype)
65frams.ExpProperties.p_xov = 0  # no crossover (the selection procedure will clone our initial genotype)
66frams.Populations[0].initial_nn_active = 1  # immediate simulation of neural network - no "waiting for stabilization" period
67frams.World.wrldg = 5  # gravity=5x default, let it fall quickly
68
69frams.Simulator.init()  # adds initial_genotype to gene pool (calls onInit() from standard.expdef)
70frams.Simulator.start()  # this does not actually start the simulation, just sets the "Simulator.running" status variable
71step = frams.Simulator.step  # cache reference to avoid repeated lookup in the loop (just for performance)
72for s in range(15):
73        step()  # first step performs selection and revives one genotype according to standard.expdef rules
74        creature = frams.Populations[0][0]  # FramScript Creature object
75        mechpart0 = creature.getMechPart(0)
76        print('Step# = %d' % frams.Simulator.stepNumber._value(),
77              '\tSimulated_creatures =', frams.Populations[0].size._value(),
78              "\tpart0_xyz = (% .2f,% .2f,% .2f)" % (mechpart0.x._value(), mechpart0.y._value(), mechpart0.z._value()),
79              "\ttouch = % .3f\tgyro = % .3f" % (creature.getNeuro(0).state._value(), creature.getNeuro(1).state._value()))
80frams.Simulator.stop()
81
82# changing expdef
83testgenotype = "XrrX[G][-1:80][|,-1:0.9]X[|,-2:-21]"
84evaluations = 100
85print("\nLet's change the experiment definition (expdef) and evaluate genotype '%s' %d times." % (testgenotype, evaluations))
86frams.Simulator.expdef = "standard-eval"
87frams.ExpProperties.evalcount = evaluations
88frams.ExpProperties.cr_v = 1
89frams.ExpProperties.evalplan = ":velocity,vertpos,fit_stdev,time"
90frams.GenePools[0].add(testgenotype)
91frams.ExpProperties.evalsavefile = ""  # no need to store results in a file - we will get evaluations directly from Genotype's "data" field
92frams.Simulator.init()
93frams.Simulator.start()
94# step = frams.Simulator.step  # cache reference to avoid repeated lookup in the loop (just for performance)
95# while frams.Simulator.running._int():  # standard-eval.expdef sets running to 0 when the evaluation is complete
96#       step()
97frams.Simulator.eval("while(Simulator.running) Simulator.step();")  # loop in FramScript much faster than loop in python
98for g in frams.GenePools[0]:  # loop over all genotypes, even though we know we added only one
99        serialized_dict = frams.String.serialize(g.data[frams.ExpProperties.evalsavedata._value()])
100        print(json.loads(serialized_dict._string()))
101
102# sampling a Model in 3D
103geno = "RXX(X,CXXX)"
104print("\nNow build a Model from the genotype '%s' and sample it in 3D, then print a 2D projection." % geno)
105import numpy as np
106
107matrix = np.zeros((20, 20, 20), dtype=int)  # 3D matrix, "voxels"
108m = frams.ModelGeometry.forModel(frams.Model.newFromString(geno))
109m.geom_density = 20
110for p in m.voxels():
111        # print('%f %f %f ' % (p.x._value(), p.y._value(), p.z._value()))
112        matrix[int(p.x._value() * 5 + 2), int(p.y._value() * 5 + 5), int(p.z._value() * 5 + 6)] += 1  # scaling and offsets adjusted manually to fit the matrix nicely
113matrix = np.sum(matrix, axis=1)  # sum along axis, make 2D from 3D ("projection")
114np.set_printoptions(formatter={'int': lambda x: ('.' if x == 0 else str(x // 18))})  # print zeros as dots, x//18 to fit a larger range into a single digit
115print(matrix)
116np.set_printoptions()  # avoids straange python errors: frams.py, line 48, in __del__ AttributeError: 'NoneType' object has no attribute 'extFree'
117
118#
119#
120# Note that implementing a complete expdef, especially a complex one, entirely in python may be inconvenient or impractical
121# because you do not have access to "event handlers" like you have in FramScript - onStep(), onBorn(), onDied(), onCollision() etc.,
122# so you would have to check various conditions in python in each simulation step to achieve the same effect.
Note: See TracBrowser for help on using the repository browser.