()
| 233 | } |
| 234 | |
| 235 | export async function setUpUI() { |
| 236 | const cartPole = new CartPole(true); |
| 237 | |
| 238 | if (await SaveablePolicyNetwork.checkStoredModelStatus() != null) { |
| 239 | policyNet = await SaveablePolicyNetwork.loadModel(); |
| 240 | logStatus('Loaded policy network from IndexedDB.'); |
| 241 | hiddenLayerSizesInput.value = policyNet.hiddenLayerSizes(); |
| 242 | } |
| 243 | await updateUIControlState(); |
| 244 | |
| 245 | renderDuringTrainingCheckbox.addEventListener('change', () => { |
| 246 | renderDuringTraining = renderDuringTrainingCheckbox.checked; |
| 247 | }); |
| 248 | |
| 249 | createModelButton.addEventListener('click', async () => { |
| 250 | try { |
| 251 | const hiddenLayerSizes = |
| 252 | hiddenLayerSizesInput.value.trim().split(',').map(v => { |
| 253 | const num = Number.parseInt(v.trim()); |
| 254 | if (!(num > 0)) { |
| 255 | throw new Error( |
| 256 | `Invalid hidden layer sizes string: ` + |
| 257 | `${hiddenLayerSizesInput.value}`); |
| 258 | } |
| 259 | return num; |
| 260 | }); |
| 261 | policyNet = new SaveablePolicyNetwork(hiddenLayerSizes); |
| 262 | console.log('DONE constructing new instance of SaveablePolicyNetwork'); |
| 263 | await updateUIControlState(); |
| 264 | } catch (err) { |
| 265 | logStatus(`ERROR: ${err.message}`); |
| 266 | } |
| 267 | }); |
| 268 | |
| 269 | deleteStoredModelButton.addEventListener('click', async () => { |
| 270 | if (confirm(`Are you sure you want to delete the locally-stored model?`)) { |
| 271 | await policyNet.removeModel(); |
| 272 | policyNet = null; |
| 273 | await updateUIControlState(); |
| 274 | } |
| 275 | }); |
| 276 | |
| 277 | trainButton.addEventListener('click', async () => { |
| 278 | if (trainButton.textContent === 'Stop') { |
| 279 | stopRequested = true; |
| 280 | } else { |
| 281 | disableModelControls(); |
| 282 | |
| 283 | try { |
| 284 | const trainIterations = Number.parseInt(numIterationsInput.value); |
| 285 | if (!(trainIterations > 0)) { |
| 286 | throw new Error(`Invalid number of iterations: ${trainIterations}`); |
| 287 | } |
| 288 | const gamesPerIteration = Number.parseInt(gamesPerIterationInput.value); |
| 289 | if (!(gamesPerIteration > 0)) { |
| 290 | throw new Error( |
| 291 | `Invalid # of games per iterations: ${gamesPerIteration}`); |
| 292 | } |
no test coverage detected