Author Archives: Bahrudin Hrnjica

GPdotNET v4.0 has been released


After almost two years of implementation, I am proud to announce the forth version of the Open source project called GPdotNET v4.0. The latest version completely implements Genetic Programming and Artificial Neural Network for supervised learning tasks in three kind of problems: regression, binary and multiclass classification. Beside supervised learning tasks, with GPdotNET you can solve several Linear Programming problems: Traveling Salesman, Assignment and Transportation problems. The source code and binaries can be download from Github page: https://github.com/bhrnjica/gpdotnet/releases/tag/v4.0

Figure 1. Main Window in GPdotNET v4.0

Introduction

In 2006 the GPdotNET started as post-graduate semester project, where I was trying to implement simple C# program based on genetic programming. After successfully implemented console application, started to implement .NET Windows application to be easy to use for anyone who wants to build mathematical model from the data based on genetic programming method. In November 2009 GPdotNET became an open source project, by providing the source code and installer. Since then I have received hundreds of emails, feedbacks, questions and comments. The project was hosted on http://gpdotnet.codeplex.com. In 2016 I decided to move the project to GitHub for better collaboration and compatibility, and can be found at http://github.com/bhrnjica/gpdotnet. However, for backward compatibility, the old hosting site will be live as long as the codeplex.com would be live. Since the beginning of the development, my intention was that the GPdotNET would be cross-OS application which can be run on Windows, Linux and Mac. Since version 2, GPdotNET can be compiled against .NET and Mono, and can be run on any OS which has Mono Framework installed. Beside this fact, vast majority of users are using GPdotNET on Windows OS.

GPdotNET is primarily used on Academia by helping engineers and researchers in modelling and prediction various problems, from the air pollution, water treatment, rainfall prediction, to the various modelling of machining processes, electrical engineering, vibration, automotive industry etc. GPdotNET is used in more than ten doctoral dissertations (known to me) and master thesis, nearly hundreds paper used GPdotNET in some kind of calculation.

Modeling with GPdotNET (New in GPdotNET v4.0)

Working with GPdotNET requires the data. By providing the learning algorithms GPdotNET uses a data of the research or experimental measures to learn about the problem. The results of learning algorithms are analytical models which can describe or predict the state of the problem, or can recognize the pattern. GPdotNET is very easy to use, even if you have no deep knowledge of GA, GP or ANN. Appling those methods in finding solutions can be achieved very quickly. The project can be used in modeling any kind of engineering process, which can be described with discrete data, as well as in education during teaching students about evolutionary methods, mainly GP and GA, as well as Artificial Neural Networks.

Working in GPdotNET follows the same procedures regardless of the problem type. That means you have the same set of steps when modelling with Genetic Programming or Neural Networks. In fact, GPdotNET contains the same set of input dialogs when you try to solve Traveling Salesman Problem with Genetic Algorithm or if you try to solve handwriting recognition by using Backpropagation Neural Networks. All learning algorithms within GPdotNET share the same UI.

The picture below shows the flowchart of the modelling in GPdotNET. The five steps are depicted in the graphical forms surrounded with Start and Stop item.

Figure 2. Modelling layout in GPdotNET 4.0

After GPdotNET is started main window is show, and the modelling process can be started.

Choosing the Solver Type

The first step is choosing the type of the solver. Which solver you will use it depends on your intention what you want to do. Choosing solver type begins when you press “New” button, the “GPdotNET Model creation wizard” appear. Soler types are grouped in two categories. The first group (on the left side) contains models implemented prior to v4.0 version. It contains solvers which apply GP in modelling regression problems, and GP in optimization of the GP models. In addition, you can perform optimization of any analytically defined function by using “Optimization of the Analytic function”. Also, there are three linear programming problems which GPdotNET can solve using GA.

On the right side, there are two kind of solvers: GP or ANN, which are not limited to solve only regression. Both GP or ANN can build model for regression, binary or multi-class problems. Which type of problem GPdotNET will use, depends of the type of the output column data (label column).

Figure 3. Available model types

Loading Experimental Data (new in GPdotNET 4.0)

GPdotNET uses powerful tool for importing your experimental data regardless of the type. You can import numerical, binary or classification data by using Importing Data Wizard. With GPdotNET importing tools you can import any kind of textual data, with any kind of separation character.

Figure 4. Importing dataset dialog

After the data is imported in forms of columns and rows, GPdotNET implemented set of very simple controls which can perform very powerful feature engineering. For each loaded column, you can set several types of metadata: column name, column type (input, output, ignore), normalization type (minmax, gauss), and missing value (min, max, avg). With those options, you can achieve most of the modelling scenarios. Before “Start Modelling” minimum conditions must be achieved.

  • At least one column must be of “input” parameter type.
  • At least one column must be of “output” parameter type.

Which type of problem (regression, binary or multi class) will be used depends of the type of the output column. The following cases are considered:

  1. in case of regression problems ouput column must be of numeric type.
  2. in case of binary classinfication output column must be of binary type.
  3. in case of multi class classinfication output column must be of categorical type.

Figure 5. Defining metadata for training data set

When the column should not be part of the feature list, it can be easily ignored when the Column Type is set to “ignore“, or Param type is set to “string“.

Figure 6. Changing column type to binary

Change value of metadata by double click on the current value, select new values from available popup list. When you done with Feature Engineering press “Start Modelling” button and the process of modelling can be start.

Note: After you press Start Modelling button you can still change values of metadta, but after every change of the metadata values, Start Modelling button must be pressed.

Setting Learning Parameters

Figure 7. Setting parameters Dialog

After data is loaded and prepared successfully, you have to set parameters for the selected method. GPdotNET provides various parameters for each method, so you can set parameters which can provides and generates best output model. Every parameter is self-explanatory.

Searching for the solution

GPdotNET provides visualization of the searching solution so you can visually monitor how GPdotNET finds better solution as the iteration number is increasing. Beside searching simulation, GPdotNET provides instant result representation (only GP models), so any time the user can see what is the best solution, and how currently best solution is good against validate or predicted set of data. (Result and Prediction tabs).

Figure 8. Searching simulation in GPdotNET

Saving and exporting the results:

GPdotNET provides several options you can choose while exporting your solution. You can export your solution in Excel or text file, as well as in Wolfram Mathematica or R programming languages (GP Models only). In case of ANN model the result can se exported only to Excel.


Figure 9. Searching simulation in GPdotNET

Besides parameters specific to learning algorithm, GPdotNET provides set of parameters which control the way of how iteration process should terminates as well as how iteration process should be processed by means of parallelization to use the multicore processors. During the problem searching GPdotNET records the history, so you can see when the best solution is found, how much time pass since the last iteration process start, or how much time is remaining to finish currently running iteration process.

Due to the fact that GP is the method which requires lot of processing time, GPdotNET provides parallelization, which speed up the process of searching. Enabling or disabling the parallelization processing is just a click of the button.

GPdotNET Start Page

In case you have no data or just want to test the application, GPdotNET providers 15 data samples for demo purposes. All samples are grouped in problems specific groups: Approximation and Regressions, Binary Classification, Multi-class classification, Time series modelling and Linear Programming.

Figure 10. Modelling layout in GPdotNET

By click on appropriate link sample can be opened to see current result and parameter values. You can easily change parameter, press Run button and search for another solution. This is very handy to introduce with GPdotNET. In any time, you can stop searching and export current model or save current state of the program.

Final note: The project is licensed under GNU Library General Public License (LGPL). For information about license and other kind of copyright e.g. using the application in commercial purpose please see http://github.com/bhrnjica/gpdotnet/blob/master/license.md.

In case you need to cite it in scientific paper or book please refer to  https://wordpress.com/post/bhrnjica.net/5995

Introduction to Microsoft Machine Learning package in Microsoft R Server 9.0


MicrosoftML package

Microsoft has released Microsoft R Server 9.0 (MRS9.0) with very interesting package called MicrosoftML. “Micrsooft ML” stands for Microsoft Machine Learning R package which you can use on R Server. R Server is commercial version of popular R Client distribution, which solves mayor problems when working with R. R Server contains set of cutting-edge technology to work with big data, as well as set of enhanced packages for parallelization and distributing computing.
MRS 9.0 is coming with “MicrosoftML” package which contains set of several Machine Learning algorithms developed in various Microsoft products in the last 10 years. You can combine the algorithms delivered in this package with pre-existing parallel external memory algorithms such as the RevoScaleR package as well as open source innovations such as CRAN R packages to deliver the best predictive analytic.
MicrosoftML package includes the following algorithms:

  • Fast linear learner, with support for L1 and L2 regularization,
  • Fast boosted decision tree,
  • Fast random forest,
  • Logistic regression, with support for L1 and L2 regularization,
  • GPU-accelerated Deep Neural Networks (DNNs) with convolutions,
  • Binary classification using a One-Class Support Vector Machine.

How to start with MicrosoftML package

In order to fully use the power of MicrosoftML, and RevoScaleR you need to download MRS 9.0 from the MSDN or Visual Studio Dev Essentials subscription. Once the zip file is downloaded, unzip it, and run setup file.

The following required components  were missing when my installation is started.  Seems the MRS contains the latest .NET Core components, which is pretty cool:

 

blog_mrs_9_11

After the prerequested components installed, the MRS installation process can start.

blog_mrs_9_01

By clicking the Next button the Installation process starts:

blog_mrs_9_02

Select the path where you want to install MRS, and press the Next button:

blog_mrs_9_03

If everything went ok, the installation process is finished after less than minute, and the final dialog window appears:

blog_mrs_9_04

By clicking the Finish button MRS is installed on you PC.

Run MRS 9.0 by using R Tool fo Visual Studio, RTVS

Now it is time to run some R code. YOu have two posibilities to run R code. The first option is that you use the R Studio proffesion tool for running R code. It is free and open source which you can download from rstudio.com. If you are MS Developer you usualy write the code in the Visual Studio. So you can download RTVS from this link and run R code from Visual Studio.

Now that you have right tool to run R code, we can start with setting the MRS environment.

First thing you should do is to point RTVS to use MRS 9.0 instead of curently using some other distribution. So open the Visual Studio, select R Tools->Edit Options

blog_mrs_9_08

The Option dialog appears. Set the R Engine to point installation folder of the MRS. Since my installation location was on Program Files folder, the picture below show my installation path.

blog_mrs_9_07

After you set the right installation folder , restart the Visual Studio:

When the Visual Studio is running, open R Open R Interactive window. You should have similar text if you set up MRS path correctly:

blog_mrs_9_13

Select New Project from the File->New menu option.

blog_mrs_9_10

Name it FirstRServerDemo and click Ok. Now you are ready to write first MRS R code:

blog_mrs_9_14

In the next post we will continue exploration the MicrosoftML library package and new set of Machine Learning algorithms added in this latest version.

Using SSH Keys for License generation and validation in .NET Applications


license_generationpng

This blog post is going to present how can you implement license functionality in your .NET application. Providing license in your .NET application is very challenging because there is no standard procedure for the implementation. You are free to use whatever you want. But be notice, there is no license which is 100% safe and cannot be cracked or bypassed.

For this purpose I have selected the CocoaFob library for registration code generation and verification in Objective-C applications. Mainly the library is for Objective -C based applications, like iQS mobile applications and other OSX based applications.  This is very interesting library but you cannot use it in .NET applications, there is no implementation for .NET Framework.

The library uses DSA to generate registration keys, which is very hard for hackers to produce key generators. The library is also specific because it generates license key in human readable form, when the bytes are converted in to Base32 string to avoid ambiguous characters. It also groups codes in sets of five characters separated by dashes. Also DSA has encryption algorithm generates the license which is different every time because of a random element introduced during the process.

So the License key is produced using a 512-bit DSA key looks like on the following sample:

GAWQE-FCUGU-7Z5JE-WEVRA-PSGEQ-Y25KX-9ZJQQ-GJTQC-CUAJL-ATBR9-WV887-8KAJM-QK7DT-EZHXJ-CR99C-A

More information about CocoaFob can be found at GitHub page: https://github.com/glebd/cocoafob

The library is using BouncyCastle.Crypto Nuget package for DSA encryption and decryption.

The library CocoaFob for .NET contains two classes:

  1. LicenseData class which provide License properties which is used in license generation. It an be anything: Name, Product number, email, date of expiration etc.
  2. LicenseGenerator  class which is responsible for encrypting and validating the license.

For this blog post the License data class has the flowing implementation:

public class LicenseData
{

protected internal string productCode;
protected internal string name;
protected internal string email;

public virtual string toLicenseStringData()
{
 StringBuilder result = new StringBuilder();
 if (productCode != null)
 {
 result.Append(productCode);
 result.Append(',');
 }

 //name is mandatory property
 if (name == null)
 throw new System.Exception("name cannot be null");
 result.Append(name);

 if (email != null)
 {
 result.Append(',');
 result.Append(email);
 }
 return result.ToString();
}
......
}

As can be seen from the code snippet above the License data contains username, product key and email address. Also, only name property is mandatory, which means you can generate license key based on the user name only.

Generating the License Key

One we have License data we can process of License key generation. License is generated using DSA encryption which uses SSH private key.  You can generate public and private SSH keys  using any of the available tools, eg. OpenSSH, GitHub bash, …. More information about private and public key generation you can find at this link. Once we have public and private keys we can generate license and validate it. One important thing to remember is that you have to care about your private key. It should always be secure and no one should have access to it.

The public key is used for license validation, and it is usualy packed with the application as a part of the deployment stuff. So the process of generating the license is show on the flowing code snippet:

public string makeLicense(LicenseData licenseData)
{
     if (!CanMakeLicenses)
     {
       throw new System.InvalidOperationException("The LicenseGenerator cannot make licenses as it was not configured with a private key");
     }
     try
     {
        //
        var dsa = SignerUtilities.GetSigner("SHA1withDSA");
        dsa.Init(true, privateKey);
        //
        string stringData = licenseData.toLicenseStringData();
        byte[] licBytes = Encoding.UTF8.GetBytes(stringData);
        dsa.BlockUpdate(licBytes, 0, licBytes.Length);
        //
        byte[] signed = dsa.GenerateSignature();
        string license = ToLicenseKey(signed);
        return license;
    }
    catch (Exception e)
    {
        throw new LicenseGeneratorException(e);
    }
}

First the DSA encryption is created based on the publicKey we have provided as an argument. Then licBytes is generated from the License data, and converted in to UTF8 formatted bytes. Then we have update DSA provider with licBytes. Now the DSA provider can generate signature in bytes. The signature is converted in to LicenseKey by calling ToLicenseKey method. The method is shown on the following code snippet:

private string ToLicenseKey(byte[] signature)
{
    /* base 32 encode the signature */
    var result = Base32.ToString(signature);

    /* replace O with 8 and I with 9 */
    result = result.Replace("O", "8").Replace("I", "9");

    /* remove padding if any. */
    result = result.Replace("=", "");
           

    /* chunk with dashes */
    result = split(result, 5);
    return result;
}

The magic happen in this method during the conversion of signature from bytes to human readable string. Conversion is done using Base32 string helper method.

Verify the License Key

The License verification process is defined in varifyLicense method. You have to provide SSH publicKey as well as

public virtual bool verifyLicense(LicenseData licenseData, string license)
{
	if (!CanVerifyLicenses)
	{
		throw new System.InvalidOperationException("The LicenseGenerator cannot verify licenses as it was not configured with a public key");
	}
    try
     {
        //Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
        var dsa = SignerUtilities.GetSigner("SHA1withDSA");
        dsa.Init(false, publicKey);

        //
        string stringData = licenseData.toLicenseStringData();
        byte[] msgBytes = Encoding.UTF8.GetBytes(stringData);
        dsa.BlockUpdate(msgBytes, 0, msgBytes.Length);


        var dec = FromLicenseKey(license);
        var retVal = dsa.VerifySignature(dec);
        //
        return retVal; 
	}
    catch (Exception e)
    {
        throw new LicenseGeneratorException(e);
    }
}

As can be seen from the code above, the validation process is done by generating licenseData, converting the license Key in to signatere and the validation process return true is the license is valid, otherwize return false.

The whole project is published at git hub, an can be downloaded from http://github.com/bhrnjica/cocoafob

 

Testing the Library

The Library solution contains unit test project which you can see how to use this library in the real scenario in order to implement licensing in .NET app.

Happy programming!

Details of my session at ATD12


atd12_sl02.png

Today, I gave session at Advanced Technology Day conference in Zagreb. It was very excited to see full room of people at the presentation, mostly developers from .NET world interesting in R and Data Science. This is good sign that the Data Science and the R are becoming more and more popular at daily basis. Most popularity for the R will bring R Tool for Visual Studio, which means the R language became member of the family of the Visual Studio.

atd12_sl01.jpg

For those who was asking about my slides and demo sample here is the information:

  1. Presentation slides can be downloaded here,
  2. Source code of the demo is hosted at git hub at: http://github.com/bhrnjica/R-Workshop
  3. For more information about R , you can see my YouTube channel about R and Machine Learning at this link.

See you next time!