-
Notifications
You must be signed in to change notification settings - Fork 1
/
ppo_class_worker_main.js
68 lines (56 loc) · 2.73 KB
/
ppo_class_worker_main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import * as tf from '@tensorflow/tfjs'
tf.disableDeprecationWarnings();
import {FlatAreaEatWorld_c, Agent as FlatAgent} from "./src/jsm/envs/FlatAreaWorld/FlatAreaEatWorld_c"
import {TestWorld_c, Agent as TestAgent} from "./src/jsm/envs/TestWorld/TestWorld_c"
import {HungryWorld as HungryWorld2D, Agent as HungryAgent} from "./src/jsm/envs/HungryWorld/HungryWorld"
import {HungryWorld as HungryWorld3D, Agent as HungryAgent3D} from "./src/jsm/envs/World3D/HungryWorld3D"
import {build_full_connected} from './src/jsm/neuralnetworks';
import {get_serialized_layers_data, create_model_by_serialized_data} from './src/jsm/utils';
import {SimpleUI} from './src/jsm/ui/SimplePPOUI'
let curretWorldClass = HungryAgent2D;
let curAgent = HunterAgent;
var PPOworker = new Worker(new URL('./src/jsm/agents/policy_gradients/ppo_class_worker.js', import.meta.url), {type: 'module'});
var a = new curAgent({eyes_count: 10});
let cur_nn = build_full_connected(a.observation_space.shape, [128, 128], a.action_space.shape, 'tanh', 'tanh');
/*Create neural network*/
let weights_obj = get_serialized_layers_data(cur_nn);
/*Create UI*/
let ui = new SimpleUI({parent: document.body, policy_nn: cur_nn, worker: PPOworker});
/*Create environment*/
var w = new curretWorldClass({});
/*Adding agent to the environment object*/
w.addAgent(a);
PPOworker.onmessage = async function(e){
if(e.data.msg_type === "step"){
var step_data = w.step(e.data.action);
PPOworker.postMessage({msg_type: "step", step_data: step_data, n_obs: w.n_obs, e_r: w.get_episode_reward(), e_l: w.get_episode_length()});
}
/*When user recieves neural network weights from agent worker*/
if(e.data.msg_type === "get_policy_weights_answer"){
/*create neural network from serialized weights*/
let model_p = create_model_by_serialized_data(e.data.policy_weights);
/*downloading policy weights*/
model_p.save('downloads://policy');
/*Create neural network from serialized weights*/
let model_v = create_model_by_serialized_data(e.data.value_weights);
/*downloading value function weights*/
model_v.save('downloads://value');
}
/*Answer from worker that policy weights have been set*/
if(e.data.msg_type === "load_policy_weigths_by_path_answer"){
alert('Policy weights have been set');
}
/*Answer from worker that value function weights have been set*/
if(e.data.msg_type === "load_value_weigths_by_path_answer"){
alert('Value weights have been set');
}
}
tf.setBackend("webgl").then(()=>{
PPOworker.postMessage({
msg_type: "start",
observation_space: a.observation_space,
action_space: a.action_space,
n_obs: w.n_obs,
policy_nn: weights_obj
});
});