ppt - Duke Computer Science
Download
Report
Transcript ppt - Duke Computer Science
Today’s topics
Java Applications
Simulation
Upcoming
Software Engineering (Chapter 7)
Reading
Great Ideas, Chapters 6
CompSci 001
19.1
What does it mean to be human?
Tool User?
Speech?
Some animals (whales?) seem to communicate by sound
Do simulations?
Some animals use tools
???
Many things we do could be called simulations
Drawing a diagram of something to build
Using a map to give directions
Moveable furniture cutouts on a floor plan
CompSci 001
19.2
Computer Simulation
As suggested before, can simulate without
computer
Computer greatly extends the domain
What are some of the things made possible by
computer simulation?
Early efforts:
Nowadays almost everything built is simulated first
Fancy camera lenses among first beneficiaries
Efficient paths for space ships
Population projections in relation to birth control policies
Let’s use the computer to find solution to simple
problem
CompSci 001
19.3
Dog Lot Fence
Optimize:
i.e., give your dog the biggest lot in the face of constraints
Build lot against side of house
Fixed length roll of fencing (and posts)
Rectangular layout
x
y
Length of fence is
2*x + y
Use program to try different values of x and y
Better than actually trying many layouts with a posthole digger!!
CompSci 001
19.4
Fence
public class Fence extends java.applet.Applet implements
ActionListener {
TextField mInstruct;
Label lLength;
DoubleField gLength;
Button bSimulate, bDisplay;
TextArea mResults;
int k;
public void init() {
lLength = new Label("Length");
mInstruct = new TextField(70);
mInstruct.setText(
"Enter length of fence, the press Simulate or Display");
gLength = new DoubleField(10);
bSimulate = new Button("Simulate");
bDisplay = new Button("Display");
mResults = new TextArea(25,60);
CompSci 001
19.5
Fence.2
bSimulate.addActionListener(this);
bDisplay.addActionListener(this);
add(mInstruct); add(lLength); add(gLength);
add(bSimulate); add(bDisplay); add(mResults);
}
public void actionPerformed(ActionEvent event) {
Object cause = event.getSource();
double fenceLength;
if (cause == bSimulate) {
fenceLength = gLength.getDouble();
fenceTable(fenceLength);
}
if (cause == bDisplay) {
fenceLength = gLength.getDouble();
fencePlot(fenceLength);
}
}
CompSci 001
19.6
Fence.3
void fenceTable(double fenceLength) {
double area, x, y;
x = 0.0;
y = fenceLength - 2.0 * x;
mResults.setText("Fence Optimization Table\n");
while (y >= 0.0) {
area = x * y;
mResults.append("x = " + x + " y = " + y +
" area = " + area + "\n");
x = x + 1.0;
y = fenceLength - 2.0 * x;
}
}
CompSci 001
19.7
Fence.4
void fencePlot(double fenceLength) {
double area, x, y;
x = 0.0;
y = fenceLength - 2.0 * x;
mResults.setText("Fence Optimization Plot\n");
while (y >= 0.0) {
area = x * y;
mResults.append(x+"\t"+plotString(area)+"\n");
x = x + 1.0;
y = fenceLength - 2.0 * x;
}
}
String plotString(double area) {
String s = "";
while (area > 0) { s = s + "*"; area = area - 5.0;}
return s;
}
}
CompSci 001
19.8
Fence Optimization
Output makes it clear how fence should be arranged
Note we eyeballed the output to get answer
Not necessarily intuitive (makes simulation useful)
(Can use other tricks -- non computer -- to get answer)
Could have had computer pick the maximum area
Could you sketch that program out?
Let’s use slightly different approach; answer not obvious
Fix area
Minimize amount of fencing used
Change scenario a bit
o Build into corner
o Put in a tree!
CompSci 001
19.9
Fence with Tree
x
2
y
tree (xt, yt)
y2
x
Program a bit more complicated
Will not go over details
However, intuitive methods not likely to work
Must use program to get right answer
Program is on-line
CompSci 001
19.10
Pitfalls in Automatic Methods
Optimization problems seem straightforward enough
May involve many variables
Not always the case
Exhaustively checking all possible values may take too long
Need to intelligently look for optimal solution
However, can have local maxima or minima
Can lead to wrong answer
Sometimes optimal Solution is computationally out of reach
Will come back to that theme at end of semester
CompSci 001
19.11
Simulation in Microelectronics
Modern microchips too complicated to be build without
simulation
One chip takes tens of thousands of dollar to make
Additional ones are almost free
One error and it’s useless
Each much too complex to check by hand
It take computers to build computers (recursion?)
Modern chips have millions of transistors
Every aspect of the process is simulated
Logic
Layout
Circuit characteristics
Fabrication Process
CompSci 001
19.12
Other Popular Simulation Targets
Games that are Simulations
Graphics
SimCity
Flight Simulator
Often serious simulation tools make interesting games
Many movies now use computer graphics
More and more are entirely graphics
o Not voices, though!
Pioneering: UNC Computer Science Walk-through
Virtual Reality
Headsets
Caves
CompSci 001
19.13
Other Popular Simulation Targets
Architecture
Artificial Aging
Computerized generation of suspect’s face
Beauty
Project what missing child would look like now
Police Work
Models
“Walk-through” extensions
Design your own kitchen
Your image with different hairdos, makeup, etc.
Your entry here:____________
CompSci 001
19.14