Freeing Instances.

Thorolf Tonjum <[email protected]>
Newsgroups gmane.comp.breve
Message-ID <[email protected]>
First of all, thanks for a truy innovative creation, as a opengl programmer Im impressed.

1. When freeing my multibody object tripod using : free Tripod.
it vanishes from the : print "all Mobiles ", myMobile. collection
but the simulation rappidly deteriorates to a siruplike slowmotion state,
I take it that this is because the former ghost tripods are still in the moddel..
I used :(controller): self disable-freed-instance-protection.

? How can you propperly kill or reset the state of an object (joint positions, velocities, ect-)

2. Is there anyway to reload a multiboddy back into the original object, without
causing a new instance ?

Background :
Im using Breve to make a tripod walk with genetic algorithms.
The tripod is dependent on starting from the exact same conditions each
time, therefore I instantain it for each run of the training algorithm.

Sincerely,
Thorolf Tonjum
Msc Intelligent systems

Code : Built upon the walker example.

# 
# Walker
#
# - reset position : kompletely kill, load obj from xml file
# - reset time     : Reset_time = Reset_time - Global_time
# - fix genes !
#
#
#
#
#
#

@use PhysicalControl.
@use Link.
@use Genome.
@use Shape.
@use Stationary.
@use MultiBody.

@define SPEED_K 12.


Controller Walker.

PhysicalControl : Walker {
	+ variables:
		SelectedGenomes, GenePopulation (list).
		currentSelectedGenome (int).
		Tripod (object).

		# the following are flags that can be controlled by the user
		# via the simulation menu

		locked (int).
		lockMenu (object).
		cloudTexture (int).


	+ to init:
		floorShape (object).
		floor (object).
		number (int).
		item (object).

    self disable-freed-instance-protection.

		locked = 0.
		self set-random-seed-from-dev-random.
		self enable-lighting.
		self enable-smooth-drawing.
		self move-light to (0, 20, 0).

		# Create the floor for the critter to walk on.
		floorShape = (new Shape init-with-cube size (1000, 2, 1000)).
		floor = new Stationary.
		floor register with-shape floorShape at-location (0, 0, 0).
		floor catch-shadows.
		floor set-color to (1.0, 1.0, 1.0).

		cloudTexture = (self load-image from "images/clouds.png").

		self enable-shadow-volumes.
		self enable-reflections.
    self half-gravity.

		self set-background-color to (.4, .6, .9).
		self set-background-texture to cloudTexture.

		# Create the Tripod.
		Tripod = new TripodTemplate.
		Tripod move to (0, 6, 0).
		self offset-camera by (3, 13, -13).
		self watch item Tripod.

    # Tripod Re-set-shape.
    Tripod archive-as-xml file "Tri.xml".

		# The "genoms" are the individuals that will control the TripodTemplate.
		# Create the genoms and assign them numbers.
		GenePopulation = 10 new genoms.
		foreach item in GenePopulation: {
			(item set-number to number).
			print "Genome ", (GenePopulation{number} get-number), (GenePopulation{number} get-distance).
			number += 1.

		}



		self pick-Genomes.

		# set up the menus...
		lockMenu = (self add-menu named "Lock Genome" for-method "toggle-Genome-lock").
		self add-menu-separator.
		self add-menu named "Save Current Genome" for-method "save-current-genome".
		self add-menu named "Load Into Current Genome" for-method "load-into-current-genome".

		# schedule the first Genome change and we're ready to go.
		self schedule method-call "change-Genomes" at-time (self get-time) + 20.0.
		self display-current-Genome.


	+ to display-current-Genome:
		currentNumber (int).
		currentNumber = (SelectedGenomes{currentSelectedGenome} get-number).
		self set-display-text to "Genome #$currentNumber" at-x -.95 at-y -.9.

	+ to iterate:
		SelectedGenomes{currentSelectedGenome} control robot Tripod at-time (self get-time).
		super iterate.

	+ to pick-Genomes:

		sort GenePopulation with compare-distance.

		SelectedGenomes{0} = GenePopulation{0}.
		SelectedGenomes{1} = GenePopulation{1}.
		SelectedGenomes{2} = GenePopulation{2}.
		SelectedGenomes{3} = GenePopulation{3}.
		SelectedGenomes{4} = GenePopulation{4}.
		SelectedGenomes{5} = GenePopulation{5}.
		SelectedGenomes{6} = GenePopulation{6}.
		SelectedGenomes{7} = GenePopulation{7}.
		SelectedGenomes{8} = GenePopulation{8}.
		SelectedGenomes{9} = GenePopulation{9}.

		currentSelectedGenome = 0.

	+ to change-Genomes:
		newGenome (int).
		newOffset (vector).
    myMobile  (list).

		# if they have locked the current Genome, do nothing.
		if locked: return.

		# we change the Genomes every time a Genome is finished it's
		# turn.  if we have seen the last Genome, breed them together.
		
		SelectedGenomes{currentSelectedGenome} set-distance to |(Tripod get-location)|.
    

    free Tripod.

    myMobile = all Mobiles.
    print "all Mobiles ", myMobile.
    # self report-object-allocation.

    Tripod = new TripodTemplate.
		Tripod move to (0, 6, 0).
		self offset-camera by (3, 13, -13).
		self watch item Tripod.

    # Tripod center.
		Tripod set-color.
		
		newOffset = random[(50, 10, 50)] + (-15, 1, -15).
		if |newOffset| < 14: newOffset = 14 * newOffset/|newOffset|.
		self bullet-pan-camera-offset by newOffset steps 5.


		currentSelectedGenome += 1.

		if currentSelectedGenome > 8: {
			self breed-new-genoms.
			self pick-Genomes.
		}

		newGenome = (SelectedGenomes{currentSelectedGenome} get-number).

		# schedule a new Genome change in 30 seconds.
		self schedule method-call "change-Genomes" at-time (self get-time) + 10.0.
		self display-current-Genome.

	+ to breed-new-genoms:
    Testnum (int).
    number(int).
    item (object).
    
		print "breeding genoms...".
    number =-1.

    sort SelectedGenomes with compare-distance.

		# print out the distance for each individual tested

    foreach item in GenePopulation: {
      number += 1.
			print "Genome ", (GenePopulation{number} get-number), (GenePopulation{number} get-distance).
		}
		
		number =-1.
		
		foreach item in SelectedGenomes: {
      number += 1.
      GenePopulation{number} = SelectedGenomes{number}.
			print "Selected ", (SelectedGenomes{number} get-number), (SelectedGenomes{number} get-distance).
		}

    number =-1.
    
		foreach item in SelectedGenomes: {
      number += 1.
      GenePopulation{number} = SelectedGenomes{number}.
			print "Genome ", (GenePopulation{number} get-number), (GenePopulation{number} get-distance).
  		}
		
		

		# breed

		SelectedGenomes{0} breed with SelectedGenomes{1} to-child GenePopulation{9}.
		SelectedGenomes{1} breed with SelectedGenomes{0} to-child GenePopulation{8}.
		
		SelectedGenomes{2} breed with SelectedGenomes{3} to-child GenePopulation{7}.
		SelectedGenomes{3} breed with SelectedGenomes{2} to-child GenePopulation{6}.

		
		#  mutation
		(GenePopulation{9} get-genome) mutate.
		(GenePopulation{7} get-genome) mutate.
		(GenePopulation{5} get-genome) mutate.
		(GenePopulation{4} get-genome) mutate.


	+ to compare-distance of a (object) with b (object):
		result (float).
		result = (b get-distance) - (a get-distance).
		return result.

	# the following methods are accessed from the simulation menu.

	+ to toggle-Genome-lock:
		if locked == 1: {
			locked = 0.
			Tripod center.
			self schedule method-call "change-Genomes" at-time (self get-time) + 10.0.
			lockMenu uncheck.
		} else {
			locked = 1.
			lockMenu check.
		}

	+ to save-current-genome:
		(SelectedGenomes{currentSelectedGenome} get-genome) save-with-dialog.

	+ to load-into-current-genome:
		(SelectedGenomes{currentSelectedGenome} get-genome) load-with-dialog.
		
		+ to catch-key-1-down:
		newOffset (vector).
		
		newOffset = random[(90, 10, 90)] + (-15, 1, -15).
		if |newOffset| < 14: newOffset = 14 * newOffset/|newOffset|.
		self bullet-pan-camera-offset by newOffset steps 100.
		
		newOffset = random[(90, 10, 90)] + (-15, 1, -15).
		if |newOffset| < 14: newOffset = 14 * newOffset/|newOffset|.
		self bullet-pan-camera-offset by newOffset steps 100.
		
    newOffset = random[(90, 10, 90)] + (-15, 1, -15).
		if |newOffset| < 14: newOffset = 14 * newOffset/|newOffset|.
		self bullet-pan-camera-offset by newOffset steps 100.
		# look at self from newOffset.

  

}

Object : genoms {
	+ variables:
		distanceTraveled (float).
		genome (object).

		number (int).

	+ to set-number to n (int):
		number = n.

	+ to get-number:
		return number.
	
	+ to init:
		genome = new TripodGenome.
		self randomize.

	+ to randomize:
		genome randomize.

	+ to get-genome:
		return genome.

	+ to breed with otherGenome (object) to-child child (object):
		(child get-genome) crossover from-parent-1 (otherGenome get-genome) from-parent-2 (self get-genome).

	+ to control robot theRobot (object) at-time t (float):

		theRobot set-joint-velocity-1 to SPEED_K * (genome calculate-torque-1 at t).
		theRobot set-joint-velocity-2 to SPEED_K * -(genome calculate-torque-2 at t).
		theRobot set-joint-velocity-3 to SPEED_K * -(genome calculate-torque-3 at t).
		theRobot set-joint-velocity-4 to SPEED_K * -(genome calculate-torque-4 at t).
		theRobot set-joint-velocity-5 to SPEED_K * -(genome calculate-torque-5 at t).
		theRobot set-joint-velocity-6 to SPEED_K * (genome calculate-torque-6 at t).

	+ to set-distance to value (float):
		distanceTraveled = value.

	+ to get-distance:
		return distanceTraveled.
}

Genome : TripodGenome {
	+ variables:
		genomeData (13 floats).

	+ to randomize:
		genomeData[0] = random[5.0] - 2.5.

		genomeData[1] = random[2.0] - 1.0 .
		genomeData[2] = random[2.0] - 1.0 .
		genomeData[3] = random[2.0] - 1.0 .
		genomeData[4] = random[2.0] - 1.0 .
		genomeData[5] = random[2.0] - 1.0 .
		genomeData[6] = random[2.0] - 1.0 .

		genomeData[7] = random[6.3] - 3.15.
    genomeData[8] = random[6.3] - 3.15.
		genomeData[9] = random[6.3] - 3.15.
    genomeData[10] = random[6.3] - 3.15.
		genomeData[11] = random[6.3] - 3.15.
		genomeData[12] = random[6.3] - 3.15.


	+ to calculate-torque-1 at time (float):
		return .5 * (sin(genomeData[0] * (time + genomeData[7])) - (genomeData[1])).

	+ to calculate-torque-2 at time (float):
		return .5 * (sin(genomeData[0] * (time + genomeData[8])) - (genomeData[2])).

	+ to calculate-torque-3 at time (float):
		return .5 * (sin(genomeData[0] * (time + genomeData[9])) - (genomeData[3])).
		
 + to calculate-torque-4 at time (float):
		return .5 * (sin(genomeData[0] * (time + genomeData[10])) - (genomeData[4])).
		
 + to calculate-torque-5 at time (float):
		return .5 * (sin(genomeData[0] * (time + genomeData[11])) - (genomeData[5])).
		
 + to calculate-torque-6 at time (float):
		return .5 * (sin(genomeData[0] * (time + genomeData[12])) - (genomeData[6])).


	+ to mutate:
		n (int).

		n = random[12].
		if n < 7: genomeData[n] = random[2.0] - 1.0.
		else if n > 6: genomeData[n] = random[6.3] - 3.15.
		else if n = 0: random[5.0] - 2.5.

		print "mutated item $n of $self".
}

MultiBody : TripodTemplate {
	+ variables:
		bodyLink (object).
		links (list).
    Color1 (vector).
    Color2 (vector).
		joints (list).

	+ to get-root:
		return bodyLink.

	+ to init:
		
		linkShape, FootShape, lowerLinkShape, bodyShape (object).

		self add-menu named "Send to Center" for-method "center".

		lowerLinkShape = new Shape.
		lowerLinkShape init-with-cube size (.26, 2.5, .26).

		linkShape = new Shape.
		linkShape init-with-cube size (.28, 1.5, .28).

		bodyShape = new Shape.
		bodyShape init-with-sphere radius (0.5).
	  
		FootShape = new Shape.
		FootShape init-with-polygon-disk radius (0.55) sides (8) height (0.2).

    Color1 = random[(1.0, 1.0, 1.0)].
    Color2 = random[(1.0, 1.0, 1.0)].

		links = 6 new Links.
		joints = 6 new RevoluteJoints.

		links{0} set shape linkShape.
		links{0} set-color to Color1.
		links{2} set shape linkShape.
		links{2} set-color to Color1.
		links{4} set shape linkShape.
		links{4} set-color to Color1.

		links{1} set shape lowerLinkShape.
		links{1} set-color to Color2.
		links{3} set shape lowerLinkShape.
		links{3} set-color to Color2.
		links{5} set shape lowerLinkShape.
		links{5} set-color to Color2.
		


		bodyLink = new Link.
		bodyLink set shape bodyShape.
		bodyLink set-color to Color2.


		joints{0} link parent bodyLink to-child links{0}
								with-normal ( 1.5, 0, 1 )
								with-parent-point (0.85, 1, 0.65)
								with-child-point (0, 2.2, 0).
								joints{0}  set-joint-limit-vectors min ( 0.8, 0, 0) max ( 1.3, 0, 0).
		joints{1} link parent links{0} to-child links{1}
							  with-normal (-10, 0, 0)
								with-parent-point (0, -.5, 0)
								with-child-point (0, 2, 0).

		

		joints{2} link parent bodyLink to-child links{2}
								with-normal (-1.5, 0, 1)
								with-parent-point ( -0.85, 1, 0.65)
								with-child-point (0, 2.2, 0).
                joints{2}  set-joint-limit-vectors min ( 0.8, 0, 0) max ( 1.3, 0, 0).
		joints{3} link parent links{2} to-child links{3}
							  with-normal (-10, 0, 0)
								with-parent-point (0, -.5, 0)
								with-child-point (0, 2, 0).



		joints{4} link parent bodyLink to-child links{4}
								with-normal ( -1.5, 0, -1 )
							 	with-parent-point ( 0 , 1, -0.65)
								with-child-point (0, 2.2, 0).
                joints{4}  set-joint-limit-vectors min ( 0.8, 0, 0) max ( 1.3, 0, 0).
		joints{5} link parent links{4} to-child links{5}
							  with-normal (-10, 0, 0)
								with-parent-point (0, -.5, 0)
								with-child-point (0, 2, 0).

		

		self register with-link bodyLink.
		self rotate around-axis (1, 0, 0) by 1.57.

		joints set-double-spring with-strength 30 with-max 1.5 with-min -2 .
		joints set-strength-limit to 40.
		
		
	+ to Re-set:


	+ to center:
		# to center the object, we set the X and Z components to 0, but not
		# the Y, otherwise we would push the walker into the ground
		currentLocation (vector).
		currentLocation = (self get-location).
		self move to (0, currentLocation::y, 0).


  + to set-color:
  	
  	Color3 (vector).
    Color4 (vector).
  	
  	Color3 = random[(1.0, 1.0, 1.0)].
    Color4 = random[(1.0, 1.0, 1.0)].
  	
		links{0} set-color to Color3.
		links{2} set-color to Color3.
		links{4} set-color to Color3.
		links{1} set-color to Color4.
		links{3} set-color to Color4.
		links{5} set-color to Color4.

		bodyLink set-color to Color4.


	# The following six methods allow external objects move the joints
	+ to set-joint-velocity-1 to value (float):
		joints{1} set-joint-velocity to value.

	+ to set-joint-velocity-2 to value (float):
		joints{2} set-joint-velocity to value.

	+ to set-joint-velocity-3 to value (float):
		joints{3} set-joint-velocity to value.
		
	+ to set-joint-velocity-4 to value (float):
  	joints{4} set-joint-velocity to -value.
		
	+ to set-joint-velocity-5 to value (float):
		joints{5} set-joint-velocity to -value.
		
   + to set-joint-velocity-6 to value (float):
		joints{0} set-joint-velocity to value.
		
  + to Re-set-shape:
  joints{0} set-joint-velocity to 2.
  joints{1} set-joint-velocity to 2.
  joints{2} set-joint-velocity to 2.
  joints{3} set-joint-velocity to 2.
  joints{4} set-joint-velocity to 2.
  joints{5} set-joint-velocity to 2.

  

	+ to destroy:
		free links.
		free bodyLink.
		free joints.
}



_______________________________________________
breve mailing list
[email protected]
http://www.spiderland.org/mailman/listinfo/breve
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.