{"id":26068,"student_id":10,"content":"setBackdrop(\"bg.jpg\");\nvar road = createSprite(\"road.png\");\nvar car = createSprite(\"car_red.png\");\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(road)) {\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(road)) {\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-04-05T21:28:42.907+08:00","updated_at":"2019-10-23T16:22:26.356+08:00","name":"迷你賽車","language":"javascript","screenshot":{"url":"https://cdn5.koding.school/uploads/project/screenshot/26068/f8636a54d589bc48fcae2367c44e2169.jpg"},"parent_id":9072,"plugin":"","description":"遊戲重要規則:\n雙方玩家要有良心,不能穿牆不能折返跑!!\n\n紅色車車:左、右鍵操控\n藍色車車:a\b、d 鍵操控\n","note":"","status":"public","like_student_ids":[],"is_featured":false,"views":304,"hashid":"9ygsnmng","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":482206,"file_name":"road.png","project_id":26068,"asset_id":24105,"created_at":"2018-04-05T21:28:42.914+08:00","updated_at":"2018-04-05T21:28:42.914+08:00"},{"id":482207,"file_name":"bg.jpg","project_id":26068,"asset_id":24106,"created_at":"2018-04-05T21:28:42.916+08:00","updated_at":"2018-04-05T21:28:42.916+08:00"},{"id":482208,"file_name":"car_black.png","project_id":26068,"asset_id":24107,"created_at":"2018-04-05T21:28:42.917+08:00","updated_at":"2018-04-05T21:28:42.917+08:00"},{"id":482209,"file_name":"car_red.png","project_id":26068,"asset_id":24108,"created_at":"2018-04-05T21:28:42.919+08:00","updated_at":"2018-04-05T21:28:42.919+08:00"},{"id":482210,"file_name":"car_yellow.png","project_id":26068,"asset_id":24109,"created_at":"2018-04-05T21:28:42.920+08:00","updated_at":"2018-04-05T21:28:42.920+08:00"},{"id":482211,"file_name":"car_blue.png","project_id":26068,"asset_id":24110,"created_at":"2018-04-05T21:28:42.922+08:00","updated_at":"2018-04-05T21:28:42.922+08:00"},{"id":482212,"file_name":"end.png","project_id":26068,"asset_id":24111,"created_at":"2018-04-05T21:28:42.923+08:00","updated_at":"2018-04-05T21:28:42.923+08:00"},{"id":482213,"file_name":"dot.jpg","project_id":26068,"asset_id":57180,"created_at":"2018-04-05T21:28:46.905+08:00","updated_at":"2018-04-05T21:28:46.905+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦