rl-language 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rl_language-0.1.0/MANIFEST.in +2 -0
- rl_language-0.1.0/PKG-INFO +32 -0
- rl_language-0.1.0/bin/rlc +0 -0
- rl_language-0.1.0/bin/rlc-lsp +0 -0
- rl_language-0.1.0/command_line/__init__.py +15 -0
- rl_language-0.1.0/command_line/utils.py +93 -0
- rl_language-0.1.0/lib/libruntime.a +0 -0
- rl_language-0.1.0/lib/rlc/stdlib/.action.rl.swp +0 -0
- rl_language-0.1.0/lib/rlc/stdlib/.machine_learning.rl.swp +0 -0
- rl_language-0.1.0/lib/rlc/stdlib/.string.rl.swp +0 -0
- rl_language-0.1.0/lib/rlc/stdlib/action.rl +287 -0
- rl_language-0.1.0/lib/rlc/stdlib/algorithms/equal.rl +29 -0
- rl_language-0.1.0/lib/rlc/stdlib/bounded_arg.rl +143 -0
- rl_language-0.1.0/lib/rlc/stdlib/collections/.graph.rl.swp +0 -0
- rl_language-0.1.0/lib/rlc/stdlib/collections/.vector.rl.swp +0 -0
- rl_language-0.1.0/lib/rlc/stdlib/collections/graph.rl +179 -0
- rl_language-0.1.0/lib/rlc/stdlib/collections/hidden_information.rl +17 -0
- rl_language-0.1.0/lib/rlc/stdlib/collections/vector.rl +121 -0
- rl_language-0.1.0/lib/rlc/stdlib/machine_learning.rl +43 -0
- rl_language-0.1.0/lib/rlc/stdlib/math/numeric.rl +23 -0
- rl_language-0.1.0/lib/rlc/stdlib/none.rl +12 -0
- rl_language-0.1.0/lib/rlc/stdlib/serialization/print.rl +34 -0
- rl_language-0.1.0/lib/rlc/stdlib/serialization/to_byte_vector.rl +168 -0
- rl_language-0.1.0/lib/rlc/stdlib/string.rl +396 -0
- rl_language-0.1.0/loader/__init__.py +10 -0
- rl_language-0.1.0/loader/simulation.py +237 -0
- rl_language-0.1.0/ml/__init__.py +12 -0
- rl_language-0.1.0/ml/action_oracle.py +61 -0
- rl_language-0.1.0/ml/raylib/__init__.py +0 -0
- rl_language-0.1.0/ml/raylib/action_mask.py +87 -0
- rl_language-0.1.0/ml/raylib/environment.py +159 -0
- rl_language-0.1.0/ml/raylib/module_config.py +122 -0
- rl_language-0.1.0/ml/raylib/pebble_runner.py +462 -0
- rl_language-0.1.0/ml/raylib/raylib_runner.py +139 -0
- rl_language-0.1.0/ml/training_data.py +121 -0
- rl_language-0.1.0/ml/transformer.py +268 -0
- rl_language-0.1.0/ml/utils.py +16 -0
- rl_language-0.1.0/rl_language.egg-info/PKG-INFO +32 -0
- rl_language-0.1.0/rl_language.egg-info/SOURCES.txt +78 -0
- rl_language-0.1.0/rl_language.egg-info/dependency_links.txt +1 -0
- rl_language-0.1.0/rl_language.egg-info/entry_points.txt +3 -0
- rl_language-0.1.0/rl_language.egg-info/requires.txt +12 -0
- rl_language-0.1.0/rl_language.egg-info/top_level.txt +6 -0
- rl_language-0.1.0/setup.cfg +4 -0
- rl_language-0.1.0/setup.py +76 -0
- rl_language-0.1.0/solvers/__init__.py +11 -0
- rl_language-0.1.0/solvers/ender.py +59 -0
- rl_language-0.1.0/solvers/monte_carlo.py +103 -0
- rl_language-0.1.0/test/__init__.py +9 -0
- rl_language-0.1.0/test/example_test.py +11 -0
- rl_language-0.1.0/test/monte_carlo_test.py +91 -0
- rl_language-0.1.0/tool/__init__.py +0 -0
- rl_language-0.1.0/tool/action.py +113 -0
- rl_language-0.1.0/tool/disassembly.py +59 -0
- rl_language-0.1.0/tool/gen_dataset.py +71 -0
- rl_language-0.1.0/tool/learn.py +87 -0
- rl_language-0.1.0/tool/predict.py +75 -0
- rl_language-0.1.0/tool/print_dataset.py +45 -0
- rl_language-0.1.0/tool/rlc.py +9 -0
- rl_language-0.1.0/tool/rlc_lsp.py +10 -0
- rl_language-0.1.0/tool/solve.py +29 -0
- rl_language-0.1.0/tool/test.py +54 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: rl_language
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Author: Massimo Fioravanti
|
|
5
|
+
Author-email: massimo.fioravanti@polimi.it
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
8
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
9
|
+
Classifier: Operating System :: Unix
|
|
10
|
+
Classifier: Operating System :: POSIX
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Natural Language :: English
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Classifier: Topic :: Utilities
|
|
20
|
+
Requires-Python: >=3.9
|
|
21
|
+
Requires-Dist: vulture
|
|
22
|
+
Requires-Dist: pytest
|
|
23
|
+
Requires-Dist: pylint
|
|
24
|
+
Requires-Dist: torch
|
|
25
|
+
Requires-Dist: torchviz
|
|
26
|
+
Requires-Dist: numpy
|
|
27
|
+
Requires-Dist: ray[data,serve,train,tune]==2.12.0
|
|
28
|
+
Requires-Dist: hyperopt
|
|
29
|
+
Requires-Dist: gymnasium
|
|
30
|
+
Requires-Dist: dm_tree
|
|
31
|
+
Requires-Dist: typer
|
|
32
|
+
Requires-Dist: scikit-image
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#
|
|
2
|
+
# This file is part of the RLC project.
|
|
3
|
+
#
|
|
4
|
+
# RLC is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation.
|
|
5
|
+
#
|
|
6
|
+
# RLC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
7
|
+
#
|
|
8
|
+
# You should have received a copy of the GNU General Public License along with RLC. If not, see <https://www.gnu.org/licenses/>.
|
|
9
|
+
#
|
|
10
|
+
from .utils import (
|
|
11
|
+
make_rlc_argparse,
|
|
12
|
+
load_simulation_from_args,
|
|
13
|
+
load_dataset,
|
|
14
|
+
load_network,
|
|
15
|
+
)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
#
|
|
2
|
+
# This file is part of the RLC project.
|
|
3
|
+
#
|
|
4
|
+
# RLC is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation.
|
|
5
|
+
#
|
|
6
|
+
# RLC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
7
|
+
#
|
|
8
|
+
# You should have received a copy of the GNU General Public License along with RLC. If not, see <https://www.gnu.org/licenses/>.
|
|
9
|
+
#
|
|
10
|
+
from shutil import which
|
|
11
|
+
import argparse
|
|
12
|
+
from loader import Simulation, compile
|
|
13
|
+
import pickle
|
|
14
|
+
from ml import RLCTransformer
|
|
15
|
+
import torch
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def load_network(path, ntokens, device):
|
|
19
|
+
if path == "":
|
|
20
|
+
return RLCTransformer(ntokens=ntokens, device=device)
|
|
21
|
+
return torch.load(path)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def load_dataset(path):
|
|
25
|
+
to_return = []
|
|
26
|
+
with open(path, "rb") as f:
|
|
27
|
+
while True:
|
|
28
|
+
try:
|
|
29
|
+
loaded = pickle.load(f)
|
|
30
|
+
for (state, actions_and_scores) in loaded:
|
|
31
|
+
to_return.append((state, actions_and_scores))
|
|
32
|
+
except Exception as e:
|
|
33
|
+
break
|
|
34
|
+
return to_return
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def make_rlc_argparse(name, description):
|
|
38
|
+
parser = argparse.ArgumentParser(name, description=description)
|
|
39
|
+
parser.add_argument(
|
|
40
|
+
"--wrapper",
|
|
41
|
+
"-w",
|
|
42
|
+
type=str,
|
|
43
|
+
nargs="?",
|
|
44
|
+
help="path to wrapper to load",
|
|
45
|
+
default="",
|
|
46
|
+
)
|
|
47
|
+
parser.add_argument(
|
|
48
|
+
"--include",
|
|
49
|
+
"-i",
|
|
50
|
+
type=str,
|
|
51
|
+
nargs="*",
|
|
52
|
+
help="path to folder where rl files can be found",
|
|
53
|
+
default=[],
|
|
54
|
+
)
|
|
55
|
+
parser.add_argument(
|
|
56
|
+
"--rlc",
|
|
57
|
+
"-c",
|
|
58
|
+
type=str,
|
|
59
|
+
nargs="?",
|
|
60
|
+
help="path to rlc compiler",
|
|
61
|
+
default="rlc",
|
|
62
|
+
)
|
|
63
|
+
parser.add_argument(
|
|
64
|
+
"--source",
|
|
65
|
+
"-rl",
|
|
66
|
+
type=str,
|
|
67
|
+
nargs="?",
|
|
68
|
+
help="path to .rl source file",
|
|
69
|
+
)
|
|
70
|
+
parser.add_argument(
|
|
71
|
+
"--runtime",
|
|
72
|
+
"-rt",
|
|
73
|
+
type=str,
|
|
74
|
+
default="",
|
|
75
|
+
nargs="?",
|
|
76
|
+
help="path to runtime library",
|
|
77
|
+
)
|
|
78
|
+
return parser
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def load_simulation_from_args(args):
|
|
82
|
+
assert (
|
|
83
|
+
which(args.rlc) is not None
|
|
84
|
+
), "could not find executable {}, use --rlc <path_to_rlc> to configure it".format(
|
|
85
|
+
args.rlc
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
sim = (
|
|
89
|
+
Simulation(args.wrapper)
|
|
90
|
+
if args.wrapper != ""
|
|
91
|
+
else compile(args.source, args.rlc, args.include, args.runtime)
|
|
92
|
+
)
|
|
93
|
+
return sim
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
#
|
|
2
|
+
#Copyright 2024 Massimo Fioravanti
|
|
3
|
+
#
|
|
4
|
+
#Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
#you may not use this file except in compliance with the License.
|
|
6
|
+
#You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
#Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
#distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
#See the License for the specific language governing permissions and
|
|
14
|
+
#limitations under the License.
|
|
15
|
+
#
|
|
16
|
+
|
|
17
|
+
import collections.vector
|
|
18
|
+
import serialization.to_byte_vector
|
|
19
|
+
import serialization.print
|
|
20
|
+
import bounded_arg
|
|
21
|
+
import math.numeric
|
|
22
|
+
|
|
23
|
+
trait<FrameType, ActionType> ApplicableTo:
|
|
24
|
+
fun apply(ActionType action, FrameType frame)
|
|
25
|
+
|
|
26
|
+
fun<FrameType, ActionType> can_apply_impl(ActionType action, FrameType frame) -> Bool:
|
|
27
|
+
for alternative of action:
|
|
28
|
+
using Type = type(alternative)
|
|
29
|
+
if action is Type:
|
|
30
|
+
if action is ApplicableTo<FrameType>:
|
|
31
|
+
return can action.apply(frame)
|
|
32
|
+
return false
|
|
33
|
+
|
|
34
|
+
fun<FrameType, ActionType> apply(ActionType action, FrameType frame) { can_apply_impl(action, frame) }:
|
|
35
|
+
for alternative of action:
|
|
36
|
+
using Type = type(alternative)
|
|
37
|
+
if action is Type:
|
|
38
|
+
if action is ApplicableTo<FrameType>:
|
|
39
|
+
action.apply(frame)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
fun<FrameType, AllActionsVariant> parse_and_execute(FrameType state, AllActionsVariant variant, Vector<Byte> input, Int read_bytes):
|
|
43
|
+
while read_bytes + 8 <= input.size():
|
|
44
|
+
if from_byte_vector(variant, input, read_bytes):
|
|
45
|
+
if can apply(variant, state):
|
|
46
|
+
apply(variant, state)
|
|
47
|
+
|
|
48
|
+
fun<AllActionsVariant> parse_actions(AllActionsVariant variant, Vector<Byte> input, Int read_bytes) -> Vector<AllActionsVariant>:
|
|
49
|
+
let to_return : Vector<AllActionsVariant>
|
|
50
|
+
while read_bytes + 8 < input.size():
|
|
51
|
+
if from_byte_vector(variant, input, read_bytes):
|
|
52
|
+
to_return.append(variant)
|
|
53
|
+
return to_return
|
|
54
|
+
|
|
55
|
+
# parses actions taking only one byte and by taking taking the reminder of the parsed number divided by the number of actions, so that no action is ever marked as invalid
|
|
56
|
+
fun<AllActionsVariant> parse_action_optimized(AllActionsVariant variant, Vector<Byte> input, Int read_bytes) -> Bool:
|
|
57
|
+
let num_actions : Byte
|
|
58
|
+
for field of variant:
|
|
59
|
+
num_actions = num_actions + byte(1)
|
|
60
|
+
|
|
61
|
+
let counter : Byte
|
|
62
|
+
if !from_byte_vector(counter, input, read_bytes):
|
|
63
|
+
return false
|
|
64
|
+
|
|
65
|
+
counter = counter % num_actions
|
|
66
|
+
|
|
67
|
+
for field of variant:
|
|
68
|
+
if counter == byte(0):
|
|
69
|
+
using Type = type(field)
|
|
70
|
+
let to_parse : Type
|
|
71
|
+
if !from_byte_vector(to_parse, input, read_bytes):
|
|
72
|
+
return false
|
|
73
|
+
field = to_parse
|
|
74
|
+
return true
|
|
75
|
+
counter = counter - byte(1)
|
|
76
|
+
|
|
77
|
+
return false
|
|
78
|
+
|
|
79
|
+
fun<AllActionsVariant> parse_actions(AllActionsVariant variant, Vector<Byte> input) -> Vector<AllActionsVariant>:
|
|
80
|
+
return parse_actions(variant, input, 0)
|
|
81
|
+
|
|
82
|
+
fun<FrameType, AllActionsVariant> parse_and_execute(FrameType state, AllActionsVariant variant, Vector<Byte> input):
|
|
83
|
+
parse_and_execute(state, variant, input, 0)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
fun<FrameType, AllActionsVariant> gen_python_methods(FrameType state, AllActionsVariant variant):
|
|
87
|
+
let state : FrameType
|
|
88
|
+
let serialized = as_byte_vector(state)
|
|
89
|
+
from_byte_vector(state, serialized)
|
|
90
|
+
let x : AllActionsVariant
|
|
91
|
+
apply(x, state)
|
|
92
|
+
to_string(state)
|
|
93
|
+
to_string(x)
|
|
94
|
+
from_string(x, ""s)
|
|
95
|
+
from_string(state, ""s)
|
|
96
|
+
parse_actions(x, serialized)
|
|
97
|
+
from_byte_vector(x, serialized)
|
|
98
|
+
parse_action_optimized(x, serialized, 0)
|
|
99
|
+
enumerate(x).size()
|
|
100
|
+
let v : Vector<Float>
|
|
101
|
+
to_observation_tensor(state, v)
|
|
102
|
+
print(variant)
|
|
103
|
+
v.resize(10)
|
|
104
|
+
v.size()
|
|
105
|
+
observation_tensor_size(state)
|
|
106
|
+
|
|
107
|
+
trait<T> Enumerable:
|
|
108
|
+
fun enumerate(T obj, Vector<T> output)
|
|
109
|
+
|
|
110
|
+
fun enumerate(Bool b, Vector<Bool> output):
|
|
111
|
+
output.append(true)
|
|
112
|
+
output.append(false)
|
|
113
|
+
|
|
114
|
+
fun<T> _enumerate_impl(T obj, Int current_argument, Vector<T> out, Int num_args):
|
|
115
|
+
let counter = 0
|
|
116
|
+
for field of obj:
|
|
117
|
+
if counter == current_argument:
|
|
118
|
+
if field is Enumerable:
|
|
119
|
+
using Type = type(field)
|
|
120
|
+
let vec : Vector<Type>
|
|
121
|
+
field.enumerate(vec)
|
|
122
|
+
|
|
123
|
+
let next_current_argument = current_argument + 1
|
|
124
|
+
let is_last_one = next_current_argument == num_args
|
|
125
|
+
let counter_2 = 0
|
|
126
|
+
while counter_2 != vec.size():
|
|
127
|
+
field = vec.get(counter_2)
|
|
128
|
+
if is_last_one:
|
|
129
|
+
out.append(obj)
|
|
130
|
+
else:
|
|
131
|
+
_enumerate_impl(obj, next_current_argument, out, num_args)
|
|
132
|
+
counter_2 = counter_2 + 1
|
|
133
|
+
return
|
|
134
|
+
counter = counter + 1
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
fun<T> enumerate(T obj) -> Vector<T>:
|
|
138
|
+
let to_return : Vector<T>
|
|
139
|
+
if obj is Enumerable:
|
|
140
|
+
obj.enumerate(to_return)
|
|
141
|
+
else if obj is Alternative:
|
|
142
|
+
for field of obj:
|
|
143
|
+
using Type = type(field)
|
|
144
|
+
let field : Type
|
|
145
|
+
let alternatives = enumerate(field)
|
|
146
|
+
let counter = 0
|
|
147
|
+
while counter < alternatives.size():
|
|
148
|
+
obj = alternatives.get(counter)
|
|
149
|
+
to_return.append(obj)
|
|
150
|
+
counter = counter + 1
|
|
151
|
+
else:
|
|
152
|
+
let num_fields = 0
|
|
153
|
+
for field of obj:
|
|
154
|
+
num_fields = num_fields + 1
|
|
155
|
+
if num_fields == 0:
|
|
156
|
+
to_return.append(obj)
|
|
157
|
+
return to_return
|
|
158
|
+
_enumerate_impl(obj, 0, to_return, num_fields)
|
|
159
|
+
|
|
160
|
+
return to_return
|
|
161
|
+
|
|
162
|
+
trait<T> Tensorable:
|
|
163
|
+
fun write_in_observation_tensor(T obj, Vector<Float> output, Int counter)
|
|
164
|
+
fun size_as_observation_tensor(T obj) -> Int
|
|
165
|
+
|
|
166
|
+
fun write_in_observation_tensor(Int value, Int min, Int max, Vector<Float> output, Int index):
|
|
167
|
+
let counter = min
|
|
168
|
+
while counter < max:
|
|
169
|
+
if counter == value:
|
|
170
|
+
output.get(index) = 1.0
|
|
171
|
+
else:
|
|
172
|
+
output.get(index) = 0.0
|
|
173
|
+
index = index + 1
|
|
174
|
+
counter = counter + 1
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
fun write_in_observation_tensor(Int obj, Vector<Float> output, Int index) :
|
|
178
|
+
return
|
|
179
|
+
|
|
180
|
+
fun size_as_observation_tensor(Int obj) -> Int :
|
|
181
|
+
return 0
|
|
182
|
+
|
|
183
|
+
fun write_in_observation_tensor(Float obj, Vector<Float> output, Int index) {false}:
|
|
184
|
+
return
|
|
185
|
+
|
|
186
|
+
fun size_as_observation_tensor(Float obj) -> Int {false}:
|
|
187
|
+
return 0
|
|
188
|
+
|
|
189
|
+
fun write_in_observation_tensor(Bool obj, Vector<Float> output, Int index):
|
|
190
|
+
if obj:
|
|
191
|
+
output.get(index) = 1.0
|
|
192
|
+
else:
|
|
193
|
+
output.get(index) = 0.0
|
|
194
|
+
index = index + 1
|
|
195
|
+
|
|
196
|
+
fun size_as_observation_tensor(Bool obj) -> Int:
|
|
197
|
+
return 1
|
|
198
|
+
|
|
199
|
+
fun write_in_observation_tensor(Byte obj, Vector<Float> output, Int index):
|
|
200
|
+
write_in_observation_tensor(int(obj), -128, 127, output, index)
|
|
201
|
+
|
|
202
|
+
fun size_as_observation_tensor(Byte obj) -> Int:
|
|
203
|
+
return 256
|
|
204
|
+
|
|
205
|
+
fun<T, Int X> write_in_observation_tensor(T[X] obj, Vector<Float> output, Int index):
|
|
206
|
+
let counter = 0
|
|
207
|
+
while counter < X:
|
|
208
|
+
_to_observation_tensor(obj[counter], output, index)
|
|
209
|
+
counter = counter + 1
|
|
210
|
+
|
|
211
|
+
fun<T, Int X> size_as_observation_tensor(T[X] obj) -> Int:
|
|
212
|
+
return _size_as_observation_tensor_impl(obj[0]) * X
|
|
213
|
+
|
|
214
|
+
fun<T> write_in_observation_tensor(Vector<T> obj, Vector<Float> output, Int index):
|
|
215
|
+
let counter = 0
|
|
216
|
+
while counter < obj.size():
|
|
217
|
+
_to_observation_tensor(obj.get(counter), output, index)
|
|
218
|
+
counter = counter + 1
|
|
219
|
+
|
|
220
|
+
fun<T> size_as_observation_tensor(Vector<T> obj) -> Int:
|
|
221
|
+
return _size_as_observation_tensor_impl(obj.get(0)) * obj.size()
|
|
222
|
+
|
|
223
|
+
fun<Int min, Int max> write_in_observation_tensor(BInt<min, max> obj, Vector<Float> output, Int index):
|
|
224
|
+
write_in_observation_tensor(obj.value, min, max, output, index)
|
|
225
|
+
|
|
226
|
+
fun<Int min, Int max> size_as_observation_tensor(BInt<min, max> obj) -> Int:
|
|
227
|
+
return max - min
|
|
228
|
+
|
|
229
|
+
fun<T> _size_as_observation_tensor_impl(T obj) -> Int:
|
|
230
|
+
if obj is Tensorable:
|
|
231
|
+
return obj.size_as_observation_tensor()
|
|
232
|
+
else if obj is Alternative:
|
|
233
|
+
let alternative_count = 0
|
|
234
|
+
for field of obj:
|
|
235
|
+
alternative_count = alternative_count + 1
|
|
236
|
+
let max_size = 0
|
|
237
|
+
for field of obj:
|
|
238
|
+
using Type = type(field)
|
|
239
|
+
let fake : Type
|
|
240
|
+
max_size = max(_size_as_observation_tensor_impl(fake), max_size)
|
|
241
|
+
return max_size + alternative_count
|
|
242
|
+
else:
|
|
243
|
+
let to_return = 0
|
|
244
|
+
for field of obj:
|
|
245
|
+
to_return = to_return + _size_as_observation_tensor_impl(field)
|
|
246
|
+
return to_return
|
|
247
|
+
|
|
248
|
+
fun<T> _to_observation_tensor(T obj, Vector<Float> output, Int index):
|
|
249
|
+
if obj is Tensorable:
|
|
250
|
+
obj.write_in_observation_tensor(output, index)
|
|
251
|
+
return
|
|
252
|
+
else if obj is Alternative:
|
|
253
|
+
let alternative_count = 0
|
|
254
|
+
for field of obj:
|
|
255
|
+
alternative_count = alternative_count + 1
|
|
256
|
+
let current = 0
|
|
257
|
+
let largest_field = _size_as_observation_tensor_impl(obj) - alternative_count
|
|
258
|
+
for field of obj:
|
|
259
|
+
using Type = type(field)
|
|
260
|
+
if obj is Type:
|
|
261
|
+
let zeros_to_add = largest_field - _size_as_observation_tensor_impl(obj)
|
|
262
|
+
write_in_observation_tensor(current, 0, alternative_count, output, index)
|
|
263
|
+
_to_observation_tensor(field, output, index)
|
|
264
|
+
while zeros_to_add != 0:
|
|
265
|
+
zeros_to_add = zeros_to_add - 1
|
|
266
|
+
output.get(index) = 0.0
|
|
267
|
+
index = index + 1
|
|
268
|
+
return
|
|
269
|
+
current = current + 1
|
|
270
|
+
else:
|
|
271
|
+
for field of obj:
|
|
272
|
+
_to_observation_tensor(field, output, index)
|
|
273
|
+
|
|
274
|
+
fun<T> to_observation_tensor(T obj, Vector<Float> output):
|
|
275
|
+
_to_observation_tensor(obj, output, 0)
|
|
276
|
+
|
|
277
|
+
fun<T> to_observation_tensor(T obj) -> Vector<Float>:
|
|
278
|
+
let output : Vector<Float>
|
|
279
|
+
let x = _size_as_observation_tensor_impl(obj)
|
|
280
|
+
while x != 0:
|
|
281
|
+
x = x - 1
|
|
282
|
+
output.append(0.0)
|
|
283
|
+
_to_observation_tensor(obj, output, 0)
|
|
284
|
+
return output
|
|
285
|
+
|
|
286
|
+
fun<T> observation_tensor_size(T obj) -> Int:
|
|
287
|
+
return _size_as_observation_tensor_impl(obj)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
trait<T> Comparable
|
|
2
|
+
fun custom_equal(T lhs, T rhs) -> Bool
|
|
3
|
+
|
|
4
|
+
fun custom_equal(Int lhs, Int rhs) -> Bool:
|
|
5
|
+
return lhs == rhs
|
|
6
|
+
|
|
7
|
+
fun custom_equal(Bool lhs, Bool rhs) -> Bool:
|
|
8
|
+
return lhs == rhs
|
|
9
|
+
|
|
10
|
+
fun custom_equal(Byte lhs, Byte rhs) -> Bool:
|
|
11
|
+
return lhs == rhs
|
|
12
|
+
|
|
13
|
+
fun custom_equal(Float lhs, Float rhs) -> Bool:
|
|
14
|
+
return lhs == rhs
|
|
15
|
+
|
|
16
|
+
fun<T, Int X> custom_equal(T[X] lhs, T[X] rhs):
|
|
17
|
+
let counter = 0
|
|
18
|
+
while counter < X:
|
|
19
|
+
if !(lhs == rhs):
|
|
20
|
+
return false
|
|
21
|
+
counter = counter + 1
|
|
22
|
+
return true
|
|
23
|
+
|
|
24
|
+
# to implement
|
|
25
|
+
fun<T> equal(T lhs, T rhs):
|
|
26
|
+
if lhs is Comparable:
|
|
27
|
+
if rhs is Comparable:
|
|
28
|
+
return custom_equal(lhs, rhs)
|
|
29
|
+
for field of lhs
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# Copyright 2024 Massimo Fioravanti
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
import serialization.to_byte_vector
|
|
15
|
+
import string
|
|
16
|
+
|
|
17
|
+
ent<Int min, Int max> BInt:
|
|
18
|
+
Int value
|
|
19
|
+
|
|
20
|
+
fun equal(Int other) -> Bool:
|
|
21
|
+
return self.value == other
|
|
22
|
+
|
|
23
|
+
fun equal(BInt<min, max> other) -> Bool:
|
|
24
|
+
return self.value == other.value
|
|
25
|
+
|
|
26
|
+
fun less(BInt<min, max> other) -> Bool:
|
|
27
|
+
return self.value < other.value
|
|
28
|
+
|
|
29
|
+
fun less(Int other) -> Bool:
|
|
30
|
+
return self.value < other
|
|
31
|
+
|
|
32
|
+
fun greater(BInt<min, max> other) -> Bool:
|
|
33
|
+
return self.value > other.value
|
|
34
|
+
|
|
35
|
+
fun greater(Int other) -> Bool:
|
|
36
|
+
return self.value > other
|
|
37
|
+
|
|
38
|
+
fun greater_equal(BInt<min, max> other) -> Bool:
|
|
39
|
+
return self.value >= other.value
|
|
40
|
+
|
|
41
|
+
fun greater_equal(Int other) -> Bool:
|
|
42
|
+
return self.value >= other
|
|
43
|
+
|
|
44
|
+
fun less_equal(BInt<min, max> other) -> Bool:
|
|
45
|
+
return self.value <= other.value
|
|
46
|
+
|
|
47
|
+
fun less_equal(Int other) -> Bool:
|
|
48
|
+
return self.value <= other
|
|
49
|
+
|
|
50
|
+
fun assign(Int other):
|
|
51
|
+
self.value = other
|
|
52
|
+
if self.value >= max:
|
|
53
|
+
self.value = max - 1
|
|
54
|
+
if self.value < min:
|
|
55
|
+
self.value = min
|
|
56
|
+
|
|
57
|
+
fun not_equal(Int other) -> Bool:
|
|
58
|
+
return self.value != other
|
|
59
|
+
|
|
60
|
+
fun not_equal(BInt<min, max> other) -> Bool:
|
|
61
|
+
return self.value != other.value
|
|
62
|
+
|
|
63
|
+
fun add(Int val) -> BInt<min, max>:
|
|
64
|
+
let other : BInt<min, max>
|
|
65
|
+
other.value = val
|
|
66
|
+
return self + other
|
|
67
|
+
|
|
68
|
+
fun add(BInt<min, max> other) -> BInt<min, max>:
|
|
69
|
+
let to_return : BInt<min, max>
|
|
70
|
+
to_return.value = self.value + other.value
|
|
71
|
+
if to_return.value >= max:
|
|
72
|
+
to_return.value = max - 1
|
|
73
|
+
if to_return.value < min:
|
|
74
|
+
to_return.value = min
|
|
75
|
+
return to_return
|
|
76
|
+
|
|
77
|
+
fun sub(BInt<min, max> other) -> BInt<min, max>:
|
|
78
|
+
let to_return : BInt<min, max>
|
|
79
|
+
to_return.value = self.value - other.value
|
|
80
|
+
if to_return.value >= max:
|
|
81
|
+
to_return.value = max - 1
|
|
82
|
+
if to_return.value < min:
|
|
83
|
+
to_return.value = min
|
|
84
|
+
return to_return
|
|
85
|
+
|
|
86
|
+
fun sub(Int val) -> BInt<min, max>:
|
|
87
|
+
let other : BInt<min, max>
|
|
88
|
+
other.value = val
|
|
89
|
+
return self - other
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
fun<Int min, Int max> max(BInt<min, max> l, BInt<min, max> r) -> BInt<min, max>:
|
|
93
|
+
if l < r:
|
|
94
|
+
return r
|
|
95
|
+
return l
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
fun<Int min, Int max> min(BInt<min, max> l, BInt<min, max> r) -> BInt<min, max>:
|
|
99
|
+
if r < l:
|
|
100
|
+
return r
|
|
101
|
+
return l
|
|
102
|
+
|
|
103
|
+
fun<Int min, Int max> append_to_vector(BInt<min, max> to_add, Vector<Byte> output):
|
|
104
|
+
if max - min < 256:
|
|
105
|
+
let to_append = byte(to_add.value - min - 128)
|
|
106
|
+
append_to_vector(to_append, output)
|
|
107
|
+
else:
|
|
108
|
+
let to_append = to_add.value - min
|
|
109
|
+
append_to_vector(to_append, output)
|
|
110
|
+
|
|
111
|
+
fun<Int min, Int max> parse_from_vector(BInt<min, max> to_add, Vector<Byte> output, Int index) -> Bool:
|
|
112
|
+
if max - min < 256:
|
|
113
|
+
let value : Byte
|
|
114
|
+
if !parse_from_vector(value, output, index):
|
|
115
|
+
return false
|
|
116
|
+
let value_casted = int(value) + 128
|
|
117
|
+
value_casted = value_casted % (max - min)
|
|
118
|
+
to_add.value = int(value_casted) + min
|
|
119
|
+
return true
|
|
120
|
+
else:
|
|
121
|
+
let value : Int
|
|
122
|
+
if !parse_from_vector(value, output, index):
|
|
123
|
+
return false
|
|
124
|
+
to_add.value = (value % (max - min)) + min
|
|
125
|
+
return true
|
|
126
|
+
|
|
127
|
+
fun<Int min, Int max> append_to_string(BInt<min, max> to_add, String output):
|
|
128
|
+
append_to_string(to_add.value, output)
|
|
129
|
+
|
|
130
|
+
fun<Int min, Int max> parse_string(BInt<min, max> to_add, String input, Int index) -> Bool:
|
|
131
|
+
if !parse_string(to_add.value, input, index):
|
|
132
|
+
return false
|
|
133
|
+
if to_add.value < min or to_add.value >= max:
|
|
134
|
+
return false
|
|
135
|
+
return true
|
|
136
|
+
|
|
137
|
+
fun<Int min, Int max> enumerate(BInt<min, max> to_add, Vector<BInt<min, max>> output):
|
|
138
|
+
let counter = min
|
|
139
|
+
while counter < max:
|
|
140
|
+
let x : BInt<min, max>
|
|
141
|
+
x.value = counter
|
|
142
|
+
output.append(x)
|
|
143
|
+
counter = counter + 1
|
|
Binary file
|
|
Binary file
|