{"id":24466,"student_id":10,"content":"let road = createSprite(\"road.png\");\nroad.rotationStyle = 'fixed';\nlet border= createSprite(\"border.png\");\nborder.rotationStyle = 'fixed';\nlet car = createSprite(\"car.png\");\ncar.scale = 0.4;\ncar.direction = 180;\n\nlet offsetX = 1500;\nlet offsetY = 650;\n\ncar.reset = function () {\n this.direction = 180;\n // this.y = 30;\n // this.x = 200;\n road.x = 320 + offsetX;\n road.y = 240 + offsetY;\n border.x = 320 + offsetX;\n border.y = 240 + offsetY;\n this.miles = 0;\n this.sensors.forEach(s =\u003e s.length = 70);\n}\n\ncar.reset = function () {\n this.direction = 270;\n this.y = 30;\n this.x = 200;\n this.miles = 0;\n this.sensors.forEach(s =\u003e s.length = 30);\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] + i[3]*w[3] + i[4]*w[4]\n var b = i[0]*w[5] + i[1]*w[6] + i[2]*w[7] + i[3]*w[8] + i[4]*w[9]\n var c = i[0]*w[10] + i[1]*w[11] + i[2]*w[12] + i[3]*w[13] + i[4]*w[14]\n var d = i[0]*w[15] + i[1]*w[16] + i[2]*w[17] + i[3]*w[18] + i[4]*w[19]\n\n a = sigmoid(a);\n b = sigmoid(b);\n c = sigmoid(c);\n d = sigmoid(d);\n\n var d = a*w[20] + b*w[21] + c*w[22] + d*w[23]\n var e = a*w[24] + b*w[25] + c*w[26] + d*w[27]\n\n d = sigmoid(d);\n e = sigmoid(e);\n\n return [d, e];\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(border)) {\n if (dot.length \u003e 0) dot.length--;\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, -22),\n sensor(car, 0),\n sensor(car, 22),\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 for (var i=0; i\u003c28; i++) dna.push(Math.random()*0.2 - 0.1);\n return dna;\n}\n\nfunction randomEq (num) {\n var arr = [];\n var sum = 0;\n for (var i=0; i\u003cnum; i++) {\n var rand = Math.random();\n sum += rand;\n arr[i] = rand;\n }\n for (var i=0; i\u003cnum; i++) {\n arr[i] /= sum;\n }\n return arr;\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\u003c20; 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\n populations = newPops;\n}\n\nvar count = 0;\nvar running = true;\nvar currentTrain = 0;\nvar populations = [];\nvar bestScore = 0;\nfor (var i=0; i\u003c20; i++) {\n populations.push(randomDNA());\n}\n\ncar.reset();\ncar.brain = populations[0];\n\nforever(()=\u003e{\n if (running) {\n\n var inputs = car.getSensorsData();\n var outputs = NN(inputs, car.brain);\n\n if (outputs[0] \u003e 0) {\n car.direction += 1.5;\n }\n if (outputs[1] \u003e 0) {\n car.direction -= 1.5;\n }\n car.stepForward(1);\n car.miles++;\n\n if (car.touched(border)) {\n running = false;\n }\n\n print(bestScore, 10, 400);\n print(count + '/' + currentTrain, 10, 420);\n print(inputs, 10, 440);\n print(outputs, 10, 460);\n\n } else {\n populations[currentTrain].score = car.miles;\n currentTrain++;\n\n if (currentTrain \u003c populations.length) {\n car.brain = populations[currentTrain];\n car.reset(); \n } else {\n nextGeneration();\n currentTrain = 0;\n count++;\n }\n running = true;\n }\n});","created_at":"2018-03-14T15:54:51.948+08:00","updated_at":"2019-11-11T13:46:40.165+08:00","name":"急速賽車 Q-learning","language":"javascript","screenshot":{"url":"https://cdn6.koding.school/uploads/project/screenshot/24466/0999389cfcc4e0fa6c4ae33cdc279084.jpg"},"parent_id":9144,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":338,"hashid":"2pdsrzq9","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":463072,"file_name":"car.png","project_id":24466,"asset_id":24181,"created_at":"2018-03-14T15:54:51.955+08:00","updated_at":"2018-03-14T15:54:51.955+08:00"},{"id":463073,"file_name":"border.png","project_id":24466,"asset_id":24182,"created_at":"2018-03-14T15:54:51.957+08:00","updated_at":"2018-03-14T15:54:51.957+08:00"},{"id":463074,"file_name":"road.png","project_id":24466,"asset_id":24183,"created_at":"2018-03-14T15:54:51.958+08:00","updated_at":"2018-03-14T15:54:51.958+08:00"},{"id":463075,"file_name":"check.png","project_id":24466,"asset_id":24184,"created_at":"2018-03-14T15:54:51.960+08:00","updated_at":"2018-03-14T15:54:51.960+08:00"},{"id":463077,"file_name":"dot.jpg","project_id":24466,"asset_id":57180,"created_at":"2018-03-14T16:11:35.056+08:00","updated_at":"2018-03-14T16:11:35.056+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦