SNU OOPSLA Lab. 1 Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java...

53
1 SNU OOPSLA Lab. Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java Ch 1: The World Wide Web Ch 2: Watch out: Here comes Java Ch 3: Numerical computation & Function Ch 4: Subroutines & Databases Ch 5: Graphics Ch 6: Simulation Ch 7: Software engineering Part 2 Understanding what a computer is and how it works Ch 8: Machine architecture Ch 9: Language translation Ch 10: Virtual Environment for Computing Ch 11: Security, Privacy, and Wishful thinking Ch 12: Computer Communication Part 3 Advanced topics Ch 13: Program Execution Time Ch 14: Parallel Computation Ch 15: Noncomputability Ch 16: Artificial intelligence

Transcript of SNU OOPSLA Lab. 1 Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java...

1SNUOOPSLA Lab.

Great Ideas of CS with Java

   Part 1 WWW & Computer programming in the language Java              Ch 1:  The World Wide Web           Ch 2:  Watch out: Here comes Java            Ch 3:  Numerical computation & Function            Ch 4:  Subroutines & Databases            Ch 5:  Graphics            Ch 6:  Simulation            Ch 7:  Software engineering         Part 2 Understanding what a computer is and how it works            Ch 8:   Machine architecture            Ch 9:   Language translation            Ch 10: Virtual Environment for Computing           Ch 11: Security, Privacy, and Wishful thinking           Ch 12: Computer Communication      Part 3 Advanced topics             Ch 13: Program Execution Time            Ch 14: Parallel Computation            Ch 15: Noncomputability            Ch 16: Artificial intelligence 

2SNUOOPSLA Lab.

Ch6. Simulation

Copyright © SNU OOPSLA Lab.

3SNUOOPSLA Lab.

Table of Contents What are computer simulations? A Simple simulation we can build

Building our simulation Building the model The next-step function Applying the drive method Look at the code

More simulations Plaque simulation Evolution simulation Fractal simulation

Other roles for simulations Summary

4SNUOOPSLA Lab.

What are computer simulations?

Want to use the software technology for predicting the future The sun is a certainly a complex system. What will happen to it in the future? Earthquake, Storm, Stock market growth, Safety of a new car….

Simulations attempt to answer the question "What will happen next" about complex systems.

Methods to study complex systems and understand how the systems operate.  Overall Idea

We first must know the basic facts about the system. These facts can be fed into a computer. Then a method to predict what is next can be written. (the next-setp function) After setting the simulation model in an initial state, keep applying the next-step f

unction

5SNUOOPSLA Lab.

The purpose of simulations Simulations allow us to observe a system as it goes through a

sequence of steps. It helps us understand the given system. If a simulation is built, then does not come up with known

behavior, then we know the original model has to be adjusted. It is often used to optimize design decisions

What happened if the wings of an aircraft are designed this way; what if they are designed that way……

6SNUOOPSLA Lab.

The methodology of a simulation

First we must devise a model that captures the essence of the system we want to study

It usually consists of equations or other ways to represent the relationships of the system

Next we develop a next-step function The input for this function will be a listing of the significant

information about the system at an instant of time The output will be the new state the model will change to after

a given interval of time. To begin, we set the model in an initial state, and apply the

next-step function repeatedly to discover future states

7SNUOOPSLA Lab.

Table of Contents What are computer simulations? A simple simulation we can build

Building our simulation Building the model The next-step function Applying the drive method Look at the code

More simulations Plaque simulation Evolution simulation Fractal simulation

Other roles for simulations Summary

8SNUOOPSLA Lab.

A simple simulation we can build

Suppose we wish to simulate an auto race that involves a curve. The auto is capable of accelerating and decelerating at 3 m/sec It has no maximum speed The lateral acceleration on the curve may not exceed

without the auto’s losing traction and sliding off the track The main goal is to find the strategy that gives us the fastest time The simulation will let us propose any strategy, then find out how it

works.

2sec/9m

9SNUOOPSLA Lab.

Figure 6.1

10SNUOOPSLA Lab.

Building our simulation

We must go through the three steps: Step 1: Build the model Step 2: Write the next-step function Step 3: Put the model in an initial state, then

keep applying the next-step function

11SNUOOPSLA Lab.

Building the model (1) Representing the model (state description)

The position of the car (x, y) coordinates (Represented as doubles)

Its velocity X velocity, Y velocity (Represented as doubles)

The state, represented as integers Running normally is 1, Skidding is 0 Crashed into the wall is -1, Successful completion is –2

The time t in seconds since the beginning of the race

12SNUOOPSLA Lab.

Building the model (2) Representing the race track

This is arbitrary, but we will use: The start and finish line are at x=500 The top of the track is at y=1800 The bottom of the track is at y=200 The far wall in the x direction is at x=2500 The dividing line in the middle is at y=1000 extending

from x=500 to x=2000

13SNUOOPSLA Lab.

Figure 6.2

14SNUOOPSLA Lab.

The next-step function

We will define a RaceCar class RaceCar.java

The next-step function, called drive,  will be a method in this class The drive method will know the state of the car

The state is its position, velocity and whether it is running, skidding or crashed

The drive method will compute the next state after 1 second of driving The drive method has two arguments

The acceleration we have decided to apply The turn radius

15SNUOOPSLA Lab.

import java.awt.*;import java.lang.*;

public class RaceCar{  protected double[] state = new double[10];    protected int amax;  protected int amin;

  public RaceCar()  {     state[1]=0;      //  x position     state[2]=0;      //  y position     state[3]=0;      //  x velocity     state[4]=0;      //  y velocity     state[5]=1;      //  x direction     state[6]=0;      //  y direction     state[7]=1;      //  1 if not skidding; 0 if skidding  (control)     state[8]=0;      //  0 not complete; 1 crashed into wall; 2 complete     state[9]=0;      //  time     amax=9;     amin=-amax;  }    public int state()  {    return (int)state[8];  }

  public void setPos(int xPos,int yPos)  {    state[1]=xPos;    state[2]=yPos;  }

    public int getXPos()  {     return (int)state[1];    }    public int getYPos()  {     return (int)state[2];  }    public int getXVelocity()  {    return (int)state[3];  }    private void state8(int x0,int y0,int x1,int y1)  {    if (x1>2500)      state[8]=1;    if ((y1>1800) ||(y1<200))      state[8]=1;    if ( (y0>1000) && (y1<1000) )      if(x1<2000)        state[8]=1;    if ( (y0<1000) && (y1>1000) )      if(x1<2000)        state[8]=1;                if ((y1<1000) && (x1<500))      state[8]=2;  }

RaceCar.java

16SNUOOPSLA Lab.

  public void drive(int a,double tr)  {   double xold,yold,vel,le,le2;   xold=0;   yold=0;   double slopepathi,slopepathf,sloperadiusi,sloperadiusf;   double xnew,ynew,signe;   double xcenter=0;   double ycenter=0;   int flagpathi,flagradiusi,flagpathf,flagradiusf;   int LEFT, RIGHT;   double turndir;   int gvar, gtvar;   turndir = 0;   LEFT = -1; RIGHT = 1;   state[9] = state[9] + 1;   if (state[8] == 0) //if the boundaries have not been violated then   {        if (state[7] == 0)   //if control has been lost        {           xold = state[1];           yold = state[2];           state[1] = state[1] + state[3];           state[2] = state[2] + state[4];        }        else  //when control has not been lost        {           if (a > amax)              a = amax;           if (a < amin)              a = amin;           xold = state[1];           yold = state[2];           state[3] = state[3] + state[5]*a;           //update velocity           state[4] = state[4] + state[6]*a;           

           vel = java.lang.Math.sqrt(state[3]*state[3] + state[4]*state[4]);           state[1] = state[1] + state[3];             //update position           state[2] = state[2] + state[4];           if (tr != 0)                  //if the car is turning           {              if ( ((vel*vel)/java.lang.Math.abs(tr)) > amax) //check to see              {                                        // if control is lost                  state[7] = 0;                 tr = 0;              }           }           if (tr != 0)                  //if turn is to be made           {                 if ( java.lang.Math.abs(state[1]-xold) < 0.001 ) //find                 {                                  // slope of path (initial)                        flagpathi = 1;                        flagradiusi = 0;                        slopepathi = 1000;                        sloperadiusi = 0;                 }                 else                 {                        flagpathi = 0;                        slopepathi = (state[2]-yold)/(state[1]-xold);                 }                 if ((java.lang.Math.abs(slopepathi) < 0.001) &&                       (flagpathi==0))                 {//find slope of radius (initial)                        flagradiusi = 1;                        sloperadiusi = 1000;                 }                 else                 {                        flagradiusi = 0;                        sloperadiusi = -1/slopepathi;                 }

RaceCar.java

17SNUOOPSLA Lab.

                 if (tr < 0) {                   turndir = LEFT;                 }                 else {                   turndir = RIGHT;                 }                                  if ( (flagpathi == 0) && (flagradiusi == 0) ) {                    if (slopepathi > 0)                      signe = 1;                     else                       signe = -1;                    if ((xold-state[1]) > 0)                       signe = -signe;                                          xcenter = xold +                       signe*turndir*java.lang.Math.sqrt((tr*tr)/                       (1 + sloperadiusi*sloperadiusi));                    ycenter = yold + sloperadiusi*(xcenter-xold);                           //compute position of center of turning circle                 }                                  if ((flagpathi == 1) && (flagradiusi == 0)) {                    if (state[2] > yold)                    {                       xcenter = xold + tr;                       ycenter = yold;                    }                    else                    {                       xcenter = xold - tr;                       ycenter = yold;                    }                 }

                 if ((flagpathi == 0) && (flagradiusi == 1) )                 {                    if (state[1] > xold)                     {                       xcenter = xold;                       ycenter = yold - tr;                    }                    else                    {                       xcenter = xold;                       ycenter = yold + tr;                    }                 }                 le2 = (state[1]-xcenter)*(state[1]-xcenter)  +                           (state[2]-ycenter)*(state[2]-ycenter);                 le = java.lang.Math.sqrt(le2);  //find distance from center                                                 // to position                 xnew = xcenter+(java.lang.Math.abs(tr)/le)*(state[1]-xcenter);                 ynew = ycenter+(java.lang.Math.abs(tr)/le)*(state[2]-ycenter);                              //find new position                 if (java.lang.Math.abs(state[1]-xcenter) < 0.001)//find                                                      // slope - radius(final)                 {                    flagpathf = 0;                    flagradiusf = 1;                    slopepathf = 0;                    sloperadiusf = 1000;                 }                 else                 {                    flagradiusf = 0;                    sloperadiusf = (state[2]-ycenter)/(state[1]-xcenter);                 }

RaceCar.java

18SNUOOPSLA Lab.

                 if ((java.lang.Math.abs(sloperadiusf)<0.001)&&                      (flagradiusf==0))                              //find slope of path (final)                 {                    flagpathf = 1;                    slopepathf = 1000;                 }                 else                 {                    flagpathf = 0;                    slopepathf = -1/sloperadiusf;                 }                 state[1] = xnew;                 state[2] = ynew;                 if ( (flagpathf == 0) && (flagradiusf == 0))                 {                    if ((sloperadiusf*turndir) > 0)                       signe = 1;                    else                       signe = -1;                    if ((state[1]-xcenter) < 0)                       signe = -signe;                    state[3] = signe*java.lang.Math.sqrt((vel*vel)/                                                  (slopepathf*slopepathf+1));                    state[4] = slopepathf*state[3];                    state[5] = signe*java.lang.Math.sqrt(1/                                                  (slopepathf*slopepathf+1));                    state[6] = slopepathf*state[5];                 }

                 if ((flagpathf == 0) && (flagradiusf == 1))                 {                    if ( ((ycenter-ynew)*(0-turndir)) > 0 )                       signe = 1;                    else                       signe = -1;                    state[3] = vel * signe;                    state[4] = 0;                    state[5] = signe;                    state[6] = 0;                 }                 if ((flagpathf == 1) && (flagradiusf == 0))                  {                    if ( ((xcenter-xnew)*turndir) > 0 )                       signe = 1;                     else                       signe = -1;                    state[3] = 0;                    state[4] = vel * signe;                    state[5] = 0;                    state[6] = signe;                 }              }           }        }        state8((int)xold,(int)yold,(int)state[1],(int)state[2]); //check state8   } // end of drive method} // end of RaceCar class

RaceCar.java

19SNUOOPSLA Lab.

Applying the drive method The first argument:  acceleration(a)

This is a double It may not exceed 3 it can be positive or negative

Negative is applying the brake The second argument: turning(tunr)

A positive double means turn right, negative means turn left, zero means go straight

the radius of the turn is indicated by the size of the number Example: turnr = 100 means turn right in an arc with a center that has a distanc

e of 100 meters from the vehicle A call to the drive method looks like this

auto.drive(a, turnr) where a and turnr have been given values and auto is an object of type RaceCar

20SNUOOPSLA Lab.

Look at the code (1/2) Here are the three files you will need

Race.java RaceTrack.java RaceCar.java

To run simulation, execute the following file Race.html

init method in Race class sets up the screen

21SNUOOPSLA Lab.

Figure 6.3

22SNUOOPSLA Lab.

Look at the code (2/2) actionPerformed method in Race class

Initialize variables Puts car in x,y position Sets acceleration to max (3) Sets turn to zero

Four while loops Drive straight for ten seconds at max acceleration Reset acceleration, drive until turn Reset turn, drive until through turn Reset turn and acceleration, drive to finish

Probably the only thing that should be changed is the actionPerformed method in the Race class

23SNUOOPSLA Lab.

import java.awt.*;import java.lang.*;import java.awt.event.*;

public class Race extends java.applet.Applet                          implements Runnable, ActionListener{

  Button startB;  RaceTrack track;  Thread animator = null;  RaceCar auto1;  Label outLabel;  int x=0;  int y=0;  int time;  double sec;   /**  * Defines a new thread.  Threads are being used to   * slow the animation  */  public void start()  {      animator = new Thread(this);      animator.start();  }   /**  * Tells the track to update with new coordinates and puts  * the thread to  sleep for sec seconds  */  public void run()  {     x=auto1.getXPos();

         y=auto1.getYPos();     track.draw(x,y);     int temp = (int)(1000 * sec);     try {Thread.sleep(temp);} catch (InterruptedException e)       {System.out.println("error in thread sleep");}  }  

  /**  * Makes the current thread null  */    public void stop()  {    animator = null;  }    /**  * Variables initialized and objects added to the applet  */  public void init()  {    outLabel=new Label ("                         ");    track = new RaceTrack();    startB = new Button("Start");    startB.addActionListener(this);    track.setSize(600,400);    add(track);    add(startB);    add(outLabel);    track.repaint();    sec=.1;              //sec * 1 equals delay after each move in seconds                            //sec = 1 creates a 1 second delay,                            //sec = .5 creates a half second delay, etc  }

Race.java

24SNUOOPSLA Lab.

  /**  * Button handler method  */  public void actionPerformed(ActionEvent event)  {    Object cause = event.getSource();    if (cause == startB)     {       auto1=new RaceCar();       outLabel.setText(" ");       x=500;                      //Initial x coordinate       y=1400;                     //Initial y coordinate       start();                    //Begins thread       auto1.setPos(x,y);          //Sets the car to start position       time=1;                      int a=3;                    int turnr=0;              //complete() has been defined below       while ((time<=11) && ( !complete() ))       {         auto1.drive(a,turnr); //tells car acceleration and turn radius,                                   //  must be followed by run()         run();                    //Updates graphics on screen         time=time+1;              //Increment the simulation time       }

       a=0;       while ((x<1700) && ( !complete() ) )         {         auto1.drive(a,turnr);         run();         time=time+1;       }

       turnr=400;       while ( ((x>1700) || (y>1000)) && ( !complete() ) )       {         auto1.drive(a,turnr);         run();         time=time+1;       }

       a=3;       turnr=0;       while (!complete())       {         auto1.drive(a,turnr);         run();         time=time+1;       }         stop();                     //Makes thread null       output();                   //Prints output of race to screen    }  }

  /**  * Outputs the result of the race to the screen  */  private void output()  {    String tempString = new String();    if ((complete()) && (!finish()))      tempString = "Crashed ";    else       tempString = "Finished ";    Integer tempInt = new Integer(time-1);    tempString=tempString + "in " +tempInt.toString() +" seconds";    outLabel.setText(tempString);  }

Race.java

25SNUOOPSLA Lab.

  /**  * Returns true if the race is over (crash or finished) and false if it  * is still going  */  private boolean complete()  {    if (auto1.state()>0)      return true;    return false;  }    /**  * Returns true if race successfully finished or false if crash resulted  * Assumes that complete() is true. If complete is not true, procedure  * returns false  */  private boolean finish()  {    if (complete())      if (auto1.state()==2)        return true;      else        return false;    return false;  }}

Race.java

26SNUOOPSLA Lab.

 import java.awt.*;

 public class RaceTrack extends Canvas{  private Image offscreen = null;        private Graphics dbuffer;  int imagewidth,imageheight;

  /**  * Constructor - no variables need to be initialized  */  public RaceTrack()  {      }    /**  * Converts the simulations x coordinate to a screen x coordinate  */  private int screenX(int x)  {    Integer temp = new Integer(x/5);    return temp.intValue();  }

 /**  * Converts the simulations y coordinate to a screen y coordinate  */  private int screenY(int y)  {    Integer temp = new Integer((1800-y+200)/5);    return temp.intValue();  }

    public void draw(int x,int y)  {    Dimension d = this.getSize();

    if((offscreen == null) || ((imagewidth!=d.width)||(imageheight!=d.height)))

    {offscreen = this.createImage(d.width,d.height);imagewidth = d.width;imageheight = d.height;

    }    Graphics g = offscreen.getGraphics();    paint(g,x,y);    g = this.getGraphics();    g.drawImage(offscreen,0,0,this);

  }  public void paint(Graphics g,int x,int y)  {          g.setColor(Color.white);      g.fillRect(0,0,600,600);          g.setColor(Color.green);      g.drawLine(100,40,100,360);//start/finish            g.setColor(Color.black);      g.drawLine(100,360,500,360);//bot wall      g.drawLine(100,200,400,200);//mid wall      g.drawLine(100,40,500,40);//top wall      g.drawLine(500,40,500,360);//right wall            g.fillOval(screenX(x),screenY(y),15,15);      }}  

RaceTrack.java

27SNUOOPSLA Lab.

Table of Contents What are computer simulations? A simple simulation we can build

Building our simulation Building the model The next-step function Applying the drive method Look at the code

More simulations Plaque simulation Evolution simulation Fractal simulation

Other roles for simulations

28SNUOOPSLA Lab.

Plaque Simulation: To avoid the Disaster(1/7)

We would like to know How many people will be affected How long the disease will be a problem How preventative and curative efforts may

change the situation

29SNUOOPSLA Lab.

Step 1 for the simulation Devise a model of a set of individuals and the

characteristics of the disease Model a set of individuals as a linear array Place a 0 if a person is well and a positive integer if the person

become infected Positive integer indicates the number of days since the person

was infected The model works as follows:

All individuals start in the well condition with zeros and One individual is infected by placing a 1

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Plaque Simulation: To avoid the Disaster(2/7)

30SNUOOPSLA Lab.

Step 1 for the simulation The model works as follows:

Next, contagion rule randomly selects individuals near the currently ill individual and place 1’s

We then model the process of getting over the disease and having an immune period

- infectious: the number of days that the illness will last - immune: the number of days past the first day of

infection that an individual cannot be re-infected

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 2 0 1 1 0 0 0 0 0 3 0 2 0 1 0 0 1 0 0 1 0 1 0 1 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0

Plaque Simulation: To avoid the Disaster(3/7)

31SNUOOPSLA Lab.

Step 2 for the simulation Consider the next-step function (infect function)

Computation finding which individuals are infected by an existing ill individual

int [] infect(int [] B, int k, int rate){    int i, j;    i = 1;    while( i <= rate)    {         // select individual j to be infected and infect it        B[j] = 1;        i = i + 1;    }    return B;}

B: A linear array representing a set of individuals

k: indicates which individual is doing the infecting

rate: the number of individuals who will be infected

Plaque Simulation: To avoid the Disaster(4/7)

32SNUOOPSLA Lab.

Step 2 for the simulation How to compute j each time a new individual is to

be infected We do this by calculating a pseudo random number

computed by a carefully designed functionint [] infect(int [] B, int k, int iInc, int rate){    int i, j;    i = 1;    while( i <= rate)    {         // select individual j to be infected and infect it        iInc = ((iInc * 23) % 31);        j = k + iInc – 13;        if((j > 0) && (j <= 60))            B[j] = 1;        i = i + 1;    }    return B;}

Plaque Simulation: To avoid the Disaster(5/7)

33SNUOOPSLA Lab.

Example of the simulation Infectious = 3; immune = 7; rate = 3

Each sick individual(*); each immune individual(1); all others(0)

Plaque Simulation: To avoid the Disaster(6/7)

34SNUOOPSLA Lab.

Example of the simulation A statistical summary of what happened over the nine days

Plaque Simulation: Avoiding the Disaster(7/7)

35SNUOOPSLA Lab.

Evolution Simulation (1/5)

Let’s watch the beings survive in the world and evolve to become a better species

Create an 8 by 8 grid The beings(called neds) can wander the grid looking for food and mating Each ned must eat (age/20) +1 units of food per day

Age: the number of days the ned has lived Three more rules related to food

Each grid square starts with some food units and has more food units added to it each day (controlled by a random number generator)

The ned can pick up as many as three units of food per day if these are available and put them in a bag

The ned uses up two extra units of food if it mates

36SNUOOPSLA Lab.

Evolution Simulation (2/5) The activities of a ned on a given day

First, to eat the required amount Second, to make a move given by one of the following activity codes

00  Move forward one step (if a wall is hit, stand still) 01  Rotate right by one-eight of a full turn 10  Mate (if a member of the opposite sex is present in the same square) 11  Mate (if a member of the opposite sex is present in the same square)

Each ned is created with its DNA(40-bit binary string of digits)

0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 01 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1

1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

parents

child

i) A random integer i is selected between 1 and 40 (in this case, 17)ii) The first i bits in one parent are combined with the bits starting from i+1 on to the end for the other parent

37SNUOOPSLA Lab.

Evolution Simulation (3/5) Each ned’s behavior is governed by its DNA

The preceding child ned would follow these actions on its first few days: Day 1  00  move forward one step;  Day 2  00  move forward one step Day 3  01  rotate Day 4  00  move forward one step Day 5  00  move forward one step Day 6  11  mate

Fig. 6.4

38SNUOOPSLA Lab.

Evolution Simulation (4/5)

Let’s run the simulation First, create four neds, two males and two females, with random geneti

c codes Place them in random cells and watch what happens in the first three d

ays (Fig. 6.5) “*” means an attempted mate “$” means death from lack of food

However, the simulation goes only a few steps There are not enough of neds to find each other and mate

39SNUOOPSLA Lab.

Fig. 6.5

40SNUOOPSLA Lab.

Evolution Simulation (5/5)

Let’s start again with 20 neds Simulation lasts for about 100 days and then dies out

We try several similar simulations and one goes for 491 days These neds must have a fairly good genetic code to have survived suc

h a long time

1 0 0 0 0 0 1 0 1 1 0 1 1 1 0 1 1 0 1 0 0 1 0 0 1 0 0 1 0 0 1 1 0 1 1 1 1 0 1 0

Example of a good genetic code

41SNUOOPSLA Lab.

Fractal Simulation: What will it look like? (1/6)

With computers, we can create an object not existing in reality

Example: the design of buildings A program allowing us to enter a new world of both

recreational and scientific interest It is the world of fractals It is a study that could not progress practically until

computer graphics came into existence

42SNUOOPSLA Lab.

A point transformation on the geometric plane f (transformation function)

f((x,y)) to be (x1, y1), where x1 = x*x – y*y + c1 y1 = 2*x*y + c2 c1 and c2 are constants

If we choose c1=0.36 and c2=0.10 (Fig. 6.7)f(0,0) = (0.36, 0.10)f(0.36,0.10) = (0.48, 0.17)f(0.48,0.17) = (0.56, 0.27)f(0.56,0.27) = (0.60, 0.40)f(0.60,0.40) = (0.57, 0.58)f(0.57,0.58) = (0.34, 0.76)f(0.34,0.76) = (-0.10, 0.62)f(-0.10,0.62) = (-0.01, -0.02)f(-0.01,-0.02) = (0.36, 0.10)f(0.36,0.10) = (0.48, 0.17)…

- This sequence has the property that it bends back on itself

- The last two points are, in fact, the same as the first two points

Fractal Simulation: What will it look like? (2/6)

43SNUOOPSLA Lab.

Fig. 6.7

44SNUOOPSLA Lab.

In the theory of fractals, We are interested in points having converging sequences

like (0,0) We are also interested in diverging sequences

Example: Sequence with beginning point(0.70, 0.70)

f(0.70, 0.70) = (0.36, 1.08)f(0.36, 1.08) = (-0.68, 0.88)f(-0.68, 0.88) = (0.05, -1.09)f(0.05, -1.09) = (-0.83, 0.00)f(-0.83, 0.00) = (1.04, 0.10)f(1.04, 0.10) = (1.43, 0.31)f(1.43, 0.31) = (2.32, 1.00)f(2.32, 1.00) = (4.75, 4.72)f(4.75, 4.72) = (0.69, 44.97)f(0.69, 44.97) = (-2021.58, 62.48)f(-2021.58, 62.48) = (4,082,867.15, -252,602.95 …

Fractal Simulation: What will it look like? (3/6)

45SNUOOPSLA Lab.

Fig. 6.8

46SNUOOPSLA Lab.

Let’s color black all points resulting in converging sequences and color white all points resulting in diverging sequences Example

(0,0) black and (0.70, 0.70) white

Fig. 6.9

Fractal Simulation: What will it look like? (4/6)

47SNUOOPSLA Lab.

What figure will result if we apply this rule to the coloring of all the points on the plane ?

c1 = 0.360284; c2 = 0.100376 recommended by Robert L. Devaney in his book ”Chaos, Fractals, and Dynamics”

Fig. 6.10

- This figure is known as a fractal

- By varying the constants c1 and c2, one can obtain an astounding variety of complex figures

Fractal Simulation: What will it look like? (5/6)

48SNUOOPSLA Lab.

Coding theory A possible application for fractals How many bits need to be sent to convey a picture like Fig.

6.10? Possibly millions of bits of information will be needed

But, how much information is needed to transmit the equations for the generation of the figure?

Possibly only a few hundred Figure could be regenerated from the equations at the

receiving end

Fractal Simulation: What will it look like? (6/6)

49SNUOOPSLA Lab.

Table of Contents What are computer simulations? A simple simulation we can build

Building our simulation Building the model The next-step function Applying the drive method Look at the code

More simulations Plaque simulation Evolution simulation Fractal simulation

Other roles for simulations Summary

50SNUOOPSLA Lab.

Other roles for simulations

Economy forecast Policy (e.g. birth control) validity check Marketing strategy check Camera Lenses Virtual Reality Simulation in Microelectronics

Logic Layout Circuit Process

Design and Manufacturing

51SNUOOPSLA Lab.

Table of Contents What are computer simulations? A simple simulation we can build

Building our simulation Building the model The next-step function Applying the drive method Look at the code

More simulations Plaque simulation Evolution simulation Fractal simulation

Other roles for simulations Summary

52SNUOOPSLA Lab.

Summary

Computer simulations are very useful in the real world

Trying out an idea in a very cheap way The building of a structure The playing of a symphony The exploding of SUN

3 basic steps of simulation Build a mathematical model and the next-step function Put the system in the initial state Keep applying the next-step function

53SNUOOPSLA Lab.

Ch 6: Simulation Text Review Time