├── src └── fogml │ ├── __init__.py │ ├── generators │ ├── skeletons │ │ ├── __init__.py │ │ ├── min_max_scaler_skeleton.txt │ │ ├── kmeans_anomaly_skeleton.txt │ │ ├── qlearning_model_skeleton.txt │ │ ├── bayes_skeleton.txt │ │ ├── qstates_discretizer_skeleton.txt │ │ └── random_forest_template.c │ ├── __init__.py │ ├── utils.py │ ├── base_generator.py │ ├── scaler_generator.py │ ├── qstatesintervals_code_generator.py │ ├── qlearning_code_generator.py │ ├── generator.py │ ├── arduino_generator.py │ ├── tree_code_generator.py │ ├── anomaly_detector_generator.py │ ├── random_forest_generator.py │ ├── bayes_code_generator.py │ └── mlp_code_generator.py │ ├── anomaly │ ├── __init__.py │ └── detectors.py │ └── rl │ ├── __init__.py │ └── qlearning.py ├── examples ├── simple │ ├── models │ │ ├── __init__.py │ │ ├── iris_tree_model.c │ │ ├── bayes_model.c │ │ ├── iris_bayes_model.c │ │ ├── network_model.c │ │ └── iris_network_model.c │ ├── iris.py │ ├── moa_dataset.py │ ├── tree.dot │ └── mnist.py ├── TFT_ML │ ├── tree.h │ ├── bayes_model.h │ ├── neural_nets.h │ ├── TFT_ML.ino │ └── tree.c ├── rl │ ├── FogML_RL_Arduino │ │ ├── fogml_rl.h │ │ ├── qstates_discretizer_test.c │ │ ├── FogML_RL_Arduino.ino │ │ └── qlearning_model_test.c │ ├── cart_pole_iot.py │ ├── mountain_car_iot.py │ ├── mountain_car.py │ └── cart_pole.py └── anomaly │ ├── kmeans_anomaly.py │ ├── 2_snake.csv │ ├── 3_up_down.csv │ ├── 0_idle.csv │ └── 1_circle.csv ├── docs ├── requirements.in ├── fogml_logo1.png ├── fogml_logo2.png ├── images │ ├── algorithms.png │ ├── arduino_tft.png │ ├── dsp_concept.png │ ├── rl_concept.png │ ├── classification.png │ ├── anomaly_detection.png │ ├── pipeline_classification.png │ ├── pipeline_online_anomaly.png │ └── examples_zephyr_classification.png ├── pipelines.rst ├── usage.rst ├── Makefile ├── requirements.txt ├── conf.py ├── examples.rst ├── generators.rst ├── index.rst └── sdk.rst ├── .gitignore ├── pyproject.toml ├── setup.cfg ├── readthedocs.yaml ├── README.md └── LICENSE /src/fogml/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /examples/simple/models/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/fogml/generators/skeletons/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/requirements.in: -------------------------------------------------------------------------------- 1 | Sphinx>=5,<6 2 | sphinx_rtd_theme 3 | -------------------------------------------------------------------------------- /src/fogml/anomaly/__init__.py: -------------------------------------------------------------------------------- 1 | from .detectors import KMeansAnomalyDetector -------------------------------------------------------------------------------- /src/fogml/rl/__init__.py: -------------------------------------------------------------------------------- 1 | from .qlearning import QStatesIntervals, QLearning -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | dist 3 | fixes 4 | *.egg-info/ 5 | docs/_build 6 | docs/generated 7 | -------------------------------------------------------------------------------- /docs/fogml_logo1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tszydlo/FogML/HEAD/docs/fogml_logo1.png -------------------------------------------------------------------------------- /docs/fogml_logo2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tszydlo/FogML/HEAD/docs/fogml_logo2.png -------------------------------------------------------------------------------- /docs/images/algorithms.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tszydlo/FogML/HEAD/docs/images/algorithms.png -------------------------------------------------------------------------------- /docs/images/arduino_tft.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tszydlo/FogML/HEAD/docs/images/arduino_tft.png -------------------------------------------------------------------------------- /docs/images/dsp_concept.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tszydlo/FogML/HEAD/docs/images/dsp_concept.png -------------------------------------------------------------------------------- /docs/images/rl_concept.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tszydlo/FogML/HEAD/docs/images/rl_concept.png -------------------------------------------------------------------------------- /docs/images/classification.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tszydlo/FogML/HEAD/docs/images/classification.png -------------------------------------------------------------------------------- /docs/images/anomaly_detection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tszydlo/FogML/HEAD/docs/images/anomaly_detection.png -------------------------------------------------------------------------------- /docs/images/pipeline_classification.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tszydlo/FogML/HEAD/docs/images/pipeline_classification.png -------------------------------------------------------------------------------- /docs/images/pipeline_online_anomaly.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tszydlo/FogML/HEAD/docs/images/pipeline_online_anomaly.png -------------------------------------------------------------------------------- /docs/images/examples_zephyr_classification.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tszydlo/FogML/HEAD/docs/images/examples_zephyr_classification.png -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = [ 3 | "setuptools>=42", 4 | "wheel" 5 | ] 6 | build-backend = "setuptools.build_meta" 7 | -------------------------------------------------------------------------------- /examples/TFT_ML/tree.h: -------------------------------------------------------------------------------- 1 | #ifndef __TREE_H__ 2 | #define __TREE_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | int evaluate_tree(uint8_t * x); 9 | 10 | #ifdef __cplusplus 11 | } // extern "C" 12 | #endif 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /examples/TFT_ML/bayes_model.h: -------------------------------------------------------------------------------- 1 | #ifndef __BAYES_MODEL_H__ 2 | #define __BAYES_MODEL_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | int evaluate_bayes(uint8_t * x); 9 | 10 | #ifdef __cplusplus 11 | } // extern "C" 12 | #endif 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /examples/TFT_ML/neural_nets.h: -------------------------------------------------------------------------------- 1 | #ifndef __NEURAL_NETS_H__ 2 | #define __NEURAL_NETS_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | int evaluate_network(uint8_t * x); 9 | 10 | #ifdef __cplusplus 11 | } // extern "C" 12 | #endif 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /src/fogml/generators/skeletons/min_max_scaler_skeleton.txt: -------------------------------------------------------------------------------- 1 | #include "../fogml_sdk/scaler/fogml_scaler.h" 2 | 3 | float mins_tab[] = ; 4 | float maxs_tab[] = ; 5 | 6 | fogml_min_max_scaler_config_t min_max_scaler_config = { 7 | .vector_size = , 8 | .mins = mins_tab, 9 | .maxs = maxs_tab 10 | }; -------------------------------------------------------------------------------- /docs/pipelines.rst: -------------------------------------------------------------------------------- 1 | :parenttoc: True 2 | 3 | Processing Pipelines 4 | ==================== 5 | 6 | Classification 7 | -------------- 8 | 9 | .. figure:: ./images/pipeline_classification.png 10 | :width: 800 11 | 12 | Online Anomaly Detection 13 | ------------------------ 14 | 15 | .. figure:: ./images/pipeline_online_anomaly.png 16 | :width: 800 -------------------------------------------------------------------------------- /docs/usage.rst: -------------------------------------------------------------------------------- 1 | :parenttoc: True 2 | 3 | Usage 4 | ===== 5 | 6 | .. _installation: 7 | 8 | Installation 9 | ------------ 10 | 11 | To use FogML, first install it using pip: 12 | 13 | .. code-block:: console 14 | 15 | $ pip install fogml 16 | 17 | eventually, if you are using Google Colab, you can use: 18 | 19 | .. code-block:: console 20 | 21 | !pip install fogml 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/fogml/generators/skeletons/kmeans_anomaly_skeleton.txt: -------------------------------------------------------------------------------- 1 | #include "../fogml_sdk/anomaly/fogml_anomaly.h" 2 | 3 | float centroids_tab[] = ; 4 | float zscores_tab[] = ; 5 | 6 | fogml_anomaly_config_t kmeans_anomaly_config = { 7 | .vector_size = , 8 | .clusters = , 9 | .centroids = centroids_tab, 10 | .zscores = zscores_tab 11 | }; 12 | -------------------------------------------------------------------------------- /src/fogml/generators/__init__.py: -------------------------------------------------------------------------------- 1 | from .generator import GeneratorFactory 2 | from .bayes_code_generator import BayesCodeGenerator 3 | from .mlp_code_generator import MlpCodeGenerator 4 | from .random_forest_generator import RandomForestCodeGenerator 5 | from .tree_code_generator import TreeCodeGenerator 6 | from .anomaly_detector_generator import KMeansAnomalyDetectorGenerator 7 | from .scaler_generator import MinMaxScalerGenerator 8 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line, and also 5 | # from the environment for the first two. 6 | SPHINXOPTS ?= 7 | SPHINXBUILD ?= sphinx-build 8 | SOURCEDIR = . 9 | BUILDDIR = _build 10 | 11 | # Put it first so that "make" without argument is like "make help". 12 | help: 13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 14 | 15 | .PHONY: help Makefile 16 | 17 | # Catch-all target: route all unknown targets to Sphinx using the new 18 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 19 | %: Makefile 20 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 21 | -------------------------------------------------------------------------------- /src/fogml/generators/skeletons/qlearning_model_skeleton.txt: -------------------------------------------------------------------------------- 1 | static float Q_table[] = ; 2 | 3 | unsigned long RL_STATES = ; 4 | unsigned int RL_ACTIONS = ; 5 | 6 | unsigned long internal_state; 7 | 8 | void fogml_qlearning_init(){ 9 | } 10 | 11 | void fogml_qlearning_update_state(unsigned long state){ 12 | internal_state = state; 13 | } 14 | 15 | unsigned int fogml_qlearning_select_action(){ 16 | unsigned long index = internal_state * RL_ACTIONS; 17 | 18 | unsigned int action = 0; 19 | float max = Q_table[index]; 20 | 21 | for(int i = 0; i < RL_ACTIONS; i++){ 22 | if (Q_table[index + i] > max) { 23 | max = Q_table[index + i]; 24 | action = i; 25 | 26 | } 27 | } 28 | return action; 29 | } 30 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | name = fogml 3 | version = 0.0.8 4 | author = Tomasz Szydlo 5 | author_email = tszydlo@agh.edu.pl 6 | description = Tiny Federated ML for Fog Computing 7 | long_description = file: README.md 8 | long_description_content_type = text/markdown 9 | url = https://github.com/tszydlo/FogML 10 | project_urls = 11 | Bug Tracker = https://github.com/tszydlo/FogML/issues 12 | classifiers = 13 | Programming Language :: Python :: 3 14 | License :: OSI Approved :: Apache Software License 15 | Operating System :: OS Independent 16 | license = Apache 2.0 17 | 18 | [options] 19 | package_dir = 20 | = src 21 | packages = find: 22 | python_requires = >=3.6 23 | include_package_data = true 24 | 25 | [options.package_data] 26 | * = *.txt, *.c 27 | 28 | [options.packages.find] 29 | where = src 30 | -------------------------------------------------------------------------------- /readthedocs.yaml: -------------------------------------------------------------------------------- 1 | # .readthedocs.yaml 2 | # Read the Docs configuration file 3 | # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details 4 | 5 | # Required 6 | version: 2 7 | 8 | # Set the version of Python and other tools you might need 9 | build: 10 | os: ubuntu-22.04 11 | tools: 12 | python: "3.10" 13 | # You can also specify other tool versions: 14 | # nodejs: "16" 15 | # rust: "1.55" 16 | # golang: "1.17" 17 | 18 | # Build documentation in the docs/ directory with Sphinx 19 | sphinx: 20 | configuration: docs/conf.py 21 | 22 | # If using Sphinx, optionally build your docs in additional formats such as PDF 23 | # formats: 24 | # - pdf 25 | 26 | # Optionally declare the Python requirements required to build your docs 27 | python: 28 | install: 29 | - requirements: docs/requirements.txt 30 | - method: pip 31 | path: . 32 | -------------------------------------------------------------------------------- /examples/simple/models/iris_tree_model.c: -------------------------------------------------------------------------------- 1 | int classify(double * x){ 2 | if (x[3] <= 0.8000000119) { 3 | return 0; 4 | } 5 | else { 6 | if (x[3] <= 1.7500000000) { 7 | if (x[2] <= 4.9499998093) { 8 | if (x[3] <= 1.6500000954) { 9 | return 1; 10 | } 11 | else { 12 | return 2; 13 | } 14 | } 15 | else { 16 | if (x[3] <= 1.5499999523) { 17 | return 2; 18 | } 19 | else { 20 | if (x[0] <= 6.9499998093) { 21 | return 1; 22 | } 23 | else { 24 | return 2; 25 | } 26 | } 27 | } 28 | } 29 | else { 30 | if (x[2] <= 4.8500003815) { 31 | if (x[0] <= 5.9499998093) { 32 | return 1; 33 | } 34 | else { 35 | return 2; 36 | } 37 | } 38 | else { 39 | return 2; 40 | } 41 | } 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /src/fogml/generators/skeletons/bayes_skeleton.txt: -------------------------------------------------------------------------------- 1 | int (double* x){ 2 | 3 | double sigma[][] = ; 4 | double theta[][] = ; 5 | double log_sigma[][] = ; 6 | 7 | double prior[] = ; 8 | 9 | double n_ij; 10 | double temp_sum; 11 | double joint_log_likelihood[]; 12 | 13 | for (int i = 0; i < ; i++){ 14 | temp_sum = 0; 15 | for (int j = 0; j < ; j++){ 16 | temp_sum += log_sigma[i][j]; 17 | } 18 | n_ij = -0.5 * temp_sum; 19 | 20 | temp_sum = 0; 21 | for (int j = 0; j < ; j++){ 22 | temp_sum += ((x[j] - theta[i][j]) * (x[j] - theta[i][j])) / (sigma[i][j]); 23 | } 24 | n_ij -= 0.5 * temp_sum; 25 | joint_log_likelihood[i] = prior[i] + n_ij; 26 | } 27 | 28 | int max_index = 0; 29 | for (int i = 0; i < ; i++){ 30 | printf("%lf\n", joint_log_likelihood[i]); 31 | if (joint_log_likelihood[i] > joint_log_likelihood[max_index]){ 32 | max_index = i; 33 | } 34 | } 35 | return max_index; 36 | } -------------------------------------------------------------------------------- /src/fogml/generators/skeletons/qstates_discretizer_skeleton.txt: -------------------------------------------------------------------------------- 1 | static float Q_states[] = ; 2 | 3 | unsigned int RL_OBSERVATION_SIZE = ; 4 | 5 | unsigned int *observation_states; 6 | 7 | void fogml_discretizer_init(){ 8 | 9 | observation_states = (float*)malloc(sizeof(unsigned int) * RL_OBSERVATION_SIZE); 10 | } 11 | 12 | unsigned long fogml_discretizer_get_state(float* observation){ 13 | for(int i = 0; i < RL_OBSERVATION_SIZE; i++){ 14 | float window = (Q_states[i*3+1] - Q_states[i*3+0]) / Q_states[i*3+2]; // (max-min) / num 15 | int state = (int)((observation[i] - Q_states[i*3+0]) / window) + 1; 16 | 17 | if (state < 0) { 18 | state = 0; 19 | } 20 | 21 | if (state > Q_states[i*3+2] + 1) { 22 | state = Q_states[i*3+2] + 1; 23 | } 24 | 25 | observation_states[i] = state; 26 | } 27 | 28 | unsigned long result = 0; 29 | unsigned long mul = 1; 30 | 31 | for(int i = 0; i < RL_OBSERVATION_SIZE; i++){ 32 | result = result + mul * observation_states[i]; 33 | mul = mul * (Q_states[i*3+2] + 2); 34 | 35 | } 36 | 37 | return result; 38 | } 39 | -------------------------------------------------------------------------------- /examples/rl/FogML_RL_Arduino/fogml_rl.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2021 FogML 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 | #ifndef FOGML_RL_H 18 | #define FOGML_RL_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | void fogml_qlearning_init(); 25 | void fogml_qlearning_update_state(unsigned long state); 26 | unsigned int fogml_qlearning_select_action(); 27 | 28 | void fogml_discretizer_init(); 29 | unsigned long fogml_discretizer_get_state(float* observation); 30 | 31 | extern unsigned long RL_STATES; 32 | extern unsigned int RL_ACTIONS; 33 | extern unsigned int RL_OBSERVATION_SIZE; 34 | 35 | #ifdef __cplusplus 36 | } 37 | #endif 38 | 39 | #endif 40 | 41 | 42 | -------------------------------------------------------------------------------- /src/fogml/generators/utils.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 | def generate_c_matrix(matrix): 18 | result = "{\n" 19 | for i in range(matrix.shape[0]): 20 | result += "{" 21 | for j in range(matrix.shape[1]): 22 | result += "%.6f, " % matrix[i][j] 23 | result += "},\n" 24 | result += "}" 25 | return result 26 | 27 | 28 | def generate_c_array(array): 29 | result = "{" 30 | for i in range(len(array)): 31 | result += "%.6f, " % array[i] 32 | result += "}" 33 | return result 34 | 35 | 36 | def generate_c_function(function_body="", return_type="void", name="fun", args=""): 37 | return "%s %s(%s){\n %s\n }; \n " % (return_type, name, args, function_body) 38 | -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is autogenerated by pip-compile with python 3.10 3 | # To update, run: 4 | # 5 | # pip-compile docs/requirements.in 6 | # 7 | alabaster==0.7.12 8 | # via sphinx 9 | babel==2.10.3 10 | # via sphinx 11 | certifi==2022.6.15 12 | # via requests 13 | charset-normalizer==2.1.0 14 | # via requests 15 | docutils==0.17.1 16 | # via 17 | # sphinx 18 | # sphinx-rtd-theme 19 | idna==3.3 20 | # via requests 21 | imagesize==1.4.1 22 | # via sphinx 23 | jinja2==3.1.2 24 | # via sphinx 25 | markupsafe==2.1.1 26 | # via jinja2 27 | packaging==21.3 28 | # via sphinx 29 | pygments==2.12.0 30 | # via sphinx 31 | pyparsing==3.0.9 32 | # via packaging 33 | pytz==2022.1 34 | # via babel 35 | requests==2.28.1 36 | # via sphinx 37 | snowballstemmer==2.2.0 38 | # via sphinx 39 | sphinx==5.0.2 40 | # via 41 | # -r docs/requirements.in 42 | # sphinx-rtd-theme 43 | sphinx-rtd-theme==1.0.0 44 | # via -r docs/requirements.in 45 | sphinxcontrib-applehelp==1.0.2 46 | # via sphinx 47 | sphinxcontrib-devhelp==1.0.2 48 | # via sphinx 49 | sphinxcontrib-htmlhelp==2.0.0 50 | # via sphinx 51 | sphinxcontrib-jsmath==1.0.1 52 | # via sphinx 53 | sphinxcontrib-qthelp==1.0.3 54 | # via sphinx 55 | sphinxcontrib-serializinghtml==1.1.5 56 | # via sphinx 57 | urllib3==1.26.9 58 | # via requests 59 | -------------------------------------------------------------------------------- /examples/simple/iris.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 pickle 18 | 19 | from sklearn import datasets, tree, naive_bayes 20 | from sklearn.neural_network import MLPClassifier 21 | 22 | from fogml.generators import GeneratorFactory 23 | 24 | factory = GeneratorFactory() 25 | 26 | iris = datasets.load_iris() 27 | X = iris.data 28 | y = iris.target 29 | 30 | #clf = tree.DecisionTreeClassifier(random_state=3456) 31 | #clf = naive_bayes.GaussianNB() 32 | clf = MLPClassifier(hidden_layer_sizes=(4,), random_state=34, solver='adam', max_iter=1500) 33 | #clf = RandomForestClassifier(n_estimators=10) 34 | 35 | generator = factory.get_generator(clf) 36 | 37 | clf.fit(X, y) 38 | 39 | print( 'accuracy: ',clf.score(X,y)) 40 | 41 | dumped = pickle.dumps(clf) 42 | print("SIZE: " + str(len(dumped))) 43 | 44 | generator.generate() 45 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # Configuration file for the Sphinx documentation builder. 2 | 3 | # -- Project information 4 | 5 | project = 'FogML' 6 | copyright = '2024, Tomasz Szydlo' 7 | author = 'Tomasz Szydlo' 8 | 9 | release = '0.1' 10 | version = '0.1.0' 11 | 12 | # -- General configuration 13 | 14 | extensions = [ 15 | 'sphinx.ext.duration', 16 | 'sphinx.ext.doctest', 17 | 'sphinx.ext.autodoc', 18 | 'sphinx.ext.autosummary', 19 | 'sphinx.ext.intersphinx', 20 | ] 21 | 22 | intersphinx_mapping = { 23 | 'python': ('https://docs.python.org/3/', None), 24 | 'sphinx': ('https://www.sphinx-doc.org/en/master/', None), 25 | } 26 | intersphinx_disabled_domains = ['std'] 27 | 28 | templates_path = ['_templates'] 29 | 30 | # -- Options for HTML output 31 | 32 | html_theme = 'sphinx_rtd_theme' 33 | html_logo = "fogml_logo2.png" 34 | 35 | html_theme_options = { 36 | #'analytics_id': 'G-XXXXXXXXXX', # Provided by Google in your dashboard 37 | #'analytics_anonymize_ip': False, 38 | 'logo_only': True, 39 | 'display_version': True, 40 | 'prev_next_buttons_location': 'bottom', 41 | 'style_external_links': False, 42 | 'vcs_pageview_mode': '', 43 | 'style_nav_header_background': 'white', 44 | # Toc options 45 | 'collapse_navigation': True, 46 | 'sticky_navigation': True, 47 | 'navigation_depth': 4, 48 | 'includehidden': True, 49 | 'titles_only': False 50 | } 51 | 52 | # -- Options for EPUB output 53 | epub_show_urls = 'footnote' 54 | -------------------------------------------------------------------------------- /examples/anomaly/kmeans_anomaly.py: -------------------------------------------------------------------------------- 1 | from sklearn.preprocessing import MinMaxScaler 2 | import numpy as np 3 | import pandas as pd 4 | 5 | from fogml.anomaly import KMeansAnomalyDetector 6 | from generators import GeneratorFactory 7 | 8 | DATA = ["0_idle.csv", "1_circle.csv", "2_snake.csv"] 9 | DATA_TEST = ["0_idle.csv", "1_circle.csv", "2_snake.csv", "3_up_down.csv"] 10 | 11 | def fogml_prepare_data(data): 12 | spX_t = [] 13 | spY_t = [] 14 | 15 | i = 0 16 | for fname in data: 17 | tmp = pd.read_csv(fname, delimiter=" ", header=None) 18 | spX_t.append(tmp) 19 | spY_t.append([i] * tmp.shape[0]) 20 | i = i + 1 21 | 22 | spX = np.concatenate(spX_t, axis=0) 23 | spY = np.concatenate(spY_t, axis=0) 24 | 25 | return (spX, spY) 26 | 27 | (spX, _) = fogml_prepare_data(DATA) 28 | (spX_test, y) = fogml_prepare_data(DATA_TEST) 29 | 30 | print(y) 31 | 32 | scaler = MinMaxScaler() 33 | transformer = scaler.fit(spX) 34 | data_norm = transformer.transform(spX) 35 | 36 | print("Scaler:") 37 | print(scaler.data_min_) 38 | print(scaler.data_max_) 39 | 40 | anomalyDetector = KMeansAnomalyDetector(n_clusters=16) 41 | anomalyDetector.fit(data_norm) 42 | 43 | data_test_norm = transformer.transform(spX_test) 44 | res = anomalyDetector.predict(data_test_norm) 45 | print(res) 46 | 47 | factory = GeneratorFactory() 48 | generator = factory.get_generator(transformer) 49 | generator.generate() 50 | 51 | generator = factory.get_generator(anomalyDetector) 52 | generator.generate() 53 | -------------------------------------------------------------------------------- /examples/simple/models/bayes_model.c: -------------------------------------------------------------------------------- 1 | int evaluate_bayes(double* x){ 2 | 3 | double sigma[3][4] = { 4 | {0.121764, 0.140816, 0.029556, 0.010884, }, 5 | {0.261104, 0.096500, 0.216400, 0.038324, }, 6 | {0.396256, 0.101924, 0.298496, 0.073924, }, 7 | }; 8 | double theta[3][4] = { 9 | {5.006000, 3.428000, 1.462000, 0.246000, }, 10 | {5.936000, 2.770000, 4.260000, 1.326000, }, 11 | {6.588000, 2.974000, 5.552000, 2.026000, }, 12 | }; 13 | double log_sigma[3][4] = { 14 | {-0.267793, -0.122424, -1.683591, -2.682584, }, 15 | {0.495041, -0.500335, 0.307250, -1.423802, }, 16 | {0.912182, -0.445651, 0.628878, -0.766841, }, 17 | }; 18 | 19 | double prior[3] = {-1.098612, -1.098612, -1.098612, }; 20 | 21 | double n_ij; 22 | double temp_sum; 23 | double joint_log_likelihood[3]; 24 | 25 | for (int i = 0; i < 3; i++){ 26 | temp_sum = 0; 27 | for (int j = 0; j < 4; j++){ 28 | temp_sum += log_sigma[i][j]; 29 | } 30 | n_ij = -0.5 * temp_sum; 31 | 32 | temp_sum = 0; 33 | for (int j = 0; j < 4; j++){ 34 | temp_sum += ((x[j] - theta[i][j]) * (x[j] - theta[i][j])) / (sigma[i][j]); 35 | } 36 | n_ij -= 0.5 * temp_sum; 37 | joint_log_likelihood[i] = prior[i] + n_ij; 38 | } 39 | 40 | int max_index = 0; 41 | for (int i = 0; i < 3; i++){ 42 | printf("%lf\n", joint_log_likelihood[i]); 43 | if (joint_log_likelihood[i] > joint_log_likelihood[max_index]){ 44 | max_index = i; 45 | } 46 | } 47 | return max_index; 48 | } -------------------------------------------------------------------------------- /examples/simple/models/iris_bayes_model.c: -------------------------------------------------------------------------------- 1 | int evaluate_bayes(double* x){ 2 | 3 | double sigma[3][4] = { 4 | {0.121764, 0.142276, 0.029504, 0.011264, }, 5 | {0.261104, 0.096500, 0.216400, 0.038324, }, 6 | {0.396256, 0.101924, 0.298496, 0.073924, }, 7 | }; 8 | double theta[3][4] = { 9 | {5.006000, 3.418000, 1.464000, 0.244000, }, 10 | {5.936000, 2.770000, 4.260000, 1.326000, }, 11 | {6.588000, 2.974000, 5.552000, 2.026000, }, 12 | }; 13 | double log_sigma[3][4] = { 14 | {-0.267793, -0.112109, -1.685352, -2.648266, }, 15 | {0.495041, -0.500335, 0.307250, -1.423802, }, 16 | {0.912182, -0.445651, 0.628878, -0.766841, }, 17 | }; 18 | 19 | double prior[3] = {-1.098612, -1.098612, -1.098612, }; 20 | 21 | double n_ij; 22 | double temp_sum; 23 | double joint_log_likelihood[3]; 24 | 25 | for (int i = 0; i < 3; i++){ 26 | temp_sum = 0; 27 | for (int j = 0; j < 4; j++){ 28 | temp_sum += log_sigma[i][j]; 29 | } 30 | n_ij = -0.5 * temp_sum; 31 | 32 | temp_sum = 0; 33 | for (int j = 0; j < 4; j++){ 34 | temp_sum += ((x[j] - theta[i][j]) * (x[j] - theta[i][j])) / (sigma[i][j]); 35 | } 36 | n_ij -= 0.5 * temp_sum; 37 | joint_log_likelihood[i] = prior[i] + n_ij; 38 | } 39 | 40 | int max_index = 0; 41 | for (int i = 0; i < 3; i++){ 42 | printf("%lf\n", joint_log_likelihood[i]); 43 | if (joint_log_likelihood[i] > joint_log_likelihood[max_index]){ 44 | max_index = i; 45 | } 46 | } 47 | return max_index; 48 | } -------------------------------------------------------------------------------- /src/fogml/generators/skeletons/random_forest_template.c: -------------------------------------------------------------------------------- 1 | {%- macro recurse(tree, node, depth, index) -%} 2 | {%- set indent = " " * depth -%} 3 | {%- if tree["feature"][node] >= 0 -%} 4 | {%- set name = tree["feature"][node] -%} 5 | {%- set threshold = tree["threshold"][node] -%} 6 | {{ indent }}if (x[{{ name }}] <= {{ '%0.10f' %threshold }}) { 7 | {{ recurse(tree, tree["children_left"][node], depth + 1, index) }} 8 | {{ indent }}} 9 | {{ indent }}else { 10 | {{ recurse(tree, tree["children_right"][node], depth + 1, index) }} 11 | {{ indent }}} 12 | {%- else -%} 13 | {%- set class_index = argmax(tree["value"][node]) -%} 14 | {{ indent }}results[{{ index }}] = {{ clf["classes_"][class_index] }}; 15 | {%- endif -%} 16 | {%- endmacro -%} 17 | int classify(float * x){ 18 | int results[{{ clf["estimators_"]|length }}]; 19 | {%- set ns = namespace(index=0) -%} 20 | {% for estimator in clf["estimators_"] %} 21 | {% set tree = estimator.tree_ -%} 22 | {{ recurse(tree, 0, 1, ns.index) }} 23 | {%- set ns.index = ns.index + 1 -%} 24 | {% endfor %} 25 | int classes_amount = 0; 26 | for(int i=0; i<{{ clf["estimators_"]|length }}; i++){ 27 | if(results[i]+1 > classes_amount) classes_amount = results[i]+1; 28 | } 29 | int result_class = -1; 30 | int max_apperance = 0; 31 | for(int i=0; i max_apperance){ 35 | max_apperance = apperance; 36 | result_class = i; 37 | } 38 | } 39 | return result_class; 40 | -------------------------------------------------------------------------------- /examples/anomaly/2_snake.csv: -------------------------------------------------------------------------------- 1 | 7.39 0.01 2.04 -0.73 1.21 -9.46 115.53 10.17 3.28 5.00 2.00 6.00 2 | 8.49 0.23 1.37 -0.49 1.02 -9.16 186.39 3.44 2.20 8.00 8.00 5.00 3 | 8.08 0.41 2.28 -1.13 1.26 -9.30 148.12 7.35 3.54 5.00 4.00 5.00 4 | 9.73 -0.06 0.94 -0.62 1.18 -9.23 264.31 1.44 1.90 9.00 5.00 6.00 5 | 8.41 -0.00 1.65 -0.92 1.43 -9.32 169.70 4.26 3.87 7.00 6.00 4.00 6 | 7.86 -0.16 1.40 -0.35 0.94 -9.35 191.86 4.38 2.36 6.00 4.00 5.00 7 | 6.67 0.03 1.87 -0.77 0.93 -9.44 140.07 6.65 1.58 8.00 7.00 4.00 8 | 6.51 -0.69 1.58 -0.70 1.10 -9.27 120.97 3.99 1.49 7.00 5.00 4.00 9 | 8.82 0.09 1.32 -0.41 1.10 -9.18 182.28 3.44 2.60 8.00 3.00 6.00 10 | 8.39 -0.31 1.36 -1.04 1.11 -9.21 147.25 3.91 2.19 8.00 6.00 4.00 11 | 8.40 -0.03 1.70 -0.41 0.78 -9.26 233.04 4.10 1.54 9.00 6.00 8.00 12 | 9.74 0.39 2.20 -0.85 0.82 -9.38 215.86 7.00 1.25 8.00 8.00 3.00 13 | 9.27 -0.19 1.24 -0.84 0.79 -9.17 189.88 2.36 0.76 8.00 3.00 5.00 14 | 8.78 0.35 1.29 -0.40 0.95 -9.34 225.57 3.45 1.54 8.00 4.00 9.00 15 | 9.69 0.01 2.51 -1.05 0.76 -9.47 261.71 9.64 0.68 9.00 7.00 5.00 16 | 8.54 -0.07 2.03 -0.86 1.11 -9.36 224.30 5.84 3.34 8.00 8.00 6.00 17 | 9.46 -0.24 1.67 -0.60 1.21 -9.46 290.93 4.00 2.44 8.00 4.00 5.00 18 | 8.84 0.27 2.36 -0.85 1.07 -9.32 200.90 10.57 2.61 8.00 6.00 5.00 19 | 8.08 -0.92 1.26 -0.73 1.25 -9.33 145.23 2.90 2.98 7.00 7.00 4.00 20 | 9.07 -0.13 1.73 -0.67 0.95 -9.22 225.49 5.73 2.07 5.00 7.00 6.00 21 | 7.71 -0.21 1.69 -0.71 0.77 -9.14 165.74 7.22 1.34 7.00 7.00 4.00 22 | 9.54 0.05 1.70 -0.89 1.30 -9.08 188.68 5.13 2.67 5.00 6.00 6.00 23 | 8.95 -0.13 2.20 -0.52 0.94 -9.32 209.69 9.12 1.87 8.00 8.00 3.00 24 | -------------------------------------------------------------------------------- /src/fogml/generators/base_generator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 | from abc import abstractmethod, ABC 18 | 19 | class BaseGenerator(ABC): 20 | 21 | def license_header(self): 22 | license = """/* 23 | Copyright 2021 FogML 24 | 25 | Licensed under the Apache License, Version 2.0 (the "License"); 26 | you may not use this file except in compliance with the License. 27 | You may obtain a copy of the License at 28 | 29 | http://www.apache.org/licenses/LICENSE-2.0 30 | 31 | Unless required by applicable law or agreed to in writing, software 32 | distributed under the License is distributed on an "AS IS" BASIS, 33 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | See the License for the specific language governing permissions and 35 | limitations under the License. 36 | */ 37 | 38 | """ 39 | return license 40 | 41 | @abstractmethod 42 | def generate(self, **kwargs): 43 | pass 44 | -------------------------------------------------------------------------------- /examples/anomaly/3_up_down.csv: -------------------------------------------------------------------------------- 1 | 2.75 0.20 3.27 -1.80 19.49 -11.53 7.57 24.49 1084.65 8.00 5.00 5.00 2 | 2.50 -0.03 3.45 -1.53 21.50 -7.53 7.87 19.90 987.68 7.00 4.00 5.00 3 | 2.74 0.07 3.78 -1.21 21.26 -9.70 10.26 24.84 1181.15 5.00 4.00 4.00 4 | 3.62 0.55 2.15 -1.10 19.77 -10.77 16.35 10.03 903.57 4.00 3.00 4.00 5 | 2.51 -0.31 1.19 -0.74 18.81 -8.73 12.70 3.17 880.99 3.00 5.00 3.00 6 | 2.93 -0.22 1.28 -0.52 17.47 -9.93 13.39 3.68 668.57 5.00 4.00 3.00 7 | 2.04 -0.04 2.24 -0.89 15.42 -10.73 5.97 8.15 545.00 6.00 5.00 3.00 8 | 2.19 0.08 2.82 -0.54 14.37 -8.73 7.13 15.19 598.21 4.00 3.00 3.00 9 | 1.74 0.33 2.20 -0.98 11.70 -9.53 4.97 7.82 329.70 4.00 3.00 3.00 10 | 1.57 -0.06 2.39 -0.74 13.90 -9.03 5.42 14.44 462.59 3.00 3.00 3.00 11 | 0.95 -0.39 4.00 -1.49 14.91 -10.34 2.86 25.86 442.86 4.00 3.00 3.00 12 | 2.05 -0.30 4.46 -1.22 13.79 -8.70 6.64 42.85 512.60 6.00 3.00 3.00 13 | 1.26 -0.38 3.61 -1.08 13.78 -8.86 2.50 24.44 368.48 5.00 3.00 3.00 14 | 1.86 -0.70 3.76 -1.07 14.67 -8.71 5.23 27.24 513.37 4.00 3.00 3.00 15 | 2.59 0.20 4.09 -1.86 17.02 -11.17 12.41 35.71 658.52 5.00 3.00 3.00 16 | 1.33 0.23 3.45 -1.35 15.46 -9.07 4.90 18.43 465.64 6.00 3.00 3.00 17 | 2.12 0.17 3.64 -2.22 14.83 -10.68 8.44 36.28 582.40 6.00 4.00 4.00 18 | 1.49 0.39 4.55 -1.15 15.61 -7.98 4.27 41.13 573.24 3.00 3.00 3.00 19 | 1.77 0.45 3.62 -1.05 14.60 -9.11 3.38 22.13 403.67 5.00 3.00 3.00 20 | 1.73 0.32 3.38 -0.73 14.27 -8.81 4.11 26.99 456.15 6.00 2.00 3.00 21 | 0.90 0.36 2.47 -0.98 12.58 -11.51 2.18 11.28 372.60 6.00 6.00 4.00 22 | 1.88 0.13 3.91 -1.10 15.57 -8.30 5.95 36.47 552.32 6.00 2.00 3.00 23 | 0.93 0.02 2.72 -0.79 14.59 -9.30 1.39 11.79 380.33 5.00 4.00 3.00 24 | 1.53 0.12 2.98 -0.74 14.23 -8.05 3.47 15.08 437.28 7.00 3.00 3.00 25 | -------------------------------------------------------------------------------- /examples/simple/moa_dataset.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 | from sklearn import tree, naive_bayes 18 | from sklearn.neural_network import MLPClassifier 19 | from sklearn.tree import export_graphviz 20 | import pandas as pd 21 | import numpy as np 22 | 23 | from fogml.generators import GeneratorFactory 24 | 25 | for data_chunk in pd.read_csv('./mldata/dataset.arff', sep=',', chunksize=10 ** 6): 26 | clf = tree.DecisionTreeClassifier() 27 | # clf = naive_bayes.GaussianNB() 28 | # clf = MLPClassifier(hidden_layer_sizes=(15,), random_state=34) 29 | 30 | clf.fit(data_chunk.iloc[:, 0:5], data_chunk.iloc[:, 5:6]) 31 | 32 | factory = GeneratorFactory() 33 | 34 | generator = factory.get_generator(clf) 35 | generator.generate(fname="models\classifier_moa.c") 36 | 37 | # print(clf.predict(np.array([0.3, 0.3, 0.4, 0.5, 0.6]).reshape(1, -1))) 38 | print(clf.predict(np.array([5.7, 0.9, 0.4, 0.5, 0.6]).reshape(1, -1))) 39 | 40 | export_graphviz(clf, out_file="tree.dot", max_depth=3, 41 | filled=True, rotate=True, rounded=True 42 | ) 43 | -------------------------------------------------------------------------------- /src/fogml/anomaly/detectors.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn.base import ClassifierMixin 3 | from sklearn.cluster import KMeans 4 | from sklearn.metrics import euclidean_distances 5 | 6 | 7 | class KMeansAnomalyDetector(ClassifierMixin): 8 | def __init__(self, n_clusters=3, z_score=99.7): 9 | self.n_clusters = n_clusters 10 | self.z_score = z_score 11 | 12 | def fit(self, X): 13 | # print("Clusters: ") 14 | # print(self.n_clusters) 15 | # print("---") 16 | 17 | kmeans = KMeans(n_clusters=self.n_clusters, random_state=0).fit(X) 18 | self.clusters = kmeans.cluster_centers_ 19 | data_pred = kmeans.predict(X) 20 | 21 | self.z_scores = [] 22 | 23 | for k in range(self.n_clusters): 24 | #print("Cluster: ") 25 | #print(k) 26 | 27 | elem_cluster = X[np.where(data_pred == k)] 28 | 29 | cluster_dist = euclidean_distances(elem_cluster, [self.clusters[k]]) 30 | 31 | # print("Center: ") 32 | # print(kmeans.cluster_centers_[k]) 33 | 34 | z_score = np.percentile(cluster_dist, [self.z_score], axis=0)[0][0] 35 | 36 | # print("Z-Score: ") 37 | # print(z_score) 38 | 39 | self.z_scores.append(z_score) 40 | 41 | def predict_proba(self, X): 42 | cluster_dist = euclidean_distances(X, self.clusters) 43 | 44 | results = [] 45 | 46 | for row in cluster_dist: 47 | k = np.argmin(row) 48 | v = np.min(row) 49 | 50 | results.append(v - self.z_scores[k]) 51 | 52 | return np.matrix(results) 53 | 54 | def predict(self, X): 55 | proba = self.predict_proba(X) 56 | 57 | proba[proba <= 0] = 0 58 | proba[proba > 0] = 1 59 | 60 | return proba 61 | -------------------------------------------------------------------------------- /examples/anomaly/0_idle.csv: -------------------------------------------------------------------------------- 1 | 0.70 0.10 1.17 -0.42 1.03 -9.59 0.75 1.90 2.02 7.00 3.00 4.00 2 | 0.68 0.14 0.75 -0.47 1.02 -9.61 0.75 0.65 1.50 6.00 6.00 3.00 3 | 2.19 0.22 0.95 -0.52 1.73 -9.48 7.54 0.93 4.80 4.00 5.00 4.00 4 | 6.06 5.43 5.17 3.79 11.46 -3.63 88.74 41.67 303.08 1.00 1.00 1.00 5 | 2.28 7.93 9.57 -0.77 5.10 2.52 8.44 215.81 47.31 5.00 1.00 2.00 6 | 9.53 1.87 2.67 -4.87 8.00 -6.81 167.14 14.74 108.84 3.00 4.00 1.00 7 | 1.73 0.35 9.95 -0.48 3.48 -8.90 5.59 184.59 17.79 4.00 1.00 4.00 8 | 2.42 -2.11 3.68 6.99 5.10 -4.52 15.03 17.15 66.83 1.00 2.00 2.00 9 | 4.82 -5.87 1.84 4.39 4.07 -4.83 41.69 5.47 24.36 2.00 4.00 2.00 10 | 7.52 0.05 1.27 5.61 1.73 -6.79 89.72 2.58 3.40 3.00 1.00 3.00 11 | 1.24 1.92 13.18 -2.42 2.67 -7.76 3.54 339.68 19.61 5.00 1.00 1.00 12 | 5.10 -1.03 1.81 -8.67 7.45 -2.44 37.44 8.49 107.73 1.00 1.00 1.00 13 | 3.47 -6.17 5.25 -6.53 8.09 -0.33 25.53 33.09 159.15 1.00 1.00 1.00 14 | 7.15 -1.68 6.98 -0.76 3.91 -8.41 112.90 80.94 19.20 1.00 1.00 3.00 15 | 2.28 1.37 4.47 5.62 6.97 -6.62 13.25 46.90 78.81 1.00 1.00 1.00 16 | 2.90 3.50 7.81 6.00 9.56 3.13 16.07 131.33 163.17 3.00 1.00 1.00 17 | 1.01 4.85 4.17 -2.56 1.12 7.24 1.70 25.95 2.36 5.00 1.00 4.00 18 | 4.46 2.77 9.30 -0.45 3.80 7.78 33.45 108.57 15.25 1.00 1.00 2.00 19 | 4.39 -0.38 2.67 7.42 9.66 0.97 29.42 11.27 223.79 1.00 2.00 1.00 20 | 7.58 -3.99 7.81 1.07 6.29 -8.29 114.40 68.65 37.83 2.00 1.00 1.00 21 | 6.42 3.61 2.69 -2.06 4.71 -8.11 85.42 8.74 35.90 1.00 1.00 1.00 22 | 1.54 7.47 2.20 -4.93 9.29 0.59 4.26 9.14 169.69 4.00 2.00 1.00 23 | 4.06 4.77 5.53 -6.27 6.08 4.50 23.68 92.21 73.97 1.00 1.00 1.00 24 | 4.18 0.28 2.76 -8.63 6.43 -2.66 31.45 20.38 114.19 1.00 1.00 1.00 25 | 3.89 -0.06 7.23 -3.45 3.84 -8.17 28.05 148.00 22.79 4.00 1.00 1.00 26 | 2.74 -0.06 2.28 -0.29 2.27 -9.29 9.97 8.96 7.11 3.00 1.00 4.00 27 | 2.90 -0.52 1.37 -1.47 2.39 -9.06 13.26 2.67 8.23 2.00 3.00 5.00 28 | -------------------------------------------------------------------------------- /src/fogml/generators/scaler_generator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 os 18 | from .base_generator import BaseGenerator 19 | 20 | 21 | class MinMaxScalerGenerator(BaseGenerator): 22 | skeleton_path = 'skeletons/min_max_scaler_skeleton.txt' 23 | 24 | def __init__(self, clf): 25 | self.clf = clf 26 | 27 | @staticmethod 28 | def generate_c_array(array): 29 | size = len(array) 30 | result = "{" 31 | for pos in range(size): 32 | result += "%.6f" % array[pos] 33 | if (pos < size - 1): 34 | result += ", " 35 | result += "}" 36 | return result 37 | 38 | def generate(self, fname='min_max_scaler_test.c', **kwargs): 39 | with open(os.path.join(os.path.dirname(__file__), self.skeleton_path)) as skeleton: 40 | code = skeleton.read() 41 | code = self.license_header() + code 42 | code = code.replace('', self.generate_c_array(self.clf.data_min_)) 43 | code = code.replace('', self.generate_c_array(self.clf.data_max_)) 44 | code = code.replace('', str(len(self.clf.data_min_))) 45 | 46 | with open(fname, 'w') as output_file: 47 | output_file.write(code) 48 | -------------------------------------------------------------------------------- /src/fogml/generators/qstatesintervals_code_generator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 os 18 | from .base_generator import BaseGenerator 19 | 20 | 21 | class QStatesIntervalsCodeGenerator(BaseGenerator): 22 | skeleton_path = 'skeletons/qstates_discretizer_skeleton.txt' 23 | 24 | def __init__(self, clf): 25 | self.clf = clf 26 | 27 | @staticmethod 28 | def generate_c_array(array): 29 | size = len(array) 30 | result = "{" 31 | for pos in range(size): 32 | for elem in range(3): 33 | result += "%.6f, " % array[pos][elem] 34 | result += "\n" 35 | result += "}" 36 | return result 37 | 38 | def generate_q_states_table(self): 39 | return self.generate_c_array(self.clf.stateSpace) 40 | 41 | def generate(self, fname = 'qstates_discretizer_test.c', **kwargs): 42 | with open(os.path.join(os.path.dirname(__file__), self.skeleton_path)) as skeleton: 43 | code = skeleton.read() 44 | code = self.license_header() + code 45 | code = code.replace('', self.generate_q_states_table()) 46 | 47 | code = code.replace('', str(len(self.clf.stateSpace))) 48 | 49 | with open(fname, 'w') as output_file: 50 | output_file.write(code) 51 | -------------------------------------------------------------------------------- /examples/rl/cart_pole_iot.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 gym 18 | import serial.tools.list_ports 19 | from serial import Serial 20 | 21 | def fformat(float): 22 | return str("{:.2f}".format(float)) 23 | 24 | env = gym.make('CartPole-v1') 25 | 26 | print("Available serial ports:") 27 | for i in serial.tools.list_ports.comports(): 28 | print(i) 29 | 30 | ser = Serial('COM11', baudrate=115200) # open serial port 31 | print("Selected port:") 32 | print(ser.name) # check which port was really used 33 | 34 | while True: 35 | observation = env.reset() 36 | 37 | for step in range(500): 38 | env.render() 39 | # send observation to the device 40 | obs_str = ';'.join(map(fformat, observation)) 41 | # For debug purposes 42 | #print("-> " + obs_str) 43 | obs_str = obs_str + "\n" 44 | ser.write(obs_str.encode()) # write a string 45 | 46 | #read action from the device 47 | line = ser.readline() # read a '\n' terminated line 48 | action = int(line) 49 | # For debug purposes 50 | #print("<- " + str(line)) 51 | 52 | observation, reward, done, info = env.step(action) 53 | 54 | if done: 55 | print(" Steps: {}".format(step)) 56 | break 57 | 58 | env.close() 59 | 60 | ser.close() # close port -------------------------------------------------------------------------------- /src/fogml/generators/qlearning_code_generator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 os 18 | from .base_generator import BaseGenerator 19 | 20 | 21 | class QLearningCodeGenerator(BaseGenerator): 22 | skeleton_path = 'skeletons/qlearning_model_skeleton.txt' 23 | 24 | def __init__(self, clf): 25 | self.clf = clf 26 | 27 | @staticmethod 28 | def generate_c_array(array): 29 | states, actions = array.shape 30 | result = "{" 31 | for state in range(states): 32 | for action in range(actions): 33 | result += "%.6f, " % array[state][action] 34 | result += "\n" 35 | result += "}" 36 | return result 37 | 38 | def generate_q_table(self): 39 | return self.generate_c_array(self.clf.Q) 40 | 41 | def generate(self, fname='qlearning_model_test.c', **kwargs): 42 | with open(os.path.join(os.path.dirname(__file__), self.skeleton_path)) as skeleton: 43 | code = skeleton.read() 44 | code = self.license_header() + code 45 | code = code.replace('', self.generate_q_table()) 46 | 47 | code = code.replace('', str(self.clf.states)) 48 | code = code.replace('', str(self.clf.actions)) 49 | 50 | with open(fname, 'w') as output_file: 51 | output_file.write(code) 52 | -------------------------------------------------------------------------------- /examples/rl/FogML_RL_Arduino/qstates_discretizer_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2021 FogML 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 | static float Q_states[] = {-1.600000, 1.600000, 1.000000, 18 | -0.170000, 0.170000, 1.000000, 19 | -0.420000, 0.420000, 6.000000, 20 | -0.870000, 0.870000, 6.000000, 21 | }; 22 | 23 | unsigned int RL_OBSERVATION_SIZE = 4; 24 | 25 | unsigned int *observation_states; 26 | 27 | void fogml_discretizer_init(){ 28 | 29 | observation_states = (float*)malloc(sizeof(unsigned int) * RL_OBSERVATION_SIZE); 30 | } 31 | 32 | unsigned long fogml_discretizer_get_state(float* observation){ 33 | for(int i = 0; i < RL_OBSERVATION_SIZE; i++){ 34 | float window = (Q_states[i*3+1] - Q_states[i*3+0]) / Q_states[i*3+2]; // (max-min) / num 35 | int state = (int)((observation[i] - Q_states[i*3+0]) / window) + 1; 36 | 37 | if (state < 0) { 38 | state = 0; 39 | } 40 | 41 | if (state > Q_states[i*3+2] + 1) { 42 | state = Q_states[i*3+2] + 1; 43 | } 44 | 45 | observation_states[i] = state; 46 | } 47 | 48 | unsigned long result = 0; 49 | unsigned long mul = 1; 50 | 51 | for(int i = 0; i < RL_OBSERVATION_SIZE; i++){ 52 | result = result + mul * observation_states[i]; 53 | mul = mul * (Q_states[i*3+2] + 2); 54 | 55 | } 56 | 57 | return result; 58 | } 59 | -------------------------------------------------------------------------------- /examples/rl/mountain_car_iot.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 gym 18 | import serial.tools.list_ports 19 | from serial import Serial 20 | 21 | def fformat(float): 22 | return str("{:.2f}".format(float)) 23 | 24 | env = gym.make('MountainCar-v0') 25 | 26 | print("Available serial ports:") 27 | for i in serial.tools.list_ports.comports(): 28 | print(i) 29 | 30 | ser = Serial('COM11', baudrate=115200) # open serial port 31 | print("Selected port:") 32 | print(ser.name) # check which port was really used 33 | 34 | while True: 35 | observation = env.reset() 36 | 37 | for step in range(300): 38 | # send observation to the device 39 | obs_str = ';'.join(map(fformat, observation)) 40 | #For debug purposes 41 | #print("-> " + obs_str) 42 | obs_str = obs_str + "\n" 43 | ser.write(obs_str.encode()) # write a string 44 | 45 | #read action from the device 46 | line = ser.readline() # read a '\n' terminated line 47 | action = int(line) 48 | # For debug purposes 49 | #print("<- " + str(action)) 50 | 51 | observation, reward, done, info = env.step(action) 52 | 53 | env.render() 54 | 55 | if done: 56 | if (observation[0] >= 0.5): 57 | print("Success!") 58 | else: 59 | print("Failure!") 60 | print(" Steps: {}".format(step)) 61 | break 62 | 63 | env.close() 64 | 65 | ser.close() # close port -------------------------------------------------------------------------------- /examples/anomaly/1_circle.csv: -------------------------------------------------------------------------------- 1 | 10.25 -1.37 9.97 8.86 4.24 -1.67 242.01 218.04 31.65 3.00 4.00 3.00 2 | 10.54 -1.70 11.05 7.34 4.39 -1.62 230.42 310.40 36.63 3.00 4.00 4.00 3 | 9.30 -0.61 9.40 9.94 3.99 -2.20 231.91 215.98 21.90 3.00 2.00 3.00 4 | 10.59 -1.23 10.10 7.30 3.88 -1.34 218.15 231.06 20.41 3.00 3.00 3.00 5 | 9.14 -1.14 9.84 8.28 3.37 -1.68 213.69 187.96 22.50 2.00 3.00 3.00 6 | 10.51 -1.78 9.91 9.55 3.12 -1.86 299.03 245.40 17.44 3.00 3.00 2.00 7 | 8.08 -1.92 8.83 7.40 3.81 -1.27 188.22 161.87 26.70 3.00 2.00 4.00 8 | 9.10 -0.98 9.72 8.43 3.19 -1.16 231.28 232.53 14.11 3.00 3.00 3.00 9 | 8.71 -1.34 9.63 9.12 2.21 -1.47 171.05 248.90 12.92 3.00 3.00 4.00 10 | 8.28 -2.49 8.05 7.53 2.97 -1.53 135.78 162.02 17.21 3.00 2.00 3.00 11 | 7.90 -0.35 8.27 8.44 3.80 -1.10 166.78 183.30 21.90 3.00 3.00 5.00 12 | 8.23 -1.44 6.97 8.45 2.58 -1.12 148.62 141.70 10.21 3.00 2.00 2.00 13 | 8.01 -2.15 8.78 8.92 3.70 -1.24 180.57 171.00 21.59 2.00 3.00 3.00 14 | 7.16 -0.40 8.93 7.66 1.54 -0.96 117.36 174.01 4.69 3.00 3.00 4.00 15 | 10.26 -1.51 7.52 9.18 2.04 -1.10 188.55 131.35 7.85 3.00 4.00 3.00 16 | 9.88 -1.83 8.72 8.22 2.90 -0.66 225.79 208.37 12.55 3.00 2.00 2.00 17 | 8.36 -0.58 8.83 7.48 2.54 -0.72 216.06 191.10 10.29 3.00 2.00 5.00 18 | 8.92 -0.57 10.28 9.54 3.23 -1.40 228.45 186.81 17.12 2.00 2.00 3.00 19 | 9.89 -1.92 10.40 8.37 4.08 -1.19 273.24 253.99 24.60 3.00 3.00 5.00 20 | 8.47 -1.88 9.09 7.76 2.85 -0.37 174.16 231.51 10.64 3.00 2.00 4.00 21 | 8.17 -0.04 8.62 9.00 3.18 -0.62 143.92 173.67 11.47 3.00 3.00 3.00 22 | 9.10 -2.16 10.50 7.70 4.20 -1.14 232.60 240.91 25.25 3.00 3.00 5.00 23 | 8.60 -1.49 10.03 7.64 2.94 -0.57 188.05 243.93 17.36 3.00 2.00 2.00 24 | 9.56 -0.60 10.40 8.80 3.18 -1.03 195.76 256.46 19.58 3.00 3.00 3.00 25 | 10.58 -1.85 7.92 9.24 2.51 -1.23 267.18 152.98 11.10 2.00 2.00 2.00 26 | 7.69 -1.52 9.11 7.89 3.06 -1.03 173.23 172.36 12.90 2.00 3.00 4.00 27 | 7.22 -1.39 7.92 8.03 2.20 -0.85 109.90 193.13 11.02 3.00 2.00 3.00 28 | 7.70 0.01 8.66 8.27 2.71 -0.79 151.71 189.37 13.61 2.00 3.00 2.00 29 | 7.93 -1.46 7.85 9.46 3.09 -1.04 161.16 153.18 11.22 3.00 2.00 2.00 30 | 8.20 -0.67 8.36 8.27 3.09 -1.07 138.88 192.36 19.14 3.00 3.00 3.00 31 | -------------------------------------------------------------------------------- /examples/rl/FogML_RL_Arduino/FogML_RL_Arduino.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2021 FogML 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 | #include "fogml_rl.h" 18 | 19 | float *observations; 20 | unsigned long state; 21 | unsigned int action; 22 | 23 | // For Arduino Nano BLE 33 24 | #define LED 22 25 | 26 | int led_state; 27 | 28 | void toggle_led(){ 29 | digitalWrite(LED, led_state); 30 | led_state = 1 - led_state; 31 | } 32 | 33 | void setup() { 34 | //initialize serial: 35 | Serial.begin(115200); 36 | 37 | pinMode(LED, OUTPUT); 38 | led_state = 0; 39 | 40 | //states = (float*)malloc(sizeof(float) * RL_STATES); 41 | observations = (float*)malloc(sizeof(float) * RL_STATES); 42 | 43 | fogml_qlearning_init(); 44 | fogml_discretizer_init(); 45 | } 46 | 47 | void loop() { 48 | toggle_led(); 49 | 50 | while (Serial.available() > 0) { 51 | 52 | char c; 53 | for(int i = 0; i < RL_OBSERVATION_SIZE; i++){ 54 | float obs = Serial.parseFloat(SKIP_NONE); 55 | observations[i]=obs; 56 | 57 | //read delimiter or new line 58 | c = Serial.read(); 59 | } 60 | 61 | //after last float should be a new line 62 | if (c == '\n' || c == '\r') { 63 | 64 | if (Serial.peek() == '\n'){ 65 | Serial.read(); 66 | } 67 | 68 | //RL Discretize Observations 69 | state = fogml_discretizer_get_state(observations); 70 | //RL Update State 71 | fogml_qlearning_update_state(state); 72 | //RL Select Action 73 | action = fogml_qlearning_select_action(); 74 | 75 | Serial.println(action); 76 | } else { 77 | Serial.println("ERROR"); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/fogml/generators/generator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 sklearn.ensemble 18 | 19 | from sklearn import * 20 | from sklearn.neural_network import * 21 | 22 | import fogml.rl 23 | import fogml.anomaly 24 | from .anomaly_detector_generator import KMeansAnomalyDetectorGenerator 25 | from .bayes_code_generator import BayesCodeGenerator 26 | from .mlp_code_generator import MlpCodeGenerator 27 | from .qlearning_code_generator import QLearningCodeGenerator 28 | from .qstatesintervals_code_generator import QStatesIntervalsCodeGenerator 29 | from .random_forest_generator import RandomForestCodeGenerator 30 | from .scaler_generator import MinMaxScalerGenerator 31 | from .tree_code_generator import TreeCodeGenerator 32 | 33 | 34 | class GeneratorFactory: 35 | 36 | recognized_classifiers = { 37 | sklearn.tree.DecisionTreeClassifier: TreeCodeGenerator, 38 | sklearn.naive_bayes.GaussianNB: BayesCodeGenerator, 39 | sklearn.neural_network.MLPClassifier: MlpCodeGenerator, 40 | sklearn.ensemble.RandomForestClassifier: RandomForestCodeGenerator, 41 | sklearn.preprocessing.MinMaxScaler: MinMaxScalerGenerator, 42 | fogml.rl.QLearning: QLearningCodeGenerator, 43 | fogml.rl.QStatesIntervals: QStatesIntervalsCodeGenerator, 44 | fogml.anomaly.KMeansAnomalyDetector: KMeansAnomalyDetectorGenerator 45 | } 46 | 47 | def get_generator(self, clf): 48 | if clf.__class__ in self.recognized_classifiers.keys(): 49 | return self.recognized_classifiers[clf.__class__](clf) 50 | else: 51 | print("Sorry, but this is not recognized type of classifier") 52 | return None 53 | -------------------------------------------------------------------------------- /docs/examples.rst: -------------------------------------------------------------------------------- 1 | :parenttoc: True 2 | 3 | Examples 4 | ======== 5 | 6 | Arduino Digit Recognition 7 | ------------------------- 8 | 9 | 10 | .. figure:: ./images/arduino_tft.png 11 | :width: 600 12 | 13 | Zephyr OS 14 | --------- 15 | 16 | The application recognizes gestures an IoT device performs based on the built-in accelerometer. It uses the 17 | classification algorithms and programming tools provided by FogML: 18 | 19 | * FogML-SDK [https://github.com/tszydlo/fogml_sdk] 20 | * FogML tools [https://github.com/tszydlo/FogML] 21 | 22 | The application is implemented for the Zephyr-OS operating system. The tests were carried out on a ``Thingy:91`` 23 | device containing nrf9160 and manufactured by Nordic Semiconductor. 24 | 25 | **Classification** 26 | 27 | The accelerometer data processing pipeline is presented in the figure. 28 | 29 | .. figure:: ./images/examples_zephyr_classification.png 30 | :width: 600 31 | 32 | **Classification pipeline** 33 | 34 | In the example, gestures are classified using the random forest algorithm. The generated classifier source code is in 35 | the ``src/fogml_generated/random_forest_model.c`` file. 36 | 37 | For the device to recognize other gestures, it is necessary to collect training data, assign labels, and train the 38 | model. The entire process is described in Jupyter Notebook, along with training data in the tools directory. Notepad 39 | can be run in eg Google Colab or Anaconda. 40 | 41 | **Anomaly detection** 42 | 43 | The example uses the modification of the K-Means algorithm and z-score to detect anomalies - deviations from training 44 | data. Model generation for this purpose is described in Jupyter Notebook located in the tools directory. 45 | Building 46 | 47 | The application is implemented for the Zephyr OS operating system. Since the application is for ``Thingy:91`` device, 48 | we use NRF Connect SDK 2.0.0 and VSCode plugin, which simplifies the process of building, flashing and debugging 49 | applications for Zephyr. The application can also be built directly using the west tool and the base Zephyr. 50 | 51 | The application uses the basic drivers available in the operating system, and porting to another device should be 52 | relatively easy. 53 | 54 | 55 | Arduino 56 | ------- 57 | 58 | 59 | 60 | 61 | Complex examples 62 | ---------------- 63 | -------------------------------------------------------------------------------- /examples/simple/models/network_model.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "neural_nets.h" 3 | const float pmdata_00[] PROGMEM = {-0.035431, -0.046410, -0.002355, 0.009097, }; 4 | const float pmdata_01[] PROGMEM = {0.387625, 0.601107, -0.278682, -0.069962, }; 5 | const float pmdata_02[] PROGMEM = {-0.017275, -0.000000, -0.046636, 0.002225, }; 6 | const float pmdata_03[] PROGMEM = {0.233183, -0.519569, 0.625201, 1.157216, }; 7 | const float pmdata_10[] PROGMEM = {0.000109, 0.558352, 0.029461, -2.040082, }; 8 | const float pmdata_11[] PROGMEM = {0.000000, 0.366993, -0.008656, -0.624923, }; 9 | const float pmdata_12[] PROGMEM = {-0.000000, -0.875632, 0.000000, 0.348823, }; 10 | int evaluate_network(uint8_t * x){ 11 | float result0[4]; 12 | result0[0]=0; 13 | for(int i=0;i<4;i++){result0[0]+=(float)pgm_read_float_near(pmdata_00+i) * (float)x[i];} 14 | result0[1]=0; 15 | for(int i=0;i<4;i++){result0[1]+=(float)pgm_read_float_near(pmdata_01+i) * (float)x[i];} 16 | result0[2]=0; 17 | for(int i=0;i<4;i++){result0[2]+=(float)pgm_read_float_near(pmdata_02+i) * (float)x[i];} 18 | result0[3]=0; 19 | for(int i=0;i<4;i++){result0[3]+=(float)pgm_read_float_near(pmdata_03+i) * (float)x[i];} 20 | result0[0] = result0[0] + (-0.395456); 21 | result0[1] = result0[1] + (0.519410); 22 | result0[2] = result0[2] + (-0.376649); 23 | result0[3] = result0[3] + (-0.785912); 24 | for (int i=0; i < 4; i++){ if (result0[i] < 0) { result0[i] = 0;}} 25 | float result1[3]; 26 | result1[0]=0; 27 | for(int i=0;i<4;i++){result1[0]+=(float)pgm_read_float_near(pmdata_10+i) * (float)result0[i];} 28 | result1[1]=0; 29 | for(int i=0;i<4;i++){result1[1]+=(float)pgm_read_float_near(pmdata_11+i) * (float)result0[i];} 30 | result1[2]=0; 31 | for(int i=0;i<4;i++){result1[2]+=(float)pgm_read_float_near(pmdata_12+i) * (float)result0[i];} 32 | result1[0] = result1[0] + (1.563457); 33 | result1[1] = result1[1] + (0.325254); 34 | result1[2] = result1[2] + (0.414821); 35 | double max_el = result1[0];for (int i=1; i < 3; i++){max_el = max(max_el, result1[i]);}double exp_sum = 0.0; 36 | for (int i=0; i<3; i++){ exp_sum += exp(result1[i]-max_el); } 37 | for (int i=0; i<3; i++){ result1[i] = exp(result1[i]-max_el) / exp_sum; } 38 | int max_index = 0; 39 | for (int i = 0; i < 3; i++){ if (result1[i] > result1[max_index]){ max_index = i; }} 40 | return max_index; 41 | 42 | }; 43 | -------------------------------------------------------------------------------- /src/fogml/generators/arduino_generator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 | from .utils import generate_c_array 18 | 19 | class ArduinoGenerator: 20 | @staticmethod 21 | def generate_loop_vector_mul_matrix(neurons=10, result_name="r", vector_size=10, vector_name="x", matrix_type="int", 22 | matrix_name="m"): 23 | return "for(int n=0; n<%d; n++){\n" \ 24 | "%s[n]=0;\n" \ 25 | "for(int i=0;i<%d;i++){" \ 26 | "%s[n]+=(%s)pgm_read_%s_near(%s+i*%d+n) * (%s)%s[i];}\n" \ 27 | "}\n" % (neurons, result_name, vector_size, result_name, matrix_type, matrix_type, matrix_name, neurons, 28 | matrix_type, vector_name) 29 | 30 | @staticmethod 31 | def generate_decomposed_loop_vector_mul_matrix(neurons=10, result_name="r", vector_size=10, vector_name="x", 32 | matrix_type="int", matrix_name="m"): 33 | result = "" 34 | for k in range(neurons): 35 | result += "%s[%d]=0;\n" \ 36 | "for(int i=0;i<%d;i++){" \ 37 | "%s[%d]+=(%s)pgm_read_%s_near(%s%d+i) * (%s)%s[i];}\n" % ( 38 | result_name, k, vector_size, result_name, k, matrix_type, matrix_type, matrix_name, k, 39 | matrix_type, vector_name 40 | ) 41 | return result 42 | 43 | @staticmethod 44 | def get_progmem_array(array, arr_type="float", name="arr"): 45 | return "const %s %s[] PROGMEM = %s;\n" % (arr_type, name, generate_c_array(array)) 46 | 47 | @staticmethod 48 | def get_arduino_header(): 49 | return "#include \n" 50 | -------------------------------------------------------------------------------- /src/fogml/generators/tree_code_generator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 numpy as np 18 | 19 | from .base_generator import BaseGenerator 20 | 21 | 22 | class TreeCodeGenerator(BaseGenerator): 23 | def __init__(self, clf): 24 | self.clf = clf 25 | 26 | def generate_statements(self): 27 | tree = self.clf.tree_ 28 | 29 | def recurse(node, depth): 30 | indent = " " * depth 31 | if tree.feature[node] >= 0: 32 | name = tree.feature[node] 33 | threshold = tree.threshold[node] 34 | # TODO: two version on method - for int and floats 35 | # return indent + "if (x[%d] <= " % name + "%d" % threshold + ") {\n" + \ 36 | # recurse(tree.children_left[node], depth + 1) + \ 37 | # indent + "}\n" + indent + "else {\n" + \ 38 | # recurse(tree.children_right[node], depth + 1) + \ 39 | # indent + "}\n" 40 | return indent + "if (x[%d] <= " % name + "%.10f" % threshold + ") {\n" + \ 41 | recurse(tree.children_left[node], depth + 1) + \ 42 | indent + "}\n" + indent + "else {\n" + \ 43 | recurse(tree.children_right[node], depth + 1) + \ 44 | indent + "}\n" 45 | else: 46 | return indent + 'return %s;\n' % str(self.clf.classes_[np.argmax(tree.value[node])]) 47 | 48 | return recurse(0, 1) 49 | 50 | def generate(self, fname = 'tree_model.c', cname="classifier", **kwargs): 51 | with open(fname, 'w') as c_file: 52 | c_file.write(self.license_header()) 53 | c_file.write("int %s(double * x){\n%s};\n" % (cname, self.generate_statements())) 54 | -------------------------------------------------------------------------------- /src/fogml/generators/anomaly_detector_generator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 os 18 | from .base_generator import BaseGenerator 19 | 20 | 21 | class KMeansAnomalyDetectorGenerator(BaseGenerator): 22 | skeleton_path = 'skeletons/kmeans_anomaly_skeleton.txt' 23 | 24 | def __init__(self, clf): 25 | self.clf = clf 26 | 27 | @staticmethod 28 | def generate_c_array(array): 29 | size = len(array) 30 | result = "{" 31 | for pos in range(size): 32 | result += "%.6f" % array[pos] 33 | if (pos < size - 1): 34 | result += ", " 35 | result += "}" 36 | return result 37 | 38 | @staticmethod 39 | def generate_clusters_array(array): 40 | size = len(array) 41 | result = "{" 42 | for pos in range(size): 43 | cluster = array[pos] 44 | for elem in range(len(cluster)): 45 | result += "%.6f" % cluster[elem] 46 | if not (pos == size - 1 and elem == len(cluster) - 1): 47 | result += ", " 48 | if (pos < size - 1): 49 | result += "\n " 50 | result += "}" 51 | return result 52 | 53 | def generate(self, fname='kmeans_anomaly_test.c', **kwargs): 54 | with open(os.path.join(os.path.dirname(__file__), self.skeleton_path)) as skeleton: 55 | code = skeleton.read() 56 | code = self.license_header() + code 57 | code = code.replace('', self.generate_clusters_array(self.clf.clusters)) 58 | code = code.replace('', self.generate_c_array(self.clf.z_scores)) 59 | code = code.replace('', str(len(self.clf.clusters[0]))) 60 | code = code.replace('', str(len(self.clf.clusters))) 61 | 62 | with open(fname, 'w') as output_file: 63 | output_file.write(code) 64 | -------------------------------------------------------------------------------- /docs/generators.rst: -------------------------------------------------------------------------------- 1 | :parenttoc: True 2 | 3 | Code Generators 4 | =============== 5 | 6 | Classification 7 | -------------- 8 | 9 | FogML allows generation of the C source code for simple classifiers trained in the ``scikit-learn library``. The library supports: 10 | 11 | - DecisionTreeClassifier 12 | - GaussianNB 13 | - MLPClassifier 14 | - RandomForestClassifier 15 | 16 | The example generates a classifier code for one of the selected methods: 17 | 18 | .. code-block:: python 19 | 20 | from sklearn import datasets, tree 21 | from fogml.generators import GeneratorFactory 22 | 23 | iris = datasets.load_iris() 24 | X = iris.data 25 | y = iris.target 26 | 27 | #clf = tree.DecisionTreeClassifier(random_state=3456) 28 | #clf = naive_bayes.GaussianNB() 29 | clf = MLPClassifier(hidden_layer_sizes=(4,), random_state=34, solver='adam', max_iter=1500) 30 | #clf = RandomForestClassifier(n_estimators=10) 31 | 32 | clf.fit(X, y) 33 | print( 'accuracy: ',clf.score(X,y)) 34 | 35 | factory = GeneratorFactory() 36 | generator = factory.get_generator(clf) 37 | generator.generate() 38 | 39 | 40 | The main classifier code will be generated in the current folder. Examples of generated files are located in the ``fogML/examples/simple/models directory``. 41 | 42 | Anomaly detection 43 | ----------------- 44 | 45 | The example uses the modification of the *K-Means* algorithm and *z-score* to detect anomalies - deviations from training data. Model generation for this purpose is described in Jupyter Notebook located in the tools directory. 46 | 47 | .. figure:: ./images/anomaly_detection.png 48 | :width: 800 49 | 50 | .. code-block:: python 51 | 52 | from sklearn.preprocessing import MinMaxScaler 53 | from fogml.anomaly import KMeansAnomalyDetector 54 | from fogml.generators import GeneratorFactory 55 | 56 | scaler = MinMaxScaler() 57 | transformer = scaler.fit(spX) 58 | data_norm = transformer.transform(spX) 59 | 60 | anomalyDetector = KMeansAnomalyDetector(n_clusters=16) 61 | anomalyDetector.fit(data_norm) 62 | 63 | factory = GeneratorFactory() 64 | generator = factory.get_generator(transformer) 65 | generator.generate(fname="min_max_scaler_model.c") 66 | 67 | generator = factory.get_generator(anomalyDetector) 68 | generator.generate(fname="kmeans_anomaly_model.c") 69 | 70 | 71 | Copy generated ``kmeans_anomaly_model.c`` and ``min_max_scaler_model.c`` to the folder ``fogml_generated`` in the MCU project. 72 | 73 | Time-series data processing 74 | --------------------------- 75 | 76 | Processing time series from sensors using FogML requires their pre-processing directly on the device. More information 77 | in the SDK and Examples section. 78 | 79 | Reinforcement learning 80 | ---------------------- 81 | 82 | .. figure:: ./images/rl_concept.png 83 | :width: 800 -------------------------------------------------------------------------------- /src/fogml/generators/random_forest_generator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 numpy as np 18 | 19 | from .base_generator import BaseGenerator 20 | 21 | 22 | class RandomForestCodeGenerator(BaseGenerator): 23 | def __init__(self, clf): 24 | self.clf = clf 25 | 26 | def generate_statements(self, tree, index): 27 | 28 | def recurse(node, depth): 29 | indent = " " * depth 30 | if tree.feature[node] >= 0: 31 | name = tree.feature[node] 32 | threshold = tree.threshold[node] 33 | return indent + "if (x[%d] <= " % name + "%.10f" % threshold + ") {\n" + \ 34 | recurse(tree.children_left[node], depth + 1) + \ 35 | indent + "}\n" + indent + "else {\n" + \ 36 | recurse(tree.children_right[node], depth + 1) + \ 37 | indent + "}\n" 38 | else: 39 | return indent + 'results[%s] = %s;\n' % (index, str(self.clf.classes_[np.argmax(tree.value[node])])) 40 | 41 | return recurse(0, 1) 42 | 43 | def generate(self, fname = 'random_forest_model.c', cname="classifier", **kwargs): 44 | result = self.license_header() 45 | result += "int %s(float * x){\n" % cname 46 | result += " int results[%s];\n" % len(self.clf.estimators_) 47 | index = 0 48 | for estimator in self.clf.estimators_: 49 | result += self.generate_statements(estimator.tree_, index) 50 | index += 1 51 | result += " int classes_amount = 0;\n" 52 | result += " for(int i=0; i<%s; i++){\n" % len(self.clf.estimators_) 53 | result += " if(results[i]+1 > classes_amount) classes_amount = results[i]+1;" 54 | result += " }\n" 55 | result += " int result_class = -1;\n" 56 | result += " int max_apperance = 0;\n" 57 | result += " for(int i=0; i 1 [labeldistance=2.5, labelangle=-45, headlabel="True"] ; 8 | 2 [label="X[2] <= 0.08\ngini = 0.56\nsamples = 486\nvalue = [66, 304, 45, 71]", fillcolor="#98f090"] ; 9 | 1 -> 2 ; 10 | 3 [label="X[4] <= 0.537\ngini = 0.707\nsamples = 192\nvalue = [56, 76, 24, 36]", fillcolor="#e4fbe2"] ; 11 | 2 -> 3 ; 12 | 4 [label="(...)", fillcolor="#C0C0C0"] ; 13 | 3 -> 4 ; 14 | 61 [label="(...)", fillcolor="#C0C0C0"] ; 15 | 3 -> 61 ; 16 | 104 [label="X[3] <= 0.259\ngini = 0.378\nsamples = 294\nvalue = [10, 228, 21, 35]", fillcolor="#76ec6b"] ; 17 | 2 -> 104 ; 18 | 105 [label="(...)", fillcolor="#C0C0C0"] ; 19 | 104 -> 105 ; 20 | 134 [label="(...)", fillcolor="#C0C0C0"] ; 21 | 104 -> 134 ; 22 | 203 [label="X[4] <= 0.435\ngini = 0.316\nsamples = 2070\nvalue = [1693, 204, 25, 148]", fillcolor="#ea9a61"] ; 23 | 1 -> 203 ; 24 | 204 [label="X[0] <= 0.115\ngini = 0.299\nsamples = 902\nvalue = [742, 12, 9, 139]", fillcolor="#ea9b63"] ; 25 | 203 -> 204 ; 26 | 205 [label="(...)", fillcolor="#C0C0C0"] ; 27 | 204 -> 205 ; 28 | 220 [label="(...)", fillcolor="#C0C0C0"] ; 29 | 204 -> 220 ; 30 | 535 [label="X[2] <= 0.514\ngini = 0.31\nsamples = 1168\nvalue = [951, 192, 16, 9]", fillcolor="#eb9d65"] ; 31 | 203 -> 535 ; 32 | 536 [label="(...)", fillcolor="#C0C0C0"] ; 33 | 535 -> 536 ; 34 | 711 [label="(...)", fillcolor="#C0C0C0"] ; 35 | 535 -> 711 ; 36 | 986 [label="X[2] <= 0.179\ngini = 0.55\nsamples = 2443\nvalue = [180, 769, 60, 1434]", fillcolor="#efb0f5"] ; 37 | 0 -> 986 [labeldistance=2.5, labelangle=45, headlabel="False"] ; 38 | 987 [label="X[2] <= 0.078\ngini = 0.527\nsamples = 436\nvalue = [57, 287, 43, 49]", fillcolor="#8fef87"] ; 39 | 986 -> 987 ; 40 | 988 [label="X[4] <= 0.489\ngini = 0.684\nsamples = 172\nvalue = [50, 76, 26, 20]", fillcolor="#d8f9d5"] ; 41 | 987 -> 988 ; 42 | 989 [label="(...)", fillcolor="#C0C0C0"] ; 43 | 988 -> 989 ; 44 | 1022 [label="(...)", fillcolor="#C0C0C0"] ; 45 | 988 -> 1022 ; 46 | 1073 [label="X[4] <= 0.411\ngini = 0.344\nsamples = 264\nvalue = [7, 211, 17, 29]", fillcolor="#70eb66"] ; 47 | 987 -> 1073 ; 48 | 1074 [label="(...)", fillcolor="#C0C0C0"] ; 49 | 1073 -> 1074 ; 50 | 1127 [label="(...)", fillcolor="#C0C0C0"] ; 51 | 1073 -> 1127 ; 52 | 1158 [label="X[1] <= 0.751\ngini = 0.462\nsamples = 2007\nvalue = [123, 482, 17, 1385]", fillcolor="#e78af0"] ; 53 | 986 -> 1158 ; 54 | 1159 [label="X[4] <= 0.449\ngini = 0.207\nsamples = 951\nvalue = [21, 78, 9, 843]", fillcolor="#dc51e8"] ; 55 | 1158 -> 1159 ; 56 | 1160 [label="(...)", fillcolor="#C0C0C0"] ; 57 | 1159 -> 1160 ; 58 | 1193 [label="(...)", fillcolor="#C0C0C0"] ; 59 | 1159 -> 1193 ; 60 | 1412 [label="X[2] <= 0.696\ngini = 0.581\nsamples = 1056\nvalue = [102, 404, 8, 542]", fillcolor="#f7d5f9"] ; 61 | 1158 -> 1412 ; 62 | 1413 [label="(...)", fillcolor="#C0C0C0"] ; 63 | 1412 -> 1413 ; 64 | 1590 [label="(...)", fillcolor="#C0C0C0"] ; 65 | 1412 -> 1590 ; 66 | } -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | Welcome to FogML's documentation! 2 | =================================== 3 | 4 | Due to the development of IoT solutions, we can observe the constantly growing number of these devices in almost every aspect of our lives. The machine learning may improve increase their intelligence and smartness. Unfortunately, the highly regarded programming libraries consume to much resources to be ported to the embedded processors. 5 | 6 | The FogML is a set of tools enabling TinyML on microcontrollers as low resource-limited as ARM M0 cores. In contrast to many other frameworks, the FogML utilises classic machine learning methods such as density-based anomaly detection and classifiers based on Bayesian networks, decision forests and vanilla MLP. In addition, it supports off-device learning for the classification problem and on-device learning for anomaly detection. The active learning anomaly detection is based on reservoir sampling and outlier detection algorithms which are trained directly on the device. The dedicated library performs the time series processing on the devices, computing the feature vector consisting of RMS, FFT, amplitude and other low-level signal metrics. One of the techniques used in FogML is source code generation of the inferencing functions for embedded devices. It leads to a much smaller memory footprint than for more computationally advanced solutions such as deep neural networks. 7 | 8 | .. figure:: ./images/algorithms.png 9 | :width: 800 10 | 11 | The structure of the project is as follows: 12 | 13 | * the `src` folder contains the source code generators for machine learning models i.e.: naive bayes, decision trees/forrest and neural nets; 14 | * the `example` folder contains the simple examples and the MNIST digit recognition for Arduino board and the simple TFT touchscreen. 15 | 16 | Examples of anomaly detection and classification algorithms provided by the FogML project for embedded devices: 17 | 18 | * `FogML-SDK` [https://github.com/tszydlo/fogml_sdk] 19 | * `FogML Arduino` [https://github.com/tszydlo/FogML-Arduino] 20 | * `FogML Zephyr OS` [https://github.com/tszydlo/FogML-Zephyr] 21 | 22 | Example of connectivity and device management provided by LwM2M protocol: 23 | 24 | * `FogML-Zephyr-LwM2M` [https://github.com/tszydlo/FogML-Zephyr-LwM2M] 25 | 26 | TinyML lifecycle Management with FOTA using LwM2M protocol: 27 | 28 | * `FogML-Zephyr-FOTA` [https://github.com/tszydlo/FogML-Zephyr-FOTA] 29 | 30 | Papers 31 | ------ 32 | 33 | * *Tomasz Szydlo, Marcin Nagy*, Device management and network connectivity as missing elements in TinyML landscape, *TinyML Research Summit 2023* 34 | 35 | * *Tomasz Szydlo*, Online Anomaly Detection Based On Reservoir Sampling and LOF for IoT devices, *CoRR abs/2206.14265 (2022)* 36 | 37 | * *Tomasz Szydlo, Prem Prakash Jayaraman, Yinhao Li, Graham Morgan, Rajiv Ranjan, TinyRL*, Towards Reinforcement Learning on Tiny Embedded Devices. *CIKM 2022, 4985-4988* 38 | 39 | * *Tomasz Szydlo, Joanna Sendorek, Robert Brzoza-Woch*, Enabling machine learning on resource constrained devices by source code generation of the learned models, *ICCS 2018: 682-694* 40 | 41 | 42 | .. note:: 43 | 44 | This project is under active development. 45 | 46 | .. toctree:: 47 | :maxdepth: 3 48 | :hidden: 49 | :caption: Contents: 50 | 51 | Home 52 | usage 53 | generators 54 | sdk 55 | pipelines 56 | examples 57 | -------------------------------------------------------------------------------- /examples/simple/models/iris_network_model.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "neural_nets.h" 3 | const float pmdata_00[] PROGMEM = {-0.482009, -0.280405, -0.177204, 0.454541, }; 4 | const float pmdata_01[] PROGMEM = {0.281752, -0.240362, 0.384827, -0.657338, }; 5 | const float pmdata_02[] PROGMEM = {-0.408962, -0.516171, -0.164047, 0.422353, }; 6 | const float pmdata_03[] PROGMEM = {0.373066, 0.401159, -0.363002, -0.397834, }; 7 | const float pmdata_04[] PROGMEM = {-0.515531, 0.358335, 0.160756, -0.011017, }; 8 | const float pmdata_05[] PROGMEM = {0.470599, 0.000930, -0.152029, -0.476776, }; 9 | const float pmdata_06[] PROGMEM = {-0.030902, 0.489950, 0.158440, -0.396958, }; 10 | const float pmdata_07[] PROGMEM = {-0.256793, 0.339329, -0.093367, -0.008804, }; 11 | const float pmdata_10[] PROGMEM = {-0.091420, -0.372177, 0.091856, 0.244726, 0.017972, -0.532419, -0.253732, 0.109035, }; 12 | const float pmdata_11[] PROGMEM = {-0.410996, -0.282101, -0.252966, -0.870360, -0.353737, 0.236617, 0.395521, 0.330120, }; 13 | const float pmdata_12[] PROGMEM = {0.543101, 0.193929, 0.287204, -0.900307, -0.229414, 0.413531, -0.772979, -0.174224, }; 14 | int evaluate_network(uint8_t * x){ 15 | float result0[8]; 16 | result0[0]=0; 17 | for(int i=0;i<4;i++){result0[0]+=(float)pgm_read_float_near(pmdata_00+i) * (float)x[i];} 18 | result0[1]=0; 19 | for(int i=0;i<4;i++){result0[1]+=(float)pgm_read_float_near(pmdata_01+i) * (float)x[i];} 20 | result0[2]=0; 21 | for(int i=0;i<4;i++){result0[2]+=(float)pgm_read_float_near(pmdata_02+i) * (float)x[i];} 22 | result0[3]=0; 23 | for(int i=0;i<4;i++){result0[3]+=(float)pgm_read_float_near(pmdata_03+i) * (float)x[i];} 24 | result0[4]=0; 25 | for(int i=0;i<4;i++){result0[4]+=(float)pgm_read_float_near(pmdata_04+i) * (float)x[i];} 26 | result0[5]=0; 27 | for(int i=0;i<4;i++){result0[5]+=(float)pgm_read_float_near(pmdata_05+i) * (float)x[i];} 28 | result0[6]=0; 29 | for(int i=0;i<4;i++){result0[6]+=(float)pgm_read_float_near(pmdata_06+i) * (float)x[i];} 30 | result0[7]=0; 31 | for(int i=0;i<4;i++){result0[7]+=(float)pgm_read_float_near(pmdata_07+i) * (float)x[i];} 32 | result0[0] = result0[0] + (0.555641); 33 | result0[1] = result0[1] + (0.479223); 34 | result0[2] = result0[2] + (0.475145); 35 | result0[3] = result0[3] + (0.188473); 36 | result0[4] = result0[4] + (0.453912); 37 | result0[5] = result0[5] + (0.463577); 38 | result0[6] = result0[6] + (0.492588); 39 | result0[7] = result0[7] + (-0.066003); 40 | for (int i=0; i < 8; i++){ if (result0[i] < 0) { result0[i] = 0;}} 41 | float result1[3]; 42 | result1[0]=0; 43 | for(int i=0;i<8;i++){result1[0]+=(float)pgm_read_float_near(pmdata_10+i) * (float)result0[i];} 44 | result1[1]=0; 45 | for(int i=0;i<8;i++){result1[1]+=(float)pgm_read_float_near(pmdata_11+i) * (float)result0[i];} 46 | result1[2]=0; 47 | for(int i=0;i<8;i++){result1[2]+=(float)pgm_read_float_near(pmdata_12+i) * (float)result0[i];} 48 | result1[0] = result1[0] + (0.135601); 49 | result1[1] = result1[1] + (-0.478698); 50 | result1[2] = result1[2] + (0.547264); 51 | double max_el = result1[0];for (int i=1; i < 3; i++){max_el = max(max_el, result1[i]);}double exp_sum = 0.0; 52 | for (int i=0; i<3; i++){ exp_sum += exp(result1[i]-max_el); } 53 | for (int i=0; i<3; i++){ result1[i] = exp(result1[i]-max_el) / exp_sum; } 54 | int max_index = 0; 55 | for (int i = 0; i < 3; i++){ if (result1[i] > result1[max_index]){ max_index = i; }} 56 | return max_index; 57 | 58 | }; 59 | -------------------------------------------------------------------------------- /src/fogml/generators/bayes_code_generator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 math 18 | 19 | import numpy as np 20 | import os 21 | 22 | from .base_generator import BaseGenerator 23 | 24 | 25 | class BayesCodeGenerator(BaseGenerator): 26 | 27 | skeleton_path = 'skeletons/bayes_skeleton.txt' 28 | 29 | def __init__(self, clf): 30 | self.clf = clf 31 | 32 | @staticmethod 33 | def generate_c_matrix(matrix): 34 | result = "{\n" 35 | for i in range(matrix.shape[0]): 36 | result += "{" 37 | for j in range(matrix.shape[1]): 38 | result += "%.6f, " % matrix[i][j] 39 | result += "},\n" 40 | result += "}" 41 | return result 42 | 43 | @staticmethod 44 | def generate_c_array(array): 45 | result = "{" 46 | for i in range(len(array)): 47 | result += "%.6f, " % array[i] 48 | result += "}" 49 | return result 50 | 51 | def generate_sigma_code(self): 52 | return self.generate_c_matrix(self.clf.var_) 53 | 54 | def generate_theta_code(self): 55 | return self.generate_c_matrix(self.clf.theta_) 56 | 57 | def generate_log_sigma_code(self): 58 | return self.generate_c_matrix(self.calculate_log_sigma()) 59 | 60 | def generate_log_priors_code(self): 61 | return self.generate_c_array(self.calculate_log_priors()) 62 | 63 | def calculate_log_sigma(self): 64 | log_sigma = np.copy(self.clf.var_) 65 | for i in range(log_sigma.shape[0]): 66 | for j in range(log_sigma.shape[1]): 67 | log_sigma[i][j] = math.log(2 * math.pi * self.clf.var_[i][j]) 68 | return log_sigma 69 | 70 | def calculate_log_priors(self): 71 | log_priors = [] 72 | for prior in self.clf.class_prior_: 73 | log_priors.append(math.log(prior)) 74 | return log_priors 75 | 76 | def generate(self, fname = 'bayes_model.c', cname="classifier", **kwargs): 77 | classes_num = len(self.clf.classes_) 78 | features_num = self.clf.var_.shape[1] 79 | 80 | with open(os.path.join(os.path.dirname(__file__), self.skeleton_path)) as skeleton: 81 | code = skeleton.read() 82 | code = self.license_header() + code 83 | code = code.replace('', str(classes_num)) 84 | code = code.replace('', str(features_num)) 85 | code = code.replace('', self.generate_sigma_code()) 86 | code = code.replace('', self.generate_theta_code()) 87 | code = code.replace('', self.generate_log_sigma_code()) 88 | code = code.replace('', self.generate_log_priors_code()) 89 | 90 | code = code.replace('', cname) 91 | 92 | with open(fname, 'w') as output_file: 93 | output_file.write(code) 94 | -------------------------------------------------------------------------------- /examples/rl/mountain_car.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 gym 18 | 19 | from fogml.generators import GeneratorFactory 20 | from fogml.rl.qlearning import QLearning, QStatesCustom, QStatesIntervals 21 | 22 | env = gym.make('MountainCar-v0') 23 | 24 | #create QStates discretizer table using QStatesCustom() 25 | #stateSpace = [ 26 | # [x / 100 for x in range(-120, 60+1, 10)], 27 | # [x / 1000 for x in range(-70, 70+1, 10)] 28 | #] 29 | #print(stateSpace) 30 | #qStates = QStatesCustom(stateSpace) 31 | 32 | #create QStates discretizer table using QStatesIntervals() 33 | stateSpace = [ 34 | [-1.2, 0.6, 20], 35 | [-0.07, 0.07, 20] 36 | ] 37 | print(stateSpace) 38 | qStates = QStatesIntervals(stateSpace) 39 | 40 | print("Number of states = {}".format(qStates.getStates())) 41 | 42 | #create QLearning agent 43 | qAgent = QLearning(qStates.getStates(), env.action_space.n) 44 | 45 | mean_reward = 0 46 | succesful = 0 47 | 48 | EPSILON = 0.6 49 | EPISODES = 3500 50 | EXPLOITATION_EPISODE = 3000 51 | 52 | epsilon = EPSILON 53 | 54 | # env is created, now we can use it: 55 | for episode in range(EPISODES): 56 | if epsilon > 0: 57 | epsilon = EPSILON - (episode / EXPLOITATION_EPISODE) * EPSILON 58 | qAgent.setEpsilon(epsilon) 59 | 60 | if episode % 100 == 0: 61 | print("Episode {}".format(episode)) 62 | print(" Mean reward {}".format(mean_reward/100)) 63 | print(" Succesfull {}".format(succesful)) 64 | print(" Epsilon {}".format(epsilon)) 65 | mean_reward = 0 66 | succesful = 0 67 | 68 | observation = env.reset() 69 | 70 | qAgent.updateState(qStates.getState(observation)) 71 | 72 | for step in range(300): 73 | if episode % 100 == 0: 74 | env.render() 75 | 76 | if episode < EXPLOITATION_EPISODE: 77 | action = qAgent.selectActionWithExploration() 78 | else: 79 | action = qAgent.selectAction() 80 | 81 | observation, reward, done, info = env.step(action) 82 | 83 | new_state = qStates.getState(observation) 84 | 85 | if episode < EXPLOITATION_EPISODE: 86 | qAgent.update(action, new_state, reward) 87 | else: 88 | qAgent.updateState(new_state) 89 | 90 | mean_reward = mean_reward + reward 91 | 92 | if done: 93 | if (observation[0] >= 0.5): 94 | qAgent.updateQ(previous_state, action, 0) 95 | succesful = succesful + 1 96 | break 97 | 98 | previous_state = new_state 99 | 100 | env.close() 101 | 102 | print(qAgent.getQ()) 103 | 104 | factory = GeneratorFactory() 105 | 106 | generatorQAgent = factory.get_generator(qAgent) 107 | generatorQStates = factory.get_generator(qStates) 108 | 109 | generatorQAgent.generate(fname='FogML_RL_Arduino\qlearning_model_test.c') 110 | generatorQStates.generate(fname = 'FogML_RL_Arduino\qstates_discretizer_test.c') 111 | -------------------------------------------------------------------------------- /docs/sdk.rst: -------------------------------------------------------------------------------- 1 | SDK for devices 2 | =============== 3 | 4 | SDK implements FogML [https://github.com/tszydlo/FogML] machine learning algorithms for resource-constrained devices. 5 | 6 | Available modules and algorithms: 7 | 8 | * anomaly_rt 9 | * reservoir sampling, Local Outliner Factor 10 | * classifier 11 | * header file for the source code generated with FogML 12 | * dsp 13 | * digital signal processing algorithms for initial time series analysis 14 | * ports 15 | * code needed to port the SDK to various frameworks - supports Arduino and Zephyr OS 16 | * rl 17 | * header files for the reinforcement learning source code generated with FogML 18 | 19 | DSP Processing logic 20 | -------------------- 21 | 22 | .. figure:: ./images/dsp_concept.png 23 | :width: 800 24 | 25 | DSP Processing blocks 26 | --------------------- 27 | 28 | .. code-block:: cpp 29 | 30 | #define TINYML_DSP_FFT 1 //FFT 1 peak 31 | #define TINYML_DSP_BASE 2 // (max-min), mean 32 | #define TINYML_DSP_VARIANCE 3 //variance 33 | #define TINYML_DSP_ENERGY 4 //energy 34 | #define TINYML_DSP_CROSSINGS 5 //crossing through mean 35 | #define TINYML_DSP_MINMAX 6 //min, max 36 | 37 | 38 | Each block generates the predefined number of features for each axis: 39 | 40 | .. code-block:: cpp 41 | 42 | #define TINYML_DSP_FFT_LEN 2 // amp, freq 43 | #define TINYML_DSP_BASE_LEN 2 // (max-min), mean 44 | #define TINYML_DSP_VARIANCE_LEN 1 //variance 45 | #define TINYML_DSP_ENERGY_LEN 1 //energy 46 | #define TINYML_DSP_CROSSINGS_LEN 1 //crossing through mean 47 | #define TINYML_DSP_MINMAX_LEN 2 //min, max 48 | 49 | DSP Block configuration 50 | ----------------------- 51 | .. code-block:: cpp 52 | 53 | //BLOCK 1 - BASE 54 | tinyml_block_base_config_t block1_config; 55 | tinyml_dsp_block_t block1 = { 56 | .type = TINYML_DSP_BASE, 57 | .config = &block1_config 58 | }; 59 | 60 | //BLOCK 2 - ENERGY 61 | tinyml_block_energy_config_t block2_config; 62 | tinyml_dsp_block_t block2 = { 63 | .type = TINYML_DSP_ENERGY, 64 | .config = &block2_config 65 | }; 66 | 67 | //BLOCK 3 - CROSSINGS 68 | tinyml_block_crossings_config_t block3_config = { 69 | .threshold = 0.01 70 | }; 71 | tinyml_dsp_block_t block3 = { 72 | .type = TINYML_DSP_CROSSINGS, 73 | .config = &block3_config 74 | }; 75 | 76 | DSP configuration 77 | ----------------- 78 | 79 | .. code-block:: cpp 80 | 81 | // DATA ACQUISITION 82 | // accelerometer data X, Y, Z 83 | #define ACC_TIME_TICKS 64 84 | #define ACC_AXIS 3 85 | 86 | // number of features - depends on the DSP blocks 87 | #define FOGML_VECTOR_SIZE ((TINYML_DSP_BASE_LEN + TINYML_DSP_ENERGY_LEN + TINYML_DSP_CROSSINGS_LEN) * ACC_AXIS) 88 | 89 | //DSP config 90 | tinyml_dsp_block_t *blocks_tab[] = {&block1, &block2, &block3}; 91 | 92 | tinyml_dsp_config_t my_dsp_config = { 93 | .time_ticks = ACC_TIME_TICKS, 94 | .axis_n = 3, 95 | .blocks_num = 3, 96 | .blocks = blocks_tab 97 | }; 98 | 99 | 100 | SDK usage 101 | --------- 102 | .. code-block:: cpp 103 | :emphasize-lines: 3,6 104 | 105 | void fogml_classification(float *time_series_data) { 106 | float *vector = (float*)malloc(sizeof(float) * FOGML_VECTOR_SIZE); 107 | tinyml_dsp(time_series_data, vector, &my_dsp_config); 108 | int cl; 109 | 110 | cl = classifier(vector); 111 | 112 | #ifdef FOGML_VERBOSE 113 | fogml_printf("Detected class = "); 114 | fogml_printf_int(cl); 115 | fogml_printf("\n"); 116 | #endif 117 | 118 | free(vector); 119 | } 120 | 121 | -------------------------------------------------------------------------------- /examples/rl/cart_pole.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 | import math 17 | 18 | import gym 19 | import numpy as np 20 | 21 | from fogml.generators import GeneratorFactory 22 | from fogml.rl.qlearning import QLearning, QStatesIntervals 23 | 24 | env = gym.make('CartPole-v1') 25 | 26 | #After optimization 27 | #stateSpace = [ 28 | # [-1.6, 1.6, 1], 29 | # [-0.17, 0.17, 1], 30 | # [-0.28, 0.28, 4], 31 | # [-0.58, 0.58, 4], 32 | #] 33 | 34 | stateSpace = [ 35 | [-1.6, 1.6, 1], 36 | [-0.17, 0.17, 1], 37 | [-0.42, 0.42, 6], 38 | [-0.87, 0.87, 6], 39 | ] 40 | 41 | print(stateSpace) 42 | qStates = QStatesIntervals(stateSpace) 43 | 44 | print("Number of states = {}".format(qStates.getStates())) 45 | 46 | #create QLearning agent 47 | qAgent = QLearning(qStates.getStates(), env.action_space.n, learning_rate=0.2, discount=1.0, zeros=True) 48 | 49 | EXPLOITATION_EPISODE = 800 50 | EPISODES = 1200 51 | STEPS = 500 52 | 53 | mean_reward = 0 54 | 55 | obs_track=[] 56 | 57 | for episode in range(EPISODES): 58 | if episode < EXPLOITATION_EPISODE: 59 | epsilon = max(0.1, min(1., 1. - math.log10((episode + 1) / 25))) 60 | else: 61 | epsilon = 0 62 | qAgent.setEpsilon(epsilon) 63 | qAgent.setLearningRate(epsilon) 64 | 65 | if episode % 100 == 0: 66 | print("Episode {}".format(episode)) 67 | print(" Mean reward {}".format(mean_reward/100)) 68 | print(" Epsilon {}".format(epsilon)) 69 | mean_reward = 0 70 | observation = env.reset() 71 | 72 | qAgent.updateState(qStates.getState(observation)) 73 | 74 | for step in range(STEPS): 75 | if episode % 100 == 0: 76 | env.render() 77 | 78 | if episode < EXPLOITATION_EPISODE: 79 | action = qAgent.selectActionWithExploration() 80 | else: 81 | action = qAgent.selectAction() 82 | 83 | observation, reward, done, info = env.step(action) 84 | 85 | obs_track.append(observation) 86 | 87 | new_state = qStates.getState(observation) 88 | 89 | mean_reward = mean_reward + reward 90 | 91 | if episode < EXPLOITATION_EPISODE: 92 | #reward 93 | qAgent.update(action, new_state, reward) 94 | else: 95 | qAgent.updateState(new_state) 96 | 97 | if done: 98 | break 99 | 100 | env.close() 101 | 102 | print("Statistics to optimize the state discretizer function:") 103 | obs_track = np.array(obs_track) 104 | percentiles = np.percentile(obs_track, [2.5,97.5], axis=0) 105 | print(" min :" + str(obs_track.min(axis=0))) 106 | print(" low 95% CI :" + str(percentiles[0])) 107 | print(" mean :" + str(obs_track.mean(axis=0))) 108 | print(" high 95% CI:" + str(percentiles[1])) 109 | print(" max :" + str(obs_track.max(axis=0))) 110 | 111 | factory = GeneratorFactory() 112 | 113 | generatorQAgent = factory.get_generator(qAgent) 114 | generatorQStates = factory.get_generator(qStates) 115 | 116 | generatorQAgent.generate(fname='FogML_RL_Arduino\qlearning_model_test.c') 117 | generatorQStates.generate(fname = 'FogML_RL_Arduino\qstates_discretizer_test.c') 118 | 119 | -------------------------------------------------------------------------------- /src/fogml/rl/qlearning.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | class QLearning(): 5 | def __init__(self, states, actions, learning_rate=0.1, discount=0.8, epsilon=0.2, zeros=True): 6 | self.states = states 7 | self.actions = actions 8 | self.learning_rate = learning_rate 9 | self.discount = discount 10 | self.epsilon = epsilon 11 | 12 | if zeros: 13 | self.Q = np.zeros((states, actions)) 14 | else: 15 | self.Q = np.random.rand(states, actions) 16 | 17 | def setEpsilon(self, epsilon): 18 | self.epsilon = epsilon 19 | 20 | def setLearningRate(self, learning_rate): 21 | self.learning_rate = learning_rate 22 | 23 | def selectActionWithExploration(self): 24 | if np.random.random() > self.epsilon: 25 | action = np.argmax(self.Q[self.state]) 26 | else: 27 | action = np.random.randint(0, self.actions) 28 | return action 29 | 30 | def selectAction(self): 31 | action = np.argmax(self.Q[self.state]) 32 | return action 33 | 34 | def updateState(self, state): 35 | self.state = state 36 | 37 | def update(self, action, newState, reward): 38 | self.Q[self.state][action] = self.Q[self.state][action] + self.learning_rate * ( 39 | reward + 40 | self.discount * np.max(self.Q[newState]) - self.Q[self.state][action] 41 | ) 42 | self.state = newState 43 | 44 | def updateQ(self, state, action, reward): 45 | self.Q[state][action] = reward 46 | 47 | def getQ(self): 48 | return self.Q 49 | 50 | 51 | class QStatesCustom(): 52 | def __init__(self, stateSpace): 53 | self.stateSpace = stateSpace 54 | 55 | self.states = 1 56 | self.nums = [] 57 | 58 | for feature in stateSpace: 59 | self.nums.append(len(feature) + 1) 60 | self.states = self.states * (len(feature) + 1) 61 | 62 | def getStates(self): 63 | return self.states 64 | 65 | def getState(self, features): 66 | features_states = [] 67 | 68 | for index in range(len(features)): 69 | for index_val in range(len(self.stateSpace[index])): 70 | found = False 71 | if features[index] <= self.stateSpace[index][index_val]: 72 | features_states.append(index_val) 73 | found = True 74 | break 75 | if not found: 76 | features_states.append(len(self.stateSpace[index])) 77 | 78 | result = 0 79 | mul = 1 80 | for index in range(len(features_states)): 81 | result = result + mul * features_states[index] 82 | 83 | mul = mul * self.nums[index] 84 | 85 | return result 86 | 87 | 88 | class QStatesIntervals(): 89 | def __init__(self, stateSpace): 90 | self.stateSpace = stateSpace 91 | 92 | self.states = 1 93 | self.nums = [] 94 | 95 | for feature in stateSpace: 96 | self.nums.append(feature[2]) 97 | self.states = self.states * (feature[2] + 2) 98 | 99 | def getStates(self): 100 | return self.states 101 | 102 | def getState(self, observation): 103 | observation_states = [] 104 | 105 | for index in range(len(observation)): 106 | window = (self.stateSpace[index][1] - self.stateSpace[index][0]) / self.stateSpace[index][2] # (max-min) / num 107 | state = int((observation[index] - self.stateSpace[index][0]) / window) + 1 108 | 109 | if state < 0: 110 | state = 0 111 | if state > self.stateSpace[index][2] + 1: 112 | state = self.stateSpace[index][2] + 1 113 | 114 | observation_states.append(state) 115 | 116 | result = 0 117 | mul = 1 118 | for index in range(len(observation_states)): 119 | result = result + mul * observation_states[index] 120 | 121 | mul = mul * (self.nums[index] + 2) 122 | 123 | return result 124 | -------------------------------------------------------------------------------- /examples/simple/mnist.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 | from sklearn import tree, naive_bayes 18 | from sklearn.datasets import load_digits 19 | from sklearn.model_selection import train_test_split 20 | import numpy as np 21 | import pickle 22 | from sklearn.neural_network import MLPClassifier 23 | 24 | from fogml.generators import GeneratorFactory 25 | 26 | X_TEST = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30 | 0, 0, 0, 0, 0, 0, 0, 0, 20, 158, 227, 240, 236, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31 | 0, 22, 179, 248, 254, 254, 254, 246, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 123, 243, 32 | 254, 196, 192, 172, 254, 206, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 183, 254, 254, 239, 33 | 14, 0, 111, 246, 81, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 153, 234, 254, 254, 254, 32, 0, 61, 34 | 249, 243, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 78, 254, 254, 254, 254, 254, 81, 64, 234, 162, 35 | 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 54, 254, 254, 251, 166, 179, 157, 232, 163, 8, 0, 0, 0, 36 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 178, 254, 254, 254, 215, 252, 140, 7, 0, 0, 0, 0, 0, 0, 0, 0, 37 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 176, 254, 254, 254, 254, 191, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 38 | 0, 0, 0, 0, 0, 0, 0, 0, 10, 187, 254, 223, 136, 220, 254, 237, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39 | 0, 0, 0, 0, 78, 237, 254, 252, 85, 0, 25, 158, 254, 239, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 58, 40 | 203, 252, 246, 81, 8, 0, 0, 0, 2, 90, 254, 185, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 58, 237, 254, 254, 41 | 125, 0, 0, 0, 0, 0, 0, 53, 254, 218, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 149, 254, 211, 102, 6, 0, 0, 0, 42 | 0, 0, 0, 102, 254, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 149, 254, 108, 0, 0, 0, 0, 0, 0, 0, 54, 238, 43 | 246, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 137, 254, 35, 0, 0, 0, 0, 0, 0, 35, 185, 254, 155, 0, 0, 0, 44 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 34, 233, 163, 72, 6, 0, 0, 0, 54, 213, 254, 203, 8, 0, 0, 0, 0, 0, 0, 0, 45 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 34, 185, 254, 205, 121, 52, 135, 255, 254, 164, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 46 | 0, 0, 0, 0, 0, 0, 0, 0, 13, 113, 197, 254, 254, 254, 221, 133, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 47 | 0, 0, 0, 0, 0, 0, 9, 79, 82, 227, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 50 | 51 | 52 | factory = GeneratorFactory() 53 | mnist = load_digits() 54 | x_train, x_test, y_train, y_test = train_test_split(mnist.data, mnist.target, test_size=0.1, random_state=2) 55 | 56 | # clf = tree.DecisionTreeClassifier(random_state=3456) 57 | clf = naive_bayes.GaussianNB() 58 | # clf = MLPClassifier(hidden_layer_sizes=(15,), random_state=34) 59 | 60 | generator = factory.get_generator(clf) 61 | 62 | clf.fit(x_train, y_train) 63 | 64 | dumped = pickle.dumps(clf) 65 | print("SIZE: " + str(len(dumped))) 66 | 67 | generator.generate() 68 | 69 | print(clf.score(x_test, y_test)) 70 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FogML 2 | 3 | Due to the development of IoT solutions, we can observe the constantly growing number of these devices in almost every aspect of our lives. The machine learning may improve increase their intelligence and smartness. Unfortunately, the highly regarded programming libraries consume to much resources to be ported to the embedded processors. 4 | 5 | The FogML is a set of tools enabling TinyML on microcontrollers as low resource-limited as ARM M0 cores. In contrast to many other frameworks, the FogML utilises classic machine learning methods such as density-based anomaly detection and classifiers based on Bayesian networks, decision forests and vanilla MLP. In addition, it supports off-device learning for the classification problem and on-device learning for anomaly detection. The active learning anomaly detection is based on reservoir sampling and outlier detection algorithms which are trained directly on the device. The dedicated library performs the time series processing on the devices, computing the feature vector consisting of RMS, FFT, amplitude and other low-level signal metrics. One of the techniques used in FogML is source code generation of the inferencing functions for embedded devices. It leads to a much smaller memory footprint than for more computationally advanced solutions such as deep neural networks. 6 | 7 | ![Classification pipeline](docs/images/algorithms.png) 8 | 9 | The structure of the project is as follows: 10 | * the `src` folder contains the source code generators for machine learning models i.e.: naive bayes, decision trees/forrest and neural nets; 11 | * the `example` folder contains the simple examples and the MNIST digit recognition for Arduino board and the simple TFT touchscreen. 12 | 13 | Examples of anomaly detection and classification algorithms provided by the FogML project for embedded devices: 14 | * `FogML-SDK` [https://github.com/tszydlo/fogml_sdk] 15 | * `FogML Arduino` [https://github.com/tszydlo/FogML-Arduino] 16 | * `FogML Zephyr OS` [https://github.com/tszydlo/FogML-Zephyr] 17 | * `FogML ISPU` [https://github.com/tszydlo/FogML-ISPU] 18 | 19 | Example of connectivity and device management provided by LwM2M protocol: 20 | * `FogML-Zephyr-LwM2M` [https://github.com/tszydlo/FogML-Zephyr-LwM2M] 21 | 22 | TinyML lifecycle Management with FOTA using LwM2M protocol: 23 | * `FogML-Zephyr-FOTA` [https://github.com/tszydlo/FogML-Zephyr-FOTA] 24 | 25 | ## Usage 26 | 27 | `pip install fogml` 28 | 29 | 30 | ## Example 31 | 32 | ``` 33 | from sklearn import datasets, tree 34 | 35 | from fogml.generators import GeneratorFactory 36 | 37 | iris = datasets.load_iris() 38 | X = iris.data 39 | y = iris.target 40 | 41 | clf = tree.DecisionTreeClassifier(random_state=3456) 42 | clf.fit(X, y) 43 | print( 'accuracy: ',clf.score(X,y)) 44 | 45 | factory = GeneratorFactory() 46 | generator = factory.get_generator(clf) 47 | generator.generate() 48 | ``` 49 | 50 | ## Reinforcement Learning 51 | 52 | ``` 53 | import gym 54 | 55 | from fogml.generators import GeneratorFactory 56 | from fogml.rl.qlearning import QLearning, QStatesIntervals 57 | 58 | env = gym.make('MountainCar-v0') 59 | 60 | #create QStates discretizer table using QStatesIntervals() 61 | stateSpace = [ 62 | [-1.2, 0.6, 20], 63 | [-0.07, 0.07, 20] 64 | ] 65 | qStates = QStatesIntervals(stateSpace) 66 | 67 | #create QLearning agent 68 | qAgent = QLearning(qStates.getStates(), env.action_space.n) 69 | 70 | for episode in range(EPISODES): 71 | #TODO Train the model 72 | #see examples 73 | 74 | factory = GeneratorFactory() 75 | 76 | generatorQAgent = factory.get_generator(qAgent) 77 | generatorQStates = factory.get_generator(qStates) 78 | 79 | generatorQAgent.generate(fname='FogML_RL_Arduino\qlearning_model_test.c') 80 | generatorQStates.generate(fname = 'FogML_RL_Arduino\qstates_discretizer_test.c') 81 | 82 | ``` 83 | 84 | See it in action: 85 | https://www.youtube.com/watch?v=yEr5tjBrY70 86 | 87 | ## FogML research 88 | 89 | If you think that the project is interesting to you, please cite the papers: 90 | 91 | _Tomasz Szydlo, Marcin Nagy, Device management and network connectivity as missing elements in TinyML landscape, TinyML Research Summit 2023_ 92 | 93 | _Tomasz Szydlo, Online Anomaly Detection Based On Reservoir Sampling and LOF for IoT devices, CoRR abs/2206.14265 (2022)_ 94 | 95 | _Tomasz Szydlo, Prem Prakash Jayaraman, Yinhao Li, Graham Morgan, Rajiv Ranjan, TinyRL: Towards Reinforcement Learning on Tiny Embedded Devices. CIKM 2022, 4985-4988_ 96 | 97 | _Tomasz Szydlo, Joanna Sendorek, Robert Brzoza-Woch, Enabling machine learning on resource constrained devices by source code generation of the learned models, ICCS 2018: 682-694_ 98 | 99 | 100 | ## Press 101 | https://blog.arduino.cc/2022/07/22/industrial-iot-anomaly-detection-on-microcontrollers/ 102 | -------------------------------------------------------------------------------- /src/fogml/generators/mlp_code_generator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2021 FogML 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 | from .arduino_generator import ArduinoGenerator 18 | from .base_generator import BaseGenerator 19 | from .utils import generate_c_matrix, generate_c_array, generate_c_function 20 | 21 | 22 | class MlpCodeGenerator(BaseGenerator): 23 | def __init__(self, clf): 24 | self.clf = clf 25 | 26 | @staticmethod 27 | def generate_list_of_matrixes(matrixes_list): 28 | result = "{" 29 | for i in range(len(matrixes_list)): 30 | result += generate_c_matrix(matrixes_list[i]) + ", " 31 | result += "}" 32 | return result 33 | 34 | @staticmethod 35 | def generate_list_of_arrays(arrays_list): 36 | result = "{" 37 | for i in range(len(arrays_list)): 38 | result += generate_c_array(arrays_list[i]) + ", " 39 | result += "}" 40 | return result 41 | 42 | @staticmethod 43 | def generate_vector_mul_matrix(matrix, vector_name="x", result_name="r"): 44 | instruction = "" 45 | for j in range(matrix.shape[1]): 46 | instruction += "%s[%d] = " % (result_name, j) 47 | for p in range(matrix.shape[0]): 48 | if abs(matrix[p][j] - 0.0) > 1e-5: 49 | instruction += " + (%f) * %s[%d]" % (matrix[p][j], vector_name, p) 50 | if p == matrix.shape[0] - 1: 51 | instruction += ";\n" 52 | return instruction 53 | 54 | @staticmethod 55 | def generate_vector_add_vector(vector1, vector2_name="x", result_name="r"): 56 | instruction = "" 57 | for i in range(vector1.shape[0]): 58 | instruction += "%s[%d] = %s[%d] + (%f);\n" % (result_name, i, vector2_name, i, vector1[i]) 59 | return instruction 60 | 61 | @staticmethod 62 | def generate_vector_declaration(name="v", size=10, vector_type="int"): 63 | return "%s %s[%d];\n" % (vector_type, name, size) 64 | 65 | @staticmethod 66 | def generate_relu_activation_on_vector(size=10, vector_name="v"): 67 | return "for (int i=0; i < %d; i++){ if (%s[i] < 0) { %s[i] = 0;}}\n" % (size, vector_name, vector_name) 68 | 69 | @staticmethod 70 | def generate_softmax_activation_on_vector(size=10, vector_name="v"): 71 | return "float max_el = %s[0];" \ 72 | "for (int i=1; i < %d; i++){max_el = max(max_el, %s[i]);}" \ 73 | "float exp_sum = 0.0; \n" \ 74 | "for (int i=0; i<%d; i++){ exp_sum += exp(%s[i]-max_el); } \n" \ 75 | "for (int i=0; i<%d; i++){ %s[i] = exp(%s[i]-max_el) / exp_sum; }\n" \ 76 | % (vector_name, size, vector_name, size, vector_name, size, vector_name, vector_name) 77 | 78 | @staticmethod 79 | def generate_max_index_return(classes, vector_name="v"): 80 | return "int max_index = 0;\n" \ 81 | "for (int i = 0; i < %d; i++){ if (%s[i] > %s[max_index]){ max_index = i; }}\n" \ 82 | "return max_index;\n" % (classes, vector_name, vector_name) 83 | 84 | def generate_code_expanded(self): 85 | code = self.generate_vector_declaration(name="result0", size=self.clf.coefs_[0].shape[1], 86 | vector_type="double") 87 | code += self.generate_vector_mul_matrix(self.clf.coefs_[0], result_name="result0") 88 | code += self.generate_vector_add_vector(self.clf.intercepts_[0], vector2_name="result0", result_name="result0") 89 | code += self.generate_relu_activation_on_vector(size=self.clf.coefs_[0].shape[1], vector_name="result0") 90 | 91 | code += self.generate_vector_declaration(name="result1", size=self.clf.coefs_[1].shape[1], vector_type="double") 92 | code += self.generate_vector_mul_matrix(self.clf.coefs_[1], result_name="result1", vector_name="result0") 93 | code += self.generate_vector_add_vector(self.clf.intercepts_[1], vector2_name="result1", result_name="result1") 94 | code += self.generate_softmax_activation_on_vector(size=self.clf.coefs_[0].shape[1], vector_name="result1") 95 | 96 | code += self.generate_max_index_return(len(self.clf.classes_), vector_name="result1") 97 | return code 98 | 99 | def generate_layer_transformation(self, layer, vector_name="x", decomposed=False): 100 | result = self.generate_vector_declaration( 101 | name="result%d" % layer, 102 | size=self.clf.coefs_[layer].shape[1], vector_type="float" 103 | ) 104 | if decomposed: 105 | result += ArduinoGenerator.generate_decomposed_loop_vector_mul_matrix( 106 | neurons=self.clf.coefs_[layer].shape[1], 107 | result_name="result%d" % layer, 108 | vector_size=self.clf.coefs_[layer].shape[0], 109 | vector_name=vector_name, matrix_type="float", 110 | matrix_name="pmdata_%d" % layer 111 | ) 112 | else: 113 | result += ArduinoGenerator.generate_loop_vector_mul_matrix( 114 | neurons=self.clf.coefs_[layer].shape[1], 115 | result_name="result%d" % layer, 116 | vector_size=self.clf.coefs_[layer].shape[0], 117 | vector_name=vector_name, matrix_type="float", 118 | matrix_name="pmdata_%d" % layer 119 | ) 120 | return result + self.generate_vector_add_vector( 121 | self.clf.intercepts_[layer], 122 | vector2_name="result%d" % layer, 123 | result_name="result%d" % layer 124 | ) 125 | 126 | def generate_code_for_arduino(self, cname): 127 | code = self.license_header() 128 | 129 | code += ArduinoGenerator.get_arduino_header() 130 | #code += "#include \"neural_nets.h\"\n" 131 | for layer in range(self.clf.n_layers_ - 1): 132 | for col in range(self.clf.coefs_[layer].shape[1]): 133 | code += ArduinoGenerator.get_progmem_array(self.clf.coefs_[layer][:, [col]], 134 | name="pmdata_%d%d" % (layer, col)) 135 | 136 | # vector_names = ["x", "result0"] 137 | 138 | hidden_layers = len(self.clf.hidden_layer_sizes) 139 | 140 | vector_names = ["x"] 141 | for layer in range(hidden_layers): 142 | vector_names.append("result%d" % layer) 143 | 144 | function_body = "" 145 | for layer in range(hidden_layers): 146 | function_body += self.generate_layer_transformation(layer, vector_name=vector_names[layer], decomposed=True) 147 | function_body += self.generate_relu_activation_on_vector(size=self.clf.coefs_[layer].shape[1], 148 | vector_name="result%d" % layer) 149 | function_body += self.generate_layer_transformation(hidden_layers, vector_name=vector_names[hidden_layers], 150 | decomposed=True) 151 | function_body += self.generate_softmax_activation_on_vector( 152 | size=self.clf.coefs_[hidden_layers].shape[1], vector_name="result%d" % hidden_layers) 153 | 154 | function_body += self.generate_max_index_return(len(self.clf.classes_), vector_name="result%d" % hidden_layers) 155 | code += generate_c_function(function_body=function_body, return_type="int", 156 | name=cname, args="uint8_t * x") 157 | return code 158 | 159 | def generate(self, fname = 'mlp_model.c', cname="classifier", **kwargs): 160 | with open(fname, 'w') as output_file: 161 | output_file.write(self.generate_code_for_arduino(cname)) 162 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2021 FogML 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /examples/TFT_ML/TFT_ML.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "neural_nets.h" 4 | #include "tree.h" 5 | //#include "bayes_model.h" 6 | 7 | MCUFRIEND_kbv tft; // hard-wired for UNO shields anyway. 8 | 9 | // most mcufriend shields use these pins and Portrait mode: 10 | uint8_t YP = A2; // must be an analog pin, use "An" notation! 11 | uint8_t XM = A3; // must be an analog pin, use "An" notation! 12 | uint8_t YM = 8; // can be a digital pin 13 | uint8_t XP = 9; // can be a digital pin 14 | uint8_t SwapXY = 0; 15 | 16 | // For better pressure precision, we need to know the resistance 17 | // between X+ and X- Use any multimeter to read it 18 | // For the one we're using, its 300 ohms across the X plate 19 | TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); 20 | TSPoint tp; 21 | 22 | #define MINPRESSURE 20 23 | #define MAXPRESSURE 1000 24 | 25 | #define SWAP(a, b) {uint16_t tmp = a; a = b; b = tmp;} 26 | 27 | 28 | uint8_t char_buff[28*28]; 29 | 30 | int16_t BOXSIZE; 31 | int16_t PENRADIUS = 3; 32 | uint16_t identifier, oldcolor, currentcolor; 33 | uint8_t Orientation = 1; //PORTRAIT 34 | 35 | // Assign human-readable names to some common 16-bit color values: 36 | #define BLACK 0x0000 37 | #define BLUE 0x001F 38 | #define RED 0xF800 39 | #define GREEN 0x07E0 40 | #define CYAN 0x07FF 41 | #define MAGENTA 0xF81F 42 | #define YELLOW 0xFFE0 43 | #define WHITE 0xFFFF 44 | 45 | uint16_t TS_LEFT = 920; 46 | uint16_t TS_RT = 150; 47 | uint16_t TS_TOP = 940; 48 | uint16_t TS_BOT = 120; 49 | 50 | uint16_t points=0; 51 | 52 | uint16_t convertRGB( uint8_t R, uint8_t G, uint8_t B) 53 | { 54 | return ( ((R & 0xF8) << 8) | ((G & 0xFC) << 3) | (B >> 3) ); 55 | } 56 | 57 | uint16_t convertGREY( uint8_t v) 58 | { 59 | return convertRGB(v,v,v); 60 | } 61 | 62 | 63 | uint16_t scale_img(uint8_t x, uint8_t y){ 64 | return y*28 + x; 65 | } 66 | 67 | 68 | void copy_img(uint8_t* src, uint8_t* dst){ 69 | int tmp; 70 | for(tmp=0; tmp<28*28; tmp++){ 71 | dst[tmp]=src[tmp]; 72 | } 73 | } 74 | 75 | void img_move_to_corner(){ 76 | uint8_t buff_tmp[28*28]; 77 | uint8_t min_x, min_y, color; 78 | 79 | min_x=28; 80 | min_y=28; 81 | 82 | for(uint8_t x=0; x<28; x++){ 83 | for(uint8_t y=0; y<28; y++){ 84 | color = char_buff[scale_img(x, y)]; 85 | 86 | if (color==255){ 87 | min_x = min(min_x, x); 88 | min_y = min(min_y, y); 89 | } 90 | } 91 | } 92 | 93 | for(uint8_t x=0; x<28; x++){ 94 | for(uint8_t y=0; y<28; y++){ 95 | buff_tmp[scale_img(x, y)] = 0; 96 | if (x+min_x >=28 || y+min_y >=28) continue; 97 | buff_tmp[scale_img(x, y)] = char_buff[scale_img(x+min_x, y+min_y)]; 98 | } 99 | } 100 | 101 | copy_img(buff_tmp, char_buff); 102 | } 103 | 104 | void img_scale_from_corner(){ 105 | uint8_t buff_tmp[28*28]; 106 | uint8_t max_x, max_y, color; 107 | 108 | max_x=0; 109 | max_y=0; 110 | 111 | for(uint8_t x=0; x<28; x++){ 112 | for(uint8_t y=0; y<28; y++){ 113 | color = char_buff[scale_img(x, y)]; 114 | 115 | if (color==255){ 116 | max_x = max(max_x, x); 117 | max_y = max(max_y, y); 118 | } 119 | } 120 | } 121 | 122 | float sx = 19.0/max_x; 123 | float sy = 19.0/max_y; 124 | 125 | float ss = min(sx,sy); 126 | 127 | Serial.println("SCALING FACTOR " + String(ss)); 128 | 129 | for(uint8_t x=0; x<28; x++){ 130 | for(uint8_t y=0; y<28; y++){ 131 | buff_tmp[scale_img(x, y)] = 0; 132 | if (x/ss >=28 || y/ss >=28) continue; 133 | buff_tmp[scale_img(x, y)] = char_buff[scale_img(x/ss, y/ss)]; 134 | } 135 | } 136 | 137 | copy_img(buff_tmp, char_buff); 138 | } 139 | 140 | 141 | void img_center(){ 142 | uint8_t buff_tmp[28*28]; 143 | uint8_t max_x, max_y, color; 144 | uint8_t d_x, d_y; 145 | max_x=0; 146 | max_y=0; 147 | 148 | for(uint8_t x=0; x<28; x++){ 149 | for(uint8_t y=0; y<28; y++){ 150 | color = char_buff[scale_img(x, y)]; 151 | 152 | if (color==255){ 153 | max_x = max(max_x, x); 154 | max_y = max(max_y, y); 155 | } 156 | } 157 | } 158 | 159 | Serial.println("CENTERING " + String(max_x) + " , " + String(max_y)); 160 | 161 | d_x = (28-max_x)/2; 162 | d_y = (28-max_y)/2; 163 | 164 | for(uint8_t x=0; x<28; x++){ 165 | for(uint8_t y=0; y<28; y++){ 166 | buff_tmp[scale_img(x, y)] = 0; 167 | if (x-d_x < 0 || y-d_y < 0) continue; 168 | 169 | buff_tmp[scale_img(x, y)] = char_buff[scale_img(x-d_x, y-d_y)]; 170 | } 171 | } 172 | 173 | copy_img(buff_tmp, char_buff); 174 | } 175 | 176 | void img_weighted_center(){ 177 | uint8_t buff_tmp[28*28]; 178 | uint8_t d_x, d_y; 179 | 180 | float sum_x, sum_y, sum_w,w; 181 | sum_x=0; 182 | sum_y=0; 183 | sum_w=0; 184 | 185 | for(uint8_t x=0; x<28; x++){ 186 | for(uint8_t y=0; y<28; y++){ 187 | w= float(char_buff[scale_img(x, y)])/255.0; 188 | sum_x+=w*x; 189 | sum_y+=w*y; 190 | sum_w+=w; 191 | } 192 | } 193 | 194 | d_x = 14-sum_x/sum_w; 195 | d_y = 14-sum_y/sum_w; 196 | 197 | Serial.println("WEIGHTED CENTERING " + String(d_x) + " , " + String(d_y)); 198 | 199 | 200 | for(uint8_t x=0; x<28; x++){ 201 | for(uint8_t y=0; y<28; y++){ 202 | buff_tmp[scale_img(x, y)] = 0; 203 | if (x-d_x < 0 || y-d_y < 0) continue; 204 | 205 | buff_tmp[scale_img(x, y)] = char_buff[scale_img(x-d_x, y-d_y)]; 206 | } 207 | } 208 | 209 | copy_img(buff_tmp, char_buff); 210 | } 211 | 212 | 213 | void img_blur(){ 214 | uint8_t buff_tmp[28*28]; 215 | uint16_t tmp; 216 | 217 | for(uint8_t x=0; x<28; x++){ 218 | for(uint8_t y=0; y<28; y++){ 219 | buff_tmp[scale_img(x, y)] = 0; 220 | } 221 | } 222 | 223 | for(uint8_t x=1; x<27; x++){ 224 | for(uint8_t y=1; y<27; y++){ 225 | buff_tmp[scale_img(x, y)] = 0; 226 | 227 | tmp=0; 228 | 229 | tmp += char_buff[scale_img(x, y)]; 230 | tmp += char_buff[scale_img(x-1, y)]; 231 | tmp += char_buff[scale_img(x-1, y-1)]; 232 | tmp += char_buff[scale_img(x, y-1)]; 233 | tmp += char_buff[scale_img(x+1, y-1)]; 234 | tmp += char_buff[scale_img(x+1, y)]; 235 | tmp += char_buff[scale_img(x+1, y+1)]; 236 | tmp += char_buff[scale_img(x, y+1)]; 237 | tmp += char_buff[scale_img(x-1, y+1)]; 238 | 239 | buff_tmp[scale_img(x, y)] = tmp/9; 240 | } 241 | } 242 | } 243 | 244 | void img_smooth(){ 245 | uint8_t buff_tmp[28*28]; 246 | uint16_t tmp; 247 | 248 | for(uint8_t x=0; x<28; x++){ 249 | for(uint8_t y=0; y<28; y++){ 250 | buff_tmp[scale_img(x, y)] = 0; 251 | } 252 | } 253 | 254 | for(uint8_t x=1; x<27; x++){ 255 | for(uint8_t y=1; y<27; y++){ 256 | buff_tmp[scale_img(x, y)] = 0; 257 | 258 | tmp=0; 259 | 260 | //tmp += char_buff[scale_img(x, y)]; 261 | tmp += char_buff[scale_img(x-1, y)]; 262 | tmp += char_buff[scale_img(x-1, y-1)]; 263 | tmp += char_buff[scale_img(x, y-1)]; 264 | tmp += char_buff[scale_img(x+1, y-1)]; 265 | tmp += char_buff[scale_img(x+1, y)]; 266 | tmp += char_buff[scale_img(x+1, y+1)]; 267 | tmp += char_buff[scale_img(x, y+1)]; 268 | tmp += char_buff[scale_img(x-1, y+1)]; 269 | 270 | buff_tmp[scale_img(x, y)] = max(tmp/9, char_buff[scale_img(x, y)]); 271 | } 272 | } 273 | 274 | copy_img(buff_tmp, char_buff); 275 | } 276 | 277 | void img_normalize(){ 278 | uint8_t buff_tmp[28*28]; 279 | uint16_t max_val; 280 | 281 | max_val = 0; 282 | 283 | for(uint8_t x=0; x<28; x++){ 284 | for(uint8_t y=0; y<28; y++){ 285 | max_val = max(max_val, char_buff[scale_img(x, y)]); 286 | } 287 | } 288 | 289 | float d = 255.0/max_val; 290 | 291 | for(uint8_t x=0; x<28; x++){ 292 | for(uint8_t y=0; y<28; y++){ 293 | char_buff[scale_img(x, y)] *= d; 294 | } 295 | } 296 | 297 | 298 | } 299 | 300 | 301 | void setup() { 302 | uint16_t tmp; 303 | Serial.begin(9600); 304 | 305 | tft.reset(); 306 | identifier = 0x9341; 307 | 308 | TS_LEFT = 130; TS_RT = 941; TS_TOP = 132; TS_BOT = 902; 309 | SwapXY = 1; //mods 310 | 311 | switch (Orientation) { // adjust for different aspects 312 | case 0: break; //no change, calibrated for PORTRAIT 313 | case 1: tmp = TS_LEFT, TS_LEFT = TS_BOT, TS_BOT = TS_RT, TS_RT = TS_TOP, TS_TOP = tmp; break; 314 | case 2: SWAP(TS_LEFT, TS_RT); SWAP(TS_TOP, TS_BOT); break; 315 | case 3: tmp = TS_LEFT, TS_LEFT = TS_TOP, TS_TOP = TS_RT, TS_RT = TS_BOT, TS_BOT = tmp; break; 316 | } 317 | 318 | Serial.begin(9600); 319 | ts = TouchScreen(XP, YP, XM, YM, 300); //call the constructor AGAIN with new values. 320 | tft.begin(identifier); 321 | //show_Serial(); 322 | tft.setRotation(Orientation); 323 | tft.fillScreen(BLACK); 324 | 325 | tft.drawRect(0, 0, 28*4+1, 28*4+1, WHITE); 326 | 327 | tft.setTextColor(CYAN); 328 | tft.setCursor(0, 150); 329 | tft.setTextSize(2); 330 | tft.print(F("RECOGNIZE")); 331 | 332 | tft.setCursor(0, 200); 333 | tft.print(F("CLEAR")); 334 | 335 | currentcolor = RED; 336 | delay(100); 337 | 338 | 339 | for(tmp=0; tmp<28*28; tmp++){ 340 | char_buff[tmp]=0; 341 | } 342 | 343 | } 344 | 345 | void show_small_img(uint8_t off){ 346 | uint16_t x,y; 347 | uint16_t color; 348 | 349 | for(x=0; x<28; x++){ 350 | for(y=0; y<28; y++){ 351 | color = char_buff[scale_img(x, y)]; 352 | tft.fillCircle(x+161, y+1+off, 1, convertGREY(color)); 353 | } 354 | } 355 | tft.drawRect(160, 0+off, 29, 29, WHITE); 356 | } 357 | 358 | void show_small_img2(uint8_t off){ 359 | uint16_t x,y; 360 | uint16_t color; 361 | 362 | for(x=0; x<28; x++){ 363 | for(y=0; y<28; y++){ 364 | color = char_buff[scale_img(x, y)]; 365 | tft.fillCircle(x*2+161-28, y*2+1+off, 2, convertGREY(color)); 366 | 367 | } 368 | } 369 | tft.drawRect(160-28, 0+off, 29+28, 29+28, WHITE); 370 | } 371 | 372 | 373 | void loop() { 374 | uint16_t xpos, ypos; //screen coordinates 375 | tp = ts.getPoint(); //tp.x, tp.y are ADC values 376 | 377 | 378 | 379 | // if sharing pins, you'll need to fix the directions of the touchscreen pins 380 | pinMode(XM, OUTPUT); 381 | pinMode(YP, OUTPUT); 382 | pinMode(XP, OUTPUT); 383 | pinMode(YM, OUTPUT); 384 | 385 | if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) { 386 | // is controller wired for Landscape ? or are we oriented in Landscape? 387 | if (SwapXY != (Orientation & 1)) SWAP(tp.x, tp.y); 388 | // scale from 0->1023 to tft.width i.e. left = 0, rt = width 389 | // most mcufriend have touch (with icons) that extends below the TFT 390 | // screens without icons need to reserve a space for "erase" 391 | // scale the ADC values from ts.getPoint() to screen values e.g. 0-239 392 | xpos = map(tp.x, TS_LEFT, TS_RT, 0, tft.width()); 393 | ypos = map(tp.y, TS_TOP, TS_BOT, 0, tft.height()); 394 | 395 | if (xpos>=1 && xpos<=28*4+1 && ypos>=1 && ypos<=28*4+1) { 396 | tft.fillCircle(xpos, ypos, PENRADIUS, currentcolor); 397 | points++; 398 | char_buff[scale_img((xpos-1)/4, (ypos-1)/4)] = 255; 399 | } else { 400 | //tft.setCursor(0, 200); 401 | //tft.print("x=" + String(xpos) + " y="+String(ypos)); 402 | } 403 | 404 | if (ypos>120 && ypos < 150 && xpos<100){ 405 | tft.setCursor(160, 200); 406 | tft.print("points=" + String(points)); 407 | //tft.setTextColor(WHITE); 408 | show_small_img(0); 409 | img_move_to_corner(); 410 | show_small_img(30); 411 | img_scale_from_corner(); 412 | show_small_img(60); 413 | img_weighted_center(); 414 | show_small_img(90); 415 | img_smooth(); 416 | show_small_img2(120); 417 | //img_normalize(); 418 | //show_small_img(150); 419 | 420 | 421 | int result = evaluate_tree(char_buff); 422 | tft.setTextColor(GREEN); 423 | tft.setCursor(200, 50); 424 | tft.setTextSize(3); 425 | tft.print("TREE"); 426 | tft.setCursor(250, 80); 427 | tft.print(String(result)); 428 | 429 | result = evaluate_network(char_buff); 430 | tft.setCursor(200, 130); 431 | tft.setTextSize(3); 432 | tft.print("NEURAL"); 433 | tft.setCursor(250, 160); 434 | tft.print(String(result)); 435 | 436 | //result = evaluate_bayes(char_buff); 437 | //tft.setCursor(200, 180); 438 | //tft.setTextSize(3); 439 | //tft.print("BAYES"); 440 | //tft.setCursor(250, 240); 441 | //tft.print(String(result)); 442 | } 443 | 444 | if (ypos>170 && ypos < 200 && xpos<100){ 445 | setup(); 446 | } 447 | } 448 | } 449 | -------------------------------------------------------------------------------- /examples/rl/FogML_RL_Arduino/qlearning_model_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2021 FogML 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 | static float Q_table[] = {0.000000, 0.000000, 18 | 0.000000, 0.000000, 19 | 0.000000, 0.000000, 20 | 0.000000, 0.000000, 21 | 0.000000, 0.000000, 22 | 0.000000, 0.000000, 23 | 0.000000, 0.000000, 24 | 0.000000, 0.000000, 25 | 0.000000, 0.000000, 26 | 0.000000, 0.000000, 27 | 0.000000, 0.000000, 28 | 0.000000, 0.000000, 29 | 0.000000, 0.000000, 30 | 0.000000, 0.000000, 31 | 0.000000, 0.000000, 32 | 0.000000, 0.000000, 33 | 0.000000, 0.000000, 34 | 0.000000, 0.000000, 35 | 0.000000, 0.000000, 36 | 0.000000, 0.000000, 37 | 0.000000, 0.000000, 38 | 0.000000, 0.000000, 39 | 0.000000, 37.557986, 40 | 0.000000, 0.000000, 41 | 0.000000, 0.000000, 42 | 53.995864, 64.691283, 43 | 0.000000, 0.000000, 44 | 0.000000, 0.000000, 45 | 0.000000, 0.000000, 46 | 0.000000, 0.000000, 47 | 0.000000, 0.000000, 48 | 12.483977, 0.000000, 49 | 0.000000, 0.000000, 50 | 0.000000, 0.000000, 51 | 436.400180, 58.579314, 52 | 0.000000, 0.000000, 53 | 0.000000, 0.000000, 54 | 0.000000, 0.000000, 55 | 0.000000, 0.000000, 56 | 0.000000, 0.000000, 57 | 0.000000, 0.000000, 58 | 0.000000, 0.000000, 59 | 0.000000, 0.000000, 60 | 40.697625, 0.000000, 61 | 0.000000, 0.000000, 62 | 0.000000, 0.000000, 63 | 0.000000, 0.000000, 64 | 0.000000, 0.000000, 65 | 0.000000, 0.000000, 66 | 0.000000, 0.000000, 67 | 0.000000, 0.000000, 68 | 0.000000, 0.000000, 69 | 0.000000, 0.000000, 70 | 0.000000, 0.000000, 71 | 0.000000, 0.000000, 72 | 0.000000, 0.000000, 73 | 0.000000, 0.000000, 74 | 0.000000, 0.000000, 75 | 0.000000, 0.000000, 76 | 0.000000, 0.000000, 77 | 0.000000, 0.000000, 78 | 0.000000, 0.000000, 79 | 0.000000, 0.000000, 80 | 0.000000, 0.000000, 81 | 0.000000, 0.000000, 82 | 0.000000, 0.000000, 83 | 0.000000, 0.000000, 84 | 0.000000, 0.000000, 85 | 0.000000, 0.000000, 86 | 0.000000, 0.000000, 87 | 0.000000, 0.000000, 88 | 0.000000, 0.000000, 89 | 0.000000, 0.000000, 90 | 0.000000, 0.000000, 91 | 0.000000, 0.000000, 92 | 0.000000, 0.000000, 93 | 0.000000, 0.000000, 94 | 0.000000, 0.000000, 95 | 0.000000, 0.000000, 96 | 0.000000, 0.000000, 97 | 0.000000, 0.000000, 98 | 0.000000, 0.000000, 99 | 0.000000, 0.000000, 100 | 0.000000, 0.000000, 101 | 0.000000, 0.000000, 102 | 0.000000, 0.000000, 103 | 0.000000, 0.000000, 104 | 0.000000, 0.000000, 105 | 0.000000, 0.000000, 106 | 0.000000, 0.000000, 107 | 0.000000, 0.000000, 108 | 0.000000, 0.000000, 109 | 0.000000, 0.000000, 110 | 0.000000, 0.000000, 111 | 28.841657, 55.559237, 112 | 0.000000, 0.000000, 113 | 0.000000, 0.000000, 114 | 53.604903, 65.060291, 115 | 0.000000, 0.000000, 116 | 0.000000, 0.000000, 117 | 0.000000, 0.000000, 118 | 0.000000, 0.000000, 119 | 0.000000, 0.000000, 120 | 534.507120, 218.548221, 121 | 0.000000, 0.000000, 122 | 0.000000, 0.000000, 123 | 646.759029, 542.816302, 124 | 0.253837, 0.000000, 125 | 0.000000, 0.000000, 126 | 0.000000, 0.000000, 127 | 0.000000, 0.000000, 128 | 0.000000, 0.000000, 129 | 565.737580, 206.392723, 130 | 0.000000, 0.000000, 131 | 0.000000, 0.000000, 132 | 717.077617, 665.576152, 133 | 0.326355, 0.000000, 134 | 0.000000, 0.000000, 135 | 0.000000, 0.000000, 136 | 0.000000, 0.000000, 137 | 0.000000, 0.000000, 138 | 0.000000, 0.000000, 139 | 0.000000, 0.000000, 140 | 0.000000, 0.000000, 141 | 0.000000, 0.000000, 142 | 0.000000, 0.000000, 143 | 0.000000, 0.000000, 144 | 0.000000, 0.000000, 145 | 0.000000, 0.000000, 146 | 0.000000, 0.000000, 147 | 0.000000, 0.000000, 148 | 0.000000, 0.000000, 149 | 0.000000, 0.000000, 150 | 0.000000, 0.000000, 151 | 0.000000, 0.000000, 152 | 0.000000, 0.000000, 153 | 0.000000, 0.000000, 154 | 0.000000, 0.000000, 155 | 0.000000, 0.000000, 156 | 0.000000, 0.000000, 157 | 0.000000, 0.000000, 158 | 0.000000, 0.000000, 159 | 0.000000, 0.000000, 160 | 0.000000, 0.000000, 161 | 0.000000, 0.000000, 162 | 0.000000, 0.000000, 163 | 0.000000, 0.000000, 164 | 0.000000, 0.000000, 165 | 0.000000, 0.000000, 166 | 0.000000, 0.000000, 167 | 0.000000, 0.000000, 168 | 0.000000, 0.000000, 169 | 0.000000, 0.000000, 170 | 0.000000, 0.000000, 171 | 0.000000, 0.000000, 172 | 0.000000, 0.000000, 173 | 0.000000, 0.000000, 174 | 0.000000, 0.000000, 175 | 0.000000, 0.000000, 176 | 0.000000, 0.000000, 177 | 0.000000, 0.000000, 178 | 0.000000, 0.000000, 179 | 0.000000, 0.000000, 180 | 0.000000, 27.750884, 181 | 0.000000, 0.000000, 182 | 0.000000, 0.000000, 183 | 38.382120, 62.463298, 184 | 0.000000, 0.000000, 185 | 0.000000, 0.000000, 186 | 66.015482, 48.065723, 187 | 0.000000, 0.000000, 188 | 0.000000, 0.000000, 189 | 127.885895, 33.680352, 190 | 0.000000, 0.000000, 191 | 0.000000, 0.000000, 192 | 650.056251, 448.531364, 193 | 0.000000, 0.000000, 194 | 0.000000, 0.000000, 195 | 722.912753, 625.194875, 196 | 0.598718, 0.000000, 197 | 0.000000, 0.000000, 198 | 94.886799, 0.000000, 199 | 0.000000, 0.000000, 200 | 0.000000, 0.000000, 201 | 704.017055, 575.581040, 202 | 0.000000, 0.000000, 203 | 0.000000, 0.000000, 204 | 724.344793, 657.210446, 205 | 0.998786, 0.000000, 206 | 0.000000, 0.000000, 207 | 0.000000, 0.000000, 208 | 0.000000, 0.000000, 209 | 0.000000, 0.000000, 210 | 0.000000, 0.000000, 211 | 0.000000, 0.000000, 212 | 0.000000, 0.000000, 213 | 0.000000, 0.000000, 214 | 0.000000, 0.000000, 215 | 0.000000, 0.000000, 216 | 0.000000, 0.000000, 217 | 0.000000, 0.000000, 218 | 0.000000, 0.000000, 219 | 0.000000, 0.000000, 220 | 0.000000, 0.000000, 221 | 0.000000, 0.000000, 222 | 0.000000, 0.000000, 223 | 0.000000, 0.000000, 224 | 0.000000, 0.000000, 225 | 0.000000, 0.000000, 226 | 0.000000, 0.000000, 227 | 0.000000, 0.000000, 228 | 0.000000, 0.000000, 229 | 0.000000, 0.000000, 230 | 0.000000, 0.000000, 231 | 0.000000, 0.000000, 232 | 0.000000, 0.000000, 233 | 0.000000, 0.000000, 234 | 0.000000, 0.000000, 235 | 0.000000, 0.000000, 236 | 0.000000, 0.000000, 237 | 0.000000, 0.000000, 238 | 0.000000, 0.000000, 239 | 0.000000, 0.000000, 240 | 0.000000, 0.000000, 241 | 0.000000, 0.000000, 242 | 0.000000, 0.000000, 243 | 0.000000, 0.000000, 244 | 0.000000, 0.000000, 245 | 0.000000, 0.000000, 246 | 0.000000, 0.000000, 247 | 0.000000, 0.000000, 248 | 0.000000, 0.000000, 249 | 0.000000, 0.000000, 250 | 0.000000, 0.000000, 251 | 0.000000, 0.000000, 252 | 0.489455, 23.191944, 253 | 0.000000, 0.000000, 254 | 0.000000, 0.000000, 255 | 48.013332, 63.913101, 256 | 0.000000, 0.000000, 257 | 0.000000, 0.000000, 258 | 89.759651, 34.651473, 259 | 0.000000, 0.000000, 260 | 0.000000, 0.000000, 261 | 467.093712, 243.014131, 262 | 0.000000, 0.000000, 263 | 0.000000, 0.000000, 264 | 721.421504, 686.444317, 265 | 0.000000, 0.000000, 266 | 0.000000, 0.000000, 267 | 724.550349, 710.409919, 268 | 1.421973, 0.000000, 269 | 0.000000, 0.000000, 270 | 558.498001, 373.413159, 271 | 0.000000, 0.000000, 272 | 0.000000, 0.000000, 273 | 722.729849, 704.477422, 274 | 0.000000, 0.000000, 275 | 0.000000, 0.000000, 276 | 701.293207, 724.121388, 277 | 2.156170, 0.244950, 278 | 0.000000, 0.000000, 279 | 0.000000, 0.000000, 280 | 0.000000, 0.000000, 281 | 0.000000, 0.000000, 282 | 55.791874, 0.000000, 283 | 0.000000, 0.000000, 284 | 0.000000, 0.000000, 285 | 12.697514, 0.000000, 286 | 0.000000, 0.000000, 287 | 0.000000, 0.000000, 288 | 0.000000, 0.000000, 289 | 0.000000, 0.000000, 290 | 0.000000, 0.000000, 291 | 0.000000, 0.000000, 292 | 0.000000, 0.000000, 293 | 0.000000, 0.000000, 294 | 0.000000, 0.000000, 295 | 0.000000, 0.000000, 296 | 0.000000, 0.000000, 297 | 0.000000, 0.000000, 298 | 0.000000, 0.000000, 299 | 0.000000, 0.000000, 300 | 0.000000, 0.000000, 301 | 0.000000, 0.000000, 302 | 0.000000, 0.000000, 303 | 0.000000, 0.000000, 304 | 0.000000, 0.000000, 305 | 0.000000, 0.000000, 306 | 0.000000, 0.000000, 307 | 0.000000, 0.000000, 308 | 0.000000, 0.000000, 309 | 0.000000, 0.000000, 310 | 0.000000, 0.000000, 311 | 0.000000, 0.000000, 312 | 0.000000, 0.000000, 313 | 0.000000, 0.000000, 314 | 0.000000, 0.000000, 315 | 0.000000, 0.000000, 316 | 0.000000, 0.000000, 317 | 0.000000, 0.000000, 318 | 0.000000, 0.000000, 319 | 0.000000, 0.000000, 320 | 0.000000, 0.000000, 321 | 0.000000, 0.000000, 322 | 0.000000, 0.000000, 323 | 0.000000, 0.000000, 324 | 1.732922, 56.544908, 325 | 0.000000, 0.000000, 326 | 0.000000, 0.000000, 327 | 11.462823, 221.620583, 328 | 0.000000, 0.000000, 329 | 0.000000, 0.000000, 330 | 45.850441, 0.000000, 331 | 0.000000, 0.000000, 332 | 0.000000, 0.000000, 333 | 663.926896, 556.442957, 334 | 0.000000, 0.000000, 335 | 0.000000, 0.000000, 336 | 724.580798, 718.845967, 337 | 1.688255, 0.000000, 338 | 0.000000, 0.000000, 339 | 711.665856, 581.080294, 340 | 2.400735, 0.100000, 341 | 0.000000, 0.000000, 342 | 585.572276, 693.167236, 343 | 0.000000, 0.000000, 344 | 0.000000, 0.000000, 345 | 712.392306, 723.808500, 346 | 0.210811, 0.000000, 347 | 0.000000, 0.000000, 348 | 637.538926, 706.654697, 349 | 7.228656, 0.424175, 350 | 0.000000, 0.000000, 351 | 53.834104, 0.000000, 352 | 0.000000, 0.000000, 353 | 0.000000, 0.000000, 354 | 63.789693, 19.374165, 355 | 0.000000, 0.000000, 356 | 0.000000, 0.000000, 357 | 40.222598, 0.000000, 358 | 0.000000, 0.000000, 359 | 0.000000, 0.000000, 360 | 0.000000, 0.000000, 361 | 0.000000, 0.000000, 362 | 0.000000, 0.000000, 363 | 0.000000, 0.000000, 364 | 0.000000, 0.000000, 365 | 0.000000, 0.000000, 366 | 0.000000, 0.000000, 367 | 0.000000, 0.000000, 368 | 0.000000, 0.000000, 369 | 0.000000, 0.000000, 370 | 0.000000, 0.000000, 371 | 0.000000, 0.000000, 372 | 0.000000, 0.000000, 373 | 0.000000, 0.000000, 374 | 0.000000, 0.000000, 375 | 0.000000, 0.000000, 376 | 0.000000, 0.000000, 377 | 0.000000, 0.000000, 378 | 0.000000, 0.000000, 379 | 0.000000, 0.000000, 380 | 0.000000, 0.000000, 381 | 0.000000, 0.000000, 382 | 0.000000, 0.000000, 383 | 0.000000, 0.000000, 384 | 0.000000, 0.000000, 385 | 0.000000, 0.000000, 386 | 0.000000, 0.000000, 387 | 0.000000, 0.000000, 388 | 0.000000, 0.000000, 389 | 0.000000, 0.000000, 390 | 0.000000, 0.000000, 391 | 0.000000, 0.000000, 392 | 0.000000, 0.000000, 393 | 0.000000, 0.000000, 394 | 0.000000, 0.000000, 395 | 0.000000, 0.000000, 396 | 24.255567, 0.866461, 397 | 0.000000, 0.000000, 398 | 0.000000, 0.000000, 399 | 96.838113, 0.000000, 400 | 0.000000, 0.000000, 401 | 0.000000, 0.000000, 402 | 54.915844, 0.000000, 403 | 0.000000, 0.000000, 404 | 0.000000, 0.000000, 405 | 706.377364, 644.335868, 406 | 0.000000, 0.000000, 407 | 0.000000, 0.000000, 408 | 683.419319, 724.376649, 409 | 7.190572, 0.215057, 410 | 0.000000, 0.000000, 411 | 234.891982, 708.894347, 412 | 3.709860, 0.461619, 413 | 0.000000, 0.000000, 414 | 619.331187, 700.343572, 415 | 0.000000, 0.000000, 416 | 0.000000, 0.000000, 417 | 620.543921, 722.418630, 418 | 9.302170, 0.000000, 419 | 0.000000, 0.000000, 420 | 189.382641, 644.289703, 421 | 6.073894, 0.000000, 422 | 0.000000, 0.000000, 423 | 25.902462, 58.995050, 424 | 0.000000, 0.000000, 425 | 0.000000, 0.000000, 426 | 72.708669, 34.370171, 427 | 0.000000, 0.000000, 428 | 0.000000, 0.000000, 429 | 43.159326, 0.000000, 430 | 0.000000, 0.000000, 431 | 0.000000, 0.000000, 432 | 0.000000, 0.000000, 433 | 0.000000, 0.000000, 434 | 0.000000, 0.000000, 435 | 0.000000, 0.000000, 436 | 0.000000, 0.000000, 437 | 0.000000, 0.000000, 438 | 0.000000, 0.000000, 439 | 0.000000, 0.000000, 440 | 0.000000, 0.000000, 441 | 0.000000, 0.000000, 442 | 0.000000, 0.000000, 443 | 0.000000, 0.000000, 444 | 0.000000, 0.000000, 445 | 0.000000, 0.000000, 446 | 0.000000, 0.000000, 447 | 0.000000, 0.000000, 448 | 0.000000, 0.000000, 449 | 0.000000, 0.000000, 450 | 0.000000, 0.000000, 451 | 0.000000, 0.000000, 452 | 0.000000, 0.000000, 453 | 0.000000, 0.000000, 454 | 0.000000, 0.000000, 455 | 0.000000, 0.000000, 456 | 0.000000, 0.000000, 457 | 0.000000, 0.000000, 458 | 0.000000, 0.000000, 459 | 0.000000, 0.000000, 460 | 0.000000, 0.000000, 461 | 0.000000, 0.000000, 462 | 0.000000, 0.000000, 463 | 0.000000, 0.000000, 464 | 0.000000, 0.000000, 465 | 0.000000, 0.000000, 466 | 0.000000, 0.000000, 467 | 0.000000, 0.000000, 468 | 0.000000, 25.469860, 469 | 0.000000, 0.000000, 470 | 0.000000, 0.000000, 471 | 30.762840, 31.580273, 472 | 0.000000, 0.000000, 473 | 0.000000, 0.000000, 474 | 0.000000, 0.000000, 475 | 0.000000, 0.000000, 476 | 0.000000, 0.000000, 477 | 535.353833, 707.286344, 478 | 0.000000, 0.000000, 479 | 0.000000, 0.000000, 480 | 223.456571, 717.053310, 481 | 18.480106, 0.000000, 482 | 0.000000, 0.000000, 483 | 0.000000, 44.304252, 484 | 0.231784, 0.000000, 485 | 0.000000, 0.000000, 486 | 533.119195, 702.181916, 487 | 0.000000, 0.000000, 488 | 0.000000, 0.000000, 489 | 363.015378, 678.665298, 490 | 54.863216, 0.000000, 491 | 0.000000, 0.000000, 492 | 184.926678, 0.000000, 493 | 15.108856, 0.000000, 494 | 0.000000, 0.000000, 495 | 69.970529, 16.375891, 496 | 0.000000, 0.000000, 497 | 0.000000, 0.000000, 498 | 77.063869, 43.978565, 499 | 0.000000, 0.000000, 500 | 0.000000, 0.000000, 501 | 31.484423, 0.000000, 502 | 0.000000, 0.000000, 503 | 0.000000, 0.000000, 504 | 0.000000, 0.000000, 505 | 0.000000, 0.000000, 506 | 0.000000, 0.000000, 507 | 0.000000, 0.000000, 508 | 0.000000, 0.000000, 509 | 0.000000, 0.000000, 510 | 0.000000, 0.000000, 511 | 0.000000, 0.000000, 512 | 0.000000, 0.000000, 513 | 0.000000, 0.000000, 514 | 0.000000, 0.000000, 515 | 0.000000, 0.000000, 516 | 0.000000, 0.000000, 517 | 0.000000, 0.000000, 518 | 0.000000, 0.000000, 519 | 0.000000, 0.000000, 520 | 0.000000, 0.000000, 521 | 0.000000, 0.000000, 522 | 0.000000, 0.000000, 523 | 0.000000, 0.000000, 524 | 0.000000, 0.000000, 525 | 0.000000, 0.000000, 526 | 0.000000, 0.000000, 527 | 0.000000, 0.000000, 528 | 0.000000, 0.000000, 529 | 0.000000, 0.000000, 530 | 0.000000, 0.000000, 531 | 0.000000, 0.000000, 532 | 0.000000, 0.000000, 533 | 0.000000, 0.000000, 534 | 0.000000, 0.000000, 535 | 0.000000, 0.000000, 536 | 0.000000, 0.000000, 537 | 0.000000, 0.000000, 538 | 0.000000, 0.000000, 539 | 0.000000, 0.000000, 540 | 0.000000, 0.000000, 541 | 0.000000, 0.000000, 542 | 0.000000, 0.000000, 543 | 22.089214, 0.000000, 544 | 0.000000, 0.000000, 545 | 0.000000, 0.000000, 546 | 0.000000, 0.000000, 547 | 0.000000, 0.000000, 548 | 0.000000, 0.000000, 549 | 167.932213, 565.192437, 550 | 60.205210, 0.000000, 551 | 0.000000, 0.000000, 552 | 87.736157, 0.000000, 553 | 41.376219, 0.000000, 554 | 0.000000, 0.000000, 555 | 0.000000, 0.000000, 556 | 0.000000, 0.000000, 557 | 0.000000, 0.000000, 558 | 282.645316, 550.262985, 559 | 196.137684, 14.396461, 560 | 0.000000, 0.000000, 561 | 119.742513, 295.679379, 562 | 126.838184, 0.638525, 563 | 0.000000, 0.000000, 564 | 104.955075, 0.000000, 565 | 51.070035, 0.000000, 566 | 0.000000, 0.000000, 567 | 77.178186, 61.184615, 568 | 20.785988, 3.398401, 569 | 0.000000, 0.000000, 570 | 77.880295, 46.407097, 571 | 9.715591, 0.000000, 572 | 0.000000, 0.000000, 573 | 31.637835, 0.000000, 574 | 1.107662, 0.000000, 575 | 0.000000, 0.000000, 576 | 0.000000, 0.000000, 577 | 0.000000, 0.000000, 578 | 0.000000, 0.000000, 579 | 0.000000, 0.000000, 580 | 0.000000, 0.000000, 581 | 0.000000, 0.000000, 582 | 0.000000, 0.000000, 583 | 0.000000, 0.000000, 584 | 0.000000, 0.000000, 585 | 0.000000, 0.000000, 586 | 0.000000, 0.000000, 587 | 0.000000, 0.000000, 588 | 0.000000, 0.000000, 589 | 0.000000, 0.000000, 590 | 0.000000, 0.000000, 591 | 0.000000, 0.000000, 592 | 0.000000, 0.000000, 593 | }; 594 | 595 | unsigned long RL_STATES = 576; 596 | unsigned int RL_ACTIONS = 2; 597 | 598 | unsigned long internal_state; 599 | 600 | void fogml_qlearning_init(){ 601 | } 602 | 603 | void fogml_qlearning_update_state(unsigned long state){ 604 | internal_state = state; 605 | } 606 | 607 | unsigned int fogml_qlearning_select_action(){ 608 | unsigned long index = internal_state * RL_ACTIONS; 609 | 610 | unsigned int action = 0; 611 | float max = Q_table[index]; 612 | 613 | for(int i = 0; i < RL_ACTIONS; i++){ 614 | if (Q_table[index + i] > max) { 615 | max = Q_table[index + i]; 616 | action = i; 617 | 618 | } 619 | } 620 | return action; 621 | } 622 | -------------------------------------------------------------------------------- /examples/TFT_ML/tree.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by User on 15.01.2018. 3 | // 4 | 5 | #include 6 | #include "tree.h" 7 | 8 | int evaluate_tree(uint8_t * x){ 9 | if (x[350] <= 126) { 10 | if (x[435] <= 0) { 11 | if (x[597] <= 1) { 12 | if (x[486] <= 2) { 13 | if (x[404] <= 0) { 14 | if (x[538] <= 1) { 15 | if (x[153] <= 0) { 16 | if (x[460] <= 21) { 17 | if (x[542] <= 79) { 18 | return 7; 19 | } 20 | else { 21 | return 6; 22 | } 23 | } 24 | else { 25 | return 5; 26 | } 27 | } 28 | else { 29 | return 2; 30 | } 31 | } 32 | else { 33 | if (x[438] <= 39) { 34 | if (x[717] <= 21) { 35 | if (x[490] <= 146) { 36 | return 0; 37 | } 38 | else { 39 | return 5; 40 | } 41 | } 42 | else { 43 | return 7; 44 | } 45 | } 46 | else { 47 | return 0; 48 | } 49 | } 50 | } 51 | else { 52 | if (x[353] <= 100) { 53 | if (x[355] <= 33) { 54 | if (x[321] <= 146) { 55 | if (x[97] <= 8) { 56 | return 5; 57 | } 58 | else { 59 | return 6; 60 | } 61 | } 62 | else { 63 | return 5; 64 | } 65 | } 66 | else { 67 | return 4; 68 | } 69 | } 70 | else { 71 | if (x[238] <= 18) { 72 | return 4; 73 | } 74 | else { 75 | if (x[405] <= 22) { 76 | return 7; 77 | } 78 | else { 79 | if (x[437] <= 4) { 80 | return 5; 81 | } 82 | else { 83 | return 9; 84 | } 85 | } 86 | } 87 | } 88 | } 89 | } 90 | else { 91 | if (x[571] <= 46) { 92 | if (x[211] <= 6) { 93 | if (x[294] <= 4) { 94 | if (x[545] <= 190) { 95 | if (x[623] <= 28) { 96 | return 4; 97 | } 98 | else { 99 | return 2; 100 | } 101 | } 102 | else { 103 | return 6; 104 | } 105 | } 106 | else { 107 | return 5; 108 | } 109 | } 110 | else { 111 | if (x[465] <= 58) { 112 | if (x[327] <= 42) { 113 | if (x[523] <= 103) { 114 | return 5; 115 | } 116 | else { 117 | return 5; 118 | } 119 | } 120 | else { 121 | return 0; 122 | } 123 | } 124 | else { 125 | if (x[156] <= 4) { 126 | if (x[293] <= 201) { 127 | return 9; 128 | } 129 | else { 130 | return 4; 131 | } 132 | } 133 | else { 134 | return 4; 135 | } 136 | } 137 | } 138 | } 139 | else { 140 | if (x[270] <= 2) { 141 | if (x[660] <= 2) { 142 | if (x[243] <= 23) { 143 | if (x[583] <= 9) { 144 | return 6; 145 | } 146 | else { 147 | return 2; 148 | } 149 | } 150 | else { 151 | return 0; 152 | } 153 | } 154 | else { 155 | return 0; 156 | } 157 | } 158 | else { 159 | if (x[490] <= 1) { 160 | if (x[351] <= 37) { 161 | if (x[692] <= 197) { 162 | return 0; 163 | } 164 | else { 165 | return 9; 166 | } 167 | } 168 | else { 169 | return 8; 170 | } 171 | } 172 | else { 173 | return 0; 174 | } 175 | } 176 | } 177 | } 178 | } 179 | else { 180 | if (x[489] <= 1) { 181 | if (x[380] <= 1) { 182 | if (x[324] <= 194) { 183 | if (x[707] <= 3) { 184 | if (x[214] <= 0) { 185 | if (x[464] <= 128) { 186 | return 0; 187 | } 188 | else { 189 | return 6; 190 | } 191 | } 192 | else { 193 | if (x[463] <= 92) { 194 | return 0; 195 | } 196 | else { 197 | return 0; 198 | } 199 | } 200 | } 201 | else { 202 | return 7; 203 | } 204 | } 205 | else { 206 | if (x[427] <= 16) { 207 | if (x[267] <= 34) { 208 | return 5; 209 | } 210 | else { 211 | return 3; 212 | } 213 | } 214 | else { 215 | return 0; 216 | } 217 | } 218 | } 219 | else { 220 | if (x[298] <= 100) { 221 | if (x[513] <= 98) { 222 | if (x[376] <= 71) { 223 | if (x[159] <= 0) { 224 | return 3; 225 | } 226 | else { 227 | return 5; 228 | } 229 | } 230 | else { 231 | if (x[300] <= 119) { 232 | return 5; 233 | } 234 | else { 235 | return 8; 236 | } 237 | } 238 | } 239 | else { 240 | return 6; 241 | } 242 | } 243 | else { 244 | if (x[405] <= 57) { 245 | if (x[148] <= 23) { 246 | if (x[237] <= 1) { 247 | return 0; 248 | } 249 | else { 250 | return 0; 251 | } 252 | } 253 | else { 254 | return 2; 255 | } 256 | } 257 | else { 258 | return 2; 259 | } 260 | } 261 | } 262 | } 263 | else { 264 | if (x[347] <= 1) { 265 | if (x[344] <= 86) { 266 | if (x[351] <= 1) { 267 | if (x[708] <= 16) { 268 | if (x[520] <= 1) { 269 | return 5; 270 | } 271 | else { 272 | return 2; 273 | } 274 | } 275 | else { 276 | return 7; 277 | } 278 | } 279 | else { 280 | return 2; 281 | } 282 | } 283 | else { 284 | return 5; 285 | } 286 | } 287 | else { 288 | if (x[385] <= 30) { 289 | if (x[513] <= 47) { 290 | if (x[514] <= 90) { 291 | if (x[437] <= 205) { 292 | return 5; 293 | } 294 | else { 295 | return 8; 296 | } 297 | } 298 | else { 299 | return 8; 300 | } 301 | } 302 | else { 303 | if (x[405] <= 218) { 304 | return 6; 305 | } 306 | else { 307 | return 8; 308 | } 309 | } 310 | } 311 | else { 312 | if (x[400] <= 48) { 313 | return 2; 314 | } 315 | else { 316 | if (x[213] <= 0) { 317 | return 6; 318 | } 319 | else { 320 | if (x[347] <= 15) { 321 | return 9; 322 | } 323 | else { 324 | return 0; 325 | } 326 | } 327 | } 328 | } 329 | } 330 | } 331 | } 332 | } 333 | else { 334 | if (x[542] <= 2) { 335 | if (x[432] <= 0) { 336 | if (x[487] <= 55) { 337 | if (x[405] <= 13) { 338 | if (x[155] <= 0) { 339 | if (x[158] <= 8) { 340 | if (x[580] <= 33) { 341 | return 7; 342 | } 343 | else { 344 | return 2; 345 | } 346 | } 347 | else { 348 | return 1; 349 | } 350 | } 351 | else { 352 | if (x[540] <= 10) { 353 | if (x[544] <= 61) { 354 | return 1; 355 | } 356 | else { 357 | return 2; 358 | } 359 | } 360 | else { 361 | return 6; 362 | } 363 | } 364 | } 365 | else { 366 | if (x[352] <= 8) { 367 | if (x[322] <= 6) { 368 | if (x[355] <= 2) { 369 | return 5; 370 | } 371 | else { 372 | return 4; 373 | } 374 | } 375 | else { 376 | if (x[218] <= 8) { 377 | return 3; 378 | } 379 | else { 380 | return 5; 381 | } 382 | } 383 | } 384 | else { 385 | if (x[376] <= 5) { 386 | if (x[208] <= 3) { 387 | return 1; 388 | } 389 | else { 390 | return 3; 391 | } 392 | } 393 | else { 394 | if (x[434] <= 208) { 395 | return 9; 396 | } 397 | else { 398 | return 8; 399 | } 400 | } 401 | } 402 | } 403 | } 404 | else { 405 | if (x[211] <= 1) { 406 | if (x[96] <= 1) { 407 | if (x[266] <= 61) { 408 | if (x[94] <= 46) { 409 | return 4; 410 | } 411 | else { 412 | return 6; 413 | } 414 | } 415 | else { 416 | return 7; 417 | } 418 | } 419 | else { 420 | return 6; 421 | } 422 | } 423 | else { 424 | if (x[155] <= 4) { 425 | if (x[428] <= 1) { 426 | if (x[430] <= 87) { 427 | return 7; 428 | } 429 | else { 430 | return 9; 431 | } 432 | } 433 | else { 434 | if (x[209] <= 10) { 435 | return 4; 436 | } 437 | else { 438 | return 9; 439 | } 440 | } 441 | } 442 | else { 443 | return 4; 444 | } 445 | } 446 | } 447 | } 448 | else { 449 | if (x[211] <= 31) { 450 | if (x[596] <= 0) { 451 | if (x[267] <= 120) { 452 | if (x[155] <= 65) { 453 | if (x[97] <= 1) { 454 | return 4; 455 | } 456 | else { 457 | return 6; 458 | } 459 | } 460 | else { 461 | if (x[99] <= 22) { 462 | return 8; 463 | } 464 | else { 465 | return 6; 466 | } 467 | } 468 | } 469 | else { 470 | if (x[401] <= 3) { 471 | if (x[347] <= 13) { 472 | return 7; 473 | } 474 | else { 475 | return 9; 476 | } 477 | } 478 | else { 479 | if (x[381] <= 7) { 480 | return 5; 481 | } 482 | else { 483 | return 9; 484 | } 485 | } 486 | } 487 | } 488 | else { 489 | if (x[346] <= 1) { 490 | if (x[483] <= 1) { 491 | return 3; 492 | } 493 | else { 494 | if (x[351] <= 139) { 495 | return 2; 496 | } 497 | else { 498 | return 8; 499 | } 500 | } 501 | } 502 | else { 503 | if (x[355] <= 3) { 504 | if (x[185] <= 207) { 505 | return 5; 506 | } 507 | else { 508 | return 3; 509 | } 510 | } 511 | else { 512 | return 8; 513 | } 514 | } 515 | } 516 | } 517 | else { 518 | if (x[354] <= 0) { 519 | if (x[352] <= 93) { 520 | if (x[356] <= 2) { 521 | if (x[216] <= 2) { 522 | return 5; 523 | } 524 | else { 525 | return 5; 526 | } 527 | } 528 | else { 529 | return 9; 530 | } 531 | } 532 | else { 533 | if (x[209] <= 39) { 534 | if (x[153] <= 56) { 535 | return 4; 536 | } 537 | else { 538 | return 3; 539 | } 540 | } 541 | else { 542 | if (x[439] <= 3) { 543 | return 9; 544 | } 545 | else { 546 | return 7; 547 | } 548 | } 549 | } 550 | } 551 | else { 552 | if (x[155] <= 0) { 553 | if (x[317] <= 0) { 554 | if (x[319] <= 32) { 555 | return 9; 556 | } 557 | else { 558 | return 9; 559 | } 560 | } 561 | else { 562 | if (x[191] <= 3) { 563 | return 9; 564 | } 565 | else { 566 | return 4; 567 | } 568 | } 569 | } 570 | else { 571 | if (x[624] <= 0) { 572 | if (x[180] <= 1) { 573 | return 4; 574 | } 575 | else { 576 | return 8; 577 | } 578 | } 579 | else { 580 | if (x[484] <= 0) { 581 | return 3; 582 | } 583 | else { 584 | return 2; 585 | } 586 | } 587 | } 588 | } 589 | } 590 | } 591 | } 592 | else { 593 | if (x[270] <= 0) { 594 | if (x[273] <= 1) { 595 | if (x[296] <= 121) { 596 | if (x[656] <= 3) { 597 | if (x[216] <= 91) { 598 | if (x[323] <= 173) { 599 | return 6; 600 | } 601 | else { 602 | return 2; 603 | } 604 | } 605 | else { 606 | return 5; 607 | } 608 | } 609 | else { 610 | if (x[356] <= 7) { 611 | if (x[486] <= 57) { 612 | return 5; 613 | } 614 | else { 615 | return 6; 616 | } 617 | } 618 | else { 619 | return 8; 620 | } 621 | } 622 | } 623 | else { 624 | if (x[320] <= 13) { 625 | if (x[344] <= 27) { 626 | if (x[686] <= 6) { 627 | return 2; 628 | } 629 | else { 630 | return 7; 631 | } 632 | } 633 | else { 634 | return 2; 635 | } 636 | } 637 | else { 638 | return 4; 639 | } 640 | } 641 | } 642 | else { 643 | if (x[656] <= 6) { 644 | if (x[159] <= 5) { 645 | if (x[214] <= 39) { 646 | if (x[246] <= 4) { 647 | return 6; 648 | } 649 | else { 650 | return 4; 651 | } 652 | } 653 | else { 654 | if (x[566] <= 12) { 655 | return 9; 656 | } 657 | else { 658 | return 2; 659 | } 660 | } 661 | } 662 | else { 663 | if (x[319] <= 184) { 664 | if (x[344] <= 251) { 665 | return 2; 666 | } 667 | else { 668 | return 8; 669 | } 670 | } 671 | else { 672 | return 8; 673 | } 674 | } 675 | } 676 | else { 677 | if (x[404] <= 2) { 678 | if (x[319] <= 0) { 679 | return 2; 680 | } 681 | else { 682 | return 8; 683 | } 684 | } 685 | else { 686 | if (x[428] <= 160) { 687 | if (x[487] <= 1) { 688 | return 8; 689 | } 690 | else { 691 | return 8; 692 | } 693 | } 694 | else { 695 | return 8; 696 | } 697 | } 698 | } 699 | } 700 | } 701 | else { 702 | if (x[347] <= 0) { 703 | if (x[155] <= 0) { 704 | if (x[238] <= 1) { 705 | if (x[456] <= 2) { 706 | if (x[351] <= 62) { 707 | return 2; 708 | } 709 | else { 710 | return 1; 711 | } 712 | } 713 | else { 714 | if (x[488] <= 102) { 715 | return 6; 716 | } 717 | else { 718 | return 4; 719 | } 720 | } 721 | } 722 | else { 723 | if (x[681] <= 1) { 724 | if (x[344] <= 11) { 725 | return 2; 726 | } 727 | else { 728 | return 9; 729 | } 730 | } 731 | else { 732 | if (x[373] <= 182) { 733 | return 7; 734 | } 735 | else { 736 | return 8; 737 | } 738 | } 739 | } 740 | } 741 | else { 742 | if (x[373] <= 163) { 743 | if (x[349] <= 134) { 744 | if (x[371] <= 201) { 745 | return 2; 746 | } 747 | else { 748 | return 9; 749 | } 750 | } 751 | else { 752 | return 8; 753 | } 754 | } 755 | else { 756 | if (x[658] <= 0) { 757 | return 2; 758 | } 759 | else { 760 | return 8; 761 | } 762 | } 763 | } 764 | } 765 | else { 766 | if (x[657] <= 15) { 767 | if (x[354] <= 0) { 768 | if (x[487] <= 44) { 769 | if (x[385] <= 33) { 770 | return 5; 771 | } 772 | else { 773 | return 0; 774 | } 775 | } 776 | else { 777 | return 6; 778 | } 779 | } 780 | else { 781 | if (x[707] <= 0) { 782 | if (x[184] <= 4) { 783 | return 4; 784 | } 785 | else { 786 | return 2; 787 | } 788 | } 789 | else { 790 | if (x[405] <= 64) { 791 | return 7; 792 | } 793 | else { 794 | return 9; 795 | } 796 | } 797 | } 798 | } 799 | else { 800 | if (x[428] <= 4) { 801 | if (x[515] <= 6) { 802 | return 5; 803 | } 804 | else { 805 | if (x[404] <= 4) { 806 | return 8; 807 | } 808 | else { 809 | return 8; 810 | } 811 | } 812 | } 813 | else { 814 | if (x[598] <= 37) { 815 | if (x[211] <= 147) { 816 | return 4; 817 | } 818 | else { 819 | return 9; 820 | } 821 | } 822 | else { 823 | if (x[434] <= 43) { 824 | return 0; 825 | } 826 | else { 827 | return 8; 828 | } 829 | } 830 | } 831 | } 832 | } 833 | } 834 | } 835 | } 836 | } 837 | else { 838 | if (x[489] <= 26) { 839 | if (x[290] <= 33) { 840 | if (x[486] <= 58) { 841 | if (x[296] <= 2) { 842 | if (x[490] <= 90) { 843 | if (x[315] <= 21) { 844 | if (x[178] <= 3) { 845 | if (x[292] <= 77) { 846 | return 3; 847 | } 848 | else { 849 | return 5; 850 | } 851 | } 852 | else { 853 | if (x[263] <= 149) { 854 | return 3; 855 | } 856 | else { 857 | return 5; 858 | } 859 | } 860 | } 861 | else { 862 | if (x[300] <= 15) { 863 | if (x[520] <= 4) { 864 | return 5; 865 | } 866 | else { 867 | return 9; 868 | } 869 | } 870 | else { 871 | return 4; 872 | } 873 | } 874 | } 875 | else { 876 | if (x[627] <= 4) { 877 | if (x[545] <= 170) { 878 | if (x[205] <= 129) { 879 | return 1; 880 | } 881 | else { 882 | return 7; 883 | } 884 | } 885 | else { 886 | return 2; 887 | } 888 | } 889 | else { 890 | return 5; 891 | } 892 | } 893 | } 894 | else { 895 | if (x[153] <= 0) { 896 | if (x[208] <= 47) { 897 | if (x[247] <= 2) { 898 | if (x[598] <= 23) { 899 | return 4; 900 | } 901 | else { 902 | return 3; 903 | } 904 | } 905 | else { 906 | if (x[358] <= 5) { 907 | return 5; 908 | } 909 | else { 910 | return 0; 911 | } 912 | } 913 | } 914 | else { 915 | if (x[316] <= 48) { 916 | if (x[377] <= 97) { 917 | return 7; 918 | } 919 | else { 920 | return 3; 921 | } 922 | } 923 | else { 924 | return 9; 925 | } 926 | } 927 | } 928 | else { 929 | if (x[488] <= 60) { 930 | if (x[315] <= 129) { 931 | if (x[490] <= 157) { 932 | return 3; 933 | } 934 | else { 935 | return 1; 936 | } 937 | } 938 | else { 939 | return 3; 940 | } 941 | } 942 | else { 943 | return 8; 944 | } 945 | } 946 | } 947 | } 948 | else { 949 | if (x[657] <= 4) { 950 | if (x[152] <= 16) { 951 | if (x[270] <= 8) { 952 | if (x[601] <= 67) { 953 | return 5; 954 | } 955 | else { 956 | if (x[301] <= 8) { 957 | return 6; 958 | } 959 | else { 960 | return 8; 961 | } 962 | } 963 | } 964 | else { 965 | if (x[572] <= 36) { 966 | return 1; 967 | } 968 | else { 969 | if (x[579] <= 2) { 970 | return 0; 971 | } 972 | else { 973 | return 2; 974 | } 975 | } 976 | } 977 | } 978 | else { 979 | if (x[439] <= 49) { 980 | if (x[316] <= 91) { 981 | if (x[263] <= 194) { 982 | return 2; 983 | } 984 | else { 985 | return 8; 986 | } 987 | } 988 | else { 989 | return 6; 990 | } 991 | } 992 | else { 993 | return 3; 994 | } 995 | } 996 | } 997 | else { 998 | if (x[439] <= 12) { 999 | if (x[464] <= 1) { 1000 | return 2; 1001 | } 1002 | else { 1003 | if (x[149] <= 54) { 1004 | if (x[459] <= 51) { 1005 | return 3; 1006 | } 1007 | else { 1008 | return 8; 1009 | } 1010 | } 1011 | else { 1012 | return 3; 1013 | } 1014 | } 1015 | } 1016 | else { 1017 | if (x[180] <= 1) { 1018 | return 0; 1019 | } 1020 | else { 1021 | if (x[315] <= 8) { 1022 | if (x[292] <= 173) { 1023 | return 3; 1024 | } 1025 | else { 1026 | return 8; 1027 | } 1028 | } 1029 | else { 1030 | return 8; 1031 | } 1032 | } 1033 | } 1034 | } 1035 | } 1036 | } 1037 | else { 1038 | if (x[297] <= 4) { 1039 | if (x[486] <= 58) { 1040 | if (x[186] <= 6) { 1041 | if (x[294] <= 89) { 1042 | if (x[328] <= 11) { 1043 | if (x[147] <= 9) { 1044 | return 5; 1045 | } 1046 | else { 1047 | return 3; 1048 | } 1049 | } 1050 | else { 1051 | return 4; 1052 | } 1053 | } 1054 | else { 1055 | if (x[439] <= 1) { 1056 | return 9; 1057 | } 1058 | else { 1059 | return 3; 1060 | } 1061 | } 1062 | } 1063 | else { 1064 | if (x[301] <= 78) { 1065 | if (x[295] <= 202) { 1066 | if (x[299] <= 86) { 1067 | return 5; 1068 | } 1069 | else { 1070 | return 8; 1071 | } 1072 | } 1073 | else { 1074 | return 3; 1075 | } 1076 | } 1077 | else { 1078 | return 0; 1079 | } 1080 | } 1081 | } 1082 | else { 1083 | if (x[657] <= 2) { 1084 | if (x[430] <= 83) { 1085 | return 5; 1086 | } 1087 | else { 1088 | if (x[601] <= 34) { 1089 | return 4; 1090 | } 1091 | else { 1092 | if (x[216] <= 102) { 1093 | return 6; 1094 | } 1095 | else { 1096 | return 5; 1097 | } 1098 | } 1099 | } 1100 | } 1101 | else { 1102 | if (x[373] <= 37) { 1103 | return 8; 1104 | } 1105 | else { 1106 | return 5; 1107 | } 1108 | } 1109 | } 1110 | } 1111 | else { 1112 | if (x[598] <= 0) { 1113 | if (x[210] <= 4) { 1114 | if (x[321] <= 223) { 1115 | if (x[265] <= 156) { 1116 | if (x[571] <= 37) { 1117 | return 4; 1118 | } 1119 | else { 1120 | return 6; 1121 | } 1122 | } 1123 | else { 1124 | return 9; 1125 | } 1126 | } 1127 | else { 1128 | if (x[404] <= 110) { 1129 | if (x[406] <= 148) { 1130 | return 7; 1131 | } 1132 | else { 1133 | return 9; 1134 | } 1135 | } 1136 | else { 1137 | return 4; 1138 | } 1139 | } 1140 | } 1141 | else { 1142 | if (x[653] <= 6) { 1143 | if (x[377] <= 19) { 1144 | return 7; 1145 | } 1146 | else { 1147 | if (x[515] <= 70) { 1148 | return 9; 1149 | } 1150 | else { 1151 | return 8; 1152 | } 1153 | } 1154 | } 1155 | else { 1156 | if (x[513] <= 150) { 1157 | return 3; 1158 | } 1159 | else { 1160 | return 8; 1161 | } 1162 | } 1163 | } 1164 | } 1165 | else { 1166 | if (x[486] <= 7) { 1167 | if (x[454] <= 176) { 1168 | if (x[269] <= 4) { 1169 | if (x[272] <= 45) { 1170 | return 5; 1171 | } 1172 | else { 1173 | return 3; 1174 | } 1175 | } 1176 | else { 1177 | if (x[219] <= 6) { 1178 | return 3; 1179 | } 1180 | else { 1181 | return 5; 1182 | } 1183 | } 1184 | } 1185 | else { 1186 | return 0; 1187 | } 1188 | } 1189 | else { 1190 | if (x[400] <= 30) { 1191 | if (x[573] <= 240) { 1192 | if (x[404] <= 2) { 1193 | return 3; 1194 | } 1195 | else { 1196 | return 8; 1197 | } 1198 | } 1199 | else { 1200 | return 3; 1201 | } 1202 | } 1203 | else { 1204 | if (x[241] <= 5) { 1205 | return 6; 1206 | } 1207 | else { 1208 | if (x[406] <= 226) { 1209 | return 0; 1210 | } 1211 | else { 1212 | return 8; 1213 | } 1214 | } 1215 | } 1216 | } 1217 | } 1218 | } 1219 | } 1220 | } 1221 | else { 1222 | if (x[347] <= 0) { 1223 | if (x[206] <= 0) { 1224 | if (x[457] <= 1) { 1225 | if (x[550] <= 4) { 1226 | if (x[300] <= 1) { 1227 | if (x[539] <= 125) { 1228 | if (x[150] <= 204) { 1229 | return 1; 1230 | } 1231 | else { 1232 | return 2; 1233 | } 1234 | } 1235 | else { 1236 | return 5; 1237 | } 1238 | } 1239 | else { 1240 | if (x[237] <= 2) { 1241 | if (x[487] <= 145) { 1242 | return 5; 1243 | } 1244 | else { 1245 | return 1; 1246 | } 1247 | } 1248 | else { 1249 | if (x[487] <= 4) { 1250 | return 3; 1251 | } 1252 | else { 1253 | return 8; 1254 | } 1255 | } 1256 | } 1257 | } 1258 | else { 1259 | if (x[580] <= 1) { 1260 | if (x[518] <= 212) { 1261 | return 8; 1262 | } 1263 | else { 1264 | return 1; 1265 | } 1266 | } 1267 | else { 1268 | return 2; 1269 | } 1270 | } 1271 | } 1272 | else { 1273 | if (x[653] <= 1) { 1274 | if (x[125] <= 2) { 1275 | if (x[175] <= 13) { 1276 | if (x[650] <= 120) { 1277 | return 2; 1278 | } 1279 | else { 1280 | return 1; 1281 | } 1282 | } 1283 | else { 1284 | return 6; 1285 | } 1286 | } 1287 | else { 1288 | return 2; 1289 | } 1290 | } 1291 | else { 1292 | if (x[265] <= 11) { 1293 | return 1; 1294 | } 1295 | else { 1296 | if (x[268] <= 239) { 1297 | return 8; 1298 | } 1299 | else { 1300 | return 1; 1301 | } 1302 | } 1303 | } 1304 | } 1305 | } 1306 | else { 1307 | if (x[554] <= 1) { 1308 | if (x[515] <= 84) { 1309 | if (x[517] <= 39) { 1310 | if (x[372] <= 3) { 1311 | return 3; 1312 | } 1313 | else { 1314 | return 9; 1315 | } 1316 | } 1317 | else { 1318 | if (x[152] <= 1) { 1319 | if (x[607] <= 29) { 1320 | return 7; 1321 | } 1322 | else { 1323 | return 8; 1324 | } 1325 | } 1326 | else { 1327 | if (x[266] <= 56) { 1328 | return 8; 1329 | } 1330 | else { 1331 | return 1; 1332 | } 1333 | } 1334 | } 1335 | } 1336 | else { 1337 | if (x[320] <= 23) { 1338 | if (x[372] <= 15) { 1339 | if (x[684] <= 1) { 1340 | return 2; 1341 | } 1342 | else { 1343 | return 7; 1344 | } 1345 | } 1346 | else { 1347 | return 6; 1348 | } 1349 | } 1350 | else { 1351 | if (x[469] <= 37) { 1352 | if (x[266] <= 248) { 1353 | return 8; 1354 | } 1355 | else { 1356 | return 7; 1357 | } 1358 | } 1359 | else { 1360 | return 2; 1361 | } 1362 | } 1363 | } 1364 | } 1365 | else { 1366 | if (x[371] <= 44) { 1367 | if (x[685] <= 41) { 1368 | if (x[284] <= 27) { 1369 | if (x[359] <= 17) { 1370 | return 2; 1371 | } 1372 | else { 1373 | return 6; 1374 | } 1375 | } 1376 | else { 1377 | return 3; 1378 | } 1379 | } 1380 | else { 1381 | return 3; 1382 | } 1383 | } 1384 | else { 1385 | return 6; 1386 | } 1387 | } 1388 | } 1389 | } 1390 | else { 1391 | if (x[658] <= 0) { 1392 | if (x[575] <= 108) { 1393 | if (x[540] <= 1) { 1394 | if (x[211] <= 52) { 1395 | if (x[379] <= 136) { 1396 | return 7; 1397 | } 1398 | else { 1399 | if (x[410] <= 22) { 1400 | return 4; 1401 | } 1402 | else { 1403 | return 4; 1404 | } 1405 | } 1406 | } 1407 | else { 1408 | if (x[353] <= 145) { 1409 | return 4; 1410 | } 1411 | else { 1412 | if (x[212] <= 47) { 1413 | return 4; 1414 | } 1415 | else { 1416 | return 9; 1417 | } 1418 | } 1419 | } 1420 | } 1421 | else { 1422 | if (x[524] <= 3) { 1423 | if (x[213] <= 60) { 1424 | return 5; 1425 | } 1426 | else { 1427 | if (x[296] <= 247) { 1428 | return 8; 1429 | } 1430 | else { 1431 | return 1; 1432 | } 1433 | } 1434 | } 1435 | else { 1436 | return 2; 1437 | } 1438 | } 1439 | } 1440 | else { 1441 | if (x[244] <= 23) { 1442 | if (x[430] <= 2) { 1443 | return 1; 1444 | } 1445 | else { 1446 | if (x[573] <= 97) { 1447 | return 4; 1448 | } 1449 | else { 1450 | if (x[556] <= 105) { 1451 | return 6; 1452 | } 1453 | else { 1454 | return 2; 1455 | } 1456 | } 1457 | } 1458 | } 1459 | else { 1460 | if (x[267] <= 232) { 1461 | return 8; 1462 | } 1463 | else { 1464 | return 0; 1465 | } 1466 | } 1467 | } 1468 | } 1469 | else { 1470 | if (x[515] <= 56) { 1471 | if (x[322] <= 227) { 1472 | if (x[712] <= 5) { 1473 | if (x[182] <= 19) { 1474 | return 4; 1475 | } 1476 | else { 1477 | if (x[319] <= 4) { 1478 | return 3; 1479 | } 1480 | else { 1481 | return 8; 1482 | } 1483 | } 1484 | } 1485 | else { 1486 | return 9; 1487 | } 1488 | } 1489 | else { 1490 | if (x[269] <= 147) { 1491 | if (x[430] <= 120) { 1492 | if (x[495] <= 103) { 1493 | return 1; 1494 | } 1495 | else { 1496 | return 8; 1497 | } 1498 | } 1499 | else { 1500 | return 4; 1501 | } 1502 | } 1503 | else { 1504 | if (x[578] <= 11) { 1505 | return 7; 1506 | } 1507 | else { 1508 | return 3; 1509 | } 1510 | } 1511 | } 1512 | } 1513 | else { 1514 | if (x[294] <= 228) { 1515 | if (x[429] <= 175) { 1516 | if (x[433] <= 8) { 1517 | return 8; 1518 | } 1519 | else { 1520 | if (x[441] <= 214) { 1521 | return 8; 1522 | } 1523 | else { 1524 | return 8; 1525 | } 1526 | } 1527 | } 1528 | else { 1529 | return 8; 1530 | } 1531 | } 1532 | else { 1533 | if (x[290] <= 3) { 1534 | return 1; 1535 | } 1536 | else { 1537 | if (x[405] <= 237) { 1538 | return 7; 1539 | } 1540 | else { 1541 | if (x[628] <= 22) { 1542 | return 1; 1543 | } 1544 | else { 1545 | return 8; 1546 | } 1547 | } 1548 | } 1549 | } 1550 | } 1551 | } 1552 | } 1553 | } 1554 | } 1555 | }; 1556 | 1557 | --------------------------------------------------------------------------------