Datasets:
Formats:
imagefolder
Size:
< 1K
Commit
•
33faff4
1
Parent(s):
d637554
Upload folder using huggingface_hub
Browse files- AIController2D.gd +47 -0
- BallChase.csproj +1 -1
- BallChase.tscn +9 -4
- Player.gd +58 -128
- addons/godot_rl_agents/controller/ai_controller_2d.gd +76 -2
- addons/godot_rl_agents/controller/ai_controller_3d.gd +74 -2
- addons/godot_rl_agents/onnx/csharp/ONNXInference.cs +85 -75
- addons/godot_rl_agents/onnx/csharp/SessionConfigurator.cs +63 -57
- addons/godot_rl_agents/onnx/wrapper/ONNX_wrapper.gd +6 -1
- addons/godot_rl_agents/sensors/sensors_2d/ExampleRaycastSensor2D.tscn +1 -1
- addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.gd +7 -3
- addons/godot_rl_agents/sync.gd +15 -13
- project.godot +5 -5
AIController2D.gd
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
extends AIController2D
|
2 |
+
|
3 |
+
|
4 |
+
func _ready():
|
5 |
+
reset()
|
6 |
+
|
7 |
+
|
8 |
+
func _physics_process(_delta):
|
9 |
+
n_steps += 1
|
10 |
+
if n_steps > reset_after:
|
11 |
+
needs_reset = true
|
12 |
+
done = true
|
13 |
+
|
14 |
+
if needs_reset:
|
15 |
+
needs_reset = false
|
16 |
+
reset()
|
17 |
+
|
18 |
+
|
19 |
+
func get_reward():
|
20 |
+
return reward
|
21 |
+
|
22 |
+
|
23 |
+
func get_obs():
|
24 |
+
var relative = to_local(_player.get_fruit_position())
|
25 |
+
var distance = relative.length() / 1500.0
|
26 |
+
relative = relative.normalized()
|
27 |
+
var result := []
|
28 |
+
result.append(((position.x / _player.WIDTH) - 0.5) * 2)
|
29 |
+
result.append(((position.y / _player.HEIGHT) - 0.5) * 2)
|
30 |
+
result.append(relative.x)
|
31 |
+
result.append(relative.y)
|
32 |
+
result.append(distance)
|
33 |
+
var raycast_obs = _player.raycast_sensor.get_observation()
|
34 |
+
result.append_array(raycast_obs)
|
35 |
+
|
36 |
+
return {
|
37 |
+
"obs": result,
|
38 |
+
}
|
39 |
+
|
40 |
+
|
41 |
+
func set_action(action):
|
42 |
+
_player._action.x = action["move"][0]
|
43 |
+
_player._action.y = action["move"][1]
|
44 |
+
|
45 |
+
|
46 |
+
func get_action_space():
|
47 |
+
return {"move": {"size": 2, "action_type": "continuous"}}
|
BallChase.csproj
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
<Project Sdk="Godot.NET.Sdk/4.1.
|
2 |
<PropertyGroup>
|
3 |
<TargetFramework>net6.0</TargetFramework>
|
4 |
<EnableDynamicLoading>true</EnableDynamicLoading>
|
|
|
1 |
+
<Project Sdk="Godot.NET.Sdk/4.1.3">
|
2 |
<PropertyGroup>
|
3 |
<TargetFramework>net6.0</TargetFramework>
|
4 |
<EnableDynamicLoading>true</EnableDynamicLoading>
|
BallChase.tscn
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
-
[gd_scene load_steps=
|
2 |
|
3 |
[ext_resource type="Script" path="res://Player.gd" id="1"]
|
4 |
[ext_resource type="Script" path="res://addons/godot_rl_agents/sensors/sensors_2d/RaycastSensor2D.gd" id="2"]
|
5 |
-
[ext_resource type="Texture2D" uid="uid://
|
6 |
-
[ext_resource type="Texture2D" uid="uid://
|
|
|
7 |
|
8 |
[sub_resource type="CircleShape2D" id="1"]
|
9 |
radius = 30.0
|
@@ -41,7 +42,7 @@ glow_blend_mode = 0
|
|
41 |
|
42 |
[node name="BallChase" type="Node2D"]
|
43 |
|
44 |
-
[node name="Player" type="CharacterBody2D" parent="."
|
45 |
position = Vector2(281.728, 162.943)
|
46 |
script = ExtResource("1")
|
47 |
|
@@ -56,6 +57,10 @@ collide_with_areas = true
|
|
56 |
scale = Vector2(0.9, 0.9)
|
57 |
texture = ExtResource("3_ty2qt")
|
58 |
|
|
|
|
|
|
|
|
|
59 |
[node name="Fruit" type="Area2D" parent="."]
|
60 |
position = Vector2(1095.01, 578.224)
|
61 |
collision_layer = 2
|
|
|
1 |
+
[gd_scene load_steps=14 format=3 uid="uid://bj8j0v5unredq"]
|
2 |
|
3 |
[ext_resource type="Script" path="res://Player.gd" id="1"]
|
4 |
[ext_resource type="Script" path="res://addons/godot_rl_agents/sensors/sensors_2d/RaycastSensor2D.gd" id="2"]
|
5 |
+
[ext_resource type="Texture2D" uid="uid://3wc0oii4d560" path="res://cherry.png" id="3_25acu"]
|
6 |
+
[ext_resource type="Texture2D" uid="uid://cje1ar3xnl01x" path="res://lollipopGreen.png" id="3_ty2qt"]
|
7 |
+
[ext_resource type="Script" path="res://AIController2D.gd" id="4_hrp2t"]
|
8 |
|
9 |
[sub_resource type="CircleShape2D" id="1"]
|
10 |
radius = 30.0
|
|
|
42 |
|
43 |
[node name="BallChase" type="Node2D"]
|
44 |
|
45 |
+
[node name="Player" type="CharacterBody2D" parent="."]
|
46 |
position = Vector2(281.728, 162.943)
|
47 |
script = ExtResource("1")
|
48 |
|
|
|
57 |
scale = Vector2(0.9, 0.9)
|
58 |
texture = ExtResource("3_ty2qt")
|
59 |
|
60 |
+
[node name="AIController2D" type="Node2D" parent="Player" groups=["AGENT"]]
|
61 |
+
script = ExtResource("4_hrp2t")
|
62 |
+
reset_after = 20000
|
63 |
+
|
64 |
[node name="Fruit" type="Area2D" parent="."]
|
65 |
position = Vector2(1095.01, 578.224)
|
66 |
collision_layer = 2
|
Player.gd
CHANGED
@@ -1,49 +1,32 @@
|
|
1 |
extends CharacterBody2D
|
2 |
|
3 |
-
|
4 |
-
# Declare member variables here. Examples:
|
5 |
-
# var a = 2
|
6 |
-
# var b = "text"
|
7 |
const pad = 100
|
8 |
const WIDTH = 1280
|
9 |
const HEIGHT = 720
|
10 |
const MAX_FRUIT = 10
|
11 |
-
var _bounds := Rect2(pad,pad,WIDTH-2*pad,HEIGHT-2*pad)
|
12 |
|
13 |
@export var speed := 500
|
14 |
@export var friction = 0.18
|
15 |
var _velocity := Vector2.ZERO
|
16 |
var _action = Vector2.ZERO
|
17 |
-
var _heuristic = "player"
|
18 |
@onready var fruit = $"../Fruit"
|
19 |
@onready var raycast_sensor = $"RaycastSensor2D"
|
20 |
@onready var walls := $"../Walls"
|
21 |
@onready var colision_shape := $"CollisionShape2D"
|
|
|
22 |
var fruit_just_entered = false
|
23 |
var just_hit_wall = false
|
24 |
-
var done = false
|
25 |
var best_fruit_distance = 10000.0
|
26 |
var fruit_count = 0
|
27 |
-
|
28 |
-
var MAX_STEPS = 20000
|
29 |
-
var needs_reset = false
|
30 |
-
var reward = 0.0
|
31 |
|
32 |
func _ready():
|
|
|
33 |
raycast_sensor.activate()
|
34 |
-
|
35 |
-
|
36 |
-
func _physics_process(
|
37 |
-
n_steps +=1
|
38 |
-
if n_steps >= MAX_STEPS:
|
39 |
-
done = true
|
40 |
-
needs_reset = true
|
41 |
-
|
42 |
-
if needs_reset:
|
43 |
-
needs_reset = false
|
44 |
-
reset()
|
45 |
-
return
|
46 |
-
|
47 |
var direction = get_direction()
|
48 |
if direction.length() > 1.0:
|
49 |
direction = direction.normalized()
|
@@ -53,169 +36,116 @@ func _physics_process(delta):
|
|
53 |
set_velocity(_velocity)
|
54 |
move_and_slide()
|
55 |
_velocity = velocity
|
56 |
-
|
57 |
update_reward()
|
58 |
-
|
59 |
if Input.is_action_just_pressed("r_key"):
|
60 |
-
|
61 |
|
62 |
-
|
63 |
-
|
64 |
fruit_just_entered = false
|
65 |
just_hit_wall = false
|
66 |
-
#done = false
|
67 |
fruit_count = 0
|
68 |
_velocity = Vector2.ZERO
|
69 |
_action = Vector2.ZERO
|
70 |
position = _calculate_new_position()
|
71 |
spawn_fruit()
|
72 |
-
|
73 |
-
# position.x = randf_range(_bounds.position.x, _bounds.end.x)
|
74 |
-
# position.y = randf_range(_bounds.position.y, _bounds.end.y)
|
75 |
-
# fruit.position.x = randf_range(_bounds.position.x, _bounds.end.x)
|
76 |
-
# fruit.position.y = randf_range(_bounds.position.y, _bounds.end.y)
|
77 |
best_fruit_distance = position.distance_to(fruit.position)
|
78 |
-
|
|
|
79 |
|
80 |
-
func _calculate_new_position(
|
81 |
var new_position := Vector2.ZERO
|
82 |
new_position.x = randf_range(_bounds.position.x, _bounds.end.x)
|
83 |
-
new_position.y = randf_range(_bounds.position.y, _bounds.end.y)
|
84 |
-
|
85 |
-
if (
|
86 |
-
return _calculate_new_position(
|
87 |
|
88 |
var radius = colision_shape.shape.get_radius()
|
89 |
-
var rect = Rect2(new_position-Vector2(radius, radius),
|
90 |
-
Vector2(radius*2, radius*2)
|
91 |
-
)
|
92 |
for wall in walls.get_children():
|
93 |
#wall = wall as Area2D
|
94 |
var cr = wall.get_node("ColorRect")
|
95 |
-
var rect2 = Rect2(cr.get_position()+wall.position, cr.get_size())
|
96 |
if rect.intersects(rect2):
|
97 |
return _calculate_new_position()
|
98 |
-
|
99 |
return new_position
|
100 |
-
|
101 |
-
|
102 |
func get_direction():
|
103 |
-
if done:
|
104 |
_velocity = Vector2.ZERO
|
105 |
return Vector2.ZERO
|
106 |
-
|
107 |
-
if
|
108 |
return _action
|
109 |
-
|
110 |
var direction := Vector2(
|
111 |
Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
|
112 |
Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
|
113 |
)
|
114 |
-
|
115 |
return direction
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
if done:
|
123 |
-
reset()
|
124 |
-
|
125 |
-
func get_obs():
|
126 |
-
var relative = fruit.position - position
|
127 |
-
var distance = relative.length() / 1500.0
|
128 |
-
relative = relative.normalized()
|
129 |
-
var result := []
|
130 |
-
result.append(((position.x / WIDTH)-0.5) * 2)
|
131 |
-
result.append(((position.y / HEIGHT)-0.5) * 2)
|
132 |
-
result.append(relative.x)
|
133 |
-
result.append(relative.y)
|
134 |
-
result.append(distance)
|
135 |
-
var raycast_obs = raycast_sensor.get_observation()
|
136 |
-
result.append_array(raycast_obs)
|
137 |
-
|
138 |
-
return {
|
139 |
-
"obs": result,
|
140 |
-
}
|
141 |
-
|
142 |
-
|
143 |
func update_reward():
|
144 |
-
reward -= 0.01
|
145 |
-
reward += shaping_reward()
|
146 |
-
|
147 |
-
func zero_reward():
|
148 |
-
reward = 0.0
|
149 |
|
150 |
-
func get_reward():
|
151 |
-
return reward
|
152 |
|
153 |
-
|
154 |
func shaping_reward():
|
155 |
var s_reward = 0.0
|
156 |
var fruit_distance = position.distance_to(fruit.position)
|
157 |
-
|
158 |
if fruit_distance < best_fruit_distance:
|
159 |
s_reward += best_fruit_distance - fruit_distance
|
160 |
best_fruit_distance = fruit_distance
|
161 |
-
|
162 |
s_reward /= 100.0
|
163 |
return s_reward
|
164 |
-
|
165 |
-
func set_heuristic(heuristic):
|
166 |
-
self._heuristic = heuristic
|
167 |
-
|
168 |
-
func get_obs_space():
|
169 |
-
# typs of obs space: box, discrete, repeated
|
170 |
-
return {
|
171 |
-
"obs": {
|
172 |
-
"size": [len(get_obs()["obs"])],
|
173 |
-
"space": "box"
|
174 |
-
}
|
175 |
-
}
|
176 |
-
|
177 |
-
func get_action_space():
|
178 |
-
return {
|
179 |
-
"move" : {
|
180 |
-
"size": 2,
|
181 |
-
"action_type": "continuous"
|
182 |
-
}
|
183 |
-
}
|
184 |
-
|
185 |
-
func get_done():
|
186 |
-
return done
|
187 |
-
|
188 |
-
func set_done_false():
|
189 |
-
done = false
|
190 |
|
191 |
func spawn_fruit():
|
192 |
fruit.position = _calculate_new_position(position)
|
193 |
best_fruit_distance = position.distance_to(fruit.position)
|
194 |
|
|
|
195 |
func fruit_collected():
|
196 |
fruit_just_entered = true
|
197 |
-
reward += 10.0
|
198 |
fruit_count += 1
|
199 |
spawn_fruit()
|
200 |
-
# if fruit_count > MAX_FRUIT:
|
201 |
-
# done = true
|
202 |
|
203 |
-
|
204 |
func wall_hit():
|
205 |
-
done = true
|
206 |
-
reward -= 10.0
|
207 |
just_hit_wall = true
|
208 |
-
|
209 |
|
210 |
-
|
|
|
211 |
fruit_collected()
|
212 |
|
213 |
|
214 |
-
func _on_LeftWall_body_entered(
|
215 |
wall_hit()
|
216 |
-
|
|
|
|
|
217 |
wall_hit()
|
218 |
-
|
|
|
|
|
219 |
wall_hit()
|
220 |
-
|
|
|
|
|
221 |
wall_hit()
|
|
|
1 |
extends CharacterBody2D
|
2 |
|
|
|
|
|
|
|
|
|
3 |
const pad = 100
|
4 |
const WIDTH = 1280
|
5 |
const HEIGHT = 720
|
6 |
const MAX_FRUIT = 10
|
7 |
+
var _bounds := Rect2(pad, pad, WIDTH - 2 * pad, HEIGHT - 2 * pad)
|
8 |
|
9 |
@export var speed := 500
|
10 |
@export var friction = 0.18
|
11 |
var _velocity := Vector2.ZERO
|
12 |
var _action = Vector2.ZERO
|
|
|
13 |
@onready var fruit = $"../Fruit"
|
14 |
@onready var raycast_sensor = $"RaycastSensor2D"
|
15 |
@onready var walls := $"../Walls"
|
16 |
@onready var colision_shape := $"CollisionShape2D"
|
17 |
+
@onready var ai_controller := $AIController2D
|
18 |
var fruit_just_entered = false
|
19 |
var just_hit_wall = false
|
|
|
20 |
var best_fruit_distance = 10000.0
|
21 |
var fruit_count = 0
|
22 |
+
|
|
|
|
|
|
|
23 |
|
24 |
func _ready():
|
25 |
+
ai_controller.init(self)
|
26 |
raycast_sensor.activate()
|
27 |
+
|
28 |
+
|
29 |
+
func _physics_process(_delta):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
var direction = get_direction()
|
31 |
if direction.length() > 1.0:
|
32 |
direction = direction.normalized()
|
|
|
36 |
set_velocity(_velocity)
|
37 |
move_and_slide()
|
38 |
_velocity = velocity
|
39 |
+
|
40 |
update_reward()
|
41 |
+
|
42 |
if Input.is_action_just_pressed("r_key"):
|
43 |
+
game_over()
|
44 |
|
45 |
+
|
46 |
+
func game_over():
|
47 |
fruit_just_entered = false
|
48 |
just_hit_wall = false
|
|
|
49 |
fruit_count = 0
|
50 |
_velocity = Vector2.ZERO
|
51 |
_action = Vector2.ZERO
|
52 |
position = _calculate_new_position()
|
53 |
spawn_fruit()
|
|
|
|
|
|
|
|
|
|
|
54 |
best_fruit_distance = position.distance_to(fruit.position)
|
55 |
+
ai_controller.reset()
|
56 |
+
|
57 |
|
58 |
+
func _calculate_new_position(current_position: Vector2 = Vector2.ZERO) -> Vector2:
|
59 |
var new_position := Vector2.ZERO
|
60 |
new_position.x = randf_range(_bounds.position.x, _bounds.end.x)
|
61 |
+
new_position.y = randf_range(_bounds.position.y, _bounds.end.y)
|
62 |
+
|
63 |
+
if (current_position - new_position).length() < 4.0 * colision_shape.shape.get_radius():
|
64 |
+
return _calculate_new_position(current_position)
|
65 |
|
66 |
var radius = colision_shape.shape.get_radius()
|
67 |
+
var rect = Rect2(new_position - Vector2(radius, radius), Vector2(radius * 2, radius * 2))
|
|
|
|
|
68 |
for wall in walls.get_children():
|
69 |
#wall = wall as Area2D
|
70 |
var cr = wall.get_node("ColorRect")
|
71 |
+
var rect2 = Rect2(cr.get_position() + wall.position, cr.get_size())
|
72 |
if rect.intersects(rect2):
|
73 |
return _calculate_new_position()
|
74 |
+
|
75 |
return new_position
|
76 |
+
|
77 |
+
|
78 |
func get_direction():
|
79 |
+
if ai_controller.done:
|
80 |
_velocity = Vector2.ZERO
|
81 |
return Vector2.ZERO
|
82 |
+
|
83 |
+
if ai_controller.heuristic == "model":
|
84 |
return _action
|
85 |
+
|
86 |
var direction := Vector2(
|
87 |
Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
|
88 |
Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
|
89 |
)
|
90 |
+
|
91 |
return direction
|
92 |
+
|
93 |
+
|
94 |
+
func get_fruit_position():
|
95 |
+
return $"../Fruit".global_position
|
96 |
+
|
97 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
func update_reward():
|
99 |
+
ai_controller.reward -= 0.01 # step penalty
|
100 |
+
ai_controller.reward += shaping_reward()
|
|
|
|
|
|
|
101 |
|
|
|
|
|
102 |
|
|
|
103 |
func shaping_reward():
|
104 |
var s_reward = 0.0
|
105 |
var fruit_distance = position.distance_to(fruit.position)
|
106 |
+
|
107 |
if fruit_distance < best_fruit_distance:
|
108 |
s_reward += best_fruit_distance - fruit_distance
|
109 |
best_fruit_distance = fruit_distance
|
110 |
+
|
111 |
s_reward /= 100.0
|
112 |
return s_reward
|
113 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
func spawn_fruit():
|
116 |
fruit.position = _calculate_new_position(position)
|
117 |
best_fruit_distance = position.distance_to(fruit.position)
|
118 |
|
119 |
+
|
120 |
func fruit_collected():
|
121 |
fruit_just_entered = true
|
122 |
+
ai_controller.reward += 10.0
|
123 |
fruit_count += 1
|
124 |
spawn_fruit()
|
|
|
|
|
125 |
|
126 |
+
|
127 |
func wall_hit():
|
128 |
+
ai_controller.done = true
|
129 |
+
ai_controller.reward -= 10.0
|
130 |
just_hit_wall = true
|
131 |
+
game_over()
|
132 |
|
133 |
+
|
134 |
+
func _on_Fruit_body_entered(_body):
|
135 |
fruit_collected()
|
136 |
|
137 |
|
138 |
+
func _on_LeftWall_body_entered(_body):
|
139 |
wall_hit()
|
140 |
+
|
141 |
+
|
142 |
+
func _on_RightWall_body_entered(_body):
|
143 |
wall_hit()
|
144 |
+
|
145 |
+
|
146 |
+
func _on_TopWall_body_entered(_body):
|
147 |
wall_hit()
|
148 |
+
|
149 |
+
|
150 |
+
func _on_BottomWall_body_entered(_body):
|
151 |
wall_hit()
|
addons/godot_rl_agents/controller/ai_controller_2d.gd
CHANGED
@@ -1,8 +1,82 @@
|
|
1 |
-
extends
|
2 |
class_name AIController2D
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
var _player: Node2D
|
6 |
|
|
|
|
|
|
|
7 |
func init(player: Node2D):
|
8 |
_player = player
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
extends Node2D
|
2 |
class_name AIController2D
|
3 |
|
4 |
+
@export var reset_after := 1000
|
5 |
+
|
6 |
+
var heuristic := "human"
|
7 |
+
var done := false
|
8 |
+
var reward := 0.0
|
9 |
+
var n_steps := 0
|
10 |
+
var needs_reset := false
|
11 |
+
|
12 |
var _player: Node2D
|
13 |
|
14 |
+
func _ready():
|
15 |
+
add_to_group("AGENT")
|
16 |
+
|
17 |
func init(player: Node2D):
|
18 |
_player = player
|
19 |
+
|
20 |
+
#-- Methods that need implementing using the "extend script" option in Godot --#
|
21 |
+
func get_obs() -> Dictionary:
|
22 |
+
assert(false, "the get_obs method is not implemented when extending from ai_controller")
|
23 |
+
return {"obs":[]}
|
24 |
+
|
25 |
+
func get_reward() -> float:
|
26 |
+
assert(false, "the get_reward method is not implemented when extending from ai_controller")
|
27 |
+
return 0.0
|
28 |
+
|
29 |
+
func get_action_space() -> Dictionary:
|
30 |
+
assert(false, "the get get_action_space method is not implemented when extending from ai_controller")
|
31 |
+
return {
|
32 |
+
"example_actions_continous" : {
|
33 |
+
"size": 2,
|
34 |
+
"action_type": "continuous"
|
35 |
+
},
|
36 |
+
"example_actions_discrete" : {
|
37 |
+
"size": 2,
|
38 |
+
"action_type": "discrete"
|
39 |
+
},
|
40 |
+
}
|
41 |
+
|
42 |
+
func set_action(action) -> void:
|
43 |
+
assert(false, "the get set_action method is not implemented when extending from ai_controller")
|
44 |
+
# -----------------------------------------------------------------------------#
|
45 |
+
|
46 |
+
func _physics_process(delta):
|
47 |
+
n_steps += 1
|
48 |
+
if n_steps > reset_after:
|
49 |
+
needs_reset = true
|
50 |
+
|
51 |
+
func get_obs_space():
|
52 |
+
# may need overriding if the obs space is complex
|
53 |
+
var obs = get_obs()
|
54 |
+
return {
|
55 |
+
"obs": {
|
56 |
+
"size": [len(obs["obs"])],
|
57 |
+
"space": "box"
|
58 |
+
},
|
59 |
+
}
|
60 |
+
|
61 |
+
func reset():
|
62 |
+
n_steps = 0
|
63 |
+
needs_reset = false
|
64 |
+
|
65 |
+
func reset_if_done():
|
66 |
+
if done:
|
67 |
+
reset()
|
68 |
+
|
69 |
+
func set_heuristic(h):
|
70 |
+
# sets the heuristic from "human" or "model" nothing to change here
|
71 |
+
heuristic = h
|
72 |
+
|
73 |
+
func get_done():
|
74 |
+
return done
|
75 |
+
|
76 |
+
func set_done_false():
|
77 |
+
done = false
|
78 |
+
|
79 |
+
func zero_reward():
|
80 |
+
reward = 0.0
|
81 |
+
|
82 |
+
|
addons/godot_rl_agents/controller/ai_controller_3d.gd
CHANGED
@@ -1,8 +1,80 @@
|
|
1 |
-
extends
|
2 |
class_name AIController3D
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
var _player: Node3D
|
6 |
|
|
|
|
|
|
|
7 |
func init(player: Node3D):
|
8 |
_player = player
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
extends Node3D
|
2 |
class_name AIController3D
|
3 |
|
4 |
+
@export var reset_after := 1000
|
5 |
+
|
6 |
+
var heuristic := "human"
|
7 |
+
var done := false
|
8 |
+
var reward := 0.0
|
9 |
+
var n_steps := 0
|
10 |
+
var needs_reset := false
|
11 |
+
|
12 |
var _player: Node3D
|
13 |
|
14 |
+
func _ready():
|
15 |
+
add_to_group("AGENT")
|
16 |
+
|
17 |
func init(player: Node3D):
|
18 |
_player = player
|
19 |
+
|
20 |
+
#-- Methods that need implementing using the "extend script" option in Godot --#
|
21 |
+
func get_obs() -> Dictionary:
|
22 |
+
assert(false, "the get_obs method is not implemented when extending from ai_controller")
|
23 |
+
return {"obs":[]}
|
24 |
+
|
25 |
+
func get_reward() -> float:
|
26 |
+
assert(false, "the get_reward method is not implemented when extending from ai_controller")
|
27 |
+
return 0.0
|
28 |
+
|
29 |
+
func get_action_space() -> Dictionary:
|
30 |
+
assert(false, "the get get_action_space method is not implemented when extending from ai_controller")
|
31 |
+
return {
|
32 |
+
"example_actions_continous" : {
|
33 |
+
"size": 2,
|
34 |
+
"action_type": "continuous"
|
35 |
+
},
|
36 |
+
"example_actions_discrete" : {
|
37 |
+
"size": 2,
|
38 |
+
"action_type": "discrete"
|
39 |
+
},
|
40 |
+
}
|
41 |
+
|
42 |
+
func set_action(action) -> void:
|
43 |
+
assert(false, "the get set_action method is not implemented when extending from ai_controller")
|
44 |
+
# -----------------------------------------------------------------------------#
|
45 |
+
|
46 |
+
func _physics_process(delta):
|
47 |
+
n_steps += 1
|
48 |
+
if n_steps > reset_after:
|
49 |
+
needs_reset = true
|
50 |
+
|
51 |
+
func get_obs_space():
|
52 |
+
# may need overriding if the obs space is complex
|
53 |
+
var obs = get_obs()
|
54 |
+
return {
|
55 |
+
"obs": {
|
56 |
+
"size": [len(obs["obs"])],
|
57 |
+
"space": "box"
|
58 |
+
},
|
59 |
+
}
|
60 |
+
|
61 |
+
func reset():
|
62 |
+
n_steps = 0
|
63 |
+
needs_reset = false
|
64 |
+
|
65 |
+
func reset_if_done():
|
66 |
+
if done:
|
67 |
+
reset()
|
68 |
+
|
69 |
+
func set_heuristic(h):
|
70 |
+
# sets the heuristic from "human" or "model" nothing to change here
|
71 |
+
heuristic = h
|
72 |
+
|
73 |
+
func get_done():
|
74 |
+
return done
|
75 |
+
|
76 |
+
func set_done_false():
|
77 |
+
done = false
|
78 |
+
|
79 |
+
func zero_reward():
|
80 |
+
reward = 0.0
|
addons/godot_rl_agents/onnx/csharp/ONNXInference.cs
CHANGED
@@ -1,93 +1,103 @@
|
|
1 |
using Godot;
|
2 |
-
using System.Collections.Generic;
|
3 |
-
using System.Linq;
|
4 |
using Microsoft.ML.OnnxRuntime;
|
5 |
using Microsoft.ML.OnnxRuntime.Tensors;
|
|
|
|
|
6 |
|
7 |
-
namespace GodotONNX
|
8 |
-
/// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/ONNXInference/*'/>
|
9 |
-
public partial class ONNXInference : Node
|
10 |
{
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
/// Path to the ONNX model. Use Initialize to change it.
|
15 |
-
/// </summary>
|
16 |
-
private string modelPath;
|
17 |
-
private int batchSize;
|
18 |
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
/// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/Initialize/*'/>
|
23 |
-
public void Initialize(string Path, int BatchSize)
|
24 |
-
{
|
25 |
-
modelPath = Path;
|
26 |
-
batchSize = BatchSize;
|
27 |
-
SessionConfigurator.SystemCheck();
|
28 |
-
SessionOpt = SessionConfigurator.GetSessionOptions();
|
29 |
-
session = LoadModel(modelPath);
|
30 |
|
31 |
-
|
32 |
-
/// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/
|
33 |
-
|
34 |
-
{
|
35 |
-
//Current model: Any (Godot Rl Agents)
|
36 |
-
//Expects a tensor of shape [batch_size, input_size] type float named obs and a tensor of shape [batch_size] type float named state_ins
|
37 |
-
|
38 |
-
//Fill the input tensors
|
39 |
-
// create span from inputSize
|
40 |
-
var span = new float[obs.Count]; //There's probably a better way to do this
|
41 |
-
for (int i = 0; i < obs.Count; i++)
|
42 |
{
|
43 |
-
|
|
|
|
|
|
|
|
|
44 |
}
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
{
|
48 |
NamedOnnxValue.CreateFromTensor("obs", new DenseTensor<float>(span, new int[] { batchSize, obs.Count })),
|
49 |
NamedOnnxValue.CreateFromTensor("state_ins", new DenseTensor<float>(new float[] { state_ins }, new int[] { batchSize }))
|
50 |
-
};
|
51 |
-
|
52 |
|
53 |
-
IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results;
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
}
|
93 |
-
}
|
|
|
1 |
using Godot;
|
|
|
|
|
2 |
using Microsoft.ML.OnnxRuntime;
|
3 |
using Microsoft.ML.OnnxRuntime.Tensors;
|
4 |
+
using System.Collections.Generic;
|
5 |
+
using System.Linq;
|
6 |
|
7 |
+
namespace GodotONNX
|
|
|
|
|
8 |
{
|
9 |
+
/// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/ONNXInference/*'/>
|
10 |
+
public partial class ONNXInference : GodotObject
|
11 |
+
{
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
private InferenceSession session;
|
14 |
+
/// <summary>
|
15 |
+
/// Path to the ONNX model. Use Initialize to change it.
|
16 |
+
/// </summary>
|
17 |
+
private string modelPath;
|
18 |
+
private int batchSize;
|
19 |
|
20 |
+
private SessionOptions SessionOpt;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
//init function
|
23 |
+
/// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/Initialize/*'/>
|
24 |
+
public void Initialize(string Path, int BatchSize)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
{
|
26 |
+
modelPath = Path;
|
27 |
+
batchSize = BatchSize;
|
28 |
+
SessionOpt = SessionConfigurator.MakeConfiguredSessionOptions();
|
29 |
+
session = LoadModel(modelPath);
|
30 |
+
|
31 |
}
|
32 |
+
/// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/Run/*'/>
|
33 |
+
public Godot.Collections.Dictionary<string, Godot.Collections.Array<float>> RunInference(Godot.Collections.Array<float> obs, int state_ins)
|
34 |
+
{
|
35 |
+
//Current model: Any (Godot Rl Agents)
|
36 |
+
//Expects a tensor of shape [batch_size, input_size] type float named obs and a tensor of shape [batch_size] type float named state_ins
|
37 |
+
|
38 |
+
//Fill the input tensors
|
39 |
+
// create span from inputSize
|
40 |
+
var span = new float[obs.Count]; //There's probably a better way to do this
|
41 |
+
for (int i = 0; i < obs.Count; i++)
|
42 |
+
{
|
43 |
+
span[i] = obs[i];
|
44 |
+
}
|
45 |
+
|
46 |
+
IReadOnlyCollection<NamedOnnxValue> inputs = new List<NamedOnnxValue>
|
47 |
{
|
48 |
NamedOnnxValue.CreateFromTensor("obs", new DenseTensor<float>(span, new int[] { batchSize, obs.Count })),
|
49 |
NamedOnnxValue.CreateFromTensor("state_ins", new DenseTensor<float>(new float[] { state_ins }, new int[] { batchSize }))
|
50 |
+
};
|
51 |
+
IReadOnlyCollection<string> outputNames = new List<string> { "output", "state_outs" }; //ONNX is sensible to these names, as well as the input names
|
52 |
|
53 |
+
IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results;
|
54 |
+
//We do not use "using" here so we get a better exception explaination later
|
55 |
+
try
|
56 |
+
{
|
57 |
+
results = session.Run(inputs, outputNames);
|
58 |
+
}
|
59 |
+
catch (OnnxRuntimeException e)
|
60 |
+
{
|
61 |
+
//This error usually means that the model is not compatible with the input, beacause of the input shape (size)
|
62 |
+
GD.Print("Error at inference: ", e);
|
63 |
+
return null;
|
64 |
+
}
|
65 |
+
//Can't convert IEnumerable<float> to Variant, so we have to convert it to an array or something
|
66 |
+
Godot.Collections.Dictionary<string, Godot.Collections.Array<float>> output = new Godot.Collections.Dictionary<string, Godot.Collections.Array<float>>();
|
67 |
+
DisposableNamedOnnxValue output1 = results.First();
|
68 |
+
DisposableNamedOnnxValue output2 = results.Last();
|
69 |
+
Godot.Collections.Array<float> output1Array = new Godot.Collections.Array<float>();
|
70 |
+
Godot.Collections.Array<float> output2Array = new Godot.Collections.Array<float>();
|
71 |
|
72 |
+
foreach (float f in output1.AsEnumerable<float>())
|
73 |
+
{
|
74 |
+
output1Array.Add(f);
|
75 |
+
}
|
76 |
|
77 |
+
foreach (float f in output2.AsEnumerable<float>())
|
78 |
+
{
|
79 |
+
output2Array.Add(f);
|
80 |
+
}
|
81 |
|
82 |
+
output.Add(output1.Name, output1Array);
|
83 |
+
output.Add(output2.Name, output2Array);
|
84 |
+
|
85 |
+
//Output is a dictionary of arrays, ex: { "output" : [0.1, 0.2, 0.3, 0.4, ...], "state_outs" : [0.5, ...]}
|
86 |
+
results.Dispose();
|
87 |
+
return output;
|
88 |
+
}
|
89 |
+
/// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/Load/*'/>
|
90 |
+
public InferenceSession LoadModel(string Path)
|
91 |
+
{
|
92 |
+
using Godot.FileAccess file = FileAccess.Open(Path, Godot.FileAccess.ModeFlags.Read);
|
93 |
+
byte[] model = file.GetBuffer((int)file.GetLength());
|
94 |
+
//file.Close(); file.Dispose(); //Close the file, then dispose the reference.
|
95 |
+
return new InferenceSession(model, SessionOpt); //Load the model
|
96 |
+
}
|
97 |
+
public void FreeDisposables()
|
98 |
+
{
|
99 |
+
session.Dispose();
|
100 |
+
SessionOpt.Dispose();
|
101 |
+
}
|
102 |
}
|
103 |
+
}
|
addons/godot_rl_agents/onnx/csharp/SessionConfigurator.cs
CHANGED
@@ -3,34 +3,35 @@ using Microsoft.ML.OnnxRuntime;
|
|
3 |
|
4 |
namespace GodotONNX
|
5 |
{
|
6 |
-
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
/// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/GetSessionOptions/*'/>
|
14 |
-
public static SessionOptions
|
15 |
{
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
SystemCheck();
|
20 |
-
return options;
|
21 |
}
|
22 |
|
23 |
-
|
24 |
{
|
25 |
-
|
26 |
-
|
27 |
-
DirectML,
|
28 |
-
CoreML,
|
29 |
-
CPU
|
30 |
}
|
31 |
|
32 |
/// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/SystemCheck/*'/>
|
33 |
-
static public void
|
34 |
{
|
35 |
//Most code for this function is verbose only, the only reason it exists is to track
|
36 |
//implementation progress of the different compute APIs.
|
@@ -38,52 +39,59 @@ namespace GodotONNX
|
|
38 |
//December 2022: CUDA is not working.
|
39 |
|
40 |
string OSName = OS.GetName(); //Get OS Name
|
41 |
-
|
42 |
-
|
|
|
43 |
|
44 |
//Linux can use OpenVINO (C#) on x64 and ROCm on x86 (GDNative/C++)
|
45 |
//Windows can use OpenVINO (C#) on x64
|
46 |
//TODO: try TensorRT instead of CUDA
|
47 |
//TODO: Use OpenVINO for Intel Graphics
|
48 |
|
|
|
|
|
|
|
49 |
//match OS and Compute API
|
50 |
-
|
51 |
-
GD.Print("OS: " + OSName, " | Compute API: " + ComputeAPI);
|
52 |
|
|
|
|
|
|
|
|
|
53 |
switch (OSName)
|
54 |
{
|
55 |
case "Windows": //Can use CUDA, DirectML
|
56 |
if (ComputeAPI is ComputeName.CUDA)
|
57 |
{
|
58 |
//CUDA
|
59 |
-
//
|
60 |
-
|
61 |
}
|
62 |
else if (ComputeAPI is ComputeName.DirectML)
|
63 |
{
|
64 |
//DirectML
|
65 |
-
|
66 |
}
|
67 |
break;
|
68 |
case "X11": //Can use CUDA, ROCm
|
69 |
if (ComputeAPI is ComputeName.CUDA)
|
70 |
{
|
71 |
//CUDA
|
72 |
-
//
|
73 |
}
|
74 |
if (ComputeAPI is ComputeName.ROCm)
|
75 |
{
|
76 |
//ROCm, only works on x86
|
77 |
//Research indicates that this has to be compiled as a GDNative plugin
|
78 |
-
GD.Print("ROCm not supported yet, using CPU.");
|
79 |
-
|
80 |
}
|
81 |
break;
|
82 |
-
case "
|
83 |
if (ComputeAPI is ComputeName.CoreML)
|
84 |
{ //CoreML
|
85 |
//TODO: Needs testing
|
86 |
-
|
87 |
//CoreML on ARM64, out of the box, on x64 needs .tar file from GitHub
|
88 |
}
|
89 |
break;
|
@@ -91,35 +99,33 @@ namespace GodotONNX
|
|
91 |
GD.Print("OS not Supported.");
|
92 |
break;
|
93 |
}
|
|
|
94 |
}
|
95 |
-
/// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/ComputeCheck/*'/>
|
96 |
|
|
|
|
|
97 |
public static ComputeName ComputeCheck()
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
{
|
117 |
-
return ComputeName.CUDA;
|
118 |
-
}
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
}
|
125 |
}
|
|
|
3 |
|
4 |
namespace GodotONNX
|
5 |
{
|
6 |
+
/// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/SessionConfigurator/*'/>
|
7 |
|
8 |
+
public static class SessionConfigurator
|
9 |
+
{
|
10 |
+
public enum ComputeName
|
11 |
+
{
|
12 |
+
CUDA,
|
13 |
+
ROCm,
|
14 |
+
DirectML,
|
15 |
+
CoreML,
|
16 |
+
CPU
|
17 |
+
}
|
18 |
|
19 |
/// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/GetSessionOptions/*'/>
|
20 |
+
public static SessionOptions MakeConfiguredSessionOptions()
|
21 |
{
|
22 |
+
SessionOptions sessionOptions = new();
|
23 |
+
SetOptions(sessionOptions);
|
24 |
+
return sessionOptions;
|
|
|
|
|
25 |
}
|
26 |
|
27 |
+
private static void SetOptions(SessionOptions sessionOptions)
|
28 |
{
|
29 |
+
sessionOptions.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_WARNING;
|
30 |
+
ApplySystemSpecificOptions(sessionOptions);
|
|
|
|
|
|
|
31 |
}
|
32 |
|
33 |
/// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/SystemCheck/*'/>
|
34 |
+
static public void ApplySystemSpecificOptions(SessionOptions sessionOptions)
|
35 |
{
|
36 |
//Most code for this function is verbose only, the only reason it exists is to track
|
37 |
//implementation progress of the different compute APIs.
|
|
|
39 |
//December 2022: CUDA is not working.
|
40 |
|
41 |
string OSName = OS.GetName(); //Get OS Name
|
42 |
+
|
43 |
+
//ComputeName ComputeAPI = ComputeCheck(); //Get Compute API
|
44 |
+
// //TODO: Get CPU architecture
|
45 |
|
46 |
//Linux can use OpenVINO (C#) on x64 and ROCm on x86 (GDNative/C++)
|
47 |
//Windows can use OpenVINO (C#) on x64
|
48 |
//TODO: try TensorRT instead of CUDA
|
49 |
//TODO: Use OpenVINO for Intel Graphics
|
50 |
|
51 |
+
// Temporarily using CPU on all platforms to avoid errors detected with DML
|
52 |
+
ComputeName ComputeAPI = ComputeName.CPU;
|
53 |
+
|
54 |
//match OS and Compute API
|
55 |
+
GD.Print($"OS: {OSName} Compute API: {ComputeAPI}");
|
|
|
56 |
|
57 |
+
// CPU is set by default without appending necessary
|
58 |
+
// sessionOptions.AppendExecutionProvider_CPU(0);
|
59 |
+
|
60 |
+
/*
|
61 |
switch (OSName)
|
62 |
{
|
63 |
case "Windows": //Can use CUDA, DirectML
|
64 |
if (ComputeAPI is ComputeName.CUDA)
|
65 |
{
|
66 |
//CUDA
|
67 |
+
//sessionOptions.AppendExecutionProvider_CUDA(0);
|
68 |
+
//sessionOptions.AppendExecutionProvider_DML(0);
|
69 |
}
|
70 |
else if (ComputeAPI is ComputeName.DirectML)
|
71 |
{
|
72 |
//DirectML
|
73 |
+
//sessionOptions.AppendExecutionProvider_DML(0);
|
74 |
}
|
75 |
break;
|
76 |
case "X11": //Can use CUDA, ROCm
|
77 |
if (ComputeAPI is ComputeName.CUDA)
|
78 |
{
|
79 |
//CUDA
|
80 |
+
//sessionOptions.AppendExecutionProvider_CUDA(0);
|
81 |
}
|
82 |
if (ComputeAPI is ComputeName.ROCm)
|
83 |
{
|
84 |
//ROCm, only works on x86
|
85 |
//Research indicates that this has to be compiled as a GDNative plugin
|
86 |
+
//GD.Print("ROCm not supported yet, using CPU.");
|
87 |
+
//sessionOptions.AppendExecutionProvider_CPU(0);
|
88 |
}
|
89 |
break;
|
90 |
+
case "macOS": //Can use CoreML
|
91 |
if (ComputeAPI is ComputeName.CoreML)
|
92 |
{ //CoreML
|
93 |
//TODO: Needs testing
|
94 |
+
//sessionOptions.AppendExecutionProvider_CoreML(0);
|
95 |
//CoreML on ARM64, out of the box, on x64 needs .tar file from GitHub
|
96 |
}
|
97 |
break;
|
|
|
99 |
GD.Print("OS not Supported.");
|
100 |
break;
|
101 |
}
|
102 |
+
*/
|
103 |
}
|
|
|
104 |
|
105 |
+
|
106 |
+
/// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/ComputeCheck/*'/>
|
107 |
public static ComputeName ComputeCheck()
|
108 |
+
{
|
109 |
+
string adapterName = Godot.RenderingServer.GetVideoAdapterName();
|
110 |
+
//string adapterVendor = Godot.RenderingServer.GetVideoAdapterVendor();
|
111 |
+
adapterName = adapterName.ToUpper(new System.Globalization.CultureInfo(""));
|
112 |
+
//TODO: GPU vendors for MacOS, what do they even use these days?
|
113 |
+
|
114 |
+
if (adapterName.Contains("INTEL"))
|
115 |
+
{
|
116 |
+
return ComputeName.DirectML;
|
117 |
+
}
|
118 |
+
if (adapterName.Contains("AMD") || adapterName.Contains("RADEON"))
|
119 |
+
{
|
120 |
+
return ComputeName.DirectML;
|
121 |
+
}
|
122 |
+
if (adapterName.Contains("NVIDIA"))
|
123 |
+
{
|
124 |
+
return ComputeName.CUDA;
|
125 |
+
}
|
|
|
|
|
|
|
126 |
|
127 |
+
GD.Print("Graphics Card not recognized."); //Should use CPU
|
128 |
+
return ComputeName.CPU;
|
129 |
+
}
|
130 |
+
}
|
|
|
131 |
}
|
addons/godot_rl_agents/onnx/wrapper/ONNX_wrapper.gd
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
extends
|
2 |
class_name ONNXModel
|
3 |
var inferencer_script = load("res://addons/godot_rl_agents/onnx/csharp/ONNXInference.cs")
|
4 |
|
@@ -17,3 +17,8 @@ func run_inference(obs : Array, state_ins : int) -> Dictionary:
|
|
17 |
printerr("Inferencer not initialized")
|
18 |
return {}
|
19 |
return inferencer.RunInference(obs, state_ins)
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
extends Resource
|
2 |
class_name ONNXModel
|
3 |
var inferencer_script = load("res://addons/godot_rl_agents/onnx/csharp/ONNXInference.cs")
|
4 |
|
|
|
17 |
printerr("Inferencer not initialized")
|
18 |
return {}
|
19 |
return inferencer.RunInference(obs, state_ins)
|
20 |
+
|
21 |
+
func _notification(what):
|
22 |
+
if what == NOTIFICATION_PREDELETE:
|
23 |
+
inferencer.FreeDisposables()
|
24 |
+
inferencer.free()
|
addons/godot_rl_agents/sensors/sensors_2d/ExampleRaycastSensor2D.tscn
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
[gd_scene load_steps=5 format=3 uid="uid://ddeq7mn1ealyc"]
|
2 |
|
3 |
-
[ext_resource type="Script" path="res://sensors/sensors_2d/RaycastSensor2D.gd" id="1"]
|
4 |
|
5 |
[sub_resource type="GDScript" id="2"]
|
6 |
script/source = "extends Node2D
|
|
|
1 |
[gd_scene load_steps=5 format=3 uid="uid://ddeq7mn1ealyc"]
|
2 |
|
3 |
+
[ext_resource type="Script" path="res://addons/godot_rl_agents/sensors/sensors_2d/RaycastSensor2D.gd" id="1"]
|
4 |
|
5 |
[sub_resource type="GDScript" id="2"]
|
6 |
script/source = "extends Node2D
|
addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.gd
CHANGED
@@ -61,11 +61,15 @@ var geo = null
|
|
61 |
|
62 |
func _update():
|
63 |
if Engine.is_editor_hint():
|
64 |
-
|
65 |
-
|
66 |
|
67 |
func _ready() -> void:
|
68 |
-
|
|
|
|
|
|
|
|
|
69 |
|
70 |
func _spawn_nodes():
|
71 |
print("spawning nodes")
|
|
|
61 |
|
62 |
func _update():
|
63 |
if Engine.is_editor_hint():
|
64 |
+
if is_node_ready():
|
65 |
+
_spawn_nodes()
|
66 |
|
67 |
func _ready() -> void:
|
68 |
+
if Engine.is_editor_hint():
|
69 |
+
if get_child_count() == 0:
|
70 |
+
_spawn_nodes()
|
71 |
+
else:
|
72 |
+
_spawn_nodes()
|
73 |
|
74 |
func _spawn_nodes():
|
75 |
print("spawning nodes")
|
addons/godot_rl_agents/sync.gd
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
extends Node
|
2 |
# --fixed-fps 2000 --disable-render-loop
|
3 |
-
@
|
4 |
-
@
|
5 |
@export var onnx_model_path := ""
|
6 |
|
7 |
@onready var start_time = Time.get_ticks_msec()
|
@@ -10,7 +10,6 @@ const MAJOR_VERSION := "0"
|
|
10 |
const MINOR_VERSION := "3"
|
11 |
const DEFAULT_PORT := "11008"
|
12 |
const DEFAULT_SEED := "1"
|
13 |
-
const DEFAULT_ACTION_REPEAT := "8"
|
14 |
var stream : StreamPeerTCP = null
|
15 |
var connected = false
|
16 |
var message_center
|
@@ -44,17 +43,20 @@ func _initialize():
|
|
44 |
Engine.time_scale = _get_speedup() * 1.0
|
45 |
prints("physics ticks", Engine.physics_ticks_per_second, Engine.time_scale, _get_speedup(), speed_up)
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
if
|
50 |
-
|
51 |
-
_handshake()
|
52 |
-
_send_env_info()
|
53 |
-
elif onnx_model_path != "":
|
54 |
onnx_model = ONNXModel.new(onnx_model_path, 1)
|
55 |
_set_heuristic("model")
|
56 |
-
else:
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
_set_seed()
|
60 |
_set_action_repeat()
|
@@ -232,7 +234,7 @@ func _set_seed():
|
|
232 |
seed(_seed)
|
233 |
|
234 |
func _set_action_repeat():
|
235 |
-
action_repeat = args.get("action_repeat",
|
236 |
|
237 |
func disconnect_from_server():
|
238 |
stream.disconnect_from_host()
|
|
|
1 |
extends Node
|
2 |
# --fixed-fps 2000 --disable-render-loop
|
3 |
+
@export_range(1, 10, 1, "or_greater") var action_repeat := 8
|
4 |
+
@export_range(1, 10, 1, "or_greater") var speed_up = 1
|
5 |
@export var onnx_model_path := ""
|
6 |
|
7 |
@onready var start_time = Time.get_ticks_msec()
|
|
|
10 |
const MINOR_VERSION := "3"
|
11 |
const DEFAULT_PORT := "11008"
|
12 |
const DEFAULT_SEED := "1"
|
|
|
13 |
var stream : StreamPeerTCP = null
|
14 |
var connected = false
|
15 |
var message_center
|
|
|
43 |
Engine.time_scale = _get_speedup() * 1.0
|
44 |
prints("physics ticks", Engine.physics_ticks_per_second, Engine.time_scale, _get_speedup(), speed_up)
|
45 |
|
46 |
+
# Run inference if onnx model path is set, otherwise wait for server connection
|
47 |
+
var run_onnx_model_inference : bool = onnx_model_path != ""
|
48 |
+
if run_onnx_model_inference:
|
49 |
+
assert(FileAccess.file_exists(onnx_model_path), "Onnx Model Path set on Sync node does not exist: " + onnx_model_path)
|
|
|
|
|
|
|
50 |
onnx_model = ONNXModel.new(onnx_model_path, 1)
|
51 |
_set_heuristic("model")
|
52 |
+
else:
|
53 |
+
connected = connect_to_server()
|
54 |
+
if connected:
|
55 |
+
_set_heuristic("model")
|
56 |
+
_handshake()
|
57 |
+
_send_env_info()
|
58 |
+
else:
|
59 |
+
_set_heuristic("human")
|
60 |
|
61 |
_set_seed()
|
62 |
_set_action_repeat()
|
|
|
234 |
seed(_seed)
|
235 |
|
236 |
func _set_action_repeat():
|
237 |
+
action_repeat = args.get("action_repeat", str(action_repeat)).to_int()
|
238 |
|
239 |
func disconnect_from_server():
|
240 |
stream.disconnect_from_host()
|
project.godot
CHANGED
@@ -12,7 +12,7 @@ config_version=5
|
|
12 |
|
13 |
config/name="BallChase"
|
14 |
run/main_scene="res://BatchEnvs.tscn"
|
15 |
-
config/features=PackedStringArray("4.
|
16 |
config/icon="res://icon.png"
|
17 |
|
18 |
[display]
|
@@ -27,7 +27,7 @@ project/assembly_name="BallChase"
|
|
27 |
|
28 |
[editor_plugins]
|
29 |
|
30 |
-
enabled=PackedStringArray()
|
31 |
|
32 |
[input]
|
33 |
|
@@ -78,17 +78,17 @@ r_key={
|
|
78 |
}
|
79 |
zoom_out={
|
80 |
"deadzone": 0.5,
|
81 |
-
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":5,"pressed":false,"double_click":false,"script":null)
|
82 |
]
|
83 |
}
|
84 |
zoom_in={
|
85 |
"deadzone": 0.5,
|
86 |
-
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":4,"pressed":false,"double_click":false,"script":null)
|
87 |
]
|
88 |
}
|
89 |
reset_camera={
|
90 |
"deadzone": 0.5,
|
91 |
-
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":3,"pressed":false,"double_click":false,"script":null)
|
92 |
]
|
93 |
}
|
94 |
|
|
|
12 |
|
13 |
config/name="BallChase"
|
14 |
run/main_scene="res://BatchEnvs.tscn"
|
15 |
+
config/features=PackedStringArray("4.1", "C#")
|
16 |
config/icon="res://icon.png"
|
17 |
|
18 |
[display]
|
|
|
27 |
|
28 |
[editor_plugins]
|
29 |
|
30 |
+
enabled=PackedStringArray("res://addons/godot_rl_agents/plugin.cfg")
|
31 |
|
32 |
[input]
|
33 |
|
|
|
78 |
}
|
79 |
zoom_out={
|
80 |
"deadzone": 0.5,
|
81 |
+
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":5,"canceled":false,"pressed":false,"double_click":false,"script":null)
|
82 |
]
|
83 |
}
|
84 |
zoom_in={
|
85 |
"deadzone": 0.5,
|
86 |
+
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":4,"canceled":false,"pressed":false,"double_click":false,"script":null)
|
87 |
]
|
88 |
}
|
89 |
reset_camera={
|
90 |
"deadzone": 0.5,
|
91 |
+
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":3,"canceled":false,"pressed":false,"double_click":false,"script":null)
|
92 |
]
|
93 |
}
|
94 |
|