본문 바로가기
Python/Python Examples

[Python Examples] Simple Age-class Simulation

by Henry Cho 2023. 9. 29.
728x90

Simple Age-class Simulation 

포스트 난이도: HOO_Junior


# Example Codes

이번 포스트에서는 Simple Age-class Simulation를 통해서 timestep별 population 변화량을 살펴볼 수 있다. 아래의 예제를 토대로 경우의 수들을 붙여나가다가 보면 실제로 사용하는 모델링 시스템처럼 복잡한 나만의 시뮬레이션 모델을 만들어볼 수 있다. HOO_Junior의 포스트를 읽고 있는 브로라면, 이제는 코드 작성에서 벗어나 만들고자 하는 프로그래밍의 빠른 분석과 머릿속으로 구조화하는 연습을 해보는 것이 좋다. 많은 예제들을 더 살펴보면서 익혀나간다면 나중에는 머릿속으로 바로바로 그려지게 된다. 다만 연인에게는 "로봇 같다"라는 말을 들을 수 있기에 유의해야 한다.

 

(글쓴이가 그렇다....)


# Define initial population values (N_x from Table 9.1)
age_classes = [0, 1, 2, 3, 4, 5, 6]
initial_population = [0, 100, 200, 300, 200, 100, 50]

# Simulate for 12 years
years = 12
current_population = initial_population.copy()

# Print the header for the table
print(f"{'Year':<5}{'Age Class':<15}{'Population'}")

# Simulate and print population data for each year
for year in range(years + 1):
    total_population = sum(current_population)
    print(f"{year:<5}{'Total':<15}{total_population}")

    for age, population in zip(age_classes, current_population):
        print(f"{'':<5}{age:<15}{population}")

    # Calculate new population for the next year
    new_population = [0] * len(age_classes)

    # Calculate natality (births)
    # Assume a constant birth rate for age class 6
    births = current_population[-1] * 0.25
    new_population[0] = births

    # Calculate survival for other age classes
    # Assume a constant survival rate of 95%
    for i in range(len(age_classes) - 1):
        new_population[i + 1] = current_population[i] * 0.95

    current_population = new_population

    print("-" * 30)

Year Age Class      Population
0    Total          950
     0              0
     1              100
     2              200
     3              300
     4              200
     5              100
     6              50
------------------------------
1    Total          867.5
     0              12.5
     1              0.0
     2              95.0
     3              190.0
     4              285.0
     5              190.0
     6              95.0
------------------------------
2    Total          757.625
     0              23.75
     1              11.875
     2              0.0
     3              90.25
     4              180.5
     5              270.75
     6              180.5
------------------------------
3    Total          593.39375
     0              45.125
     1              22.5625
     2              11.28125
     3              0.0
     4              85.7375
     5              171.475
     6              257.2125
------------------------------
4    Total          383.6753124999999
     0              64.303125
     1              42.86875
     2              21.434375
     3              10.7171875
     4              0.0
     5              81.45062499999999
     6              162.90124999999998
------------------------------
5    Total          250.46067187499995
     0              40.725312499999994
     1              61.087968749999995
     2              40.725312499999994
     3              20.362656249999997
     4              10.181328124999999
     5              0.0
     6              77.37809374999999
------------------------------
6    Total          183.77297265624998
     0              19.344523437499998
     1              38.689046874999995
     2              58.03357031249999
     3              38.689046874999995
     4              19.344523437499998
     5              9.672261718749999
     6              0.0
------------------------------
7    Total          174.58432402343746
     0              0.0
     1              18.377297265624996
     2              36.75459453124999
     3              55.13189179687499
     4              36.75459453124999
     5              18.377297265624996
     6              9.188648632812498
------------------------------
8    Total          159.42305377929682
     0              2.2971621582031245
     1              0.0
     2              17.458432402343746
     3              34.91686480468749
     4              52.37529720703124
     5              34.91686480468749
     6              17.458432402343746
------------------------------
9    Total          139.23099840869136
     0              4.3646081005859365
     1              2.1823040502929683
     2              0.0
     3              16.585510782226557
     4              33.17102156445311
     5              49.75653234667968
     6              33.17102156445311
------------------------------
10   Total          109.0497333931396
     0              8.292755391113278
     1              4.146377695556639
     2              2.0731888477783196
     3              0.0
     4              15.756235243115228
     5              31.512470486230455
     6              47.26870572934569
------------------------------
11   Total          70.50915271294065
     0              11.817176432336423
     1              7.878117621557614
     2              3.939058810778807
     3              1.9695294053894035
     4              0.0
     5              14.968423480959466
     6              29.93684696191893
------------------------------
12   Total          46.027902203950354
     0              7.484211740479733
     1              11.226317610719601
     2              7.484211740479733
     3              3.7421058702398664
     4              1.8710529351199332
     5              0.0
     6              14.220002306911491
------------------------------

 


# github link

https://github.com/WhoisHOO/HOOAI/blob/main/Python%20Examples/simple_age_simulation


 

728x90

댓글