Jump to content

Artificial Intelligence/Circuit encoded in Java

From Wikibooks, open books for an open world

One bit neuron code:

[edit | edit source]
public class neuron
{
        private char state;

        /**
         * state represents state of neuron which is as follows
         *
         * p - Passing state, the neuron simply assigns output = input
         * t - Toggle state, the neurons output will be that of toggle of input
         * h - High state, the neuron output is always high
         * l - Low state, the neuron output is always low
         *
         */

        /*constructor definitions*/
        neuron()
        {
                state = 'p';
        }

        neuron(char s)
        {
                //constructor with set state
                sets(s);
        }

        neuron(neuron n)
        {
                //assigns state of one neuron to other
                state=n.state;
        }

        /*end of constructor definitions*/

        boolean equal(neuron n)
        {
                //function compares neuron state
                return state==n.state;
        }

        boolean call(boolean i)
        {
                /*Called by neuron for computation*/
                switch(state)
                {

                case'p':
                        return i;

                case't':
                        return !i;

                case'h':
                        return true;

                case'l':
                        return false;

                default:
                        return i;



                }
        }

        void sets(char s)
        {
                switch(s)
                {
                case'p':
                        state=s;
                        break;
                case't':
                        state=s;
                        break;
                case'h':
                        state=s;
                        break;
                case'l':
                        state=s;
                        break;
                        default:
                                state='p';
                        break;
                }
        }

        char gets()
        {
                return state;
        }

        void nxts(char s)
        {
                //function to change the current state of neuron
                switch(s)
                {
                case'p':
                        state='t';
                        break;
                case't':
                        state='h';
                        break;
                case'h':
                        state='l';
                        break;
                case'l':
                        state='p';
                        break;
                        default:
                                state='p';
                        break;
                }

        }

        void train
        (boolean i,/*trainin input*/
         boolean o /*training output*/  )
        {
                while(call(i)!=o) nxts(state);

        }
}

Two bit neuron code :

[edit | edit source]
public class neuron2b {
        neuron n0=new neuron();
        neuron n1=new neuron();
        private byte state;
        /* The two bit neuron has 16 states. This is because one bit
         * neuron has 4 - states. A two bit neuron that has 2 one bit
         * will have 4x4 = 16 states
         */
        boolean call(boolean i1,boolean i2)
        {
                boolean o;
                o= (i1&&n0.call(i2))||((!i1)&&n1.call(i2));
                return o;

        }

        void train(boolean i1, boolean i2,boolean o)
        {
                while (call( i1, i2)!= o)
                {
                        nxts();
                }
        }

        boolean equal(neuron2b n)
        {
                //Compares state of two 2 bit neurons
                return n.n0.equal(n0) && n.n1.equal(n1);
        }

        void nxts()
        {
                //Shifts neuron to next state
                n0.nxts(n0.gets());
                if (n0.gets()=='l') n1.nxts(n1.gets());
        }

        byte stateval(neuron n)
        {
                /*gets the satte of neuron and returns corresponding
                 * value
                 */
                switch(n.gets())
                {
                case 'p':
                        return 1;
                case 't':
                        return 2;
                case 'h':
                        return 3;
                case 'l':
                        return 4;
                        default:
                                return 0;
                }
        }

        char statechar(float f)
        {
                if(f>=1&&f<2) return 'p';
                if(f>=2&&f<3) return 't';
                if(f>=3&&f<4) return 'h';
                if(f>=4&&f<5) return 'l';
                else return 'o'; // this represents some unknown junk state

        }
        byte gets()
        {
                /*Gets the state of two bit neuron*/
                state = (byte)(stateval(n0)*stateval(n1));
                return state;
        }

        void sets()
        {

        }

}

Three bit neuron code :

[edit | edit source]
public class neuron3b {
        neuron2b n0=new neuron2b();
        neuron2b n1=new neuron2b();
        private int state;
        /* The two bit neuron has i6 states. This is because one bit
         * neuron has 4 - states. A two bit neuron that has 2 one bit
         * will have 4x4 = 16 states
         */
        boolean call(boolean i1,boolean i2, boolean i3)
        {
                boolean o;
                o= (i1&&n0.call(i2,i3))||((!i1)&&n1.call(i2,i3));
                return o;

        }

        void train(boolean i1, boolean i2,boolean i3,boolean o)
        {
                while (call( i1, i2, i3)!= o)
                {
                        nxts();
                }
        }

        boolean equal(neuron3b n)
        {
                //Compares state of two 2 bit neurons
                return n.n0.equal(n0) && n.n1.equal(n1);
        }

        void nxts()
        {
                //Shifts neuron to next state
                n0.nxts();
                if (n0.gets()==16) n1.nxts();
        }

        byte stateval(neuron n)
        {
                /*gets the satte of neuron and returns corresponding
                 * value
                 */
                switch(n.gets())
                {
                case 'p':
                        return 1;
                case 't':
                        return 2;
                case 'h':
                        return 3;
                case 'l':
                        return 4;
                        default:
                                return 0;
                }
        }

        int gets()
        {
                state = (n0.gets()*n1.gets());
                return state;
        }

}

Explanation will be provided shortly :

Regards

mindaslab@gmail.com