[Java] Hopfield Network Implementation

Why Java?

Well, actually I have a deep love with this language, I enjoy my time writing a Java code. Also, it is good for me to write some OOP projects to not forget its concepts. Getting used to Python and other scripted languages may effect badly to my designing and OOP skills.

So, from time to time, I like to code some Java code. And here we go!

This is a light simple Java implementation of Hopfield Recurrent Neural Network.

Supported Features (cont. updated)

  • Data Normalization
  • Network Visualization using GraphStream

An example of a hopfield network visualization with pattern of size 5 (recognizing pattern of 5 elements)

– Here you can see the repository of implementation.

– I am working on a brief tutorial that explains the hopfield network on the README file

 

Adapter Pattern

This pattern is commonly used in applications that have many functions or operations but all of them serve a specific entity.

For example, when you open your computer’s calculator, you will find many modes(Standard, Scientific, ..etc), Standard mode contains basic operations to solve any simple equation or calculation, Scientific mode contains the previous features and other advanced operations.

We can think that the basic operations in the Standard mode have been adapted with the new advanced operations to form the Scientific mode.

This is the idea of the Adapter pattern, we have 2 or more different classes and we want to make them compatible with each other.

The scenario is the following:-
[1] We have a basic standard calculator that contains basic 2 operation, add and subtract
[2] We want to create a calculator that contains the standard operation and other 2 advanced operations which are product and division

The standard calculator is inbuilt contain add and subtract operations

public interface Operations {
        public double calculate(double x, double y, char operation);
    }

The scientific calculator will have division and product operations

public interface AdvancedOperations {
        public double product(double x, double y);
        public double divide(double x, double y);
    }

Now, we will implement the scientific calculator class..

public static class Scientific implements AdvancedOperations {
        @Override
        public double product(double x, double y) {
            return x * y;
        }

        @Override
        public double divide(double x, double y) {
            return x / y;
        }
    }

The next step is to create an adapter, the adapter will use an object of scientific class and implement the standard calculator interface..

public static class CalculatorAdapter implements Operations {
        Scientific scientificMode;

        public CalculatorAdapter(String mode) {
            if (mode.equals("Scientific")) {
                this.scientificMode = new Scientific();
            }
        }

        @Override
        public double calculate(double x, double y, char operation) {
            if (operation == '*') {
                System.out.println("Scientific");
                return this.scientificMode.product(x, y);
            } else if(operation == '/') {
                System.out.println("Scientific");
                return this.scientificMode.divide(x, y);
            }

            if (operation == '+') {
                System.out.println("Standard");
                return x + y; //Standard inbuilt
            } else if (operation == '-') {
                System.out.println("Standard");
                return x - y; // Standard inbuilt
            }

            return 0.0;
        }
    }

Finally, this is our new calculator that supports old and new operations.

public static class Calculator implements Operations {
        CalculatorAdapter calculatorAdapter;

        @Override
        public double calculate(double x, double y, char operation) {
            this.calculatorAdapter = new CalculatorAdapter("Scientific");
            return this.calculatorAdapter.calculate(x, y, operation);
        }
    }

 
You can see the full implementation Here

References:

[1] GoF Book
[2] http://en.wikipedia.org/wiki/Adapter_pattern

Bridge Pattern

The main idea of Bridge pattern is separating the objects’ implementation from its abstraction, this will help when you want to change an object’s implementation code without affecting on its abstraction.

For better understanding, consider the following scenario:-
[1] Vehicle is an abstraction
[2] Cars, buses, motorcycle, …etc are objects that inherit from Vehicle and each one of them has its own implementation
[3] Each vehicle should contain an engine which has different types (Kia, Nissan .. etc), so you can consider engine is an abstraction that all vehicles should have it.

The following code represents an interface that contains some of the vehicles functions

public interface VehicleObject {
        public void move();
        public void accelerate();
        public void stop();
    }

We will create car and bus classes that inherit and implement the above functions

public static class Car implements VehicleObject {
        public void move() {
            System.out.println("Car is moving!");
        }
        
        public void accelerate() {
            System.out.println("Car is accelerating!");
        }
        
        public void stop() {
            System.out.println("Car stopped!");
        }
    }

 

public static class Bus implements VehicleObject {
        public void move() {
            System.out.println("Bus is moving!");
        }
        
        public void accelerate() {
            System.out.println("Bus is accelerating!");
        }
        
        public void stop() {
            System.out.println("Bus stopped!");
        }
    }

This is the implementation phase, each vehicle now has its own implementation.

Each one of these objects should contain an engine, so we will create an Engine interface that holds some of its functions or states.

public interface EngineObject {
        public void running();
        public void stop();
    }
public static class Engine implements engineObject {
        private vehicleObject object;
        
        public void setObject(vehicleObject object) {
            this.object = object;
        }
        
        public void running() {
            this.objectFunctions.move();
        }
        
        public void stopped() {
            this.objectFunctions.stop();
        }
    }

Engine class contains “setObject” to map the engine with its car, engine state depends on the machine (object) state, when the car is moving the engine is running too and vice versa.

We can summarize Bridge pattern implementation in the following points:-
[1] Abstract class
[2] Normal class that represents objects which implement the abstraction
[3] Find an abstraction in the object itself
[4] If it is found, create interface that represent the new abstraction
[5] Implement the new interface

You can see the full implementation Here
 
References:
http://en.wikipedia.org/wiki/Bridge_pattern