This Tutorial does not represent a practical use of a MATLAB generated .NET DLL since it requires the installation and use of MATLAB and the MATLAB Compiler Runtime (MCR) for a scenario that could be easily handled by a more elegant algorithm such as the one discussed in my earlier post on RPN expression parsing. However, it does seek to demonstrate the principles behind developing an algorithm in MATLAB which could be then exported to a .NET component and reused in any managed language. MATLAB provides a fast prototyping environment which gives the programmer a vast array of tools which she can then use to design and test her algorithms. A subset of these functionalities are provided in the MCR which is basically an installable, free, headless version of MATLAB. Since the MCR is a MATLAB instance it requires the same amount of time for initial loading into memory and will also be as memory intensive as its GUI cousin.
That said, MATLAB allows the packaging of its code into an encrypted Dll file which can then be executed by the MCR. In addition to .NET, MATLAB code can also be exported to C libraries , Java libraries or an executable file.
The first thing we will do is fire up MATLAB, we will access its computational engine through the use of of the eval function which will return the result of a mathematical expression passed to it as a string. There are the steps.
- Launch MATLAB
- Type edit in the MATLAB command window then press enter/return.
- In the new window create the function as seen below then hit the save button.
function result = calculator(input)
result = eval(input);
- Click File->New->Deployment Project then type calculator in the name box.
- Select a location to save the project and .NET Assembly from the type drop down box.
- In the .NET Assembly window, under the build tab change the default name of the class to “demo” then drag and drop the calculator.m file unto the class to add it as a method of the class, then hit the compile button. This should create the calculator.dll which can be referenced from your c# application.
- Fire up Visual Studio and create a new C# Windows Forms Application, to this application add a reference to the calculator.dll file, you will also need to add a reference to the MWArray.Dll which is located at “C:\Program Files\MATLAB\MATLAB Compiler Runtime\v716\toolbox\dotnetbuilder\bin\win64\v2.0\MWArray.dll” (version dependent path) in order to facilitate the conversion of .NET types to MATLAB types.
- GUI for expression parser application.
- C# Application code.
public partial class Form1 : Form
private void button1_Click(object sender, EventArgs e)
var calc= new demo();
- Run the application then enter a mathematical expression and hit the Calculate button, this will send the string to be interpreted by the MCR and return a result to the user, the end.
DISCLAMER: This post does not in any way prove or disprove the validity of using neural networks to predict the lottery. It is purely for the purpose of demonstrating certain capabilities available in MATLAB ® . The results and conclusions are my opinion and may or may not constitute applicable techniques of predicting the popular Jamaican Lottery Cash Pot game.
Supreme Ventures Jamaica Limited has a lottery game called Cash Pot (CP) . The game is based on 36 balls being loaded into a chamber and one ball been selected at random from the grouping. The game is ran four (4) times each day seven (7) days per week.
While doing a little tongue and cheek research at my favorite barbershop, I stumbled upon some heuristics that are employed by most patrons who play the (CP) game. One involved writing down the day, time, and winning number for each day’s lottery. After building up a sufficient dataset, they could then query a particular day and time; and with some simple arithmetic tally the most likely number to be played on that day and time. I was informed that this proved to be a very efficient way of telling which number was to be played next. Another popular heuristic involved pre-assigned symbols; these symbols were associated with each of the thirty six (36) numbers. Then based on dreams aka “rakes” numbers would be chosen that matched the symbols seen in the “rake”. These two methods were the favorite amongst the players of Cash Pot.
Procedure for predicting Cash Pot with MATLAB ANN:
- Get the dataset from Supreme Ventures Jamaica website. [contains all winning numbers with date and time]
- We will need to do some twiddling with the file in order to get it into a format that MATLAB can use. To do that we need to remove all headings/sub-headings and labels.
- Next remove the DRAW# and MARK columns since we will not be using those in our analysis.
- In column D use the =WEEKDAY() formula to get the day number from the corresponding date: repeat for all rows.
- Use find and replace to replace MORNING with 1, MIDDAY with 2, DRIVETIME with 3 and EVENING with 4. [Save the file]
- Using MATLAB folder explorer, navigate to the file then double click on it to run the import tool.
- Select columns B and D then hit the import button; this should import only columns B and D, rename the imported matrix to cpInputs .
- Select column C and hit the import button; this should import column C only, rename the imported matrix to cpTargets.
- Because MATLAB sees Neural Network(NN) features as rows, transpose the two matrices using
cpInputs = cpInputs’;
cpTargets = cpTargets’;
- In the MATLAB command window type nntool.
- Import cpInputs and cpTargets into the NN data manager.
- Hit the new button on the Neural Network Data Manager and change the default name to cpNN.
- Set Input data to cpInputs, Target data to cpTargets.
- Hit the create button to create the NN.
The newly created NN has two inputs, the first been the day of the week on which the [CP] is scheduled to be played and the second input the time of day that the [CP] is scheduled to played. It also has a hidden layer with 10 neurons with associated bias, and an output layer with 1 neuron and its associated bias. The output is a scalar double which represents the predicted winning number.
- Let’s go ahead and train this network. On the train tab of the Network: cpNN dialog, select cpInputs for Inputs and cpTargets for Targets; then press the Train Network button to start the network training.
- Results of training.
- After training the network to the desired tolerance’s go back to the Neural Network/Data Manager dialog box and hit the export button, select cpNN from the list then hit the export button.
- Go back to the MATLAB command window and type
CpNN([2;3]) % [day;time]
- The resulting value will be the NN’s best guess of what will be the winning entry for Cash Pot on a Tuesday at DRIVETIME.
My initial analysis of the results of the NN was not conclusive, maybe the parameters of the NN could be adjusted and the results compared to actual winning numbers. However, even after doing so one may find that the outputs are still random and contain no discernible patterns, which should be the case for a supposedly random game of chance.
MathWorks MATLAB® is a high-level language and interactive environment that enables you to perform computationally intensive tasks faster than with traditional programming languages such as C, C++, and Fortran. MATLAB allows for fast prototyping and testing of problems involving simulations, image processing, statistics, artificial intelligence and search/optimization requirements. These capabilities are provided by toolboxes such as Simulink, NN toolbox, Image Processing toolbox and Search/Optimization toolbox.
MATLAB is built on a foundation of matrices therefore all operations involve some form of matrix or vector manipulation, this makes it an ideal tool for use in Discrete Mathematics and Linear Algebra. MATLAB also comes packed with many common mathematical functions and operations, it also has the ability for easy graphing and display of data using generated reports.
function rtn = creator(option, state, flags)
temp = floor(5.*rand(5,1)); % ; turn off echo to command window.
rtn = temp';
clc % clears command window
clear % clears workspace window
plot % displays the plot window
MATLAB Cheat Sheet