Document 15942384

advertisement
On Robots, Probability, and Artificial Intelligence
Nelson Series Talk
Wed, 10/13
Sebastian Thrun &
robots, Stanford
7:00 pm
HMC’s Galileo Auditorium
This talk will expose the audience to recent developments in
real-world robotics. The speaker and his team have developed
mobile robots that have operated as interactive tour-guides in a
Smithsonian museum, assisted elderly people in everyday
tasks, and explored several abandoned coal mines inaccessible
to people, all completely autonomously. His current effort
aims at winning the DARPA Grand Challenge, which requires
the development of a ground vehicle that can drive from L.A.
to Las Vegas without human assistance. These developments
would not have been possible without a new paradigm in robot
software design, known as probabilistic robotics. Probabilistic
robotics imports concepts from statistics and decision theory
into the field of robotics. As part of this presentation, the
speaker will introduce the audience to the basics of
probabilistic robotics, and explain why statistical techniques
have become such an essential tool in robotics, in such a
remarkably short time.
Week 7 in CS 5
Last Names
LAB: A-M
• Showcasing unusual solutions…
• Fall break
There is no lecture next week!
There is no recitation this Friday!
There WILL BE recitation NEXT Friday (8 am)
• Getting data together -- arrays !
• HW 8 (2 problems)
due Sunday, 10/24 at midnight
due Monday, 10/25 at midnight
Seasons by Mike Maguire
M/T sections
W/Th sections
public static int numSyllables(String w)
{
int numSyls = 0;
int L = w.length();
if ( isVowel(w.charAt(0)) )
++numSyls;
Syllable
counting
// an initial vowel ?
for (int i=1 ; i < L ; ++i)
{
// vowel preceded by a consonant
if ( isVowel(w.charAt(i)) && !isVowel(w.charAt(i-1)) )
++numSyls;
}
// final ‘e’ preceded by a consonant
if ( w.charAt(L-1) == 'e’
&&
L >= 2
&&
!isVowel(w.charAt(L-2))
)
--numSyls;
if (numSyls < 1)
numSyls = 1;
return numSyls;
}
// every word has at least 1 syllable
A puzzle...
How could you print a String backwards?
String w = H.nw();
for ( int i=
{
;
;
)
}
How could you print 5 Strings in backwards order?
(Not reversing each string…)
A puzzle...
How could you print 5 Strings in backwards order?
(Not reversing each string…)
String
String
String
String
String
H.pl(
H.pl(
H.pl(
H.pl(
H.pl(
s1
s2
s3
s4
s5
s5
s4
s3
s2
s1
=
=
=
=
=
H.nw();
H.nw();
H.nw();
H.nw();
H.nw();
);
);
);
);
);
Not a very flexible solution...
Sentence palindromes
Fall leaves after leaves fall
Bores are people that say that people are bores
First Ladies rule the state and state the rule, “Ladies First!”
You can cage a swallow, can’t you,
but you can’t swallow a cage, can you?
Maybe we don’t even need to solve it?!
A palindromic poem...
Seasons by Mike Maguire
Arrays in pictures
H.out.println(“Type the number of words:
int L = H.ni();
String[] A;
A = new String[L];
for (int i=0 ; i<L ; ++i)
{
A[i] = H.nw();
}
for (int i=L-1 ; i>=0 ; --i)
{
H.pl( A[i] );
}
”);
Arrays in pictures
H.out.println(“Type the number of words:
int L = H.ni();
String[] A;
A = new String[L];
for (int i=0 ; i<L ; ++i)
{
A[i] = H.nw();
}
for (int i=L-1 ; i>=0 ; --i)
{
H.pl( A[i] );
}
”);
5
int L
Arrays in pictures
H.out.println(“Type the number of words:
int L = H.ni();
String[] A;
A = new String[L];
String[]
A
for (int i=0 ; i<L ; ++i)
{
A[i] = H.nw();
}
for (int i=L-1 ; i>=0 ; --i)
{
H.pl( A[i] );
}
”);
5
int L
Arrays in pictures
H.out.println(“Type the number of words:
int L = H.ni();
String[] A;
A = new String[L];
”);
5
int L
array reference
A[1]
A[0]
String[]
A
for (int i=0 ; i<L ; ++i)
{
A[i] = H.nw();
}
for (int i=L-1 ; i>=0 ; --i)
{
H.pl( A[i] );
}
A[3]
A[2]
A[4]
Arrays in pictures
H.out.println(“Type the number of words:
int L = H.ni();
String[] A;
A = new String[L];
”);
5
int L
A[1]
A[0]
A[3]
A[2]
A[4]
String[]
A
for (int i=0 ; i<L ; ++i)
{
A[i] = H.nw();
}
for (int i=L-1 ; i>=0 ; --i)
{
H.pl( A[i] );
}
i is 0
i is 2
i is 1
i is 4
i is 3
Arrays in pictures
H.out.println(“Type the number of words:
int L = H.ni();
String[] A;
A = new String[L];
”);
5
int L
A[1]
A[0]
A[3]
A[2]
A[4]
String[]
A
for (int i=0 ; i<L ; ++i)
{
A[i] = H.nw();
}
for (int i=L-1 ; i>=0 ; --i)
{
H.pl( A[i] );
}
i is 4
i is 3
i is 2
i is 1
i is 0
-ign
sigh-
the
saw
I
Arrays - lists of data items
declares a double array
named A
double[] A;
declares five doubles named
A[0] … A[4]
A = new double[5];
length
for (int i=0 ; i<5 ; ++i)
{
A[i] = H.nd();
}
index
loops through the
array, getting input
from the user
Arrays in code
H.pl(“How many doubles would you like to store?
int L = H.ni();
double[] A;
A = new double[L];
for (int i=0 ; i<L ; ++i)
{
A[i] = H.nd();
}
// now print them out (in forward order !)
”);
James Gosling
• Software architect for Sun Microsystems
• Primary designer of the Java programming language
• Sees Bill Gates as a dangerous force…
Strings
Element types
Example
Declaration
vs
Arrays
char
double, int, String,
boolean, char, (any type!)
String s;
double[] A;
A = new double[100];
The ith element
s.charAt(i)
A[i]
Length
s.length()
A.length
Range
Warning
from
0
up to
length-1
Be careful not to go out of bounds!
java.lang.StringIndexOutOfBoundsException: -1
java.lang.ArrayIndexOutOfBoundsException: -1
T. T. Securities
Input stock prices for a number of days in a row,
and then analyze that data… .
T. T. Securities
Input stock prices for a number of days in a row,
and then analyze that data… .
Menu
0 Display prices
1 Sum of prices
2 Average of prices
3 St. dev. of prices
4 Minimum price
5 Index of minimum price
6 Maximum price
7 Index of minimum price
8 Your TTS investment strategy
9 Quit
Which choice would you like?
Software
T. T. Securities
Input stock prices for a number of days in a row,
and then analyze that data… .
Menu
0 Display prices
1 Sum of prices
2 Average of prices
3 St. dev. of prices
4 Minimum price
5 Index of minimum price
6 Maximum price
7 Index of minimum price
8 Your TTS investment strategy
9 Quit
Which choice would you like?
Software
Hardware
T. T. Securities
Input stock prices for a number of days in a row,
and then analyze that data… .
methods
Menu
void menu()
0 Display prices
1 Sum of prices
2 Average of prices
3 St. dev. of prices
4 Minimum price
5 Index of minimum price
6 Maximum price
7 Index of minimum price
8 Your TTS investment strategy
9 Quit
Which choice would you like?
void prices(double[] A)
double sum(double[] A)
…
double average(double[] A)
double stdev(double[] A)
double min(double[] A)
int minIndex(double[] A)
double max(double[] A)
int maxIndex(double[] A)
no separate method needed
Standard Deviation
There are a number of formulas, but we will use this one:
Menu
0 Display prices
1 Sum of prices
2 Average of prices
3 St. dev. of prices
4 Minimum price
5 Index of minimum price
6 Maximum price
7 Index of minimum price
8 Your TTS investment strategy
9 Quit
Which choice would you like?
methods
double stdev(double[] A)

i
(A[i] - Aav)2
L - 1
Code Sketch: main
public static void main(String[] args)
{
H.pl(“How many days?”);
int L = H.ni();
// get array length
double[] A = new double[L]; // create array A
H.pl(“Please input your prices:”);
for (int i=0 ; i<L ; ++i)
{
A[i] = H.nd();
}
while (true)
{
// print menu and handle choices
Printing the prices
public static void prices(double[] A)
“Quiz”
Finish these two methods…
• This method returns the sum of the elements in the input array.
public static double sum(double[] A)
{
double s = 0.0;
for (
{
}
return s;
}
• This method returns the average of the elements in the input array.
public static double average(double[] A)
“Extra Credit”:
How concise can this method be?
• This method returns the maximum element from the input array.
public static double max(double[] A)
Extra:
What is something unusual and unrelated to CS 5 that you & the person next to you have in common ?!
Using sum
public static void main(String[] args)
{
// set up the array A of stock prices
H.pl( “The sum is ” + sum(A) );
}
double[]
A
90.0
10.0
60.0
public static double sum(double[] Z)
{
// see previous page or quiz …
return ans;
}
42.0
75.0
5.0
double[]
Z
Using sum
public static void main(String[] args)
{
// set up the array A of stock prices
H.pl( “The sum is ” + sum(A) );
}
double[]
A
90.0
10.0
60.0
42.0
75.0
5.0
2 references to the same list of data
public static double sum(double[] Z)
{
// see previous page or quiz …
return ans;
}
double[]
Z
Array Searching
public static double
max
(double[] A)
Option #8
Find the most profitable strategy for buying and
selling the stock among the prices in the array...
Day
Day
Day
Day
Day
Day
0
1
2
3
4
5
Price
Price
Price
Price
Price
Price
90.0
10.0
60.0
42.0
75.0
5.0
Lights Out !
Hw8 Pr2
on
on
off
off
off
Pair Program
on
A starting row of lights
0
1
2
3
4
5
Each turn, a light is selected -It and its neighbors switch states.
2 is selected
0
1
2
3
4
5
0
1
2
3
4
5
Goal: get all the lights off…
Lights Out Strategy ...
// choose HOW you’ll represent the lights !
// let the user set the # of lights from 3 to 15
// start each light randomly on or off
// draw the current set of lights
// allow the user to select a light
//
only allow valid lights !
// update the set of lights and repeat
Lights Out !
// draw the current set of lights
0
“on” lights
should be 4x4
blocks of stars
|
|
|
|
2
1
3
4
|****|****|****|
|****|****|****|
|****|****|****|
|****|****|****|
0
“off” lights should be
4x4 blocks of spaces
1
2
3
print light numbers close
to the center of each light
lights should be
separated with
vertical bars
5
|****|
|****|
|****|
|****|
4
5
6
7
may display all light
numbers up to 15
Lights Out !
// allow the user to select a light
//
only allow valid lights !
Lights Out Methods
You need to choose your own methods
to write for this program...
Feel free to use (or ignore) my two methods -public static void printLights(int[] Lts)
public static boolean allAreOff(int[] Lts)
Summary
To declare an array:
double[] A;
String[] song;
int[]
Lights;
To declare an array’s individual elements:
A = new double[L];
quip = new String[nWords];
Lights = new int[42];
To loop through an array:
for (int i=0 ; i<A.length ; ++i)
{
do something with A[i] in here ...
}
Lab this week
• Problem 1: T. T. Securities
You’ll need to write (and use)
• Problem 2: Lights Out!
|
|
|
|
|****|****|****|
|****|****|****|
|****|****|****|
|****|****|****|
0
1
2
3
|****|
|****|
|****|
|****|
4
5
Last Names A-M
void menu()
void prices(double[] A)
double sum(double[] A)
double average(double[] A)
double stdev(double[] A)
double min(double[] A)
int minIndex(double[] A)
double max(double[] A)
int maxIndex(double[] A)
You may choose what methods to write...
0
1
2
3
• Extra Credit: (1) An undo feature for Lights Out ...
(2) A sound-editing program
4
5
A palindromic poem...
Seasons by Mike Maguire
Representing Sound
physics
continuous plot of air
pressure vs. time
sampling
samples taken every ~
1/11000th of a second
quantization
Each sample is measured on a
loudness scale from 0 to 65,535.
(This fits into 2 bytes.)
storage
These two bytes are called a
frame. The frames are ordered
in an array of bytes. Thus, the
raw audio data is a byte[].
“Quiz”
Finish these two methods…
• This method returns the sum of the elements in the input array.
public static double sum(double[] A)
{
double s = 0.0;
for (
{
}
return s;
}
• This method returns the average of the elements in the input array.
public static double average(double[] A)
“Extra Credit”:
How concise can this method be?
• This method returns the maximum element from the input array.
public static double max(double[] A)
Extra:
What is something unusual and unrelated to CS 5 that you & the person next to you have in common ?!
“Quiz”
Follow this code to determine a partner!
public static void main(String[] args)
{
H.pl(“Enter your number -- see overhead slide! ”);
int x = H.ni();
if (x > 16 && x%2 == 0)
{
H.pl( “Your partner is #” + (48-x)/2 );
}
else if ( x > 16 )
{
H.pl( “Your partner is #” + (64-x) );
}
else
{
H.pl( “Your partner is #” + 2*(24-x) );
}
}
Then, find this partner and try writing the two methods on the other side -use the prices example as a starting point.
public static double sum(double[] A)
Try to write these
two methods
Names:
public static double average(double[] A)
Extra Credit:
What is something unusual and unrelated to CS 5 that you two or you three have in common ?!
Arrays in pictures
H.out.println(“Type the number of words:
int L = H.ni();
String[] A;
A = new String[L];
”);
5
int len
A[1]
A[0]
A[3]
A[2]
A[4]
String[]
A
for (int i=0 ; i<L ; ++i)
{
A[i] = H.nw();
}
for (int i=L-1 ; i>=0 ; --i)
{
H.pl( A[i] );
}
i is 0
i is 2
i is 1
i is 4
i is 3
“Quiz”
Follow this code to determine a partner!
public static void main(String[] args)
{
H.pl(“Enter your number -- see overhead slide! ”);
int x = H.ni();
if (x > 16 && x%2 == 0)
{
H.pl( “Your partner is #” + (48-x)/2 );
}
else if ( x > 16 )
{
H.pl( “Your partner is #” + (64-x) );
}
else
{
H.pl( “Your partner is #” + 2*(24-x) );
}
}
Then, find this partner and try writing the two methods on the other side -use the prices example as a starting point.
Drawing room

i
(A[i] - Aav)2
L - 1
0
1
2
3
4
5
0
1
2
3
4
5
0
1
2
3
4
5
Printed version following this slide
This week in CS 5
• Putting data together with arrays
• HW 8 (2 problems)
due Sunday, 10/28 at midnight
due Monday, 10/29 at midnight
Recitation for HW8 will be Friday 10/26
No recitation this Friday, 10/19
-- Reading: Week 7’s online notes
• Watch out for code indenting !
if ( isVowel(w.charAt(0)) )
{
++numSyls;
}
M/T sections
W/Th sections
Arrays - lists of data items
String[] quip;
- declares a String array named quip
String[]
quip
quip = new String[5];
- declares five Strings named quip[0]…quip[4]
quip[1]
quip[3]
quip[0]
quip[2]
quip[4]
String[]
quip
- looping through the array
for (int i=0 ; i<5 ; ++i)
{
quip[i] = H.in.nextWord();
}
Arrays in code
H.out.println(“Type the number of words:
int len = H.in.nextInt();
String[] quip;
quip = new String[len];
for (int i=0 ; i<len ; ++i)
{
quip[i] = H.in.nextWord();
}
// create array variable
// create data variables
// input each word
// now print them out in reverse order…
for (int i=len-1 ; i>=0 ; --i)
{
H.out.print( quip[i] + “ ” );
}
”);
Arrays in pictures
H.out.println(“Type the number of words:
int len = H.in.nextInt();
String[] quip;
quip = new String[len];
”);
5
int len
quip[1]
quip[3]
quip[0]
quip[2]
quip[4]
String[]
quip
for (int i=0 ; i<len ; ++i)
{
quip[i] = H.in.nextWord();
}
for (int i=len-1 ; i>=0 ; --i)
{
H.out.print( quip[i] + “ ” );
}
Arrays
Element types
Example
Declaration
vs.
Strings
double, int, String,
boolean, char, (any type) …
char
double[] arr;
arr = new double[100];
String s;
The ith element
arr[i]
s.charAt(i)
Length
arr.length
s.length()
Range
Warnings
from
0
up to
length-1
Be careful not to go out of bounds!
java.lang.StringIndexOutOfBoundsException: -1
java.lang.ArrayIndexOutOfBoundsException: -1
T. T. Securities
Input stock prices for a number of days in a row,
and then analyze that data… .
Menu:
1 Display prices
2 Compute average of prices
3 Compute standard deviation of prices
4 Display index and value of lowest price
5 Display index and value of highest price
6 Your TTS investment strategy
9 Quit
Which choice would you like?
Arrays and Methods
public static double sumArray(double[] arr)
Using sumArray
public static void main(String[] args)
{
// prompt for and input nStocks
double[] stocks = new double[nStocks];
// input and assign each stocks[i]
double stockSum = sumArray(stocks);
H.out.println(“The sum is ” + stockSum);
}
double[]
stocks
90.0
stocks[0]
10.0
stocks[1]
60.0
stocks[2]
42.0
stocks[3]
75.0
stocks[4]
public static double sumArray(double[] arr)
{
// see previous page …
return sum;
}
70.0
stocks[5]
double[]
arr
Array Searching
public static double findMax(double[] arr)
T. T. Securities
Find the most profitable strategy for buying and
selling the stock among the prices in the array...
Day
Day
Day
Day
Day
Day
0
1
2
3
4
5
Price
Price
Price
Price
Price
Price
is
is
is
is
is
is
90.0
10.0
60.0
42.0
75.0
70.0
Lights Out !
on
on
off
off
off
on
A starting row of lights
0
1
2
3
4
5
Each turn, a light is selected -It and its neighbors switch states.
2 is selected
0
1
2
3
4
5
0
1
2
3
4
5
Goal: get all the lights off…
Lights Out !
Features of the game:
// allow the user to set the
//
number of lights from 3 to 15
// start each light randomly on or off
// draw the current set of lights
// allow the user to select a light
//
only allow valid lights !
// update the set of lights and repeat
Lights Out !
// draw the current set of lights
0
“on” lights
should be 4x4
blocks of stars
|
|
|
|
2
1
3
4
|****|****|****|
|****|****|****|
|****|****|****|
|****|****|****|
0
“off” lights should be
4x4 blocks of spaces
1
2
3
print light numbers close
to the center of each light
lights should be
separated with
vertical bars
5
|****|
|****|
|****|
|****|
4
5
6
7
may display all light
numbers up to 15
Summary
To declare an array:
double[] stocks;
String[] quip;
int[]
grades;
To declare an array’s individual elements:
stocks = new double[nStocks];
quip = new String[nWords];
grades = new int[42];
To loop through an array:
for (int i=0 ; i<stocks.length ; ++i)
{
do something with stocks[i] in here ...
}
A puzzle...
Take in a number of words and print them out in reverse order.
(To be specific, suppose it’s 5 words.)
String
String
String
String
String
s1
s2
s3
s4
s5
=
=
=
=
=
H.in.nextWord();
H.in.nextWord();
H.in.nextWord();
H.in.nextWord();
H.in.nextWord();
H.out.print(
H.out.print(
H.out.print(
H.out.print(
H.out.print(
s1
s2
s3
s4
s5
+
+
+
+
+
“ ” );
“ ” );
“ ” );
“ ” );
“\n” );
Not a very flexible solution...
Arrays - lists of data items
declares a String
array named quip
String[] quip;
quip = new String[5];
declares five Strings named
quip[0]…quip[4]
for (int i=0 ; i<5 ; ++i)
{
quip[i] = H.in.nextWord();
}
index
loop through
the array
Arrays in code
H.out.println(“Type the number of words:
int len = H.in.nextInt();
String[] quip;
quip = new String[len];
for (int i=0 ; i<len ; ++i)
{
quip[i] = H.in.nextWord();
}
// create an empty array
// create array elements
// input each element
// now print them out in reverse order…
for (int i=len-1 ; i>=0 ; --i)
{
H.out.print( quip[i] + “ ” );
}
”);
Arrays
Element types
Example
Declaration
vs
Strings
double, int, String,
boolean, char, (any type) …
char
double[] arr;
arr = new double[100];
String s;
The ith element
arr[i]
s.charAt(i)
Length
arr.length
s.length()
Range
Warning
from
0
up to
length-1
Be careful not to go out of bounds!
java.lang.StringIndexOutOfBoundsException: -1
java.lang.ArrayIndexOutOfBoundsException: -1
A real application
T. T. Securities’ analysis package
(1) Decide how many days of analysis you’d like to do.
(2) Input stock prices for a that number of days in a row
(3) Analyze that data… .
Menu
1 Display prices
2 Compute average of prices
3 Compute variance of prices
4 Display index and value of lowest price
5 Display index and value of highest price
6 Your TTS investment strategy
9 Quit
Which choice would you like?
Methods
displayMenu
displayPrices
sumArray
averageArray
stdDevArray
indexOfSmallest
indexOfLargest
Day
Day
Day
Day
Day
Day
Day
Day
0
1
2
3
4
5
6
7
It’s not always with the
minimum or maximum !
Price
Price
Price
Price
Price
Price
Price
Price
is
is
is
is
is
is
is
is
95.0
15.0
90.0
10.0
60.0
42.0
75.0
70.0
T. T. Securities
Find the most profitable strategy for buying and
selling the stock among the prices in the array...
Day
Day
Day
Day
Day
Day
0
1
2
3
4
5
Price
Price
Price
Price
Price
Price
is
is
is
is
is
is
90.0
10.0
60.0
42.0
75.0
70.0
Using sumArray
public static void main(String[] args)
{
// prompt for and input nStocks
double[] stocks = new double[nStocks];
// input and assign each stocks[i]
double stockSum = sumArray(stocks);
H.out.println(“The sum is ” + stockSum);
}
double[]
stocks
90.0
10.0
60.0
42.0
public static double sumArray(double[] arr)
{
// see previous page …
return sum;
}
75.0
double[]
arr
70.0
Arrays in pictures
H.out.println(“Type the number of words:
int len = H.in.nextInt();
String[] quip;
quip = new String[len];
for (int i=0 ; i<len ; ++i)
{
quip[i] = H.in.nextWord();
}
for (int i=len-1 ; i>=0 ; --i)
{
H.out.print( quip[i] + “ ” );
}
”);
5
int len
Arrays in pictures
H.out.println(“Type the number of words:
int len = H.in.nextInt();
String[] quip;
quip = new String[len];
String[]
quip
for (int i=0 ; i<len ; ++i)
{
quip[i] = H.in.nextWord();
}
for (int i=len-1 ; i>=0 ; --i)
{
H.out.print( quip[i] + “ ” );
}
”);
5
int len
Arrays in pictures
H.out.println(“Type the number of words:
int len = H.in.nextInt();
String[] quip;
quip = new String[len];
”);
5
int len
array reference
quip[1]
quip[3]
quip[0]
quip[2]
quip[4]
String[]
quip
for (int i=0 ; i<len ; ++i)
{
quip[i] = H.in.nextWord();
}
for (int i=len-1 ; i>=0 ; --i)
{
H.out.print( quip[i] + “ ” );
}
Arrays in pictures
H.out.println(“Type the number of words:
int len = H.in.nextInt();
String[] quip;
quip = new String[len];
”);
5
int len
quip[1]
quip[3]
quip[0]
quip[2]
quip[4]
String[]
quip
for (int i=0 ; i<len ; ++i)
{
quip[i] = H.in.nextWord();
}
for (int i=len-1 ; i>=0 ; --i)
{
H.out.print( quip[i] + “ ” );
}
i is 0
i is 2
i is 1
i is 4
i is 3
Arrays in pictures
H.out.println(“Type the number of words:
int len = H.in.nextInt();
String[] quip;
quip = new String[len];
”);
5
int len
quip[1]
quip[3]
quip[0]
quip[2]
quip[4]
String[]
quip
for (int i=0 ; i<len ; ++i)
{
quip[i] = H.in.nextWord();
}
for (int i=len-1 ; i>=0 ; --i)
{
H.out.print( quip[i] + “ ” ); i is 4
i is 3
i is 1
i is 0
i is 2
}
fall leaves after leaves fall
Strings vs. Arrays
String s;
// input the String here
for (int i=0 ; i<s.length() ; ++i)
{
H.pl(s.charAt(i));
}
double[] A = new double[5];
// input the doubles here
for (int i=0 ; i<A.length ; ++i)
{
H.pl(A[i]);
}
differences?
Download