- Timestamp:
- 08/25/24 01:56:48 (4 months ago)
- Location:
- js/standard_expdef_demo
- Files:
-
- 14 added
- 2 deleted
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
js/standard_expdef_demo/index.html
r880 r1326 5 5 <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> 6 6 <link rel="stylesheet" type="text/css" href="static/styles.css"> 7 <title>Standard.expdef Animation</title> 7 <title>Standard.expdef demo animation</title> 8 <script type="importmap"> 9 { 10 "imports": { 11 "three": "./node_modules/three/build/three.module.js", 12 "three/addons/OrbitControls.js": "./node_modules/three/examples/jsm/controls/OrbitControls.js", 13 "three/addons/CSS3DRenderer.js": "./node_modules/three/examples/jsm/renderers/CSS3DRenderer.js", 14 "three/addons/Sky.js": "./node_modules/three/examples/jsm/objects/Sky.js", 15 "tween": "./node_modules/three/examples/jsm/libs/tween.module.js" 16 } 17 } 18 </script> 19 <script type="module"> 20 import * as TWEEN from 'tween'; 21 import * as THREE from 'three'; 22 import {OrbitControls} from "three/addons/OrbitControls.js"; 23 import {CSS3DObject, CSS3DSprite, CSS3DRenderer} from "three/addons/CSS3DRenderer.js"; 24 import {Sky} from "three/addons/Sky.js"; 25 26 // this trick is done to avoid refactoring the entire app 27 window.THREE = THREE; 28 window.Sky = Sky; 29 window.OrbitControls = OrbitControls; 30 window.CSS3DRenderer = CSS3DRenderer; 31 window.CSS3DObject = CSS3DObject; 32 window.CSS3DSprite = CSS3DSprite; 33 window.TWEEN = TWEEN; 34 </script> 35 36 37 38 8 39 </head> 9 40 <body style="margin: 0; overflow: hidden;"> 10 <!--External dependencies--> 11 <script src="js/external/threejs/three.min.js"></script> 12 <script src="js/external/threejs/CSS3DRenderer.js"></script> 13 <script src="js/external/threejs/OrbitControls.js"></script> 14 <script src="js/external/threejs/Sky.js"></script> 15 <script src="js/external/tweenjs/tween.min.js"></script> 16 <script src="../sdk/frams-sdk.js"></script> 17 <script src="js/external/framsjs/visualization/jointmeshfactory.js"></script> 18 <script src="js/external/framsjs/visualization/partmeshfactory.js"></script> 19 <script src="js/external/framsjs/visualization/transformations.js"></script> 20 <!--Project sources--> 21 <script src="js/config.js"></script> 22 <script src="js/world/core.js"></script> 23 <script src="js/world/object/framstick.js"></script> 24 <script src="js/world/object/table.js"></script> 25 <script src="js/world/object/text.js"></script> 26 <script src="js/world/object/arrow.js"></script> 27 <script src="js/simulation/core.js"></script> 28 <script src="js/app.js"></script> 41 <div class="column controls"> 42 <div class="row control-container" id="fitness-rater"> 43 <span>Rate creature:</span> 44 </div> 45 <div class="row control-container"> 46 <span>Genotype: </span> 47 <select id="genotype-selector"> 48 </select> 49 </div> 50 51 </div> 52 <!--External dependencies--> 53 54 <!-- <script src="js/external/threejs/three.min.js"></script> 55 <script src="js/external/threejs/CSS3DRenderer.js"></script> 56 <script src="js/external/threejs/OrbitControls.js"></script> 57 <script src="js/external/threejs/Sky.js"></script> --> 58 <!-- <script src="js/external/tweenjs/tween.min.js"></script> --> 59 <script src="sdk/frams-sdk.js"></script> 60 <script src="js/external/framsjs/visualization/jointmeshfactory.js" defer></script> 61 <script src="js/external/framsjs/visualization/partmeshfactory.js" defer></script> 62 <script src="js/external/framsjs/visualization/transformations.js" defer></script> 63 <!--Project sources--> 64 <script src="js/config.js" defer></script> 65 <script src="js/utils.js" defer></script> 66 <script src="js/world/core.js" defer></script> 67 <script src="js/world/object/framstick.js" defer></script> 68 <script src="js/world/object/table.js"></script> 69 <script src="js/world/object/text.js" defer></script> 70 <script src="js/world/object/arrow.js" defer></script> 71 <script src="js/neuroviewer/view.js" defer></script> 72 <script src="js/neuroviewer/neurofactory.js" defer></script> 73 <script src="js/neuroviewer/neurons/basedrawableneuron.js" defer></script> 74 <script src="js/neuroviewer/neurons/genericdrawableneuron.js" defer></script> 75 <script src="js/neuroviewer/neurons/receptordrawableneuron.js" defer></script> 76 <script src="js/components/slider.js" defer></script> 77 <script src="js/components/option.js" defer></script> 78 <script src="js/components/button.js" defer></script> 79 <script src="js/simulation/core.js" defer></script> 80 <script src="js/app.js" defer></script> 29 81 </body> 30 82 </html> -
js/standard_expdef_demo/js/app.js
r880 r1326 12 12 constructor() { 13 13 this.scene = this.Scene(); 14 this.camera = this.Camera(); 15 this.controls = this.Controls(); 14 this.camera = this.Camera(); 16 15 this.webGLRenderer = this.WebGLRenderer(); 16 this.cameraControls = this.CameraControls(); 17 17 this.css3DRenderer = this.CSS3DRenderer(); 18 18 19 this.simulation = new Simulation(new World(this.camera, this.scene)); 19 this.args = this.parseArguments(); 20 Config.Simulation.GENOTYPE = this.args["genotype_format"]?this.args["genotype_format"]:Config.Simulation.GENOTYPE; 21 this.simulation = new Simulation(new World(this.camera, this.scene),this.args); 20 22 21 23 document.body.appendChild(this.webGLRenderer.domElement); 22 24 document.body.appendChild(this.css3DRenderer.domElement); 23 25 window.addEventListener("resize", this.onWindowResize.bind(this), false); 24 return this; 26 27 this.Controls(); 28 this.Options(); 29 this.NeuroView(); 30 this.FitnessRater(); 31 } 32 33 NeuroView(){ 34 var thisApp = this; 35 window.addEventListener('DOMContentLoaded',()=>{ 36 var neuroViewer = NeuroViewerView( Config.NeuroViewer ); 37 thisApp.simulation.neuroViewer = neuroViewer; 38 neuroViewer.make3D(); 39 }) 40 } 41 42 Controls(){ 43 var thisApp = this; 44 window.addEventListener('DOMContentLoaded', () => { 45 const controls = document.getElementsByClassName('controls')[0]; 46 thisApp.mutation_slider = new Slider( 47 controls, 48 "Mutation", 49 function(prob){ 50 thisApp.simulation.setMutationProb(parseFloat(prob)); 51 }); 52 thisApp.crossover_slider = new Slider( 53 controls, 54 "Crossover", 55 function(prob){ 56 thisApp.simulation.setCrossoverProb(parseFloat(prob)); 57 }); 58 thisApp.simulation.mutationListeners.push(thisApp.mutation_slider); 59 thisApp.simulation.crossoverListeners.push(thisApp.crossover_slider); 60 thisApp.simulation.reupdateListeners(); 61 return this; 62 }); 63 } 64 65 Options(){ 66 const encodings = this.findEncodings(); 67 68 var thisApp = this; 69 window.addEventListener('DOMContentLoaded', () => { 70 const selector = document.getElementById("genotype-selector"); 71 for(let i=0;i<encodings.length;i++){ 72 new Option(selector,encodings[i]); 73 } 74 selector.onchange = function(){ 75 thisApp.simulation.setProposedGenotype(`/*${this.value}*/`); 76 // let url = new URL(window.location.href); 77 // let params = new URLSearchParams(url.search); 78 // let paramName = "genotype_format"; 79 // let paramValue = `/*${this.value}*/`; 80 81 // if(params.has(paramName)){ 82 // params.set(paramName,paramValue); 83 // }else{ 84 // params.append(paramName,paramValue); 85 // } 86 // window.location.href = window.location.origin+'/?'+params.toString(); 87 } 88 }); 89 } 90 91 FitnessRater(){ 92 var sim = this.simulation; 93 window.addEventListener('DOMContentLoaded', ()=>{ 94 const rater = document.getElementById("fitness-rater"); 95 const thumbUp = new Button(rater,"👍",()=>{ 96 sim.updateFitness(5); 97 }); 98 const thumbDown = new Button(rater,"👎",()=>{ 99 sim.updateFitness(-5); 100 }) 101 102 sim.buttons.push(thumbUp); 103 sim.buttons.push(thumbDown); 104 }); 105 } 106 107 findEncodings(){ 108 const functionName = "GenoConv_f"; 109 return Object 110 .keys(Module) //framsticks module 111 .filter((k)=>{return k.startsWith(functionName);}) 112 .map((k)=>{return k.replace(functionName,"")[0]}); 25 113 } 26 114 … … 44 132 } 45 133 46 C ontrols() {134 CameraControls() { 47 135 /** 48 136 * Creates a new Control object alowing to steer the camera. 49 137 */ 50 let controls = new THREE.OrbitControls(this.camera);138 let controls = new OrbitControls(this.camera,this.webGLRenderer.domElement ); 51 139 controls.autoRotate = Config.Controls.AUTO_ROTATE; 52 140 controls.autoRotateSpeed = Config.Controls.AUTO_ROTATE_SPEED; … … 54 142 controls.maxDistance = Config.Controls.MAX_DISTANCE; 55 143 controls.maxPolarAngle = Config.Controls.MAX_POLAR_ANGLE; 144 controls.enableKeys = false; 56 145 controls.update(); 57 146 return controls … … 77 166 * Creates a CSS3D Renderer object used to visualize HTML+CSS components in 3D scene. 78 167 */ 79 let renderer = new THREE.CSS3DRenderer();168 let renderer = new CSS3DRenderer(); 80 169 renderer.setSize(Config.CSS3DRenderer.WIDTH, Config.CSS3DRenderer.HEIGHT); 81 170 renderer.domElement.style.position = Config.CSS3DRenderer.POSITION; 82 171 renderer.domElement.style.top = Config.CSS3DRenderer.TOP; 172 renderer.domElement.style.setProperty('pointer-events','none',''); 83 173 return renderer 84 174 } … … 98 188 requestAnimationFrame(this.render.bind(this)); 99 189 TWEEN.update(); 100 this.c ontrols.update();190 this.cameraControls.update(); 101 191 this.webGLRenderer.render(this.scene, this.camera); 102 192 this.css3DRenderer.render(this.scene, this.camera); … … 112 202 this.css3DRenderer.setSize(window.innerWidth, window.innerHeight); 113 203 } 204 205 parseArguments() { 206 /** 207 * Parses URL arguemnts 208 */ 209 210 const queryString = window.location.search; 211 const urlParams = new URLSearchParams(queryString); 212 return { 213 "mutation_prob" : parseFloat(urlParams.get("mutation_prob")), 214 "crossover_prob" : parseFloat(urlParams.get("crossover_prob")), 215 "genotype_format": urlParams.get("genotype_format") 216 }; 217 } 114 218 } 115 219 -
js/standard_expdef_demo/js/config.js
r880 r1326 11 11 CROSSOVER_PROBABILITY: 0.3, 12 12 EVOLUTION_MAX_REPETITIONS: 100, 13 GENOTYPES: [ 14 "/*4*/ #5,<<X><<X>X>X>>LLX", 15 "/*4*/ <X>l<X>l<<X>X>LLLX", 16 "/*4*/ <,<,,<,X,,>X>X>X", 17 "/*4*/ <X><X><<X>X><X>X", 18 "/*4*/ <<X>RR<<X>X>X>X", 19 "/*4*/ <<X><<X>X>X>X", 20 ], 13 NUMBER_OF_GENOTYPES: 6, 14 INITIAL_MUTATIONS: 10, 15 GENOTYPE: "/*4*/", 21 16 22 17 Timing: { … … 38 33 FITNESS_TEXT: {x: 400, y: 300, z: 485}, 39 34 EVOLVED_GENOTYPE_PLANK: {x: 400, y: 240, z: 0}, 35 NEURAL_NETWORK: {x: 400, y: 100, z: 300}, 40 36 EVOLVED_FITNESS_PLANK: {x: 400, y: 240, z: 380}, 41 37 MUTATION_GENOTYPE_PLANK: {x: 400, y: 400, z: 0}, … … 83 79 Light: { 84 80 Ambient: { 85 COLOR: 0x 8a8a8a81 COLOR: 0xffffff 86 82 }, 87 83 88 84 Directional: { 89 COLOR: 0x dfebff,90 INTENSITY: 1,85 COLOR: 0xffffff, 86 INTENSITY: 2, 91 87 X_POS: 50, 92 88 Y_POS: 200, … … 167 163 }, 168 164 165 NeuroViewer: { 166 layoutType: 2, 167 emptyLabel: "No neural network", 168 canvas: { 169 id: "neuro-viewer-canvas", 170 drawMarginInPx: 20, 171 font: "14px Lucida Grande, sans-serif", 172 strokeStyle: '#DEDEDE' 173 }, 174 }, 175 169 176 Framstick: { 170 177 COLOR: 0xcacaca, … … 174 181 CAST_SHADOW: true, 175 182 183 184 176 185 Tween: { 177 186 Scale: { … … 196 205 PART: { 197 206 defaultShape: { 198 radius: 0.213,207 radius: Module.Part.prototype.BALL_AND_STICK_RADIUS, 199 208 segments: 16 200 209 }, … … 206 215 JOINT: { 207 216 cylinderShape: { 208 radius: 0.1, 217 thickness: 0.8, 218 radius: undefined, // if undefined -> depends on Part radius 209 219 radiusSegments: 10, 210 220 isTransparent: false, … … 239 249 HEIGHT: 65, 240 250 THICKNESS: 10, 241 TEXTURE: " https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/hardwood2_diffuse.jpg",251 TEXTURE: "js/hardwood2_diffuse_brighter.jpg", 242 252 REPEAT: 3, 243 253 ANISOTROPY: 8, … … 264 274 HEIGHT: 65, 265 275 THICKNESS: 10, 266 TEXTURE: " https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/hardwood2_diffuse.jpg",276 TEXTURE: "js/hardwood2_diffuse_brighter.jpg", 267 277 REPEAT: 1, 268 278 ANISOTROPY: 8, … … 288 298 LENGTH: 100, 289 299 FACES: 32, 290 TEXTURE: "https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/textures/hardwood2_diffuse.jpg",300 TEXTURE: "js/hardwood2_diffuse_brighter.jpg", 291 301 REPEAT: 1, 292 302 ANISOTROPY: 8, … … 323 333 } 324 334 }; 335 336 337 if (!Config.Framstick.Geometry.JOINT.cylinderShape.radius) { 338 Config.Framstick.Geometry.JOINT.cylinderShape.radius = Config.Framstick.Geometry.PART.defaultShape.radius * Config.Framstick.Geometry.JOINT.cylinderShape.thickness; 339 } -
js/standard_expdef_demo/js/external/framsjs/visualization/jointmeshfactory.js
r880 r1326 102 102 let shape = joint.get_shape(); 103 103 104 if (this.jointShapes['SHAPE_ FIXED'].value == shape) {105 result = this.getNewJointMesh(joint, this.config. linkShape);104 if (this.jointShapes['SHAPE_STICK'].value == shape) { 105 result = this.getNewJointMesh(joint, this.config.cylinderShape); 106 106 } else { 107 result = this.getNewJointMesh(joint, this.config.cylinderShape);107 result = this.getNewJointMesh(joint, this.config.linkShape); 108 108 } 109 109 -
js/standard_expdef_demo/js/external/framsjs/visualization/transformations.js
r880 r1326 17 17 getPartShapes() { 18 18 let shapes = []; 19 shapes["SHAPE_BALL _AND_STICK"] = { name: "Ball & Stick", value: Module.Part["SHAPE_BALL_AND_STICK"] };19 shapes["SHAPE_BALL"] = { name: "Ball", value: Module.Part["SHAPE_BALL"] }; 20 20 shapes["SHAPE_ELLIPSOID"] = { name: "Elipsoid", value: Module.Part["SHAPE_ELLIPSOID"] }; 21 21 shapes["SHAPE_CUBOID"] = { name: "Cuboid", value: Module.Part["SHAPE_CUBOID"] }; … … 30 30 getJointShapes() { 31 31 let shapes = []; 32 shapes["SHAPE_ BALL_AND_STICK"] = { name: "Ball & Stick", value: Module.Joint["SHAPE_BALL_AND_STICK"] };32 shapes["SHAPE_STICK"] = { name: "Stick", value: Module.Joint["SHAPE_STICK"] }; 33 33 shapes["SHAPE_FIXED"] = { name: "Fixed", value: Module.Joint["SHAPE_FIXED"] }; 34 shapes["SHAPE_HINGE_X"] = { name: "Fixed", value: Module.Joint["SHAPE_HINGE_X"] }; 35 shapes["SHAPE_HINGE_XY"] = { name: "Fixed", value: Module.Joint["SHAPE_HINGE_XY"] }; 34 36 return shapes; 35 37 } … … 100 102 */ 101 103 calcColorComponent(value) { 102 return THREE.Math .clamp(value, 0, 1);104 return THREE.MathUtils.clamp(value, 0, 1); 103 105 } 104 106 … … 151 153 152 154 let m = new THREE.Matrix4(); 153 m.makeRotationZ(THREE.Math .degToRad(90));155 m.makeRotationZ(THREE.MathUtils.degToRad(90)); 154 156 155 157 let m2 = new THREE.Matrix4(); … … 242 244 part.get_scale().get_x(), 243 245 part.get_scale().get_z()); 244 } else if (this.partShapes['SHAPE_BALL _AND_STICK'].value != shape) {246 } else if (this.partShapes['SHAPE_BALL'].value != shape) { 245 247 bodyElement.mesh.scale.set( 246 248 part.get_scale().get_x(), -
js/standard_expdef_demo/js/simulation/core.js
r880 r1326 10 10 */ 11 11 12 constructor(world ) {12 constructor(world, args) { 13 13 this.world = world; 14 this.buttons = []; 14 15 15 16 this.genPoolText = this.getText("Genotype Pool", EVOLUTION_METHOD_TEXT_POSITION); … … 22 23 this.fitnessText = this.getText("Fitness", Config.Simulation.Element.Position.FITNESS_TEXT); 23 24 25 this.genotypeEncoding = Config.Simulation.GENOTYPE; 26 this.proposedGenotypeEncoding = undefined; 27 24 28 this.world.scene.add(this.genPoolText); 25 29 this.world.scene.add(this.mutationText); … … 31 35 this.world.scene.add(this.fitnessText); 32 36 37 this.world.createTable(this.createGenotypes(Config.Simulation.NUMBER_OF_GENOTYPES,Config.Simulation.INITIAL_MUTATIONS),this.genotypeEncoding); 38 33 39 this.state = State.INITIAL; 34 40 this.timing = Config.Simulation.Timing.INITIAL; 41 42 this.mutationProb = 0; 43 this.mutationListeners = []; 44 this.setMutationProb(args.mutation_prob?args.mutation_prob:Config.Simulation.MUTATION_PROBABILITY); 45 46 this.crossoverProb = 0; 47 this.crossoverListeners = []; 48 this.setCrossoverProb(args.crossover_prob?args.crossover_prob:Config.Simulation.CROSSOVER_PROBABILITY); 49 50 this.neuroLayoutModel = undefined; 51 this.neuroLayoutFunctionHelper = new Module.NNLayoutFunctionHelper(); 52 this.neuroViewer = undefined; 53 54 this.evolvedFitness = 0; 55 56 this.lastTimeout = undefined; 57 } 58 59 activateButtons(){ 60 for(let i=0;i<this.buttons.length;i++){ 61 this.buttons[i].activate(); 62 } 63 } 64 65 deactivateButtons(){ 66 for(let i=0;i<this.buttons.length;i++){ 67 this.buttons[i].deactivate(); 68 } 69 } 70 71 updateListeners(list,value){ 72 for(let i=0;i<list.length;i++){ 73 list[i].update(value); 74 } 75 } 76 77 reupdateListeners(){ 78 this.updateListeners(this.mutationListeners,this.mutationProb); 79 this.updateListeners(this.crossoverListeners,this.crossoverProb); 80 } 81 82 getMutationProb(){ 83 return this.mutationProb; 84 } 85 setMutationProb(prob){ 86 this.mutationProb = Math.min(Math.max(prob,0),1); 87 if(this.crossoverProb + this.mutationProb > 1){ 88 this.setCrossoverProb(1 - this.mutationProb); 89 } 90 this.updateListeners(this.mutationListeners,this.mutationProb); 91 } 92 93 getCrossoverProb(){ 94 return this.crossoverProb; 95 } 96 setCrossoverProb(prob){ 97 this.crossoverProb = Math.min(Math.max(prob,0),1); 98 if(this.crossoverProb + this.mutationProb > 1){ 99 this.setMutationProb(1 - this.crossoverProb); 100 } 101 this.updateListeners(this.crossoverListeners,this.crossoverProb); 102 } 103 104 updateNeuroLayoutModel(model) { 105 if( this.neuroLayoutModel ) { 106 Module.destroy( this.neuroLayoutModel ); 107 } 108 this.neuroLayoutModel = new Module.NNLayoutState_Model_Fred( model ); 109 this.neuroLayoutFunctionHelper.doLayout( Config.NeuroViewer.layoutType , this.neuroLayoutModel ); 110 } 111 112 getBrainData (model) { 113 var thisSim = this; 114 return { 115 getElements: function () { 116 return thisSim.neuroLayoutModel.GetElements(); 117 }, 118 getValueXYWH: function ( i ) { 119 return thisSim.neuroLayoutModel.GetValueXYWH( i ); 120 }, 121 getNeuro: function ( i ) { 122 return model.getNeuro( i ); 123 } 124 }; 125 } 126 127 updateFitness(value){ 128 // if (this.state !== State.SHOW_FRAMSTICK && this.state !== State.DESTROY_FRAMSTICK){ 129 // return; 130 // } 131 this.evolvedFitness += value; 132 this.evolvedFitness = Math.max(this.evolvedFitness,0); 133 if(this.evolvedFitnessPlank){ 134 this.evolvedFitnessPlank.updateFitness(this.evolvedFitness); 135 } 136 } 137 138 isGenotypeChanging(){ 139 return this.proposedGenotypeEncoding && this.proposedGenotypeEncoding !== this.genotypeEncoding; 35 140 } 36 141 … … 43 148 this.state = State.EVOLUTION_METHOD_SELECTION; 44 149 this.timing = Config.Simulation.Timing.INITIAL; 150 151 if(this.isGenotypeChanging()){ 152 this.state = State.CHANGE_GENOTYPE; 153 } 154 45 155 break; 46 156 … … 50 160 this.timing = Config.Simulation.Timing.EVOLUTION_METHOD_SELECTION; 51 161 let randomVal = Math.random() 52 if (randomVal < Config.Simulation.MUTATION_PROBABILITY) {162 if (randomVal < this.mutationProb) { 53 163 this.state = State.MUTATION_GENOTYPE_SELECTION; 54 } else if (randomVal < Config.Simulation.MUTATION_PROBABILITY+55 Config.Simulation.CROSSOVER_PROBABILITY){164 } else if (randomVal < this.mutationProb + 165 this.crossoverProb){ 56 166 this.state = State.CROSSOVER_GENOTYPE_SELECTION; 57 167 } else { … … 65 175 this.hideText(this.genPoolText); 66 176 67 this.mutationIdx = parseInt(Math.random() * Config.Simulation.GENOTYPES.length);177 this.mutationIdx = this.chooseIdx(); 68 178 this.mutationArrow = this.getArrow(this.mutationIdx).mesh; 69 179 this.world.scene.add(this.mutationArrow); … … 100 210 this.hideText(this.genPoolText); 101 211 102 this.cloningIdx = parseInt(Math.random() * Config.Simulation.GENOTYPES.length);212 this.cloningIdx = this.chooseIdx(); 103 213 this.cloningArrow = this.getArrow(this.cloningIdx).mesh; 104 214 this.world.scene.add(this.cloningArrow); … … 135 245 this.hideText(this.genPoolText); 136 246 137 this.crossoverIdx1 = parseInt(Math.random() * Config.Simulation.GENOTYPES.length);247 this.crossoverIdx1 = this.chooseIdx(); 138 248 do { 139 this.crossoverIdx2 = parseInt(Math.random() * Config.Simulation.GENOTYPES.length);249 this.crossoverIdx2 = this.chooseIdx(); 140 250 } while (this.crossoverIdx1 == this.crossoverIdx2); 141 251 … … 175 285 176 286 case State.SHOW_FRAMSTICK: 287 this.activateButtons(); 177 288 this.moveCamera(Config.Simulation.Camera.Position.FRAMSTICK); 178 289 179 290 this.evolvedFramstick = new Framstick(this.evolvedGenotype); 291 const model = this.evolvedFramstick.getModelFromGenotype(this.evolvedGenotype); 180 292 this.world.scene.add(this.evolvedFramstick.mesh); 181 293 294 this.updateNeuroLayoutModel(model); 295 this.neuroViewer.updateBrain(this.getBrainData(model)); 296 Module.destroy(model); 182 297 this.timing = Config.Simulation.Timing.SHOW_FRAMSTICK; 183 298 this.state = State.DESTROY_FRAMSTICK; … … 187 302 this.evolvedFramstick.scaleUpSize = 0.01; 188 303 this.evolvedFramstick.scaleDownSize = 0.01; 304 this.neuroViewer.clear(); 305 this.world.scene.remove(this.neuroViewer.get3DContainer()); 189 306 190 307 this.showText(this.fitnessText); … … 195 312 this.evolvedFitnessPlank.move(Config.Simulation.Element.Position.EVOLVED_FITNESS_PLANK); 196 313 this.evolvedFitnessPlank.rotate(Config.Simulation.Element.Angle.SIDE_VIEW); 197 this.world.scene.add(this.evolvedFitnessPlank.mesh) 198 this.world.scene.add(this.evolvedFitnessPlank.textMesh) 314 this.world.scene.add(this.evolvedFitnessPlank.mesh); 315 this.world.scene.add(this.evolvedFitnessPlank.textMesh); 316 199 317 200 318 this.timing = Config.Simulation.Timing.DESTROY_FRAMSTICK; … … 207 325 } 208 326 break; 327 case State.CHANGE_GENOTYPE: 328 this.genotypeEncoding = this.proposedGenotypeEncoding; 329 this.world.setGenotypes(this.createGenotypes(Config.Simulation.NUMBER_OF_GENOTYPES,Config.Simulation.INITIAL_MUTATIONS),this.genotypeEncoding); 330 this.proposedGenotypeEncoding = undefined; 331 332 this.state = State.INITIAL; 333 break; 209 334 } 210 211 setTimeout(this.run.bind(this), this.timing); 212 } 213 214 getMutatedGenotype(genotype) { 215 let genoF4 = new Module.Geno_f4(); 216 let genoOperatorsHelper = new Module.GenoOperatorsHelper(genoF4); 335 if(this.isGenotypeChanging()){ 336 this.timing = 0; 337 } 338 this.lastTimeout = setTimeout(this.run.bind(this), this.timing); 339 } 340 341 setProposedGenotype(proposed){ 342 this.proposedGenotypeEncoding = proposed; 343 if (this.lastTimeout){ 344 clearTimeout(this.lastTimeout); 345 this.run(); 346 } 347 } 348 349 chooseIdx(){ 350 return this.weightedChoice(); 351 } 352 353 randomChoice(){ 354 return parseInt(Math.random() * Config.Simulation.NUMBER_OF_GENOTYPES); 355 } 356 357 weightedChoice(){ 358 const fitness = this.world.table.fitnessPlanks 359 .map((k)=>{ 360 return parseFloat(k.fitness)+0.0001; 361 }); 362 const fitness_sum = fitness.reduce((partialSum, a) => partialSum + a, 0); 363 const probs = fitness 364 .map((k)=>{ 365 return k/fitness_sum; 366 }); 367 368 var prob = Math.random(); 369 for(let i=0;i<probs.length;i++){ 370 if(prob < probs[i]){ 371 return i; 372 } 373 prob -= probs[i]; 374 } 375 return -1; 376 } 377 378 createGenotypes(n,mutation_count){ 379 var func = this.getOperator(this.genotypeEncoding); 380 let genoOper = new func(); 381 let genoOperatorsHelper = new Module.GenoOperatorsHelper(genoOper); 382 var genotypes = []; 383 384 for(let i=0;i<n;i++){ 385 var genotype = genoOperatorsHelper.getSimplest(); 386 genotypes.push(this.mutate(genotype,genoOperatorsHelper,mutation_count)); 387 } 388 389 return genotypes; 390 } 391 392 getOperator(genoName){ 393 const genoType = genoName.replace("/*","").replace("*/",""); 394 var func = Module[`Geno_f${genoType}`]; 395 func = func?func:Module[`GenoOper_f${genoType}`]; 396 return func; 397 } 398 399 getMutatedGenotype(genotype){ 400 var func = this.getOperator(this.genotypeEncoding); 401 let genoOper = new func(); 402 let genoOperatorsHelper = new Module.GenoOperatorsHelper(genoOper); 403 genotype = this.mutate(genotype,genoOperatorsHelper,1); 404 Module.destroy(genoOper); 405 Module.destroy(genoOperatorsHelper); 406 return genotype; 407 } 408 409 mutate(genotype,genoOperatorsHelper,times) { 410 for(let i = 0;i<times;i++){ 411 genotype = this.tryMutation(genotype,genoOperatorsHelper); 412 } 413 return genotype; 414 } 415 416 tryMutation(genotype,genoOperatorsHelper){ 417 let genetics = new Module.PreconfiguredGenetics(); 418 let iteration = 0; 419 do { 420 if (genoOperatorsHelper.mutate(genotype.replace(this.genotypeEncoding, "")) == 0) { 421 let mutated = genoOperatorsHelper.getLastMutateGeno().c_str(); 422 let newGenotype = `${this.genotypeEncoding}` + mutated; 423 424 let stringObj = new Module.SString(); 425 stringObj.set(newGenotype); 426 let genoObj = new Module.Geno(stringObj); 427 428 if (genoObj.isValid()) { 429 genotype = newGenotype; 430 iteration = Config.Simulation.EVOLUTION_MAX_REPETITIONS; 431 } 432 433 Module.destroy(stringObj); 434 Module.destroy(genoObj); 435 } else { 436 iteration = Config.Simulation.EVOLUTION_MAX_REPETITIONS; 437 } 438 iteration += 1; 439 } while(iteration < Config.Simulation.EVOLUTION_MAX_REPETITIONS); 440 441 return genotype; 442 } 443 444 getCrossoveredGenotype(genotype1, genotype2) { 445 var func = this.getOperator(this.genotypeEncoding); 446 let genoOper = new func(); 447 let genoOperatorsHelper = new Module.GenoOperatorsHelper(genoOper); 217 448 let genetics = new Module.PreconfiguredGenetics(); 218 449 let iteration = 0; 219 450 220 451 do { 221 if (genoOperatorsHelper.mutate(genotype.replace("/*4*/", "")) == 0) { 222 let mutated = genoOperatorsHelper.getLastMutateGeno(); 223 let newGenotype = "/*4*/ " + mutated; 452 if (genoOperatorsHelper.crossOver(genotype1.replace(this.genotypeEncoding, ""), 453 genotype2.replace(this.genotypeEncoding, "")) == 0) { 454 let crossovered = genoOperatorsHelper.getLastCrossGeno1().c_str(); 455 let newGenotype = `${this.genotypeEncoding}` + crossovered; 456 224 457 225 458 let stringObj = new Module.SString(); … … 228 461 229 462 if (genoObj.isValid()) { 230 genotype = newGenotype;463 genotype1 = newGenotype; 231 464 iteration = Config.Simulation.EVOLUTION_MAX_REPETITIONS; 232 465 } … … 240 473 } while(iteration < Config.Simulation.EVOLUTION_MAX_REPETITIONS); 241 474 242 Module.destroy(genoF4); 243 Module.destroy(genoOperatorsHelper); 244 return genotype; 245 } 246 247 getCrossoveredGenotype(genotype1, genotype2) { 248 let genoF4 = new Module.Geno_f4(); 249 let genoOperatorsHelper = new Module.GenoOperatorsHelper(genoF4); 250 let genetics = new Module.PreconfiguredGenetics(); 251 let iteration = 0; 252 253 do { 254 if (genoOperatorsHelper.crossOver(genotype1.replace("/*4*/", ""), 255 genotype2.replace("/*4*/", "")) == 0) { 256 let crossovered = genoOperatorsHelper.getLastCrossGeno1(); 257 let newGenotype = "/*4*/ " + crossovered; 258 259 let stringObj = new Module.SString(); 260 stringObj.set(newGenotype); 261 let genoObj = new Module.Geno(stringObj); 262 263 if (genoObj.isValid()) { 264 genotype1 = newGenotype; 265 iteration = Config.Simulation.EVOLUTION_MAX_REPETITIONS; 266 } 267 268 Module.destroy(stringObj); 269 Module.destroy(genoObj); 270 } else { 271 iteration = Config.Simulation.EVOLUTION_MAX_REPETITIONS; 272 } 273 iteration += 1; 274 } while(iteration < Config.Simulation.EVOLUTION_MAX_REPETITIONS); 275 276 Module.destroy(genoF4); 475 Module.destroy(genoOper); 277 476 Module.destroy(genoOperatorsHelper); 278 477 return genotype1; … … 280 479 281 480 showEvolutionResult() { 282 this.evolvedGenotypePlank = new GenotypePlank(this.evolvedGenotype, 10);481 this.evolvedGenotypePlank = new GenotypePlank(this.evolvedGenotype, this.genotypeEncoding, 10); 283 482 this.evolvedGenotypePlank.move(Config.Simulation.Element.Position.EVOLVED_GENOTYPE_PLANK); 284 483 this.evolvedGenotypePlank.rotate(Config.Simulation.Element.Angle.SIDE_VIEW); 484 485 this.neuroViewer.move(Config.Simulation.Element.Position.NEURAL_NETWORK); 486 this.neuroViewer.rotate(Config.Simulation.Element.Angle.SIDE_VIEW); 285 487 this.world.scene.add(this.evolvedGenotypePlank.mesh); 286 488 this.world.scene.add(this.evolvedGenotypePlank.textMesh); 489 this.world.scene.add(this.neuroViewer.get3DContainer()); 287 490 288 491 if (this.evolutionMethod == State.MUTATION_GENOTYPE_SELECTION) { … … 302 505 303 506 evolutionReturn() { 304 let replaceIdx = parseInt(Math.random() * Config.Simulation. GENOTYPES.length);507 let replaceIdx = parseInt(Math.random() * Config.Simulation.NUMBER_OF_GENOTYPES); 305 508 let genotypePlank = this.world.table.genotypePlanks[replaceIdx]; 306 509 let fitnessPlank = this.world.table.fitnessPlanks[replaceIdx]; … … 322 525 this.world.table.genotypePlanks[replaceIdx] = this.evolvedGenotypePlank; 323 526 this.world.table.fitnessPlanks[replaceIdx] = this.evolvedFitnessPlank; 527 this.evolvedFitnessPlank = undefined; 324 528 this.hideText(this.fitnessText); 529 this.deactivateButtons(); 325 530 } 326 531 … … 378 583 "CROSSOVER_SIDE_VIEW": 9, "CROSSOVER_RESULT": 10, "CROSSOVER_RETURN": 11, 379 584 "DESTROY_FRAMSTICK": 12, "CLONING_GENOTYPE_SELECTION": 13, 380 "CLONING_SIDE_VIEW": 14, "CLONING_RESULT": 15, "CLONING_RETURN": 16 585 "CLONING_SIDE_VIEW": 14, "CLONING_RESULT": 15, "CLONING_RETURN": 16, 586 "CHANGE_GENOTYPE": 17 381 587 }; 382 588 383 589 var EVOLUTION_METHOD_TEXT_POSITION = { 384 590 x: -210, 385 y: 80 + (Config.Table.FitnessPlank.HEIGHT + Config.Table.Board.SPACING) * (Config.Simulation. GENOTYPES.length+ 1),591 y: 80 + (Config.Table.FitnessPlank.HEIGHT + Config.Table.Board.SPACING) * (Config.Simulation.NUMBER_OF_GENOTYPES + 1), 386 592 z: -500 387 593 }; -
js/standard_expdef_demo/js/world/core.js
r880 r1326 17 17 this.sky = this.Sky(); 18 18 this.sun = this.Sun(); 19 this.table = this.Table();20 19 } 21 20 … … 25 24 texture.repeat.set(Config.Ground.REPEAT, Config.Ground.REPEAT); 26 25 texture.anisotropy = Config.Ground.ANISOTROPY; 26 texture.colorSpace = THREE.SRGBColorSpace; 27 27 28 let geometry = new THREE.Plane BufferGeometry(Config.Ground.WIDTH, Config.Ground.WIDTH);28 let geometry = new THREE.PlaneGeometry(Config.Ground.WIDTH, Config.Ground.WIDTH); 29 29 let material = new THREE.MeshLambertMaterial({map: texture}); 30 30 let ground = new THREE.Mesh(geometry, material); … … 36 36 } 37 37 38 Table() { 39 let table = new Table(Config.Simulation.GENOTYPES); 38 createTable(genotypes,genotypeEncoding){ 39 this.table = this.Table(genotypes,genotypeEncoding); 40 } 41 42 setGenotypes(genotypes, genotypeEncoding){ 43 this.table.genotypes = genotypes; 44 for (let i = 0; i < this.table.genotypes.length; i++) { 45 this.scene.remove(this.table.genotypePlanks[i].textMesh); 46 // this.table.genotypePlanks[i] = new GenotypePlank(genotypes[i],genotypeEncoding,i); 47 this.table.genotypePlanks[i].updateGenotype(genotypes[i],genotypeEncoding); 48 this.scene.add(this.table.genotypePlanks[i].textMesh); 49 this.table.fitnessPlanks[i].updateFitness((Math.random() * 100).toFixed(2)); 50 } 51 } 52 53 Table(genotypes,genotypeEncoding) { 54 let table = new Table(genotypes,genotypeEncoding); 40 55 this.scene.add(table.board); 41 56 this.scene.add(table.genotypeHeader.mesh); … … 76 91 77 92 Sky() { 78 let sky = new THREE.Sky()93 let sky = new Sky() 79 94 sky.scale.setScalar(Config.Sky.SCALE); 80 95 sky.material.uniforms.turbidity.value = Config.Sky.TURBIDITY; 81 96 sky.material.uniforms.rayleigh.value = Config.Sky.RAYLEIGH; 82 97 // sky.material.uniforms.luminance.value = Config.Sky.LUMINANCE; 83 98 sky.material.uniforms.mieCoefficient.value = Config.Sky.MIE_COEFFICIENT; 84 99 sky.material.uniforms.mieDirectionalG.value = Config.Sky.MIE_DIRECTIONAL_G; … … 89 104 Sun() { 90 105 let sun = new THREE.Mesh( 91 new THREE.Sphere BufferGeometry(Config.Sun.RADIUS, Config.Sun.WIDTH_SEGMENTS, Config.Sun.HEIGHT_SEGMENTS),106 new THREE.SphereGeometry(Config.Sun.RADIUS, Config.Sun.WIDTH_SEGMENTS, Config.Sun.HEIGHT_SEGMENTS), 92 107 new THREE.MeshBasicMaterial({color: Config.Sun.COLOR}) 93 108 ); -
js/standard_expdef_demo/js/world/object/arrow.js
r880 r1326 22 22 let material = new THREE.MeshLambertMaterial({map: texture}); 23 23 material.opacity = 0; 24 material.blending = THREE.NoBlending;24 // material.blending = THREE.NoBlending; 25 25 26 26 this.mesh = new THREE.Mesh(geometry, material); -
js/standard_expdef_demo/js/world/object/framstick.js
r880 r1326 112 112 } 113 113 114 let model = new Module.Model(genoObj, true); 114 let model = new Module.Model(genoObj, Module.Model["SHAPETYPE_UNKNOWN"], true); 115 model.open(); 116 model.close(); 115 117 Module.destroy(stringObj); 116 118 Module.destroy(genoObj); -
js/standard_expdef_demo/js/world/object/table.js
r880 r1326 10 10 */ 11 11 12 constructor(genotypes) { 13 this.genotypes = genotypes 12 constructor(genotypes,genotypeType) { 13 this.genotypes = genotypes; 14 this.genotypeType = genotypeType; 14 15 15 16 this.board = this.Board(); … … 25 26 texture.repeat.set(Config.Table.Board.REPEAT, Config.Table.Board.REPEAT); 26 27 texture.anisotropy = Config.Table.Board.ANISOTROPY; 28 texture.colorSpace = THREE.SRGBColorSpace; 27 29 28 30 let geometry = new THREE.BoxGeometry(Config.Table.Board.WIDTH, … … 32 34 let material = new THREE.MeshLambertMaterial({map: texture}); 33 35 material.opacity = 0; 34 material.blending = THREE.NoBlending;36 // material.blending = THREE.NoBlending; 35 37 36 38 let board = new THREE.Mesh(geometry, material); … … 46 48 let planks = [] 47 49 for (let i = 0; i < this.genotypes.length; i++) { 48 planks.push(new GenotypePlank(this.genotypes[i], i));50 planks.push(new GenotypePlank(this.genotypes[i],this.genotypeType, i)); 49 51 } 50 52 return planks … … 60 62 61 63 GenotypeHeader() { 62 return new GenotypePlank("Genotype", this.genotypes.length, true)64 return new GenotypePlank("Genotype",this.genotypeType, this.genotypes.length, true) 63 65 } 64 66 … … 71 73 class GenotypePlank { 72 74 73 constructor(genotype, position, bold) {75 constructor(genotype, genotypeEncoding, position, bold) { 74 76 this.genotype = genotype; 75 77 this.position = { … … 83 85 texture.repeat.set(Config.Table.GenotypePlank.REPEAT, Config.Table.GenotypePlank.REPEAT); 84 86 texture.anisotropy = Config.Table.GenotypePlank.ANISOTROPY; 87 texture.colorSpace = THREE.SRGBColorSpace; 85 88 86 89 let geometry = new THREE.BoxGeometry(Config.Table.GenotypePlank.WIDTH, … … 94 97 this.mesh.receiveShadow = Config.Table.GenotypePlank.RECEIVE_SHADOW; 95 98 96 this.textMesh = Text.getGenotypeMesh(this.genotype.replace("/*4*/ ", "")); 99 this.genotypeType = genotypeEncoding; 100 this.textMesh = Text.getGenotypeMesh(this.genotype,this.genotypeType); 97 101 if (bold == true) { 98 this.textMesh = Text.getFitnessMesh(this.genotype.replace( "/*4*/ ", ""));102 this.textMesh = Text.getFitnessMesh(this.genotype.replace(`${this.genotypeType} `, "")); 99 103 } 100 104 this.textMesh.position.set(this.position.x, this.position.y, this.position.z + 1); 105 } 106 107 updateGenotype(genotype,genotypeType){ 108 this.genotype = genotype; 109 this.genotypeType = genotypeType; 110 this.textMesh = Text.getGenotypeMesh(this.genotype,this.genotypeType); 111 this.textMesh.position.set(this.position.x, this.position.y, this.position.z + 1); 101 112 } 102 113 … … 137 148 texture.repeat.set(Config.Table.FitnessPlank.REPEAT, Config.Table.FitnessPlank.REPEAT); 138 149 texture.anisotropy = Config.Table.FitnessPlank.ANISOTROPY; 150 texture.colorSpace = THREE.SRGBColorSpace; 139 151 140 152 let geometry = new THREE.BoxGeometry(Config.Table.FitnessPlank.WIDTH, … … 150 162 this.textMesh = Text.getFitnessMesh(this.fitness); 151 163 this.textMesh.position.set(this.position.x, this.position.y, this.position.z + 1); 164 } 165 166 updateFitness(value){ 167 this.fitness = value; 168 this.textMesh.element.innerHTML = roundNumber(this.fitness,2); 152 169 } 153 170 -
js/standard_expdef_demo/js/world/object/text.js
r880 r1326 6 6 7 7 var Text = { 8 getGenotypeMesh: function(genotype) { 8 styleRegex: /<style>.*<\/style>/, 9 numberRegex : /-?\d+(\.\d+)?/g, 10 roundMatch: function(match) { 11 return roundNumber(match,2).toString(); 12 }, 13 14 getGenotypeMesh: function(genotype,genotypeType) { 15 const genman = new Module.GenMan(); 16 genotype = genotype.replace(this.numberRegex, this.roundMatch); 17 genotype = genotype.replaceAll("\n","⤶"); 9 18 if (genotype.length > 20) { 10 genotype = genotype.slice(0, 20) + "..."19 genotype = genotype.slice(0,30) + "..." 11 20 } 12 21 var element = document.createElement("div"); 13 element.className = "genotype"; 14 element.textContent = genotype; 15 return new THREE.CSS3DObject(element); 22 element.innerHTML = genman.HTMLize(genotype).c_str(); 23 element.innerHTML = element.innerHTML.replace(this.styleRegex,"").replace(genotypeType,""); 24 Module.destroy(genman); 25 return new CSS3DObject(element); 16 26 }, 17 27 … … 20 30 element.className = "fitness"; 21 31 element.textContent = fitness; 22 return new THREE.CSS3DObject(element);32 return new CSS3DObject(element); 23 33 }, 24 34 … … 27 37 element.className = "info"; 28 38 element.textContent = info; 29 return new THREE.CSS3DObject(element);39 return new CSS3DObject(element); 30 40 } 31 41 } -
js/standard_expdef_demo/static/styles.css
r880 r1326 1 .geno type{1 .geno { 2 2 font-size: 40px; 3 3 font-weight: 500; 4 4 font-family: "Helvetica", "Arial", "Verdana", serif; 5 5 text-align: center; 6 /* white outline to help when displayed on darker background: */ 7 /* text-shadow: -1px 0 white, 0 1px white, 1px 0 white, 0 -1px white */ 6 8 } 7 9 … … 20 22 width: 8em; 21 23 } 24 25 .neuro-viewer-container{ 26 background-color: black; 27 border-radius: 0px 30px 0px 0px; 28 border-color: white; 29 border-style: double double none none; 30 border-width: 5px; 31 pointer-events: none; 32 } 33 34 #neuro-viewer-2d{ 35 position: absolute; 36 bottom: 0; 37 left: 0; 38 width: 25%; 39 height: 25%; 40 } 41 42 #neuro-viewer-3d{ 43 background-color: black; 44 border-radius: 30px 30px 30px 30px; 45 border-color: white; 46 border-style: double double double double; 47 border-width: 5px; 48 pointer-events: none; 49 } 50 51 #neuro-viewer-canvas{ 52 width: 100%; 53 height: 100%; 54 } 55 56 .controls { 57 position: absolute; 58 bottom: 0; 59 right: 0; 60 background-color: gainsboro; 61 border-radius: 30px 0px 0px 0px; 62 border-color: gray; 63 border-style: double none none double; 64 border-width: 5px; 65 font-family: "Sans-serif", Verdana, Geneva, Tahoma, sans-serif; 66 67 display: flex; 68 justify-content: center; 69 align-items: safe center; 70 } 71 72 .column { 73 display: flex; 74 flex-direction: column; 75 } 76 77 .row { 78 display: flex; 79 flex-direction: row; 80 } 81 82 .slider-value{ 83 margin-left: 5%; 84 } 85 86 .slider{ 87 margin-left: 2%; 88 margin-right: 2%; 89 } 90 91 .control-container{ 92 margin-top: 2%; 93 width: 70%; 94 } 95 96 .rater-button{ 97 padding: 0; 98 border: none; 99 background: none; 100 font-size: 30px; 101 margin-left: 2%; 102 } 103 104 button.inactive{ 105 opacity: 0.33; 106 } 107 108 button:active{ 109 transform: translateY(4px); 110 } 111 112 button.inactive:active{ 113 transform: translateY(0px); 114 }
Note: See TracChangeset
for help on using the changeset viewer.