This is the first part of a multi-series article whose objective is to create a simple AI library that covers a couple of advanced AI topics such as Genetic algorithms, ANN, Fuzzy logics and other evolutionary algorithms.
Series Introduction
My objective is to create a simple AI library that covers couple of advanced AI topics such as Genetic algorithms, ANN, Fuzzy logics and other evolutionary algorithms. The only challenge to complete this series would be having enough time working on code and articles.
Having the code itself might not be the main target, however, understanding these algorithms is. Wish it will be useful to someone someday.
This series will be published in few parts, am not sure how many yet. Anyways, each part will focus on single key topic trying to cover for good.
Please, feel free to comment and ask for any clarifications or hopefully suggest better approaches.
Article Introduction - Part 1 "Basics"
This is Part 1 of multi-parts series, have dedicated it to cover basic library called "CommonLib
" that contains fundamental classes as:
- Graphics wrapper class called "
Canvas
" - Math wrapper classes to handle "
Vector
" & "Matrix
" operations and other math operations - Random generator class "
RandomFactory
"
Each of the above classes shall be explained in details below.
1. Graphics Wrap Class Canvas
Graphics shall be used to test AI libraries will build and hence, had to create one wrap class to contain all common graphics operations and make it easier than creating Graphics
objects.
Canvas
class wraps some graphics operations as DrawLine
, DrawBox
(Square
), DrawCircle
, DrawText
and Clear
.
Mainly, it contains one internal graphics object g
and bitmap object bmp
.
Constructor
Public Sub New(_width As Integer, _height As Integer)
Me._Width = _width
Me._Height = _height
Me._bmp = New Bitmap(_width, _height)
g = Graphics.FromImage(_bmp)
g.SmoothingMode = SmoothingMode.HighQuality
End Sub
Width
& Height
are mandatory integers to create Canvas
object which represents drawing area.
Here is an example of declaring canvas
object and how to use it.
Imports CommonLib.CommonLib
Public Class Form1
Private myCanvas As Canvas
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myCanvas = New Canvas(PictureBox1.Width, PictureBox1.Height)
Draw()
End Sub
Private Sub Draw()
With myCanvas
.DrawBox(50, 25, 25, Color.Black)
.FillBox(50, 100, 25, Color.Red)
.DrawCircle(50, 100, 200, Color.Blue)
.FillCircle(50, 200, 200, Color.Green)
.DrawText("Test String", 250, 200, Color.Red)
End With
PictureBox1.Image = myCanvas.Image
End Sub
Private Sub Clear()
myCanvas.Clear()
PictureBox1.Image = myCanvas.Image
End Sub
End Class
and here is the result of the above code:
2. Math Classes
MathFunctions
This class wraps some useful math functions:
Constraint Function
Public Function Constraint(Value As Single, min As Single, max As Single) As Single
If Value <= min Then
Return min
ElseIf Value >= max Then
Return max
End If
Return Value
End Function
Map Function
Public Shared Function Map(ByVal value As Single, ByVal start1 As Single, _
ByVal stop1 As Single, ByVal start2 As Single, ByVal stop2 As Single) As Single
Dim Output As Single = start2 + (stop2 - start2) * ((value - start1) / _
(stop1 - start1))
Dim errMessage As String = Nothing
If Output <> Output Then
errMessage = "NaN (not a number)"
Throw New Exception(errMessage)
ElseIf Output = Single.NegativeInfinity _
OrElse Output = Single.PositiveInfinity Then
errMessage = "infinity"
Throw New Exception(errMessage)
End If
Return Output
End Function
Norm Function
Public Shared Function Norm(ByVal value As Single, _
ByVal start As Single, ByVal [stop] As Single) As Single
Return (value - start) / ([stop] - start)
End Function
GetBitArray Function
Public Function GetBitArray(Value As Integer) As Integer()
Dim Result(7) As Integer
Dim sValue As String
Dim cValue() As Char
Value = Constraint(Value, 0, 255)
sValue = Convert.ToString(Value, 2).PadLeft(8, "0"c)
cValue = sValue.ToArray
For i As Integer = 0 To cValue.Count - 1
If cValue(i) = "1"c Then
Result(i) = 1
Else
Result(i) = 0
End If
Next
Return Result
End Function
Matrix Operations
Matrix operations are very important (especially for ANN later) and hence it is worth creating a separate class(s) to handle different cases of matrix functions, namely, Vector
and Matrix1D
.
Lots of resources are available to describe matrices and its functions and hence, I will not spend much time on that.
Just need to mention that, two special matrices are only considered so far in CommonLib
; Matrix1D
and Vector
.
Matrix1D
Is single column-matrix:
Later, this class will help in simplifying neural network creation. This class implements IMatrix
Interface.
Size
- Size or Capacity of Matrix - Simply Number of Stored Elements in Values Array Product
- Implement Matrix Product function between 2 metrics, m1
and m2
or matrix and scalar Add
- Implement Matrix addition method between 2 metrics, m1
and m2
or matrix and scalar Sub
- Implement Matrix subtraction method between 2 metrics, m1
and m2
or matrix and scalar Divide
- Implement Matrix divide method between 2 metrics, m1
and m2
or matrix and scalar Sum
- Sum of all matrix elements a1+a2+a3+...........+an RandomizeValues
- Randomize matrix elements between min and max values Copy
- Copy contents of one matrix into object starting from given starting index ForceValues
- Forces all elements of matrix to ForcedValue
GetValue
- Return index element of matrix Index
starts with 0
SetValue
- Set value of matrix element at position Index 0
indexed positions
Vector
Vector
is a special matrix with 1 row and multiple columns (in our case, only 2 and 3 columns are considered not higher). This is one way to think about vectors, however the most efficient way is to consider a vector
as "Magnitude plus Direction" object. This is a very powerful object and yet so much simple; just by giving 2 numbers, we may extract magnitude and direction.
Vectors play a very important rule in math and physics and maybe, I will have a separate article to cover vectors only. For now, you may visit this link to get more information about vectors and vectors operations.
Note: Only 2D and 3D vectors are considered in CommonLib
, for higher dimensional vectors, matrix shall be used.
Many vector operations have been implemented in CommonLib
such as:
Randomize
- Randomize X
, Y
and Z
components of vector between 0
and 1
Magnitude
- Calculates the Euclidean magnitude (length) of the vector and returns the result = SQRT (X2+Y2+Z2) Add
- Adds x
, y
, and z
components to a vector, adds one vector to another, or adds two independent vectors together. Here is a graphical representation of vector addition from Wikipedia.
Sub
- Perform vector subtraction with other vectors or scalar values:
Mult
- Implements vector scalar multiplication Div
- Is scalar divide implementation Distance
- Calculates the Euclidean distance between two vectors = SQRT (dX2+dY2+dZ2) Dot
- Implements vectors dot product. More at this link. Cross
- Implements vectors cross product. Here is the link explaining the math behind it. Normalize
- Normalize the vector to length 1 (make it a unit vector). Limit
- Limit the magnitude of this vector to the value passed as max
parameter. SetMag
- Set the magnitude of this vector to the value passed as len
parameter. Heading
- Calculate the angle of rotation for this vector. Rotate
- Rotate the vector by an angle, magnitude remains the same. AngleBetween
- Calculates and returns the angle (in radians) between two vectors.
3. RandomFactory
This class is very important for both initialization of different AI objects and for test purposes. It extends .NET Random
class and adds further methods:
It can provide different random functions in addition to numbers (integer
, Single
and Double
), as Random Color, Random Character, Random Boolean.
In addition, it has an implementation for Gaussian
random function using Box-Muller transform:
Public Function NextGaussian(Optional ByVal Mean As Double = 0, _
Optional ByVal StdDeviation As Double = 1) As Double
Dim X1 = _Gen.NextDouble()
Dim X2 = _Gen.NextDouble()
Dim StdDistribution = Math.Sqrt(-2.0 * Math.Log(X1)) * Math.Sin(2.0 * Math.PI * X2)
Dim GaussianRnd = Mean + StdDeviation * StdDistribution
Return GaussianRnd
End Function
Also, it implements Triangle
distribution random generation (you may check this wiki article):
Public Function NextTriangular(ByVal min As Double, ByVal max As Double, _
ByVal mode As Double) As Double
Dim u = _Gen.NextDouble()
If (u < (mode - min) / (max - min)) Then
Return min + Math.Sqrt(u * (max - min) * (mode - min))
Else
Return max - Math.Sqrt((1 - u) * (max - min) * (max - mode))
End If
End Function
Shuffle
method provides random shuffle implementation for a list by using the Fisher-Yates/Knuth algorithm:
Public Sub Shuffle(ByVal list As IList)
For i = 0 To list.Count - 1
Dim j = _Gen.Next(0, i + 1)
Dim temp = list(j)
list(j) = list(i)
list(i) = temp
Next i
End Sub
To get efficient result of RandomFactory
class, a global or form level object shall be created and used through out the code.
What is Next
The next article will be about Genetic Algorithms (might be fuzzy controller!) depends on which code shall be ready first.
To Do List
- Add 2D Matrix operations implementation. 1D matrix is good for now, however extend library to include 2D shall add more efficiency or practicality. I wish to complete this task before starting ANN library.
History
CommonLib
has been built to act as supporting class library for different projects, hence it shall be kept as a separate solution and only add references.
Version 1 of this library is completed by August 2017, expecting further versions to follow.