package org.kevoree.modeling.infer.impl;

import org.kevoree.modeling.KObject;
import org.kevoree.modeling.infer.KInferAlg;
import org.kevoree.modeling.memory.struct.segment.KMemorySegment;
import org.kevoree.modeling.util.maths.structure.impl.Array1D;

import java.util.Random;


public class BinaryPerceptronAlg implements KInferAlg {
    private int iterations=1;

    private Random rand =new Random();

    @Override
    public void train(double[][] trainingSet, double[][] expectedResultSet, KObject origin) {
        KMemorySegment ks = origin.manager().segment(origin.universe(), origin.now(), origin.uuid(), false, origin.metaClass(), null);
        int dependenciesIndex = origin.metaClass().dependencies().index();
        //Create initial segment if empty
        int size=origin.metaClass().inputs().length;
        if (ks.getInferSize(dependenciesIndex, origin.metaClass()) == 0) {
            ks.extendInfer(origin.metaClass().dependencies().index(),size,origin.metaClass());
            for(int i=0;i<size;i++){
                ks.setInferElem(dependenciesIndex,i,rand.nextDouble(),origin.metaClass());
            }
        }
        Array1D state = new Array1D(size,0,origin.metaClass().dependencies().index(),ks,origin.metaClass());

    }

    @Override
    public double[][] infer(double[][] features, KObject currentInferObject) {
        return new double[0][];
    }
}

@@ -357,17 +357,18 @@
 rations=
-1
+20
 ;%0A%0A    p
@@ -813,16 +813,18 @@
 ).length
++1
 ;%0A      
@@ -1257,134 +1257,1764 @@
     
-%7D%0A%0A    @Override%0A    public double%5B%5D%5B%5D infer(double%5B%5D%5B%5D features, KObject currentInferObject) %7B%0A        return new double%5B0%5D%5B%5D
+    for(int iter=0; iter%3Citerations; iter++)%7B%0A            for(int i=0;i%3CtrainingSet.length;i++)%7B%0A                double a= calculate(trainingSet%5Bi%5D,state);%0A                if(a==0)%7B%0A                    a=-1;%0A                %7D%0A                if(a*expectedResultSet%5Bi%5D%5B0%5D%3C=0)%7B%0A                    for(int j=0; j%3Corigin.metaClass().inputs().length;j++)%7B%0A                        state.add(j,expectedResultSet%5Bi%5D%5B0%5D*trainingSet%5Bi%5D%5Bj%5D);%0A                    %7D%0A                    state.add(origin.metaClass().inputs().length,expectedResultSet%5Bi%5D%5B0%5D);%0A                %7D%0A            %7D%0A        %7D%0A%0A    %7D%0A%0A    private double calculate(double%5B%5D features, Array1D state) %7B%0A        double res=0;%0A%0A        for(int i=0; i%3Cfeatures.length;i++)%7B%0A            res = res + state.get(i)*features%5Bi%5D;%0A        %7D%0A        res = res + state.get(features.length);%0A        if(res%3E=0)%7B%0A            return 1.0;%0A        %7D%0A        else %7B%0A            return 0;%0A        %7D%0A    %7D%0A%0A    @Override%0A    public double%5B%5D%5B%5D infer(double%5B%5D%5B%5D features, KObject origin) %7B%0A        KMemorySegment ks = origin.manager().segment(origin.universe(), origin.now(), origin.uuid(), false, origin.metaClass(), null);%0A        int dependenciesIndex = origin.metaClass().dependencies().index()+1;%0A        int size=origin.metaClass().inputs().length;%0A        if (ks.getInferSize(dependenciesIndex, origin.metaClass()) == 0) %7B%0A            return null;%0A        %7D%0A        Array1D state = new Array1D(size,0,origin.metaClass().dependencies().index(),ks,origin.metaClass());%0A%0A        double%5B%5D%5B%5D result = new double%5Bfeatures.length%5D%5B1%5D;%0A%0A        for(int inst=0;inst%3Cfeatures.length;inst++)%7B%0A            result%5Binst%5D=new double%5B1%5D;%0A            result%5Binst%5D%5B0%5D=calculate(features%5Binst%5D,state);%0A        %7D%0A        return result
 ;%0A  

Update perceptron alg