Due by class time Wednesday, March 29
If you wish, you may work with a partner on this assignment.
Read Chapter 10 of your Python textbook.
Study this week's code examples from class.
Finish implementing the class definitions for Car, Student, BeerCan, and Neuron from Lab 9.
Add a method to your Neuron class called adjustWeights, which takes two inputs: a pattern such as [1, 1], and a value called correctResponse, which is either 0 or 1. This method should do the following:
First, the output response (0 or 1) of the neuron on pattern is computed.
Next, the error is computed, which is equal to correctResponse - output, where output is the response obtained in the previous step. Notice that if the neuron produces the correct response, the error will be zero. Otherwise it will be +1.
Finally, each connection strength is adjusted by an amount proportional to the error multiplied by the amount of activation coming in over the connection. More precisely, we add the value 0.1 × error × pattern[k] to the kth weight, for all of the neuron's weights.
We can use this simple technique to train a neuron to recognize a particular pattern by repeatedly calling adjustWeights on the pattern and the desired output. To try this out, first randomize the weights of your neuron by calling n.randomize(). Then call n.test() to verify that the neuron produces 0 on all four test patterns. Now call n.adjustWeights([1, 1], 1) a few times, observing how the weight values and the neuron's behavior change after each weight adjustment. Eventually, the neuron should learn to produce 1 in response to the pattern.
>>> n.randomize() >>> n.weights >>> n.test() >>> n.adjustWeights([1, 1], 1) >>> n.weights >>> n.test() ...
Consider the BankAccount example. Add a too-good-to-be-true method called resetBalance, which takes no input parameters and resets the account's balance to the original amount that it was created with. For example:
>>> a = BankAccount("777-101", 500) >>> a.withdraw(350) Withdrew $350.00 >>> a.withdraw(100) Withdrew $100.00 >>> a.inquiry() Balance is currently $50.00 >>> a.resetBalance() Balance is currently $500.00 >>>
Now let's make BankAccount objects be password-protected. Modify the constructor so that it takes an extra password string as input, as in
a = BankAccount("777-101", 500, "open sesame")
The account should process a withdraw or inquiry request only if it is accompanied by the password with which the account was created, and should otherwise complain:
>>> a.withdraw(100, "open sesame") Withdrew $100.00 >>> a.withdraw(100, "abracadabra") Sorry, that password is incorrect >>> a.inquiry("stick em up") Sorry, that password is incorrect >>>
Implement a Product class. A product has a name and a price, for example: Product("Toaster", 29.95). Include a method __str__ that returns the name and price of the product as a formatted string, a method priceWithTax(percent), which returns the price with sales tax of the given percentage added on, and a method setNewPrice(amount), which changes the product's price to the new amount. Your objects should behave as follows:
>>> gizmo = Product("Ronco Tomato Scrambler", 39.95) >>> print gizmo Ronco Tomato Scrambler, price: $39.95 plus tax >>> gizmo.priceWithTax(10) 43.945 >>> gizmo.setNewPrice(49.95) Ronco Tomato Scrambler now costs $49.95 >>> gizmo.priceWithTax(10) 54.945 >>>
Implement an Auditorium class. An auditorium has a seating capacity that is specified when the auditorium is created. Once this capacity is reached, no more seats can be filled. An Auditorium object should keep track of its seating capacity, and the number of seats that are currently open. Write and test the following methods:
>>> carnegieHall = Auditorium(1500) >>> carnegieHall.lookInside() No one is inside >>> carnegieHall.fillSeats(1000) Filled 1000 seats >>> carnegieHall.lookInside() 1000 people inside with 500 seats left >>> carnegieHall.fillSeats(700) Sorry, sold out after filling 500 seats >>> carnegieHall.lookInside() 1500 people inside with 0 seats left >>> carnegieHall.fillSeats(100) Sorry, all sold out >>>
Implement a Vehicle class that, like the Auditorium class above, keeps track of the number of people inside and the total number of seats available. However, a Vehicle should also remember the names of each person inside. Write and test the following methods:
>>> chevy = Vehicle(4) >>> chevy.addPerson("Thelma") >>> chevy.addPerson("Louise") >>> chevy.lookInside() Thelma Louise 2 empty seats left >>>
Put all of your code into a single file called assign6.py. Include your name at the top of the file as a comment, and submit it using /common/cs/submit/cs30-submit. Be sure to run this command from the directory containing your file.
If you have questions about anything, don't hesitate to ask!