290 likes | 706 Views
컴퓨터 시뮬레이션 개론. 제 3 강 Random Number and Random Variate Generation 김한수 교수. 이번 시간까지 “ 반드시 ” 했어야 하는 일. Homework: Monte-Carlo Simulation 숙제 Simulation Terms 학습 시뮬레이션 분야에서 사용되는 용어 (Term) 이해한 후 암기 Discrete Event Simulation 원리 이해. Simulation Application ( 응용 모델 ) state variables
E N D
컴퓨터 시뮬레이션 개론 제 3 강 Random Number and Random Variate Generation 김한수 교수
이번 시간까지 “반드시” 했어야 하는 일 • Homework: • Monte-Carlo Simulation 숙제 • Simulation Terms 학습 • 시뮬레이션 분야에서 사용되는 용어(Term) 이해한 후 암기 • Discrete Event Simulation 원리 이해
Simulation Application (응용 모델) • state variables • code modeling system behavior • I/O and user interface software calls to schedule events calls to event handlers • Simulation Engine (시뮬레이션 엔진) • Future event list management • managing advances in simulation time (REVIEW!)Discrete Event Simulation System 순수하게 실시스템만을 묘사한 모델 시뮬레이션 응용모델과 독립적인 재사용 가능한 시뮬레이션 엔진
Event handler procedures state variables Departure Event { … } Arrival Event { … } Landed Event { … } Integer: InTheAir; Integer: OnTheGround; Boolean: RunwayFree; Simulation application Simulation engine Event processing loop While (simulation not finished) E = smallest time stamp event in FEL Remove E from FEL Now := time stamp of E call event handler procedure Now = 8:45 Future Event List (FEL) 9:00 10:10 9:16 (REVIEW!) Event-Oriented World View
Arrival Event: • 비행기가 새로 공항 상공에 도착했다. 만약 활주로가 비어있으면, 착륙을 시작하고, 그렇지 않으면, 선회하면서 활주로를 사용할수 있을때 까지 기다린다. (New aircraft arrives at airport. If the runway is free, it will begin to land. Otherwise, the aircraft must circle, and wait to land) • R = time runway is used for each landing aircraft • G = time required on the ground before departing • Now: current simulation time • InTheAir: number of aircraft landing or waiting to land • OnTheGround: number of landed aircraft • RunwayFree: Boolean, true if runway available Arrival Event: InTheAir := InTheAir+1; If (RunwayFree) RunwayFree:=FALSE; Schedule Landed event @ Now + R;
Landed Event: • 비행기가 착륙을 끝마친 사건.(An aircraft has completed its landing) • R = time runway is used for each landing aircraft • G = time required on the ground before departing • Now: current simulation time • InTheAir: number of aircraft landing or waiting to land • OnTheGround: number of landed aircraft • RunwayFree: Boolean, true if runway available Landed Event: InTheAir:=InTheAir-1; OnTheGround:=OnTheGround+1; Schedule Departure event @ Now + G; If (InTheAir>0) Schedule Landed event @ Now + R; Else RunwayFree := TRUE;
Departure Event: • 착륙한 비행기가 일정 시간이 지난 뒤 새로운 목적지로 출발(An aircraft now on the ground departs for a new destination.) • R = time runway is used for each landing aircraft • G = time required on the ground before departing • Now: current simulation time • InTheAir: number of aircraft landing or waiting to land • OnTheGround: number of landed aircraft • RunwayFree: Boolean, true if runway available Departure Event: OnTheGround := OnTheGround - 1;
State Variables R=3 G=4 InTheAir 0 1 1 0 2 1 0 OnTheGround 0 1 2 RunwayFree true false true 0 1 2 3 4 5 6 7 8 9 10 11 Simulation Time Processing: Arrival F1 Arrival F2 Landed F1 Landed F2 Depart F1 Depart F2 Time Event Time Event Time Event Time Event Time Event Time Event Time Event 1 Arrival F1 3 Arrival F2 3 Arrival F2 4 Landed F1 4 Landed F1 7 Landed F2 8 Depart F1 8 Depart F1 11 Depart F2 11 Depart F2 Now=0 Now=1 Now=3 Now=4 Now=7 Now=8 Now=11 Execution Example
Analysis of DES Engine written in JAVA(자바로 만들어진 시뮬레이션 엔진 분석 EventCalender EventRecord Simulator -------------- Private EventCalendar _eventCal; Private Clock _clock; Private Domain _Domain -------------- simulate(); getTimeNow(); getNextEventTime(); schedEvent(time, ASO, Ehandler) complete(); Domain -------------- Private Simulator _Simulator Other simulation objects -------------- Init(Simulator sim); //initialize simulation objects SimulationManager -------------- Private Simulator _Simulator Private Domain _Domain -------------- main();Init(); //initialization of Simulator and domain runSimulation(); complete(); simulationObject1 simulationObject2 main() { SimulationManager manager = new SimulationManager(); manager.init(); manager.runSimulation(); manager.complete(); }
Today’s Topic • Random Number Generation • Linear Congruential Generators (LCG) • Random Variate Generation • UniformAB( ) • Expon( ) • StdNormal( ) • Normal( )
How to generate RN is very important! • Properties that “Good” RNG should possess (좋은 RNG의 특성들) • Should be distributed uniformly on [0,1], no correlation; otherwise simulation results may be invalid • Should be fast to calculate and need small memory • Should be reproducible • For debugging or verification • For comparison models with same conditions • Should produce several separate “streams” of random numbers
Linear Congurential Generators • By Lehmer (1951)
Example: LCG Finding good a, m, c is important!!!
Java Code for RNG • * Generates uniformly random numbers in the interval [0,1]. • * It uses the recursion: IX=16087 * IX mod (2^31 - 1) • * using only 32 bits including the sign. • * • * Code modified from UNIF in Bratley, Fox, and Schrage (1987), • * A Guide to Simulation (2/e), Springer-Verlag, New York, p. 331. • public double getNextRandom() • { • long k1; • k1 = _seed /127773; • _seed = 16807 * (_seed - k1 * 127773) - k1 *2836; • if (_seed < 0) • _seed = _seed + 2147483647; //twos-complement if negative • double value = _seed * 4.656612875E-10; //scale to [0, 1] interval • return value; • }
Java Programming 사용법 • Eclipse • Test with Uniform01.java Uniform01.java는 Class webpage에 있음!
다음 공격 목표는? (Next Topic) • Random Variate Generation • UniformAB( ) • Expon( ) • StdNormal( ) • Normal( )
Random Variate Generation • Random variate: • A random variate is an artificially generated random variable. • 특정 분포를 따르는 확률변수치를 만들어 내는 법 • 예: 은행에서 고객의 서비스 시간이 평균 5분인 지수 분포를 할때, 어떻게 이 분포를 만들어 낼 것인가?
UnifomAB Distribution • RNG Uniform01이 주어졌을때, • [A,B] 사이의 균등한 분포를 어떻게 만들수 있을까? • 해법: • Let R := uniform01.getNextRandom() • Return ( R*(B-A) + A )
Exponential Distribution (지수분포) PDF: 확률밀도함수 평균과 분산 CDF: 누적확률분포함수
1.7429=-ln(1-0.825) 0.5499 =-ln(1-.423) Expon() • Use Inverse Transform Technique 0.845 When = 1명/1분=1 0.423
Weibull Distribution PDF: 확률밀도함수 Random Variate Generation: CDF: 누적확률분포함수
이 이외에도.. • (표준)정규분포 • 포아송분포 • 기하분포 • 감마분포 • 베타분포 • 등등…
Java Source Code 분석 (Uniform01) • /** • * FILENAME: Uniform01.java<BR> • * • * Generates uniformly random numbers in the interval [0,1]. • * It uses the recursion: IX=16087 * IX mod (2^31 - 1)<br> • * using only 32 bits including the sign. • * <p> • * Code modified from UNIF in Bratley, Fox, and Schrage (1987), • * <i>A Guide to Simulation (2/e)</i>, Springer-Verlag, New York, p. 331. • * <p> • * <pre> • * input: • * _seed...an integer random number from a stream of numbers • * great than 0 and less than 2147483647 (2^31 - 1) • * <p> • * output: • * _seed = new seed value • * </pre> • * • * @author David A. Thurman • * @author Alan R. Chappell • * @author Christine M. Mitchell • * @author Hansoo Kim<BR> • * Center for Human-Machine Systems Research<BR> • * School of Industrial and Systems Engineering<BR> • * Georgia Institute of Technology<BR> • * • * Last Updated: February 1998 • */ • public class Uniform01 • { • private long _seed = 0; • /** • * Sets the internal seed in this class for use with • * random number generator. • */ • public Uniform01(long seed) • { • _seed = seed; • } • /** • * Returns the next value from a Uniform distribution on [0, 1]. • */ • public double getNextRandom() • { • long k1; • k1 = _seed /127773; • _seed = 16807 * (_seed - k1 * 127773) - k1 *2836; • if (_seed < 0) • _seed = _seed + 2147483647; //twos-complement if negative • double value = _seed * 4.656612875E-10; //scale to [0, 1] interval • return value; • } • /** • * Returns current seed value. • */ • public long getSeed() • { • return _seed; • } • /** • * Resets seed value. • */ • public void reset(long newSeed) • { • _seed = newSeed; • } • /** • * Returns string representation of Uniform01. • */ • public String toString() • { • return new String("Uniform01[ seed: " + _seed + " ]"); • } • /** • * Test routine. • */ • public static void main (String argv []) • { • Uniform01 rng1 = new Uniform01((long)123456); • Uniform01 rng2 = new Uniform01((long)4567123); • for (int i = 0; i < 8; i++) • System.out.println( • rng1.toString() + " = " + rng1.getNextRandom() + " " + • rng2.toString() + " = " + rng2.getNextRandom()); • } • } // end class Uniform01
주요 Method • Creation: • Uniform01 rng1 = new Uniform01((long)123456); • Uniform01분포를 만들어 내는 rng1이라는 Object를 생성하고, 그때 초기 Seed값을 123456을 사용한다. • 사용법 • rng1.getNextRandom(); • getNextRandom()을 부르면, 새로운 Random 값이 리턴 된다.
HW #3 • Java Programming 환경 (Eclipse) 설치 • 홈페이지에서 “Source Code”다운 로드 • 각 소스코드 분석 (Uniform01(), Expon(), StdNormal()) • Main() 고치기 • Seed := 123456으로 10개의 random variate를 발생해서 프린트 하는 프로그램 • Expon -> 평균:= 5 • UniformAB()와 Normal() 코딩 • UniformAB<-Uniform01 • Normal(u,s)<-StdNormal
다음 공격 목표는? (Next Topics) 그동안 힘들었지? 좀 쉬었다 갈까? 그럼 시험 보는 건 어때? 에구.. 다음시간에 자바 소스코드 공부하고, 그 다음주에 시험 보자! 좋지?
다음시간까지 꼭 할일! • RNG, RVG 복습할것! • Java 프로그램밍 환경 구축 • Uniform01 코드 분석 • 다른 분포함수도 분석 • 숙제 시작하기->다음시간에 질문! • 그 다음주 시험! • 시험문제 다음주에 알려줌! –친절한 교수님!-