{"id":26376,"student_id":10,"content":"\nvar road = createSprite(\"bg.png\");\nvar car = createSprite(\"car.png\");\ncar.miles = 0;\nroad.rotationStyle = 'fixed';\ncar.scale = .17;\nroad.scale = 1.4;\ncar.y -= 30;\n\nfunction reset () {\n road.moveTo(320, 240);\n car.moveTo(320, 240);\n car.y -= 30;\n car.direction = 90;\n car.sensors.forEach(s =\u003e s.length = 80);\n car.miles = 0;\n}\n\n\nfunction sigmoid (x) {\n return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));\n}\n\nfunction NN (inputs, weights) {\n var w = weights;\n var i = inputs;\n\n var a = i[0]*w[0] + i[1]*w[1] + i[2]*w[2];\n var b = i[0]*w[3] + i[1]*w[4] + i[2]*w[5];\n var c = i[0]*w[6] + i[1]*w[7] + i[2]*w[8];\n var d = i[0]*w[9] + i[1]*w[10] + i[2]*w[11];\n var e = i[0]*w[12] + i[1]*w[13] + i[2]*w[14];\n var f = i[0]*w[15] + i[1]*w[16] + i[2]*w[17];\n\n a = sigmoid(a);\n b = sigmoid(b);\n c = sigmoid(c);\n d = sigmoid(d);\n e = sigmoid(e);\n f = sigmoid(f);\n\n var g = a*w[18] + b*w[19] + c*w[20] + d*w[21] + e*w[22] + f*w[23];\n return sigmoid(g);\n}\n\nfunction sensor (car, direction) {\n var dot = createSprite('dot.jpg');\n dot.scale = 2;\n dot.length = 0;\n dot.forever(()=\u003e{\n dot.moveTo(car);\n dot.direction = car.direction + direction;\n dot.stepForward(dot.length);\n pen.color = 'red';\n pen.drawLine(car.x, car.y, dot.x, dot.y);\n\n if (dot.touched(road)) {\n if (dot.length \u003e 0) dot.length-=2;\n } else {\n if (dot.length \u003c 200) dot.length++;\n }\n });\n return dot;\n}\n\ncar.sensors = [\n sensor(car, -45),\n sensor(car, 0),\n sensor(car, 45),\n]\n\ncar.getSensorsData = function () {\n var inputs = [];\n car.sensors.forEach(s =\u003e inputs.push(s.length));\n return inputs;\n}\n\nfunction randomDNA () {\n var dna = [];\n dna.score = 0;\n for (var i=0; i\u003c24; i++) dna.push(Math.random()*0.2 - 0.1);\n return dna;\n}\n\nfunction crossover (a, b) {\n var newDNA = [];\n newDNA.score = 0;\n for (var i=0; i\u003ca.length; i++) {\n newDNA[i] = Math.random() \u003e 0.5 ? a[i] : b[i];\n\n if (Math.random() \u003c 0.01) {\n newDNA[i] = Math.random()*0.2 - 0.1;\n }\n }\n return newDNA;\n}\n\nfunction nextGeneration () {\n populations.sort((a, b) =\u003e a.score - b.score);\n\n bestScore = populations[populations.length - 1].score;\n\n var newPops = [];\n\n for (var i=0; i\u003c17; i++) {\n var a = Math.floor(Math.sqrt(Math.random()*400));\n var b = Math.floor(Math.sqrt(Math.random()*400));\n newPops.push(crossover(populations[a], populations[b]));\n }\n newPops.push(populations.pop());\n newPops.push(populations.pop());\n newPops.push(populations.pop());\n\n newPops.forEach(b =\u003e b.score = 0);\n\n populations = newPops;\n}\n\nvar count = 0;\nvar running = true;\nvar currentBrain = 0;\nvar populations = [];\nvar bestScore = 0;\nfor (var i=0; i\u003c20; i++) {\n populations.push(randomDNA());\n}\n\nreset();\n\nforever(()=\u003e{\n\n if (running) {\n\n var inputs = car.getSensorsData();\n var outputs = NN(inputs, populations[currentBrain]);\n\n car.direction += outputs*3;\n car.miles++;\n\n if (car.touched(road)) {\n running = false;\n }\n road.direction = car.direction - 180;\n road.stepForward(3);\n\n print(car.miles, 10, 420, 'red');\n print(inputs, 10, 440, 'red');\n print(outputs, 10, 460, 'red');\n for (var i=0; i\u003cpopulations.length; i++) {\n print(populations[i].score, 10, i*20, 'red');\n }\n\n } else {\n populations[currentBrain].score = car.miles;\n currentBrain++;\n\n if (currentBrain \u003c populations.length) {\n car.brain = populations[currentBrain];\n reset(); \n } else {\n nextGeneration();\n currentBrain = 0;\n count++;\n }\n running = true;\n }\n});","created_at":"2018-04-14T13:42:10.351+08:00","updated_at":"2019-11-10T15:23:15.223+08:00","name":"car","language":"javascript","screenshot":{"url":"https://cdn4.koding.school/uploads/project/screenshot/26376/1f661a05ff0b202ee2480d0e9551979b.jpg"},"parent_id":2,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":331,"hashid":"36ysd88e","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":487864,"file_name":"bg.png","project_id":26376,"asset_id":60386,"created_at":"2018-04-14T13:43:21.759+08:00","updated_at":"2018-04-14T13:43:21.759+08:00"},{"id":487920,"file_name":"car.png","project_id":26376,"asset_id":3405,"created_at":"2018-04-14T13:44:24.031+08:00","updated_at":"2018-04-14T13:44:24.031+08:00"},{"id":488350,"file_name":"dot.jpg","project_id":26376,"asset_id":57180,"created_at":"2018-04-14T13:50:00.520+08:00","updated_at":"2018-04-14T13:50:00.520+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦