42
David Evans http://www.cs.virginia.edu/ evans CS201j: Engineering Software University of Virginia Computer Science Lecture 21: Here Be Snakes

David Evans cs.virginia/evans

Embed Size (px)

DESCRIPTION

Lecture 21: Here Be Snakes. David Evans http://www.cs.virginia.edu/evans. CS201j: Engineering Software University of Virginia Computer Science. Menu. Exam 2 Comments (CS201J condensed) Computer Science (CS200 condensed) Networking (CS457 condensed) Game playing (CS416 condensed). - PowerPoint PPT Presentation

Citation preview

Page 1: David Evans cs.virginia/evans

David Evanshttp://www.cs.virginia.edu/evans

CS201j: Engineering SoftwareUniversity of VirginiaComputer Science

Lecture 21: Here Be Snakes

Page 2: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 2

Menu• Exam 2 Comments (CS201J

condensed)

• Computer Science (CS200 condensed)

• Networking (CS457 condensed)

• Game playing (CS416 condensed)

Page 3: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 3

CS201J Really Condensed“abstract”

“design”

“impossible”, “challenge”, “a lot of work”

“thought provoking”, “spine-tingling”

“changing the world”

“smart safe secure coding class”

“thorough program analysis”

“efficient software development”

Page 4: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 4

CS201J Condensed, more responses“how to program if you aren’t a lazy bastard”

“programming with the knowledge that errors will happen and using that knowledge to survive such errors”

“learning to write code that other people can actually use”

“a safe playground for imagination”

“not your everyday computer science course”

“an exercise in humility”, “frustration by specification”

“Small Hall”, “language tricks overview”

“explores techniques and tools for constructing robust, complex and secure software”

“adaptable tools for software programming and design”

“secure programming”

Page 5: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 5

CS201J Condensed – My Answer

abstraction

Using abstraction to manage complexity

Page 6: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 6

Abstraction

From Gerard Holzmann’s FSE Slides

Page 7: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 7

Abstraction in CS201J• Abstraction by Specification

– Abstract away how details by saying what a procedure does

• Data Abstraction– Abstract away representation details by specifying

what you can do with something

• Subtyping– Abstract away actual type details by allowing many

types to be used in the same way (?)

• Concurrency– Abstract away when details (?)

Page 8: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 8

Is CS201J a Computer Science

class?

Page 9: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 9

What is Computer Science?

Page 10: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 10

Let AB and CD be the two given numbers not relatively prime. It is required to find the greatest common measure of AB and CD.

If now CD measures AB, since it also measures itself, then CD is a common measure of CD and AB. And it is manifest that it is also the greatest, for no greater number than CD measures CD. But, if CD does not measure AB, then, when the less of the numbers AB and CD being continually subtracted from the greater, some number is left which measures the one before it.

Euclid’s Elements, Book VII, Proposition 2 (300BC)

Page 11: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 11

By the word operation, we mean any process which alters the mutual relation of two or more things, be this relation of what kind it may. This is the most general definition, and would include all subjects in the universe. Again, it might act upon other things besides number, were objects found whose mutual fundamental relations could be expressed by those of the abstract science of operations, and which should be also susceptible of adaptations to the action of the operating notation and mechanism of the engine... Supposing, for instance, that the fundamental relations of pitched sounds in the science of harmony and of musical composition were susceptible of such expression and adaptations, the engine might compose elaborate and scientific pieces of music of any degree of complexity or extent.

Ada, Countess of Lovelace, around 1830

Page 12: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 12

What is the difference between

Euclid and Ada?

“It depends on what your definition of ‘is’ is.”

Bill Gates (at Microsoft’s anti- trust trial)

Page 13: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 13

Geometry vs. Computer Science

• Geometry (mathematics) is about declarative knowledge: “what is”

If now CD measures AB, since it also measures itself, then CD is a common measure of CD and AB

• Computer Science is about imperative knowledge: “how to”

Computer Science has nothing to do with beige (or translucent blue) boxes called “computers” and is not a science.

Page 14: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 14

Computer Science

“How to” knowledge:

• Ways of describing and carrying out imperative processes (computations)

• Ways of reasoning about (predicting) what imperative processes will do

About 50% of CS201J is about these things…but nearly all of CS200 is!

Page 15: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 15

Meetings: Mondays, Wednesdays and Fridays at 2:00-2:50 pmCS201J graduates can take (SEAS tech elective credit), but no prior background is expected.

Spring 2003

CS200: Computer ScienceFrom Ada and Euclid to Quantum Computers and the World Wide Web

www.cs.virginia.edu/cs200

Page 16: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 16

How to never lose at Tic-Tac-Toe

Page 17: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 17

Tic-Tac-Toe

X O

X

O

X O

X

Page 18: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 18

Tic-Tac-Toe Game Tree

X O

X

O

X O

X

1 2 3

4 5 6

7 8 9

12 3

XX X

X…

2 3

X

O

Page 19: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 19

Playing Tic-Tac-Toe• Goal: get three of your marks in a line before your opponent

does• Sub-goal: get to a position where it is your move, and you

have a line with 2 of your marks and one empty square• Sub2-goal: get to a position where it is your opponent’s

move, and no matter what she does, after her move sub1-goal is satisfied

• Sub3-goal: get to a position where it is your move, and you can make a move that satisfies sub2-goal is satisfied

• Sub4-goal: get to a position where it is your opponent’s move, and no matter what she does, after her move sub3-goal is satisfied

• …• Subn-goal: from the start, get to a position where no matter

how your opponent moves, you can select moves that get to the Goal

Page 20: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 20

Minimax

• Evaluate a game situation based on how good it is for you

• Try to maximize your score, assuming opponent will try to minimize your score (that is, maximize her score)

Score (tic-tac-toe board) = -1 if opponent has 3 in line 0 if neither has 3 in line 1 if I have 3 in line

Page 21: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 21

Minimax Tic-Tac

12 3

2 3

4X XX X

Score (b) = 0 0 0 0

Score (tic-tac board) = -1 if opponent has 2 in vertical line 0 if neither has 2 in vertical line 1 if I have 2 in vertical line

4O

X X X

O OScore (b) = 1 0 1

3 4O

X

X

O

X

X

Score (b) = 1 0

2 4

O

X

O

X

X

0

X

0

Max: 1 Max: 0

Min: 0

Max: 0

Page 22: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 22

Why is there no international tic-tac-toe championship?

• Total tic-tac-toe games to try =

9 * 8 * 7 * 6 * … * 1 = 9! = 362 880

• Minimax tells you that if both players pick the best move, it is always a draw

• What about chess?– ~101050 possible chess games– After 4 moves, there are (at least) 809 896

possible positions!

Page 23: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 23

Deep Blue (1997)

• Evaluates 400m

moves/second

• Need complex

scoring functions for positions– Special purpose hardware for

scoring chess positions quickly

• Database of openings, special situations, etc.

Page 24: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 24

Networking

Page 25: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 25

PS7 Networking

Snakes Server

int port = …;Server Socket ss = new ServerSocket (port);

ServerSocket listens to network port for incoming connections…

Socket client = ss.accept ();A Socket gives you an input and output stream abstraction for dealing with network communication.

Page 26: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 26

Snakes Clientimport java.io.*;import java.net.*;

public class MySnake extends SnakeInterface { private ClientDisplay display; private final int FRAME_WIDTH = 400; private final int FRAME_HEIGHT = 400;

public MySnake (Socket s) throws IOException { super (s); … // set up the display }

// The SnakeInterface run method will call this whenever the state of // the world has changed. public void updateState () { if (display != null) { display.updateGrid (); } }

Page 27: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 27

MySnake main static public void main (String args[]) { if (args.length != 2) { System.err.println ("Usage: java MySnake <server> <port>"); System.exit (1); } else { String server = args [0]; int port = Integer.valueOf (args[1]).intValue ();

try { Socket s = new Socket (server, port); MySnake ms = new MySnake (s); } catch … { // Need to catch exceptions if server is not reachable } } }

Hint: You will want to do something similar in your PS7 code!

Page 28: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 28

Who Invented the Internet?

Page 29: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 29

Who Invented Networking?

Page 30: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 30

What is a Network?

A group of three or more connected communicating entities

Page 31: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 31

Beacon Chain NetworkingThus, from some far-away beleaguered island, where all day long the men have fought a desperate battle from their city walls, the smoke goes up to heaven; but no sooner has the sun gone down than the light from the line of beacons blazes up and shoots into the sky to warn the neighbouring islanders and bring them to the rescue in their ships. Iliad, Homer, 700 BC

Chain of beacon’s signaled Agammemnon’s return (~1200BC), spread on Greek peaks over 600km.

Page 32: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 32

Chappe’s Semaphore Network

Mobile Semaphore Telegraph Used in the Crimean War 1853-1856

First Line (Paris to Lille), 1794

Page 33: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 33

Measuring Networks• Latency

Time from sending a bit until it arrives

seconds (or seconds per geographic distance)

• Bandwidth

How much information can you transmit per time unit

bits per second

Page 34: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 34

Latency and Bandwidth• Napoleon’s Network (Paris to Toulon, 475 miles)• Latency: 13 minutes (1.6s per mile)

– What is the delay at each signaling station, how many stations to reach destination

– At this rate, it would take ~1 hour to get a bit from California

• Bandwidth: 2 symbols per minute (98 possible symbols, so that is ~13 bits per minute– How fast can signalers make symbols– At this rate, it would take you 3 ½ years to download

today’s slides!

Page 35: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 35

Improving Latency• Less transfer points

– Longer distances between transfer points– Semaphores: how far can you see clearly

• Telescopes can help, but curvature of Earth is hard to overcome

– Use wires (electrical telegraphs, 1837)

• Faster travel– Hard to beat speed of light (semaphore network)– Electrons in copper travel about 1/3rd speed of light

• Faster transfers– Replace humans with machines

Page 36: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 36

Improving Bandwidth• Faster transmission

– Train signalers to move semaphore flags faster– Use something less physically demanding to transmit

• Bigger pipes– Have multiple signalers transmit every other letter at

the same time

• Better encoding– Figure out how to code more than 98 symbols with

semaphore signal– Morse code (1840s)

Page 37: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 37

internetwork

A collection of multiple networks connected together, so messages can be transmitted between nodes on different networks.

Page 38: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 38

Okay, so who invented the Internet?

Page 39: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 39

The First Internet• 1800: Sweden and Denmark worried about

Britain invading

• Edelcrantz proposes link across strait separating Sweden and Denmark to connect their (signaling) telegraph networks

• 1801: British attack Copenhagen, network transmit message to Sweden, but they don’t help.

• Denmark signs treaty with Britain, and stops communications with Sweden

Page 40: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 40

First Use of Internet

• October 1969: First packets on the ARPANet from UCLA to Stanford. Starts to send "LOGIN", but it crashes on the G.

• 20 July 1969:Live video (b/w) and audio transmitted from moon to Earth, and to several hundred televisions worldwide.

Page 41: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 41

The Modern Internet• Packet Switching: Leonard Kleinrock (UCLA)

thinks he did, Donald Davies and Paul Baran, Edelcrantz’s signalling network (1809) sort of did it

• Internet Protocol: Vint Cerf, Bob Kahn

• Vision, Funding: J.C.R. Licklider, Bob Taylor • Government: Al Gore (first politician to promote

Internet, 1986; act to connect government networks to form “Interagency Network”)

Page 42: David Evans cs.virginia/evans

26 November 2002 CS 201J Fall 2002 42

Charge• PS7 is due week from tomorrow!• New ps7.zip will be available later today:

– Grid that uses cartesian coordinates– Better server– Small changes to SnakeInterface

• If you have made enough progress that any of these changes cause problems, email me for restitution.

• Enjoy your Thanksgiving!– Remember if you eat too many apples, you will grow

and it will be harder to avoid running into your tail!