RAD Studio 10.4.1 is now available! Learn more. Looking for discounts? Visit our Special Offers page!
News

Implementing FANN (Fast Artificial Neural Network) to C++ Builder/Delphi Projects

Author: Yilmaz Yoru

Would you like to make your applications learning and predicting applications (May be just thinking applications) ? 

In this blog post we gonna use open source Fast Artificial Neural Network Library made by Steffen Nissen. FANN Library is very simple to use and it has good documentation and written in C programming language which makes it faster. It is open source so you can easily implement and modify your code. It has English, French & Polish help.

 

1. Quick Introduction To Artificial Intelligence

The human brain is one of the great parts of our building; we want to know more and more and we want to dive deeper and deeper after Alan Turing – who is the founder of computer science, mathematician, philosopher – built the first Enigma Computer. Now, large computation capabilities, multi-core CPUs and large memory capacities with 64bit systems are boosting this technology as well as with the large and high speed internet and better programming techniques. Nowadays many companies developing Artificial Intelligence (AI) applications, it is very clear that there is an apparent increment in issue of patents in the last 5 year. Still this is also a good research area in academic area. Computers thinking like a human are still absent in our world but day by day we are getting much more closer. If you are new to this area you can get more information about AI 

https://en.wikipedia.org/wiki/Artificial_intelligence

https://futureoflife.org/background/benefits-risks-of-artificial-intelligence/

 

2.  Quick Explanation to ANN

Artificial Neural Network is part of Artificial Intelligence which models artificial neurons connected each other in artificial layers. Codes based on this ANN theory works well in many applications. In a general view, ANN applications simulate connected neurons in a specific problem and this allows you to train from inputs and estimate results by trained ANN. You can compare this predictions by real results or results obtained from any other techniques.

Some of ANN applications can be File/Data Compression by ANNs, Neocognitron, Feedforward NN for Rapid Vision, Speechreading (Lipreading), Detection and Tracking of Moving Targets (ICBMs), GPS Data Mining Applications, Reconstruction of 3D Buildings and Models, Age Modeling Systems, Games, Predictions in Medical Applications, Simulation of Robotic Systems, Digital Assistants, Engineering Approaches in Analyzes of Systems … https://cw.felk.cvut.cz/w/_media/courses/a4m33bia/ann_examples_2011.pdf

https://en.wikipedia.org/wiki/Artificial_neural_network

http://neuralnetworksanddeeplearning.com/chap1.html 

https://www.analyticsvidhya.com/blog/2014/10/ann-work-simplified/

 

3. An Application from my Study

Let me give an example from my PhD thesis. I studied in a ceramic factory which has huge co-generation system with big dryers, pumps, ex-changers, pipes and heating systems. There were many points in this system and hourly Energy Efficiency and Exergy Efficiency of this system needs to be calculated by theories of thermodynamic laws. System was running and there were some difficulties in measurement. It’s done with input data of the system with calculations by many formulas, tables, and variables like inlet air velocity, temperature, pressure, power generation, heat generations etc. It was really hard to calculate enthalpy and entropy of each node when this huge system is operating. In my thesis I also use ANN which learns thermodynamics of the system from some given parameters and predicts results with the calculations made by thermodynamic results. Here are some papers about this research if u are interested.

http://www.inderscienceonline.com/doi/abs/10.1504/IJEX.2010.031239

http://onlinelibrary.wiley.com/doi/10.1002/er.1561/abstract

http://ieeexplore.ieee.org/document/5381338/?reload=true

 

4. Quick Introduction To FANN Library

Fast Artificial Neural Network (FANN) is an old ( about 14 years) open source ANN library made by  Steffen Nissen (http://leenissen.dk). It supports more than 20+ programming languages (http://leenissen.dk/fann/wp/language-bindings/) including Delphi and C++ Builder. You can reach full information and documentation (http://libfann.github.io/fann/docs/files/fann-h.html) including downloads of sources from their web page ((http://leenissen.dk).

I will not explain much more about what AI ,  ANN and FANN is. There are good documentations about those on their web page and on the internet. 

You can easily find many ANN libraries on the internet. This library is simple and written in C that makes it generic and simple to implement. It is also fast due to C programming language. Compiled library can be used in many programming languages as mentioned above.

 

5. Implementing FANN (Fast Artificial Neural Network) to C++ Builder Applications

First I want to say that you can use FANN library in console applications,  also in both VCL and FMX applications. Here I will explain how you can implement into MultiDevice applications. Therby you can use this library in your applications which runs on Win, Android, iOS, MacOS and Linux operating systems.

Step 1: Download FANN Library package from Nissen’s official web page (http://leenissen.dk). This package includes binaries, cmake files, datasets, examples, source codes in src folder and some examples.

Step 2: Create a new MultiDevice C++ Builder Application. Add a Panel (Panel1) which has 32 pixels height and two Buttons (TEST, TRAIN) into it.  Align Panel  to the bottom of Form1. Add a Memo (Memo1) into form an make it client. This will be our visual output place 🙂

Step 3Create FANN_Test folder and  save all project files with “FANN_” prefix into this folder as listed below; 

FANN_Project1.cbproj

FANN_Project1.cbproj.local

FANN_Project1.cpp 

FANN_Project1PCH1.h 

FANN_Unit1.cpp 

FANN_Unit1.fmx 

FANN_Unit1.h

 

Step 4Now we need source files to compile with our application. Unzip package , copy “src” folder from this folder into this FANN_Test folder. Make your RADS/C++ Builder/Delphi IDE size smaller and drag src folder from FANN_Test to Project Manager on the “FANN_Project1.exe”. When IDE asks “Would you like to add the selected files to project …” confirm with Yes. 

Step 5We need include folder of FANN for headers. From the IDE menu go to Project -> Options ->C++ Compiler->Directories and Conditionals->Include File Search path. Add here “src/include” folder which is in our FANN_Test folder

Step 6Now you can add a header into our FANN_Unit1.cpp. Below #include “FANN_Unit1.h” line add this #include “floatfann.h”.

If you do all steps now we implemented FANN library into our project with its original sources without any tweaks or changes on original files. That means FANN source files and headers are very friendly because of its native C codes.

Step 7Now lets test if all is fine. Build all projects from Projects -> Build All Projects menu. If Success that means all is fine. if there are errors please check steps above. 

Save All

If you want to compile for other OSes don’t Forget to add include search path “src/include” to its project options as we do above. Please check examples, other commands and options of FANN Library to understand its mechanism. 

That’s All 🙂 

Testing Code Phase:

In the folder of FANN Package, there are native examples. Please check simple_train .c and simple_test.c  codes. We gonna cut and paste those codes with some tweaks to implement our MultiDevice application.

Step 1: Go to Form view and double click to TRAIN button and add below  

 

	/* --- TRAINING FANN (from the simple_train.c) --- */

	const unsigned int num_input = 2;
	const unsigned int num_output = 1;
	const unsigned int num_layers = 3;
	const unsigned int num_neurons_hidden = 3;
	const float desired_error = (const float) 0.001;
	const unsigned int max_epochs = 500000;
	const unsigned int epochs_between_reports = 1000;

	struct fann *ann = fann_create_standard(num_layers, num_input, num_neurons_hidden, num_output);

	fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
	fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);

	fann_train_on_file(ann, "xor.data", max_epochs, epochs_between_reports, desired_error);

	fann_save(ann, "xor_float.net");

	fann_destroy(ann);

	Memo1->Lines->LoadFromFile("xor_float.net");

	Memo1->Lines->Add("Training done on xor_float.net file as above");

 

Step 2: Double click to TEST button and add below  

	/* ---- TESTING FANN (from the simple_test.c) ----*/
	char s[255];

	fann_type *calc_out;
	fann_type input[2];

	struct fann *ann = fann_create_from_file("xor_float.net");

	input[0] = -1;
	input[1] = 1;
	calc_out = fann_run(ann, input);

	Memo1->Lines->Add("");
	Memo1->Lines->Add("Testing ... ");
	sprintf(s, "xor test (%f,%f) -> %f\n", input[0], input[1], calc_out[0]);
	Memo1->Lines->Add(s);
	Memo1->Lines->Add("Testing Done ! ");

	fann_destroy(ann);

That’s all now you can run your application successfully. Please check other examples, I am sure you can do very innovative examples and you can implement it easily on your applications. 

 

6. Implementing FANN (Fast Artificial Neural Network) to Delphi Applications

Delphi coders are very lucky 🙂 Just check language bindings section (http://leenissen.dk/fann/wp/language-bindings/)  there is a ready library package with examples.

 

Good Luck All !

 

 


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Related posts
C++

What's New in the GetIt Package Manager - June 2020

C++

RAD Studio 10.4 Now Available, Learn More

C++NewsRAD Studio

Open for Business with 10.4! - May 2020 GM Update

C++DelphiNews

Embarcadero Open Source Project Sponsorship

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES