Creating a Kohonen Self-Organizing Network in Fore

The example shows how one should classify countries according to the assigned set of characteristics, using a Kohonen network. Data about countries is implemented by the Country class.

To execution the example, add links to the NN and Collections system assemblies.

Class Country: Object
    Public Name: String;
    Public GDP: Double;
    Public Population: Double;
    Public PopulationGrowthRate: Double;
    Public AreaLand: Double;
    Public AreaWater: Double;
    Public IrrigatedLand: Double;
    
    Public Constructor Create(_Name: String; _GDP: Double; _Population: Double; _PopulationGrowthRate: Double; _AreaLand: Double; _AreaWater: Double; _IrrigatedLand: Double);
    Begin
        Name := _Name;
        GDP := _GDP;
        Population := _Population;
        PopulationGrowthRate := _PopulationGrowthRate;
        AreaLand := _AreaLand;
        AreaWater := _AreaWater;
        IrrigatedLand := _IrrigatedLand;
    End Constructor Create;
    
    Public Constructor CreateEmpty;
    Begin
    End Constructor CreateEmpty;
End Class Country;

Function Max(D1, D2: Double): Double;
Begin
    If D1 > D2 Then
        Return D1;
    End If;
    Return D2;
End Function Max;

// Method for initialization of the network
Sub NetworkInitialize(Network: NeuralNetwork);
Var
    numberOfClusters: Integer =3;
    numberOfNeurons: array[0..0Of Integer;
Begin
    numberOfNeurons[0] := numberOfClusters;
    Network.CreateNetworkEx(16, numberOfNeurons, 0.53);
    Network.InitSynapsesConvex;
End Sub NetworkInitialize;

Sub Main;
Var
    network: NeuralNetwork;
    countries : ArrayList;
    normalizedCountries: ArrayList;
    i, epoch: Integer;
    normalizedCountry, currCountry: Country;
    inputs : array[0..5Of Double;
    alfa : Double;
    learnRate : Double;
    learnRadius: Integer;
    sampleClass: Integer;
Begin

// Assign initial values
    countries := New ArrayList.Create;
    countries.Add(New Country.Create("Algeria"3.9311939171.74238174005550));
    countries.Add(New Country.Create("Argentina", -3369551821.1627366903020017000));
    countries.Add(New Country.Create("Brazil"0.81728603700.9484565105545528000));
    countries.Add(New Country.Create("Canada"3.6312810921.0292209707551707100));
    countries.Add(New Country.Create("China"712618324820.99326410270550498720));
    countries.Add(New Country.Create("India"5.510140038171.582973190314400480000));
    countries.Add(New Country.Create("Indonesia"02247842101.6318264409300045970));
    countries.Add(New Country.Create("Iran"1656196360.8316360001200094000));
    countries.Add(New Country.Create("Kazakhstan"1.716733227, -0.0526698004750022000));
    countries.Add(New Country.Create("Libya"251154502.42175954004700));
    countries.Add(New Country.Create("Mexico"3.71003497661.5319230404951061000));
    countries.Add(New Country.Create("Mongolia"3.526509521.5415650000800));
    countries.Add(New Country.Create("Niger"2100755112.751266700300660));
    countries.Add(New Country.Create("Peru"2.4270128991.751280000522012800));
    countries.Add(New Country.Create("Russia"3.2146001176, -0.38169958007940040000));
    countries.Add(New Country.Create("Saudi Arabia"1.6220235063.28196058204350));
    countries.Add(New Country.Create("Sudan"3350798142.84237600012981019460));
    countries.Add(New Country.Create("United States"4.12755626730.919158960470131207000));
    normalizedCountry := New Country.CreateEmpty;
    For i := 0 To countries.Count - 1 Do
        currCountry := countries.Item(i) As Country;
        normalizedCountry.GDP := Max(normalizedCountry.GDP, currCountry.GDP);
        normalizedCountry.Population := Max(normalizedCountry.Population, currCountry.Population);
        normalizedCountry.PopulationGrowthRate := Max(normalizedCountry.PopulationGrowthRate, currCountry.PopulationGrowthRate);
        normalizedCountry.AreaLand := Max(normalizedCountry.AreaLand, currCountry.AreaLand);
        normalizedCountry.AreaWater := Max(normalizedCountry.AreaWater, currCountry.AreaWater);
        normalizedCountry.IrrigatedLand := Max(normalizedCountry.IrrigatedLand, currCountry.IrrigatedLand);
    End For;
    normalizedCountries := New ArrayList.Create;
    For i := 0 To countries.Count - 1 Do
        currCountry := countries.Item(i) As Country;
        normalizedCountries.Add(New Country.Create(currCountry.Name, currCountry.GDP / normalizedCountry.GDP, currCountry.Population / normalizedCountry.Population, 
            currCountry.PopulationGrowthRate / normalizedCountry.PopulationGrowthRate, currCountry.AreaLand / normalizedCountry.AreaLand,
            currCountry.AreaWater / normalizedCountry.AreaWater, currCountry.IrrigatedLand / normalizedCountry.IrrigatedLand));
    End For;

// Create a neural network
    network := New NeuralNetwork.Create;
// Initialize network
    NetworkInitialize(network);
// Learn network
    For epoch := 1 To 300 Do
        For i := 0 To normalizedCountries.Count - 1 Do
            currCountry := normalizedCountries.Item(i) As Country;
            inputs[0] := currCountry.GDP;
            inputs[1] := currCountry.Population;
            inputs[2] := currCountry.PopulationGrowthRate;
            inputs[3] := currCountry.AreaLand;
            inputs[4] := currCountry.AreaWater;
            inputs[5] := currCountry.IrrigatedLand;
            alfa := epoch / 100;
            network.SetInputValuesConvex(inputs, alfa);
            learnRate := 0.6 * ((300 - epoch) / 300);
            learnRadius := (3 * ((300 - epoch) / 350)) As Integer;
            network.SetLearnRadius(learnRadius);
            network.SetLearnRate(learnRate);
            network.PropagateSOFM;
            network.LearnSOFM;
        End For;
    End For;

// Test input values
    For i := 0 To normalizedCountries.Count - 1 Do
        currCountry := normalizedCountries.Item(i) As Country;
        inputs[0] := currCountry.GDP;
        inputs[1] := currCountry.Population;
        inputs[2] := currCountry.PopulationGrowthRate;
        inputs[3] := currCountry.AreaLand;
        inputs[4] := currCountry.AreaWater;
        inputs[5] := currCountry.IrrigatedLand;
        alfa := 1;
        network.SetInputValuesConvex(inputs, alfa);
        network.PropagateSOFM;
        sampleClass := network.GetClosestNeuron;
        Debug.WriteLine(CurrCountry.Name + ". Class: " + sampleClass.ToString);
    End For;
// Delete network
    network.DeleteNetwork;
End Sub Main;

After executing the example each country, which data is tested, is related to one of the three classes. Results are displayed in the console window.

See also:

Examples