Homework Four: Modeling Rolling Balls and Introduction to Neural Networks

Part 1: Modeling Rolling Balls

Purpose: build on what has been learned about graphics, and Vectors, putting it to use in models and simulations of rolling balls. In a subsequent lab, we will use this to simulate planets orbiting a star.

  1. Write a SuperPosn class that extends your Posn class. SuperPosn should augment Posn with:
    • double dx, dy fields, together with appropriate accessors and mutators methods
    • whatever constructors you find appropriate
    • public void move(); this method should update the x and y values of this SuperPosn instance according to its dx and dy values
    • public void paint(Graphics g); this method should draw a circle at the current location of this SuperPosn. Note: SuperPosn is not extending Frame
    • public boolean isOnScreen(int w, int h); this method's parameters represent the width and height of the window on which we are drawing. It should return true as losg as the position of this SuperPosn is logically on the screen.
  2. Write a class, MDot, which extends java.awt.Frame. To start, instances of this class should have a single SuperPosn field. The purpose of the class is to provide a frame on which you can draw and move the SuperPosn.
    • the constructor should initialize the SuperPosn somewhere on the screen, perhaps in the middle. It should also initialize dq and dy to random values (relatively small). For this you can either use any of the random number generators that we have encountered, including directly using java.util.Random.
    • write the public void paint(Graphics g) method, which gets called by the MDot's repaint() method (inherited from Frame). This method simply needs to call the paint(Graphics g) method that you implemented for the SuperPosn class.
    • write a main method for MDot that creates an instance of MDot, then proceeds to call the instance's repaint() method and then calls the move() method of the instance's SuperPosn. These two calls should be inside a loop that terminates when the SuperPosn's onScreen(WIDTH,HEIGHT) method returns false.
    • finally, call the dispose() method on your MDot instance.
  3. Write a class, MDots, that is like MDot but instead of a single member field storing a SuperPosn, create a member field that is a Vector<SuperPosn>. When you create an MDots instance, you should populate the Vector with some number (maybe a parameter of the constructor) of SuperPosns, each having random dx and dy's.
    • write a paint method for MDots that will call the paint method for each SuperPosn in the Vector
    • add a method, moveAll(), that should call the move() method on each SuperPosn in the Vector. Your moveAll() method should check after moving each SuperPosn to see if it is still on the screen. If it is not, then that SuperPosn should be removed from the Vector.
    • write a main method similar to that for MDot but that stops when all the SuperPosns in the Vector have gone off the screen.

Part 2: Introduction to Neural Networks

You will write the first part of a neural network. A linear threshold unit (LTU) is the fundamental building block of a neural network. This week, you will write a class that implements the functionality of a LTU. These units compute a single output value based a number of inputs. The output of a unit is determined by computing a linear weighted sum of its inputs and their corresponding weights, and using this sum as the input to an activation function. You might think of the activation function as using a threshold; if the activation is below the threshold, the output is 0, and if it is above the threshold the output is 1. In other words, the weighted sum is the input to the activation function, which is usually either a threshold function (as just described) or a sigmoid function (which we'll talk about later).

A single LTU can implement the boolean operators, AND, OR and NOT. For example, a unit with two inputs (x1 and x2 with corresponding weights w1=1 and w2=1) and threshold of 1.5 computes the logical conjunction (AND) of its two inputs (with input values either 1 or 0).

For this part of the assignment, you should write a class, MyLTU. Your class should implement the NeuralNetUnit interface that I provide (the java source for the interface is provided on the Eureka page). There are currently eleven public methods prescribed by the interface. At this stage, there are eight public methods (two of which are deprecated but which must still be implemented) that you should implement. (For now, you should throw UnsupportedOperationException for the last three methods as they appear in the interface: updateWeights, computeGradient, and getGradient. Later, we will implement those as well.) Your inputs should be Integer wrapper objects containing 0/1 values for input to the LTU.

For reasons that will become clear later, it is convenient to think of the threshold as just another weight and store it in the zero-indexed location for the LTU.

Your class should include a constructor that takes a single int value indicating how many inputs the unit combines. For example, you might want to create an instance with two inputs and then set the weights and threshold according to the example above. Or you can experiment with other boolean functions on two or more inputs of your own choosing.

Part 3: Testing with JUnit

Write JUnit test classes for each of the classes above. Thus, you should have SuperPosnTest, MDotTest, MDotsTest, and MyLTUTest. In each case, you should test as thoroughly as possible, keeping in mind that you might not be able to easily test random elements or painting methods.

Submission instructions:

Do not forget to use the javadoc constructs for your documentation and include the html file produced by the javadoc tool in your final submission. Make sure that you create a folder named with your Westmont email name followed by “HW4”. For example, someone with email address “cjones” would create a folder called “cjonesHW4”. Make sure that inside that folder you have: java source files as well as html documentation files resulting from javadoc. Finally, either tar or zip the folder so that when we extract it, the folder "<emailname>HW4" will be created. Submit the packaged file for assignment 4 on Eureka.