## Instructions

**Objective**

## Requirements and Specifications

**Source Code
**

import numpy as np

import matplotlib.pyplot as plt

# First, load the data stored in the file data.txt

'''

The data contains the years and the tonnes of plastic in millions

Since we want years since 1975, we subtract 1975 to each value

'''

data = np.loadtxt('data.txt', delimiter = ',')

years = data[:,0]-1975

tonnes = data[:,1]

def plastic_production(year):

"""This function implements a Least Square models to predict the the logbal production

rate of plastics for the given year

After testing the Least-Square method for two models: Linear and Quadratic, it was determined

that the quadratic model returned a lower MSE

The quadratic model is defined by the following function:

y = a + b*x + c*x^2

Where x is the number of years since 1975

Args:

year (int): years since 1975

"""

# Construct matrices for Least-Square method

A = np.ones((data.shape[0], 3))

A[:,1] = years

A[:,2] = years**2

y = tonnes

# Calculate coefficients

coeffs = np.matmul(np.matmul(np.linalg.inv(np.matmul(A.T, A)), A.T), y)

a = coeffs[0]

b = coeffs[1]

c = coeffs[2]

# Now, calculate the new value

y_pred = a + b*year + c*year**2

return y_pred

def new_ocean_plastic(year):

"""This function calculates the amount of plastic that ends in the ocean

It is know that around 2.96% of the global production ends in the ocean so we will assume that

for all years, that percentage does not changes

Args:

year (int): year

"""

# First, calculate the global production

; global_prod = plastic_production(year)

# Now, calculate the amount that ends in the ocean as 2.96% of the global production

ocean_plastic = (2.96/100.0)*global_prod

return ocean_plastic

def total_ocean_plastics(start, end):

'''

To have an approximate value of the accumulation of marine plastic, we have to use an numerical integration method

to calculate the area under the curve. For this case, we will use the Trapezoidal Rule

'''

# First, we declate the number of points. To have an accurate result, we will use 100 points

N = 100

# Compute step

h = (end-start)/N

# Comput the first and final value

area = new_ocean_plastic(start) + new_ocean_plastic(end)

# Now, estimate for all values between the 'start' and 'end' years

for i in range(1, N):

year_i = start + h*i

area += 2*new_ocean_plastic(year_i)

# Now divide the result by 2 and multiply by the step

area = area*h/2

return area

'''

PRINT WELCOME MESSAGE

'''

print('-'*120)

print('''

Welcome! In this interactive program you will learn about the global plastic production and the amount

of plastic that is ending in the ocean.

With this program you will be able to predict and visualize the plastic production for future years

''')

print('-'*120)

print()

'''

PRINT PATRONS TYPES

'''

print('''

There are two Patron Types:

1) Science Rookie: Not familiar with scientific terminology or notation.

Will need terminology explained using a simple vocabulary.

Is unfamiliar with graphs;

May be a younger person, possibly 10+ years of age;

Likes to press buttons.

2) Science Enthusiast: Familiar with common scientific terminology and notation (not overly technical).

Can handle terminology explained using somewhat sophisticated vocabulary.

Is prepared to read longer passages of moderate complexity.

Likes to know about modelling assumptions and limitations.

Is familiar with graphs.

Likes to press buttons.

''')

print('-'*120)

print()

patron_type = int(input("Please enter your type (1 or 2): "))

'''

DISPLAY INFO ABOUT PLASTICS

'''

if patron_type == 1: # rookie

print('''

Plastics can be natural or synthetic. One of the first natural plastics was celluloid and bakelite.

They were made from cellulose like in plants by carefully dissolving it into solution called a celluloid,

nitrating it and adding camphor. This caused the cellulose to form long chains called polymers.

The long polymer chains of cellulose would precipitate out of solution and if you then rinse and clean it,

you end up with the natural plastics left over but they are not very useful looking like a wad of gum so

you can heat them up and then press them into different shapes and when they cool it holds that shape.

We called the plastic celluloid. It could be used to make anything from cinema film, to combs and tooth brushes,

and could be molded into many different shapes. It was not very resistant to solvents and cleaning products,

and it was very flammable.

''')

else:

print('''

The term plastic refers to a broad group of synthetic polymers that have become ubiquitous in

modern manufacturing due to their low production cost and broad utility across a huge range of different

industries including packaging, consumer products, textiles, transportation, construction and

electronics. The origin of large scale plastic production dates back to the 1950s and global plastic

production has increased year-on-year in all but three years since then.

Plastics have revolutionised the cost-effectiveness and versatility of manufacturing in the

post-war era. Plastics have become so commonplace in modern times that it is hard to

imagine life without them. However, as plastic consumption has increased, so too has

our knowledge and understanding of the potentially devastating impacts of mismanaged

plastic waste. As we plan for a more sustainable future, we must examine our dependency

on plastics and the consequences of inaction on the future health and prosperity of the

planet.

The lifespan of a plastic product is the time that elapses between its creation until it becomes waste.

The lifespan of plastics varies significantly depending upon the type of plastic and how it is used. For

example, plastics used in building and construction typically have longer lifespans on the order of

decades, whereas plastics used in packaging may have an average lifespan on the order of months.

There are many different pathways for dealing with plastic wastes including reuse, recycling, thermal

destruction and disposal. However, reused and recycled plastics eventually need to be disposed of,

since these processes cannot be repeated indefinitely. Mismanagement of plastic waste can lead

to plastics entering the marine environment.

Estimating how much plastic enters the world’s oceans is a complex problem. Jambeck et al. (2015)

developed a framework for estimating the amount of mismanaged plastic waste from coastal populations that could

potentially become marine debris. Based on data from 2010, they estimated that

approximately 2.96% of the plastic produced in that year ended up as ocean plastic.

''')

print('-'*120)

print()

if patron_type == 2: # Enthusiast

# The data contains values from 1975 to 2014 so these values are shown as solid lines

plt.figure()

plt.plot(years+1975, tonnes, color='blue', label = 'Data')

# Now from 2015 to 2050, predict and display in red

predicted_values = list()

predicted_years = list()

for i in range(40, 75): # from 40 (2015) to 75 (2050) years since 1975

predicted_values.append(plastic_production(i))

predicted_years.append(1975+i)

# Plot

plt.plot(predicted_years, predicted_values, 'r--', label = 'Predicted')

plt.legend()

plt.xlabel('Years')

plt.ylabel('Tonnes per year')

plt.title('Global plastic production in tonnes per year')

plt.grid()

plt.show()

# Now as for a year between 1975 and 2050

while True:

year = int(input("Enter a year between 1975 and 2050: "))

# Calculate the estimated globla plastic production

production = plastic_production(year-1975)

if patron_type == 1: # rookie

print("The amount of plastic produced in {0} was {1:.2f}".format(year, production))

else:

print("The Global Plastic Production for {0} was {1:.2f} tonnes per year".format(year, production))

# Calculat ethe estimated rate at which plastic enters the ocean in that year

ocean_rate = new_ocean_plastic(year-1975)

if patron_type == 1: # rookie

print("The rate of plastic that entered the ocean in {0} was {1:.2f}".format(year, ocean_rate))

else:

print("The estimated rate at which plastic entered the ocean in {0} was {1:.2f} tonnes per year".format(year, ocean_rate))

# Ask if he/she wants to enter a new year

option = input("Do you want to enter a new year? (y/n): ")

if option == "n":

break

print('-'*120)

print()

# Calculate the number of years required from today (2022) for the amount of plastic entering the ocean

# to double

today_production = new_ocean_plastic(47) # 2022 is 47 years since 1975

i = 1

while(True):

# Calculate new production

new_production = new_ocean_plastic(47 + i)

# Check if the new production is 2x or more higher than today's production

if (new_production/today_production) >= 2.0:

if patron_type == 1:

print("It takes {0} years for a production of {1:.2f} tonnes in 2022 to double".format(i, today_production))

else:

print('''

At 2022, the Global Plastic Production is ~{0:.2f} (tonnes/year)."

At the rate of increase determined for previous years, that amount will double to ~{1:.2f} (tonnes/year) by {2}.

'''.format(today_production, new_production, 2022+i))

break

i += 1

print('-'*120)

print()

'''

ADVANCED PART

'''

# Ask for two years

year1 = int(input("Enter a year between 1975 and 2050: "))

year2 = int(input("Enter a year between 1975 and 2050 higher than the previous value: "))

# Compute

total_plastic = total_ocean_plastics(year1-1975, year2-1975)

if patron_type == 2: # enthusiast

# Display a graph of the ocean plastic between these eyars

ocean_plastics = list()

for i in range(year1, year2+1):

ocean_plastics.append(new_ocean_plastic(i-1975))

plt.figure()

plt.plot(range(year1, year2+1), ocean_plastics)

plt.grid(True)

plt.xlabel('Year')

plt.ylabel('Tonnes/year')

plt.title('Amount of plastic that entered the ocean')

plt.show()

# Now, display the total amount

print("The total amount of plastic that entered the ocean between {0} and {1} is: {2:.2f} tonnes.".format(year1, year2, total_plastic))

print('-'*120)

print()

print("Good bye!")