- Timestamp:
- 02/20/21 13:32:12 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/frams-test.py
r1087 r1089 1 """Simple examples of using the "frams" module to communicate directly with the Framsticks library (dll/so). 2 3 For an introduction to Framsticks, its usage and scripting, see https://www.youtube.com/playlist?list=PLkPlXm7pOPatTl3_Gecx8ZaCVGeH4UV1L 4 For a list of available classes, objects, methods and fields, see http://www.framsticks.com/files/classdoc/ 5 For a number of examples of scripting, see the "scripts" directory in Framsticks distribution. 6 """ 7 1 8 import sys 2 9 import frams … … 13 20 14 21 def extValueDetails(v): 15 return '"' + str(v) + '" frams type=' + str(v._type()) + ' frams class=' + str(v._class()) + ' python type=' + str(type(v._value())) 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())) 16 24 17 25 18 v = frams.String.deserialize('[100,2.2,"abc",[null,[]],XYZ[9,8,7]]') 26 dic_as_string = '[100,2.2,"abc",[null,[],{}],XYZ[9,8,7]]' 27 print("We have the following string:\n\t'%s'" % dic_as_string) 28 print("Looks like a serialized dictionary, let's ask Framsticks String.deserialize() to do its job.") 29 v = frams.String.deserialize(dic_as_string) 30 print("Framsticks String.deserialize() returned\n\t", type(v)) 31 print("More specifically, it is:") 19 32 print(extValueDetails(v)) 33 print("Even though it is ExtValue (Framsticks' Vector), it supports iteration like a python vector, so let's inspect its elements:") 20 34 for e in v: 21 35 print(extValueDetails(e)) 22 36 37 print("Now let's play with the Framsticks simulator. Let's create a Genotype object and set fields in its custom 'data' dictionary.") 23 38 g = frams.GenePools[0].add('X') 24 g.name = " py!"39 g.name = "Snakis Py" 25 40 g.data['custom'] = 123.456 26 g.data['a'] = 'b' 41 g.data['a'] = 'b' # implicit conversion, looks like python dictionary but still converts '3' and '4' to ExtValue 42 dic = frams.Dictionary.new() # let's create a Dictionary object from Framsticks 43 dic.set('1', '2') # calling set() from Framsticks Dictionary 44 dic['3'] = '4' # implicit conversion, looks like python dictionary but still converts '3' and '4' to ExtValue 45 g.data['d'] = dic 46 print(extValueDetails(g)) 27 47 48 print("Let's add a few mutants and display their data:") 28 49 for more in range(5): 29 50 frams.GenePools[0].add(frams.GenMan.mutate(g.geno)) 30 51 31 52 for g in frams.GenePools[0]: 32 print(str(g.index) + '. "' + str(g.name) + '" ' + str(g.genotype) + ' data=' + str(g.data)) 53 print("\t%d. name='%s'\tgenotype='%s'\tdata=%s" % (g.index._value(), str(g.name), str(g.genotype), str(g.data))) 54 55 print("Let's now change some property of the simulation. Current water level is", frams.World.wrldwat) 56 frams.World.wrldwat = 0.5 57 print("Now water level is", frams.World.wrldwat) 58 frams.World.wrldwat = frams.World.wrldwat._value() + 0.7 59 print("Now water level is", frams.World.wrldwat) 60 61 initial_genotype = 'X(X,RX(X[T],X[G]))' # simple body with gyroscope and touch sensors 62 print("Let's perform a few simulation steps of the initial genotype:", initial_genotype) 63 frams.ExpProperties.initialgen = initial_genotype 64 frams.ExpProperties.p_mut = 0 # no mutation (the selection procedure will clone our initial genotype) 65 frams.ExpProperties.p_xov = 0 # no crossover (the selection procedure will clone our initial genotype) 66 frams.Populations[0].initial_nn_active = 1 # immediate simulation of neural network - no "waiting for stabilization" period 67 frams.World.wrldg = 5 # gravity=5x default, let it fall quickly 68 69 frams.Simulator.init() # adds initial_genotype to gene pool (calls onInit() from standard.expdef) 70 frams.Simulator.start() # this does not actually start the simulation, just sets the "Simulator.running" status variable 71 step = frams.Simulator.step # cache reference to avoid repeated lookup in the loop (just for performance) 72 for 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())) 80 frams.Simulator.stop() 81 82 # Note that implementing a complete expdef, especially a complex one, entirely in python may be inconvenient or impractical 83 # because you do not have access to "event handlers" like you have in FramScript - onStep(), onBorn(), onDied(), onCollision() etc., 84 # so you would have to check various conditions in python in each simulation step to achieve the same effect.
Note: See TracChangeset
for help on using the changeset viewer.