Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPE 135: Object-Oriented Analysis and Design September 26 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:

Similar presentations


Presentation on theme: "CMPE 135: Object-Oriented Analysis and Design September 26 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:"— Presentation transcript:

1 CMPE 135: Object-Oriented Analysis and Design September 26 Class Meeting
Department of Computer Engineering San Jose State University Fall 2017 Instructor: Ron Mak

2 Simple Machine Learning for RPS
Human players of the Rock Paper Scissors game generally do not make random choices. Humans try do develop strategies to beat the opponent. Humans exhibit patterns that a computer can exploit.

3 Simple Machine Learning for RPS, cont’d
Continually record the last N choices between the human and the computer player, where N = 2, 3, 4, 5 and the last choice is the human’s. For example, suppose during the game, the choices are (the human’s choices are underlined): The table shows the sequence recorded for each value of N right after the human player chose paper. SRSPPSRSP N Sequence 2 SP 3 RSP 4 SRSP 5 PSRSP

4 Simple Machine Learning for RPS, cont’d
The computer should record all the length-2 sequences, all the length-3 sequences, etc. For each sequence, the computer should also record how many times that sequence has occurred (each sequence’s frequency). For example, for N = 3, the stored sequences and their frequencies may be (in no particular order, human choices are underlined): PRS:1, SRP:2, RPR:1, PSR:1, SPS:1, PSS:1

5 Simple Machine Learning for RPS, cont’d
PRS:1, SRP:2, RPR:1, PSR:1, SPS:1, PSS:1 Suppose the last two choices are SR. The computer can predict that the human most likely will next choose paper, since SRP appears twice in this small sample. Therefore, the computer should choose scissors to beat the human’s predicted throw of paper.

6 Simple Machine Learning for RPS, cont’d
The computer can make similar predictions with its other recorded length-N sequences. As the computer plays more matches, it records more choice sequences, giving it more data to make predictions of the human’s next choice. And so it becomes increasingly difficult for a human player to beat the computer.

7 Interfaces An interface is a form of contract between programmers.
An interface specifies the public member functions of classes that implement the interface. For each function: its name and return type, and the number, order, and types of its formal parameters. An interface specifies the API of the classes that implement the interface.

8 Interfaces, cont’d We can define an interface in C++ as an abstract class containing only pure virtual functions. An interface has no implementation. An interface cannot be instantiated. Each pure virtual function must be implemented by a class that implements the interface. Each interface must have a virtual destructor.

9 Interface Example class shape   // An interface class { public:     virtual ~shape() {}     virtual void move_x(int x) = 0;     virtual void move_y(int y) = 0;     virtual void draw() = 0; ... }; C++ interface classes are maintained in .h header files. In UML class diagrams, interface names are in italics or oblique font.

10 Interfaces and the RPS Game
You’re developing the RPS game. You’ve designed a choice calculator that determines the computer’s next choice. The choice calculator has three algorithms: Random choice: Randomly choose rock, paper, or scissors each time. Smart choice: Use the simple machine learning algorithm to make a choice. Genius choice: Use a sophisticated machine learning algorithm.

11 Interfaces and the RPS Game, cont’d
What if you don’t know ahead of time which choice algorithm you’ll want to use? What if you come up with new choice algorithms later? ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice

12 Interfaces and the RPS Game, cont’d
Make ChoiceCalculator an interface. Subclasses implement the interface and generate the computer’s choices. ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice

13 Interfaces and the RPS Game, cont’d
Now you can write: What is wrong with this code? Pointer variables rc, sc, and gc each can point only to an object of its type. ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice RandomChoice *rc = new RandomChoice(); SmartChoice *sc = new SmartChoice (); GeniusChoice *gc = new GeniusChoice();

14 Code to the Interface Instead, use a variable of the interface type
Example: Variable calc and any code that uses calc does not need to know which choice subclass. As long as the code uses the ChoiceCalculator API. ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice ChoiceCalculator *calc = new SmartChoice();

15 Code to the Interface, cont’d
ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice ChoiceCalculator *calc = new SmartChoice(); Code to the interface, not to a specific subclass. This design principle is not strict about what is an interface. The interface can also be any supertype.

16 Code to the Interface, cont’d
ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice ChoiceCalculator *calc = new SmartChoice(); The call to new is awkward. What if we invent a new choice algorithm? We would have to rewrite any code that creates choice objects.

17 A Factory Class Create a choice factory class ChoiceFactory that has a static member function make_choice_calculator that takes a string parameter. The parameter determines which choice subclass to instantiate and return. The factory class encapsulates any future changes in the choice algorithms.

18 A Factory Class, cont’d ChoiceCalculator *ChoiceFactory::make_choice_calculator(string which) { if (which == "random") return new RandomChoice(); if (which == "smart") return new SmartChoice(); if (which == "genius") return new GeniusChoice(); } ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice ChoiceCalculator *calc = ChoiceFactory::make_choice_calculator(calc);

19 Interface Variables To be a strict interface, the C++ abstract class cannot contain any member variables. An interface cannot be instantiated. However, the class can contain constants. The constants are shared by any class that implements the interface. class ErrorCodes { public: static const int MISSING_SEMICOLON = 1000; static const int MISSING_PARENTHESIS = 1001; ... }

20 Objects and Interfaces
If class C implements interface N, then objects of class C can be assigned to variables of the interface type N. ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice ChoiceCalculator *calc = ChoiceFactory::make_choice_calculator(calc); Interface type Implements the ChoiceCalculator interface

21 Objects and Interfaces, cont’d
The type of an object is never an interface type. The type of a variable can be an interface type. The value of such a variable is a reference to an object whose class implements the interface type.

22 C++ Runtime Type Identification
Not as straightforward as for Java. No simple equivalent of Java’s instanceof operator. Is an object an instance of a class? Is an object an instance of a superclass?

23 typeid() myint has type: i mystr has type: NSt3__..._9allocatorIcEEEE
#include <iostream> #include <string> #include <typeinfo> using namespace std; struct Base {}; // non-polymorphic struct Derived : Base {}; struct Base2 { virtual void foo() {} }; // polymorphic struct Derived2 : Base2 {}; int main() {     int myint = 50;     string mystr = "string";     double *mydoubleptr = nullptr;     cout << "myint has type: " << typeid(myint).name() << endl          << "mystr has type: " << typeid(mystr).name() << endl          << "mydoubleptr has type: "          << typeid(mydoubleptr).name() << endl; myint has type: i mystr has type: NSt3__..._9allocatorIcEEEE mydoubleptr has type: Pd

24 typeid(), cont’d     // cout << myint is a glvalue expression of polymorphic type;     // it is evaluated     const type_info& r1 = typeid(cout << myint);     cout << endl << "cout<<myint has type : " << r1.name() << endl;     // printf() is not a glvalue expression of polymorphic type;     // NOT evaluated     const type_info& r2 = typeid(printf("%d\n", myint));     cout << "printf(\"%d\\n\",myint) has type : " << r2.name() << endl; 50 cout<<myint has type : NSt3__113basic_ostreamIcNS_11char_traitsIcEEEE printf("%d\n",myint) has type : i

25 typeid(), cont’d // Non-polymorphic lvalue is a static type
    Derived d1;     Base& b1 = d1;     cout << "reference to non-polymorphic base: " << typeid(b1).name() << endl;     Derived2 d2;     Base2& b2 = d2;     cout << "reference to polymorphic base: " << typeid(b2).name() << endl; reference to non-polymorphic base: 4Base reference to polymorphic base: 8Derived2

26 typeid(), cont’d try { // dereferencing a null pointer:
    {         // dereferencing a null pointer:         // okay for a non-polymorphic expression         cout << "mydoubleptr points to "              << typeid(*mydoubleptr).name() << endl;         // not okay for a polymorphic lvalue         Derived2* bad_ptr = nullptr;         cout << "bad_ptr points to... ";         cout << typeid(*bad_ptr).name() << endl;     }     catch (const bad_typeid& e)          cout << " caught " << e.what() << endl; } mydoubleptr points to d bad_ptr points to...  caught std::bad_typeid


Download ppt "CMPE 135: Object-Oriented Analysis and Design September 26 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:"

Similar presentations


Ads by Google