Oil refinery production optimization

oil_refining.ipynb Open In Colab Kaggle Gradient Open In SageMaker Studio Lab Hits

1. Introduction

Context and Significance

The petroleum refining industry is one of the most crucial and intricate sectors in the global economy. It plays a pivotal role in converting crude oil into an array of valuable products such as gasoline, diesel, jet fuel, lubricants, and petrochemicals, which are indispensable to modern life and industrial processes. The industry’s complexity arises from the need to manage and optimize a series of interconnected processes, each with its own set of constraints and requirements. Furthermore, the industry operates in a highly volatile environment, influenced by fluctuating crude oil prices, stringent environmental regulations, technological advancements, and dynamic market demands.

Effective decision-making in this context is paramount. Refinery managers must balance multiple objectives: maximizing output and profitability, minimizing costs, ensuring compliance with environmental standards, and maintaining operational efficiency. These challenges underscore the necessity for sophisticated optimization models that can integrate various facets of refinery operations and provide strategic insights.

Problem Statement

The primary goal of this project is to develop a comprehensive optimization model for a petroleum refinery. This model aims to maximize the refinery’s total profit over a specified planning horizon. To achieve this, the model will encompass various stages of the refining process, including distillation, reforming, cracking, and lubricating. Additionally, it will incorporate elements such as production planning, blending operations, inventory management, financial planning, pollutant emission controls, and maintenance scheduling.

The proposed model will serve as a decision-support tool, guiding refinery managers in:

  1. Production Planning: Optimizing the production levels of intermediate and final products to align with market demand and operational capacities.

  2. Blending Operations: Determining optimal blending ratios to meet product quality specifications while maximizing efficiency.

  3. Inventory Management: Efficiently managing the storage and movement of crude oil and intermediate products to minimize holding costs and avoid shortages or surpluses.

  4. Market and Demand Management: Adjusting production and blending strategies in response to market demand and price fluctuations.

  5. Financial Planning: Ensuring effective management of cash flows, loans, and financial balances to support ongoing operations and investments.

  6. Regulatory Compliance: Adhering to environmental regulations by managing pollutant emissions and other environmental impacts.

  7. Maintenance Scheduling: Planning for equipment maintenance and plant shutdowns to minimize downtime and associated costs.

Scope and Objectives

The scope of this optimization model is to provide a holistic framework that integrates all aspects of refinery operations. The specific objectives include:

  • Decision Variables: Identifying key variables such as production levels, blending ratios, inventory levels, cash flows, loan amounts, and binary variables for plant and equipment status.

  • Constraints: Incorporating constraints related to operational capacities, storage limits, delivery schedules, pollutant emission limits, financial balances, loan limits, market demand, product quality standards, and maintenance requirements.

  • Objective Function: Developing a total profit maximization function that accounts for revenues from product sales, costs of production and blending, inventory holding costs, costs of disposal of production waste, costs of planned plant shutdown,loan interest expenses, and maintenance costs. By achieving these objectives, the model will provide actionable insights and recommendations for optimizing refinery operations, thereby enhancing profitability and sustainability.

In this example, we’ll demonstrate how to use mathematical optimization to optimize the output of a refinery. You’ll learn how to generate an optimal production plan that maximizes total profit, while taking into account production capacity and other restrictions.

Expected Outcomes

The successful implementation of this optimization model is anticipated to deliver several key benefits:

  • Enhanced Profitability: Optimizing production, blending, and financial decisions will maximize profit margins and overall financial performance.

  • Operational Efficiency: Improved planning and scheduling will enhance the efficiency of production processes and maintenance activities.

  • Market Responsiveness: The model’s ability to incorporate demand elasticity will allow the refinery to adapt swiftly to changes in market conditions.

  • Regulatory Compliance: Ensuring adherence to environmental standards will help avoid legal penalties and enhance the refinery’s reputation.

  • Strategic Financial Management: Effective management of cash flows and loans will ensure financial stability and reduce the cost of capital.

Conclusion

In conclusion, the development and implementation of this optimization model will provide a powerful tool for refinery management. It will facilitate informed decision-making, driving long-term success in a competitive and ever-changing industry. By addressing the multifaceted challenges of refinery operations, this model will help achieve a balance between profitability, efficiency, and sustainability, ensuring the refinery’s resilience and growth in the face of future challenges.

Partner with the AMPL team to transform complex problems into optimized solutions. AMPL consulting services combine deep technical knowledge with industry-leading insights, helping you unlock the full potential of optimization within your organization.

Tags: Oil production, Production optimization, Profitability, Refinery, Deterministic model, Piecewise-linear, mip, ampl-only, cbc

Notebook author: Mikhail Riabtsev <mail@solverytic.com>


The operating pattern of an oil refinery depends on its specialization (type of equipment installed) and the type(s) of oil processed. In this example, we will use the part of the general scheme of oil refining.

2. Problem Description

Stage 1: Distillation

An oil refinery purchases two types of crude oil (Crude1 and Crude2). The cost of purchased oil changes over time periods, $/barrel:

Oil type

1

2

3

4

5

6

7

8

9

10

11

12

Crude1

80

80

82

83

84

85

86

87

88

89

90

91

Crude2

85

86

87

88

89

90

90

90

90

91

91

91

The terms of the concluded contracts include restrictions on the maximum volume of oil supplied in each period, barrels:

Oil type

Max

Crude1

40000

Crude2

25000

Purchased crude oil is distilled into several fractions:

  • Light Naphta (with an octane number of 90)

  • Medium Naphta (with an octane number of 80)

  • Heavy Naphta (with an octane number of 70)

  • Light Oil (with an vapor pressur of 1)

  • Heavy Oil (with an vapor pressur of 0.6)

  • Residuum (with an vapor pressur of 0.05)

One barrel of crude separates into the following fractions:

Operating mode

Oil type

Light Naphta

Medium Naphta

Heavy Naphta

Light Oil

Heavy Oil

Residuum

D1

Crude1

0.1

0.2

0.2

0.12

0.2

0.13

D1

Crude2

0.15

0.25

0.18

0.08

0.19

0.12

D2

Crude1

0.12

0.19

0.19

0.13

0.19

0.13

D2

Crude2

0.14

0.24

0.19

0.09

0.18

0.13

The distillation process operates under different modes (e.g., D1, D2), each with its own capacity, cost, setup characteristics, pollution level and the cost of waste disposal:

Operating mode

Maximum productivity, barrels/period

Duration of the equipment setup period

Equipment reconfiguring cost, $

D1

45 000

50 000

0.5

D2

47 000

70 000

0.5

*The costs of reconfiguring equipment are taken into account only if reconfiguration is needed.

The cost of distilling 1 barrel of crude oil and the cost of disposal of process residues are:

Operating mode

Oil type

Cost of Distillation, $/barrel

Residue disposal cost, $/barrel

D1

Crude1

18

2

D1

Crude2

15

3

D2

Crude1

17

1.5

D2

Crude2

16

2.2

The process of recycling the residues of the distillation process is accompanied by the release of the following amount of harmful substances:

Operating mode

Oil type

Hydrocarbon

Sulfur dioxide

D1

Crude1

100

120

D1

Crude2

105

116

D2

Crude1

99

115

D2

Crude2

108

111

The refinery then uses some amount of the distillation products as inputs for further processing in Reforming, Cracking, and Lubricating processes.

Stage 2:

2.1. Reforning

This process converts specific distillation products (Light Naphta,Medium Naphta, Heavy Naphta ) into reforming products such as Reformed gasoline (with an octane number of 115).

From 1 barrel of distillation products, the following amounts of reformed gasoline can be obtained:

Operating mode

Distillation product

Reformed gasoline

R1

Light Naphta

0.6

R1

Medium Naphta

0.52

R1

Heavy Naphta

0.45

R2

Light Naphta

0.6

R2

Medium Naphta

0.52

R2

Heavy Naphta

0.48

The reforming installation can operate in different modes (e.g., R1, R2), each with its own capacity and cost.

Operating mode

Maximum productivity, barrels/period

Duration of the equipment setup period

Equipment reconfiguring cost, $

R1

10 000

0.5

80 000

R2

12 000

0.6

90 000

*The costs of reconfiguring equipment are taken into account only if reconfiguration is needed.

The cost of reforming 1 barrel of distilling products and the cost of disposal of process residues are:

Operating mode

Distillation product

Cost of Reforming, $/barrel

Residue disposal cost, $/barrel

R1

Light Naphta

20

2

R1

Medium Naphta

26

1.8

R1

Heavy Naphta

28

2.2

R2

Light Naphta

22

2

R2

Medium Naphta

24

3

R2

Heavy Naphta

26

1.3

The process of recycling the residues of the reforming process is accompanied by the release of the following amount of harmful substances:

Operating mode

Distillation product

Hydrocarbon

Sulfur dioxide

R1

Light Naphta

60

180

R1

Medium Naphta

70

140

R1

Heavy Naphta

72

115

R2

Light Naphta

65

180

R2

Medium Naphta

40

150

R2

Heavy Naphta

70

118

2.2. Cracking

This process converts specific distillation products (Light Oil,Heavy Oil) into cracking products such as Cracked oil (with an vapor pressur of 1.5) and Cracked gasoline (with an octane number of 105). From 1 barrel of distillation products the following amounts of cracked products can be obtained::

Operating mode

Distillation product

Cracked oil

Cracked gasoline

CR1

Light Oil

0.68

0.28

CR1

Heavy Oil

0.75

0.2

CR2

Light Oil

0.6

0.34

CR2

Heavy Oil

0.52

0.25

The cracking installation can operate in different modes (e.g., CR1, CR2), each with its own capacity and cost.

Operating mode

Maximum productivity, barrels/period

Duration of the equipment setup period

Equipment reconfiguring cost, $

CR1

8 000

0.3

25 000

CR2

9 000

0.2

36 300

*The costs of reconfiguring equipment are taken into account only if reconfiguration is needed.

The cost of cracking 1 barrel of distilling products and the cost of disposal of process residues are:

Operating mode

Distillation product

Cost of Cracking, $/barrel

Residue disposal cost, $/barrel

CR1

Light Oil

20

1

CR1

Heavy Oil

22

1.2

CR2

Light Oil

21

1.36

CR2

Heavy Oil

18

1.05

The process of recycling the residues of the cracking process is accompanied by the release of the following amount of harmful substances:

Operating mode

Distillation product

Hydrocarbon

Sulfur dioxide

CR1

Light Oil

100

200

CR1

Heavy Oil

150

250

CR2

Light Oil

120

180

CR2

Heavy Oil

150

240

2.3. Lubricating

This process converts specific distillation products (Residuum) into Lube Oil. From 1 barrel of distillation products the following amounts of lubrication products can be obtained:

Operating mode

Distillation product

Lube Oil

L1

Residuum

0.5

L2

Residuum

0.6

The lubricating installation can operate in different modes (e.g., L1, L2), each with its own capacity and cost.

Operating mode

Maximum productivity, barrels/period

Duration of the equipment setup period

Equipment reconfiguring cost, $/barrel

L1

500

0.2

5 000

L2

1 000

0.1

8 000

*The costs of reconfiguring equipment are taken into account only if reconfiguration is needed.

The cost of lubricating 1 barrel of distilling products and the cost of disposal of process residues are:

Operating mode

Distillation product

Cost of Lubricating, $/barrel

Residue disposal cost, $/barrel

L1

Residuum

21

1.2

L2

Residuum

22

1.4

The process of recycling the residues of the lubricating process is accompanied by the release of the following amount of harmful substances:

Operating mode

Distillation product

Hydrocarbon

Sulfur dioxide

L1

Residuum

60

180

L2

Residuum

70

140

It is advisable to produce Lube Oil in a volume of no more than 1000.

All intermediate components obtained at the stages Distillation, Reforming, Cracking, Lubricating can be mixed to obtain the final petroleum products:

3.1. Regular and premium gasoline

Can be made by blending Light Naphta, Medium Naphta, Heavy Naphta, Reformed gasoline and Cracked gasoline. The only requirement is that Regular gasoline must have an octane of at least 84 and Premium gasoline must have an octane number of at least 94. It is assumed that octane numbers blend linearly by volume.

Premium gasoline production must be at least 40% of Regular gasoline production.

3.2. Jet Fuel

Can be made by blending Light Oil, Heavy Oil, Residuum, and Cracked Oil. Jet fuel must have a vapor pressure that does not exceed 1.0. It is assumed that vapor pressures blend linearly by volume.

3.3. Fuel Oil

To produce Fuel Oil, it is necessary to mix Light Oil, Cracked Oil, Heavy Oil and Residuum in the ratio of 10:4:3:1.

3.4. Lube Oil

Was produced at the Stage 2.3. Lubricating.

The cost of the process of blending various petroleum products has the following cost:

Product

Cost of blending, $/barrel

Premium Gasoline

2

Regular Gasoline

1.9

Jet Fuel

2.2

Fuel Oil

2

Lube Oil

2.4

Finished petroleum products are delivered to fuel storage facilities. Storage capacity, storage cost, loss percentage during storage are presented below:

Product

Storage Capacity, barrels

Storage Cost, $/barrel

Storage Waste, %

Premium Gasoline

50 000

3

0.3

Regular Gasoline

50 000

5

0.2

Jet Fuel

30 000

7

0.1

Fuel Oil

10 000

2

0.05

Lube Oil

4 500

4

0.05

The cost of delivery petroleum products to gas stations is:

Product

Station1, $/barrel

Premium Gasoline

2

Regular Gasoline

2

Jet Fuel

3

Fuel Oil

2

Lube Oil

4

Operating an oil refinery involves fixed costs of \$300,000 each period.

In addition, during the periods under review, a planned technical shutdown of the plant lasting 1 period is required. During the planned shutdown, the plant can only release finished petroleum products to customers but not produce new ones. The cost of various technical works during the suspension is \$100,000. During the technological shutdown, all fixed costs of the plant continue to apply. At the beginning of the period under review, the plant’s current account contains \$2 million.

Historical data on demand for petroleum products demonstrates seasonal variations in demand:

Product

1

2

3

4

5

6

7

8

9

10

11

12

‘Premium Gasoline’

4 500

4 500

4 500

4 500

5 000

7 000

7 500

7 000

6 000

5 000

4 500

5 000

‘Regular Gasoline’

8 500

8 500

8 500

8 800

9 200

13 500

14 000

13 500

12 000

10 000

9 000

10 000

‘Jet Fuel’

7 000

7 000

7 000

9 000

12 000

10 000

9 000

8 000

8 000

9 000

7 000

7 000

‘Fuel Oil’

4 000

3 800

3 800

3 800

2 900

2 500

2 500

2 800

3 300

3 400

3 500

3 600

‘Lube Oil’

500

500

700

700

700

500

400

500

600

700

700

700

During the periods under review, the following changes in the average market price are expected:

Average market price:

1

2

3

4

5

6

7

8

9

10

11

12

‘Premium Gasoline’

162

165

165

165

165

165

165

167

168

168

167

165

‘Regular Gasoline’

126

126

127

127

128

128

129

129

129

129

130

130

‘Jet Fuel’

115

115

115

115

115

116

116

116

117

117

118

118

‘Fuel Oil’

154

154

154

155

155

155

155

156

156

156

155

155

‘Lube Oil’

176

176

177

177

177

178

178

178

178

178

177

178

The following price elasticities apply for all petroleum products:

Relative price change*

Relative change in demand

+6%

-20%

+4%

-12%

+2%

-5%

-2%

+2%

-4%

+5%

-6%

+9%

This means that when the price of petroleum products increases by 6% relative to the market average, demand decreases by 20% relative to average historical data.

To financially evaluate all transactions, use a discount rate of 5%. The purchase of raw materials and the sale of finished petroleum products assumes the availability of the full amount of necessary payments. In case of a shortage of working capital, the oil refinery has the opportunity to obtain bank loans every period on the following conditions:

Name

Term

Interest rate

Maximum amount, $

I

1

6.5

10 000 000

The loan received in the previous period and the interest accrued on it must be paid in the current period.

Stage 3: Blending

Stage 4: Storage

Factory restrictions

Demand

Finance

Objective

How should the operations of the refinery be planned in order to maximize total profit?


Let’s start by downloading the necessary extensions and libraries

# Install dependencies
%pip install -q amplpy pandas
import pandas as pd  # Loading panda to work with pandas.DataFrame objects (https://pandas.pydata.org/)
import numpy as np  # Loading numpy to perform multidimensional calculations numpy.matrix (https://numpy.org/)
import matplotlib.pyplot as plt  # Loading library for creating static, animated, and interactive visualizations (https://matplotlib.org/)
# Google Colab & Kaggle integration
from amplpy import AMPL, ampl_notebook

ampl = ampl_notebook(
    modules=["cbc", "highs"],  # modules to install
    license_uuid="default",  # license to use
)  # instantiate AMPL object and register magics
from amplpy import AMPL

ampl = AMPL()  # create a new AMPL object with all default settings

3. AMPL formulation of the model

Use %%ampl_eval to evaluate AMPL commands and declarations

%%ampl_eval
reset ;
### SETS
 set CRUD ;       # Types of crude oil (Crude1, Crude2)
 set DIST ;       # Distillation products
 set REF ;        # Reforming products
 set CRACK ;      # Cracking products
 set LUBR ;       # Lubricating products
 set PROD ;       # Final products
 set STAT ;       # Delivery stations
 set D_MODE ;     # Operating modes of the equipment of the Distillation installation
 set R_MODE ;     # Operating modes of the equipment of the Reforming installation
 set CR_MODE ;    # Operating modes of the equipment of the Cracking installation
 set L_MODE ;     # Operating modes of the equipment of the Lubrication process
 set POLLUT ;     # Types of pollutants

 set DISTILLATION within {D_MODE, CRUD, DIST} ;   # Pairs of the Distillation process
 set REFORMING within {R_MODE, DIST, REF} ;       # Pairs of the Reforming process
 set DIST_R := setof{(m,d,r) in REFORMING}(d) ;   # List of included components
 set CRACKING within {CR_MODE, DIST, CRACK} ;     # Pairs of the Cracking process
 set DIST_CR := setof{(m,d,cr) in CRACKING}(d) ;  # List of included components
 set LUBRICATING within {L_MODE, DIST, LUBR} ;    # Pairs of the Lubricating process
 set DIST_L := setof{(m,d,l) in LUBRICATING}(d) ; # List of included components
 set BLENDING within                              # Pairs of Intermediate and final products involved in blending
  {DIST union REF union CRACK union LUBR, PROD} ;
 set INTERMED:= setof{(i,j) in BLENDING}i ;       # Set of Intermediate products before blending              

### PARAMETERS
  param nPeriod >= 0 ;                            # Number of weeks in the planning period
  param nPeriodByYear >= 0 ;                      # Number of nPeriods in the Year

 ## Crude oil
  param crude_Min{CRUD} >= 0 ;                    # Minimum supply limits for each crude oil type
  param crude_Max{c in CRUD} >= crude_Min[c] ;    # Maximum supply limits for each crude oil type
  param crude_Cost{CRUD, 1..nPeriod} >= 0 ;       # Cost of crude oil per week

 ## Distillation
  param distill_Yield{DISTILLATION} >= 0 ;        # Yield of products
  param distill_Pollute{D_MODE, CRUD, POLLUT} ;   # Pollutant emissions
  param distill_Cost{D_MODE, CRUD} >= 0 ;         # Cost of process
  param distill_Waste_Cost{D_MODE, CRUD} >= 0 ;   # Residue disposal cost
  param distill_Equipment_Setup_Period{D_MODE} >= 0 ;   # Equipment setup period
  param distill_Equipment_Setup_Cost{D_MODE} >= 0 ;     # Equipment setup cost
  param distill_Cap_Max{D_MODE} >= 0 ;            # Maximum capacity

 ## Reforming
  param reform_Yield{REFORMING} >= 0 ;            # Yield of products
  param reform_Pollute{R_MODE, DIST_R, POLLUT} ;  # Pollutant emissions
  param reform_Cost{R_MODE, DIST_R} >= 0 ;        # Cost of process
  param reform_Waste_Cost{R_MODE, DIST_R} >= 0 ;  # Residue disposal cost
  param reform_Cap_Max{R_MODE} >= 0 ;             # Maximum capacity
  param reform_Equipment_Setup_Period{R_MODE} >= 0 ;  # Equipment setup period
  param reform_Equipment_Setup_Cost{R_MODE} >= 0 ;# Equipment setup cost

 ## Cracking
  param crack_Yield{CRACKING} >= 0 ;              # Yield of products
  param crack_Pollute{CR_MODE, DIST, POLLUT} ;    # Pollutant emissions
  param crack_Cost{CR_MODE, DIST} >= 0 ;          # Cost of process
  param crack_Waste_Cost{CR_MODE, DIST_CR} >= 0 ; # Residue disposal cost
  param crack_Cap_Max{CR_MODE} >= 0 ;             # Maximum capacity
  param crack_Equipment_Setup_Period{CR_MODE} >= 0 ;  # Equipment setup period
  param crack_Equipment_Setup_Cost{CR_MODE} >=0 ; # Equipment setup cost
 
 ## Lubrication
  param lube_Yield{LUBRICATING} >= 0 ;            # Yield of products
  param lube_Pollute{L_MODE, DIST_L, POLLUT} ;    # Pollutant emissions
  param lube_Waste_Cost{L_MODE, DIST_L} >= 0 ;    # Residue disposal cost
  param lube_Cost{L_MODE, DIST_L} >= 0 ;          # Cost of process
  param lube_Cap_Max{L_MODE} >= 0 ;               # Maximum production of lube oil
  param lube_Equipment_Setup_Period{L_MODE} >= 0 ;# Equipment setup period
  param lube_Equipment_Setup_Cost{L_MODE} >= 0 ;  # Equipment setup cost
  param lube_limit_Min >= 0 ;                     # Minimum production of lube oil
  param lube_limit_Max >= lube_limit_Min ;        # Maximum production of lube oil

 ## Intermediate components
  param Intermed_Octane{INTERMED} >= 0 ;          # Octane number
  param Intermed_VaporPressure{INTERMED} >= 0 ;   # Vapor pressure   

 ## Blending
  param blending_Cost{PROD} >= 0 ;                # Cost of blending

 ## Products
  param prod_Octane_Min{PROD} >= 0 ;              # Minimum octane number for final products
  param prod_VaporPressure_Max{PROD} >= 0 ;       # Maximum vapor pressure for final products
  param prod_Premium_Regular_Gas_Min >= 0 ;       # Minimum production of premium gas
  param prod_FuelOil_Ratio{INTERMED} >= 0 ;       # Ratios for fuel oil production components
   
 ## Storage
  param storage_Capacity{PROD} >=0 ;              # Storage capacity for each product
  param storage_Cost{PROD} >= 0 ;                 # Storage cost per product
  param storage_Waste{PROD} >= 0 ;                # Waste during storage

 ## Product delivery
  param delivery_Cost{PROD, STAT} >= 0 ;          # Delivery cost per product to each station

 ## Plant
  param plant_Shutdown_Period >= 0 ;              # Equipment setup period
  param plant_Shutdown_Cost >= 0 ;                # Equipment setup cost
  param plant_Const_Cost >= 0 ;                   # Plant fixed costs
    
 ## Market
  param seasonal_Base_Demand{PROD, 1..nPeriod} >= 0 ; # Base demand for products per week.
  param seasonal_Base_Price {PROD, 1..nPeriod} >= 0 ; # Base price for products per week.
  # Price elasticity
  param nStep integer > 0 ;                       # Number of steps for price elasticity
  param price_nStep_Value{1..nStep+1} >= 0 ;      # Step values for price elasticity
  param demand_nStep_Value{1..nStep+1} >= 0 ;     # Step values for price elasticity

 ## Finance
  param discount_Rate >= 0 ;                      # Discount rate for future cash flows
  param initial_Cash  >= 0 ;                      # Initial cash available
  
 ## Loans
  set LOANS;                                      # Set of loan periods
  set LOAN_param;                                 # Parameters of loans (term, interest, Max_Money)
  param loan{LOANS, LOAN_param} >= 0 ;            # Conditions for obtaining credit

### VARIABLES
 ## Plant working
  var Plant_Working{t in 1..nPeriod} binary;                  # 1 if the plant is running. 0 if the plant is shutdown

 ## Distillation
  var Crude_Supply{D_MODE, CRUD, 1..nPeriod} >= 0 ;           # Amount of crude supplied
  var Distill_X{D_MODE, 1..nPeriod} binary ;                  # Additional binary variable for selecting the operating mode of the equipment

 ## Reforming
  var Distill_to_Reforming{R_MODE, DIST_R, 1..nPeriod} >= 0 ; # Quantity of distillation products used for Reforming
  var Reform_X{R_MODE, 1..nPeriod} binary ;                   # Additional binary variable for selecting the operating mode of the equipment

 ## Cracking
  var Distill_to_Cracking{CR_MODE, DIST_CR, 1..nPeriod} >= 0 ;# Quantity of distillation products used for Cracking
  var Cracking_X{CR_MODE, 1..nPeriod} binary ;                # Additional binary variable for selecting the operating mode of the equipment


 ## Lubricating
  var Distill_to_Lubricating{L_MODE, DIST_L, 1..nPeriod} >= 0 ;# Quantity of distillation products used for Lubricating
  var Lubricating_X{L_MODE, 1..nPeriod} binary ;              # Additional binary variable for selecting the operating mode of the equipment
 
 ## Blending:
  var Blending{BLENDING, 1..nPeriod} >= 0 ;                   # Amount of ingredients mixed to obtain final products     

 ## Demand
  var Demand{PROD, STAT, 1..nPeriod, 1..nStep} >= 0 ;         # Demand for each pr oduct at each station over time
  var X{PROD, STAT, 1..nPeriod, 1..nStep} binary ;            # Additional binary variable for demand steps (1 if for product p in period t the price is selected at step nStep, or 0 otherwise)
 
 ## Storage
  var Storage_Fraction{p in PROD, t in 1..nPeriod} =          # Amount of each product in Storage each period
    sum{tt in 1..t} (sum{(i,p) in BLENDING} Blending[i,p,tt]
    - sum{s in STAT, n in 1..nStep} Demand[p,s,tt,n]) * (1-storage_Waste[p]) ;

 ## Loan 
 # Amount of loan taken every period
  var Loan_In{l in LOANS, 1..nPeriod-1} >= 0 , <= loan[l,'Max_Money'] ;

 # Amount of loan taken every period
  var Loan_Out{l in LOANS, t in 2..nPeriod} = Loan_In[l,t-1] * (1+loan[l, 'interest']/nPeriodByYear);

 ## Pollutant emissions
 var Waste_Pollutant{p in POLLUT, t in 1..nPeriod} = 
  # Pollution from waste disposal from the Distillation process
  sum{m in D_MODE, c in CRUD} Crude_Supply[m,c,t] * (1 - sum{d in DIST}distill_Yield[m,c,d]) * distill_Pollute[m,c,p]
  # Pollution from waste disposal from the Reforming process
  + sum{m in R_MODE, c in DIST_R} Distill_to_Reforming[m,c,t] * (1 - sum{d in REF} reform_Yield[m,c,d]) * reform_Pollute[m,c,p]
  # Pollution from waste disposal from the Cracking process
  + sum{m in CR_MODE, c in DIST_CR} Distill_to_Cracking[m,c,t] * (1 - sum{d in CRACK} crack_Yield[m,c,d]) * crack_Pollute[m,c,p]
  # Pollution from waste disposal from the Lubricating process
  + sum{m in L_MODE, c in DIST_L} Distill_to_Lubricating[m,c,t] * (1 - sum{d in LUBR} lube_Yield[m,c,d]) * lube_Pollute[m,c,p];

 ## Cash flow with incomes and costs
  var CashFlow{t in 1..nPeriod} =
   # sales income
    sum{p in PROD, s in STAT, n in 1..nStep} Demand[p,s,t,n] * (seasonal_Base_Price[p,t] * price_nStep_Value[n] - delivery_Cost[p,s])
   # minus the cost of purchasing crude oil	+ the costs of the Distillation process	+ costs for disposal of waste from the Distillation process 			
    - sum{m in D_MODE, c in CRUD} Crude_Supply[m,c,t] * (crude_Cost[c,t] + distill_Cost[m,c] + (1 - sum{d in DIST}distill_Yield[m,c,d]) * distill_Waste_Cost[m,c])
   # minus the costs of the Reforming process	+ costs for disposal of waste from the Reforming process
    - sum{m in R_MODE, c in DIST_R} Distill_to_Reforming[m,c,t] * (reform_Cost[m,c] + (1 - sum{d in REF} reform_Yield[m,c,d]) * reform_Waste_Cost[m,c])
   # minus the costs of the Cracking process	+ costs for disposal of waste from the Cracking process
    - sum{m in CR_MODE, c in DIST_CR} Distill_to_Cracking[m,c,t] * (crack_Cost[m,c] + (1 - sum{d in CRACK} crack_Yield[m,c,d]) * crack_Waste_Cost[m,c])
   # minus the costs of the Lubricating process	+ costs for disposal of waste from the Lubricating process
    - sum{m in L_MODE, c in DIST_L} Distill_to_Lubricating[m,c,t] * (lube_Cost[m,c] + (1 - sum{d in LUBR} lube_Yield[m,c,d]) * lube_Waste_Cost[m,c])
   # minus the costs of reconfiguring equipment (if available)   
    - (if t > 1 then          /*Nonliner piece*/
      # Distillation equipment
        sum{m in D_MODE} (if Distill_X[m,t] - Distill_X[m,t-1] > 0 then distill_Equipment_Setup_Cost[m] else 0)
      # Reforming equipment
        + sum{m in R_MODE} (if Reform_X[m,t] - Reform_X[m,t-1] > 0 then reform_Equipment_Setup_Cost[m] else 0)
      # Cracking equipment
        + sum{m in CR_MODE} (if Cracking_X[m,t] - Cracking_X[m,t-1] > 0 then crack_Equipment_Setup_Cost[m] else 0)
      # Lubricating equipment
        + sum{m in L_MODE}(if Lubricating_X[m,t] - Lubricating_X[m,t-1] > 0 then lube_Equipment_Setup_Cost[m] else 0) 
        else 0)
   # minus the cost of the Blending	
    - sum{(i,p) in BLENDING} Blending[i,p,t] * blending_Cost[p]
    # minus cost of shutdown	
    - (1 - Plant_Working[t]) * plant_Shutdown_Cost
   # minus the storage cost
    - sum{p in PROD} Storage_Fraction[p,t] * storage_Cost[p]
   # minus fixed plant costs 
    - plant_Const_Cost
   # plus the amount of the loan received
    + (if t = nPeriod then 0 else sum{l in LOANS} Loan_In[l,t])
   # minus the amount of repaid loans with accrued interest 
    - (if t = 1 then 0  else sum{l in LOANS} Loan_Out[l,t]);

### OBJECTIVE FUNCTION
 # Maximize the total profit considering all incomes and costs, discounted by the discount rate
  maximize Total_Profit: 
    sum{t in 1..nPeriod} CashFlow[t] / (1 + discount_Rate/nPeriodByYear)^t ;

### CONSTRAINTS
 subject to
 ## Distillation
  # Ensure that total supply of crude oil does not exceed the maximum capacity. 
  Crude_Supply_Min_Max{c in CRUD, t in 1..nPeriod}:crude_Min[c] <= sum{m in D_MODE} Crude_Supply[m,c,t] <= crude_Max[c];

  # Ensure distillation capacity does not exceed the maximum. Сapacity is reduced during downtime caused by equipment reconfiguring.
  DistillCapacity_Max {m in D_MODE, t in 1..nPeriod}: sum{c in CRUD} Crude_Supply[m,c,t] <= distill_Cap_Max [m] 
    /* Nonliner piece*/ * (if t > 1 then (if Distill_X[m,t] > Distill_X[m,t-1] then 1 - distill_Equipment_Setup_Period[m] else 1) else 1) ; 
     
  # Ensure only one mode per period
  ForEachPeriodOnlyOne_Distill_X { t in 1..nPeriod}: sum{m in D_MODE} Distill_X[m,t] <= 1 ;
  
  # Ensure that Crude_Supply > 0 only when Distill_X > 0
  Distill_{m in D_MODE, t in 1..nPeriod}: sum{c in CRUD} Crude_Supply[m,c,t] <= Distill_X[m,t] * 10e5;

 ## Reforming
  # Ensure reforming capacity does not exceed the maximum. Сapacity is reduced during downtime caused by equipment reconfiguring.
  Reforming_Capacity_Max {m in R_MODE, t in 1..nPeriod}: sum{c in DIST_R} Distill_to_Reforming[m,c,t] <= reform_Cap_Max[m] 
    /* Nonliner piece*/ * (if t > 1 then (if Reform_X[m,t] > Reform_X[m,t-1] then 1 - reform_Equipment_Setup_Period[m] else 1) else 1);

  # Ensure only one mode per period   
  ForEachPeriodOnlyOne_Reform_X { t in 1..nPeriod}:  sum{m in R_MODE} Reform_X[m,t] <= 1 ;
  
  # Ensure that Distill_to_Reforming > 0 only when Reform_X > 0
  Reform_{m in R_MODE, t in 1..nPeriod}:sum{c in DIST_R}Distill_to_Reforming[m,c,t] <= Reform_X[m,t] * 10e5;
 
 ## Cracking
  # Ensure cracking capacity does not exceed the maximum. Сapacity is reduced during downtime caused by equipment reconfiguring.
  CrackingCapacity_Max {m in CR_MODE, t in 1..nPeriod}: sum{c in DIST_CR} Distill_to_Cracking[m,c,t] <= crack_Cap_Max[m] 
    /* Nonliner piece*/ * (if t > 1 then (if Cracking_X[m,t] > Cracking_X[m,t-1] then 1 - crack_Equipment_Setup_Period[m] else 1) else 1);
  
  # Ensure only one mode per period
  ForEachPeriodOnlyOne_Cracking_X { t in 1..nPeriod}: sum{m in CR_MODE} Cracking_X[m,t] <= 1 ;
  
  # Ensure that Distill_to_Cracking > 0 only when Cracking_X > 0
  Cracking_{m in CR_MODE, t in 1..nPeriod}: sum{c in DIST_CR} Distill_to_Cracking[m,c,t] <= Cracking_X[m,t] * 10e5;
 
 ## Lubricating 
  # Ensure cracking does not exceed the minimum & maximum volume
  Lube_Oil_Min_Max {t in 1..nPeriod}:        
  lube_limit_Min <= sum{(m,d,l)in LUBRICATING} Distill_to_Lubricating[m,d,t] * lube_Yield[m,d,l] <= lube_limit_Max  ;

  # Сapacity is reduced during downtime caused by equipment reconfiguring.
  Lube_Oil_Capacity_Max {m in L_MODE, t in 1..nPeriod}: sum{c in DIST_L} Distill_to_Lubricating[m,c,t] <= lube_Cap_Max[m] 
    /* Nonliner piece*/ * (if t > 1 then (if Lubricating_X[m,t] > Lubricating_X[m,t-1] then 1 - lube_Equipment_Setup_Period[m] else 1) else 1); # Nonliner piece

  # Ensure only one mode per period
  ForEachPeriodOnlyOne_Lubricating_X { t in 1..nPeriod}: sum{m in L_MODE} Lubricating_X[m,t] <= 1 ;
  
  # Ensure that Distill_to_Lubricating> 0 only when Lubricating_X > 0
  Lubricating_{m in L_MODE, t in 1..nPeriod}: sum{c in DIST_L}Distill_to_Lubricating[m,c,t] <= Lubricating_X[m,t] * 10e5;

 ## Blending   
  PremiumRegularGasRatio {t in 1..nPeriod}:                   # Premium gasoline production: at least 40% of regular gasoline production
    sum{(i,p) in BLENDING: p='Premium Gasoline'} Blending[i,p,t] >=
    sum{(i,p) in BLENDING: p='Regular Gasoline'} Blending[i,p,t] * prod_Premium_Regular_Gas_Min ;
       
  OctaneNumberMin {p in PROD, t in 1..nPeriod: p = 'Premium Gasoline'}:# Ensure octane number requirements for final products
    sum{(i,p) in BLENDING} Blending[i,p,t] * Intermed_Octane[i] >= 
    sum{(ii,p) in BLENDING} Blending[ii,p,t] * prod_Octane_Min[p] ;

  OctaneNumberMin_2 {p in PROD, t in 1..nPeriod: p = 'Regular Gasoline'}:# Ensure octane number requirements for final products
    sum{(i,p) in BLENDING} Blending[i,p,t] * Intermed_Octane[i] >= 
    sum{(ii,p) in BLENDING} Blending[ii,p,t] * prod_Octane_Min[p] ;

  VaporPressure_Max {p in PROD, t in 1..nPeriod: p='Jet Fuel'}:# Ensure vapor pressure limits for products
    sum{(i,p) in BLENDING} Blending[i,p,t] * Intermed_VaporPressure[i] <= 
    sum{(i,p) in BLENDING} Blending[i,p,t] * prod_VaporPressure_Max[p] ;
    
  FuelOilRatio {(i,p) in BLENDING, t in 1..nPeriod: p = 'Fuel Oil'}:   # Maintain the correct ratio for fuel oil production
    Blending['Residuum',p, t] = Blending[i,p,t] * prod_FuelOil_Ratio[i] ; 

 ## Storage
 # Ensure storage fractions are non-negative and within capacity
  Storage_non_negative {p in PROD, t in 1..nPeriod}:  
    0 <= Storage_Fraction[p,t] <= storage_Capacity[p];

 ## Financial calculations
  CashFlow_Balance{t in 1..nPeriod}:                  # Cash flow balance
    initial_Cash
    + sum{tt in 1..t}CashFlow[tt] >= 0;         

 ## Ensure sufficient distillation output
  Distillation_Out {d in DIST, t in 1..nPeriod}:
    sum{m in D_MODE, c in CRUD} Crude_Supply[m,c,t] * distill_Yield[m,c,d]
    >= 
    sum{m in R_MODE, dd in DIST_R: dd=d} Distill_to_Reforming[m,dd,t]
    + sum{m in CR_MODE, dd in DIST_CR: dd=d} Distill_to_Cracking[m,dd,t]
    + sum{m in L_MODE, dd in DIST_L: dd=d} Distill_to_Lubricating[m,dd,t] ;
 
 ## Balance INTERMED products before blending
  INTERMED_Balance{i in INTERMED, t in 1..nPeriod}: 
    sum{(i,p) in BLENDING} Blending[i,p,t] <=
    sum{(m,c,d) in DISTILLATION:d=i} Crude_Supply[m,c,t] * distill_Yield[m,c,d]

    - sum{m in R_MODE, d in DIST_R: d=i} Distill_to_Reforming[m,d,t]
    - sum{m in CR_MODE, d in DIST_CR: d=i} Distill_to_Cracking[m,d,t]
    - sum{m in L_MODE, d in DIST_L: d=i} Distill_to_Lubricating[m,d,t]

    + sum{(m,d,r) in REFORMING: r=i} Distill_to_Reforming[m,d,t] * reform_Yield[m,d,r]
    + sum{(m,d,cr) in CRACKING: cr=i} Distill_to_Cracking[m,d,t] * crack_Yield[m,d,cr]
    + sum{(m,d,l) in LUBRICATING: l=i} Distill_to_Lubricating[m,d,t] * lube_Yield[m,d,l] ;

 ## Prices elastisity
  ForEachPeriodOnlyOne_X {p in PROD, s in STAT, t in 1..nPeriod}: 
    sum{i in 1..nStep} X [p,s,t,i] = 1 ;            # Ensure only one price per product per period
  
  # Double restriction. Ensure demand is within the range dictated by price steps. 
  # The constraint of type min must be set after solving a model without this constraint. #Use Min constraint after ensuring that it does not conflict with other model constraints.
   Demand_Min {p in PROD, s in STAT, t in 1..nPeriod, n in 1..nStep}:
     Demand[p,s,t,n] >= demand_nStep_Value[n] * X[p,s,t,n] * seasonal_Base_Demand[p,t];
  Demand_Max {p in PROD, s in STAT, t in 1..nPeriod, n in 1..nStep}: 
    Demand[p,s,t,n] <= X[p,s,t,n] * demand_nStep_Value[n+1] * seasonal_Base_Demand[p,t];

 ## Shutdown contstraints
  # The number of working periods is equal to the total number of periods under consideration minus the duration of the planned suspension
  Plant_Working_nPeriods: sum{t in 1..nPeriod - plant_Shutdown_Period + 1} Plant_Working[t] = nPeriod - plant_Shutdown_Period;

  # Additionally, there is a restriction in case of a longer (more than 1 period) plant shutdown.
  Shutdown_Distill{t in 1..nPeriod - plant_Shutdown_Period + 1}:
    sum{m in D_MODE, c in CRUD, tt in 0..plant_Shutdown_Period-1} Crude_Supply[m,c,t+tt] <= Plant_Working[t] * 10e5;

4. Load data

%%ampl_eval

data;
### SETS
 # Set of crude oil types
  set CRUD = 'Crude1','Crude2' ;  
        
 # Set of distillation products
  set DIST:= 'Light Naphta','Medium Naphta','Heavy Naphta','Light Oil','Heavy Oil','Residuum' ;

 # Set of reforming products
  set REF:= 'Reformed gasoline' ; 

 # Set of cracking products
  set CRACK:= 'Cracked oil','Cracked gasoline' ;

 # Set of Lubricating products
  set LUBR:= 'Lube Oil';

 # Set of final products
  set PROD:= 'Premium Gasoline','Regular Gasoline','Jet Fuel','Fuel Oil','Lube Oil' ;

 # Set of delivery stations
  set STAT := Station1 ;

 # Set of operating modes for Distillation equipment    
  set D_MODE := D1, D2 ;

 # Set of operating modes for Reforming equipment  
  set R_MODE := R1, R2 ;

 # Set of operating modes for Cracking equipment 
  set CR_MODE := CR1, CR2 ;

 # Set of operating modes for Lubrication equipment 
  set L_MODE := L1, L2 ;

 # Set of pollutants 
  set POLLUT := 'hydrocarbon', 'sulfur dioxide';

 # Set of components and products of mixing process
  set BLENDING:         'Premium Gasoline'  'Regular Gasoline'  'Jet Fuel'  'Fuel Oil'  'Lube Oil':=    
  'Light Naphta'                +                 +               -             -           -   
  'Medium Naphta'               +                 +               -             -           -   
  'Heavy Naphta'                +                 +               -             -           -   
  'Light Oil'                   -                 -               +             +           -  
  'Heavy Oil'                   -                 -               +             +           -   
  'Residuum'                    -                 -               +             +           -   
  'Reformed gasoline'           +                 +               -             -           -   
  'Cracked oil'                 -                 -               +             +           -   
  'Cracked gasoline'            +                 +               -             -           -    
  'Lube Oil'                    -                 -               -             -           + ;

### PARAMETERS
  param nPeriod:= 12 ;                        # Scheduling period (weeks)
  param nPeriodByYear = 12;                   # Number of nPeriods in the Year

 ## Crude Oil
  # Minimum, Maximum supply of crude oil barrels per week
  param:  crude_Min   crude_Max:=
  Crude1  0        40000
  Crude2  0        25000 ;

  # Cost of purchasing crude oil per barrel through the period            
  param crude_Cost :  1   2   3   4   5   6   7   8   9   10  11  12 :=
              Crude1  80  80  82  83  84  85  86  87  88  89  90  91
              Crude2  85  86  87  88  89  90  90  90  90  91  91  91; 

 ## Distillation
  # Yield of products after distillation
  param: DISTILLATION: distill_Yield: 'Light Naphta'  'Medium Naphta'     'Heavy Naphta'  'Light Oil'     'Heavy Oil'     'Residuum' :=
                          D1  Crude1    0.1             0.2                 0.2             0.12            0.2             0.13
                          D1  Crude2    0.15            0.25                0.18            0.08            0.19            0.12 
                          D2  Crude1    0.12            0.19                0.19            0.13            0.19            0.13
                          D2  Crude2    0.14            0.24                0.19            0.09            0.18            0.13  ; 
  
  # Pollutant emissions
    param distill_Pollute: 'hydrocarbon' 'sulfur dioxide':= 
    D1  Crude1              100           120           
    D1  Crude2              105           116
    D2  Crude1              99            115           
    D2  Crude2              108           111 ;

  # Cost of Distillation 1 barrel of crude oil. Residue disposal cost. 
    param:  distill_Cost, distill_Waste_Cost:=
    D1 Crude1  18           2
    D1 Crude2  15           3
    D2 Crude1  17           1.5
    D2 Crude2  16           2.2 ;
  
  # Maximum distillation capacity. Equipment setup period 
    param: distill_Cap_Max, distill_Equipment_Setup_Period, distill_Equipment_Setup_Cost:=
    D1      45000               0.5                               50000
    D2      47000               0.6                               70000;

 ## Reforming
  # Yield of products after Reforming
    param: REFORMING: reform_Yield: 'Reformed gasoline':=
        R1 'Light Naphta'           0.6 
        R1 'Medium Naphta'          0.52
        R1 'Heavy Naphta'           0.45 
        R2 'Light Naphta'           0.6 
        R2 'Medium Naphta'          0.52
        R2 'Heavy Naphta'           0.48  ;

  # Pollutant emissions
    param reform_Pollute: 'hydrocarbon' 'sulfur dioxide':= 
    R1  'Light Naphta'      60           180           
    R1  'Medium Naphta'     70           140
    R1  'Heavy Naphta'      72           115
    R2  'Light Naphta'      65           180           
    R2  'Medium Naphta'     40           150
    R2  'Heavy Naphta'      70           118  ;

  # Cost of Reforming 1 barrel of distilled fraction. Residue disposal cost
    param:           reform_Cost, reform_Waste_Cost:= 
    R1 'Light Naphta'     20        2   
    R1 'Medium Naphta'    26        1.8
    R1 'Heavy Naphta'     28        2.2
    R2 'Light Naphta'     22        2 
    R2 'Medium Naphta'    24        3
    R2 'Heavy Naphta'     26        1.3; 

  # Maximum Reforming capacity. Equipment setup period
    param: reform_Cap_Max, reform_Equipment_Setup_Period, reform_Equipment_Setup_Cost:=
    R1    10000             0.5                             80000
    R2    12000             0.6                             90000;

 ## Cracking
  # Yield of products after Craking
    param: CRACKING: crack_Yield: 'Cracked oil' 'Cracked gasoline':=
                CR1 'Light Oil'      0.68           0.28
                CR1 'Heavy Oil'      0.75           0.2
                CR2 'Light Oil'      0.6            0.34
                CR2 'Heavy Oil'      0.52           0.25  ;
  
  # Pollutant emissions
    param crack_Pollute: 'hydrocarbon' 'sulfur dioxide':= 
    CR1 'Light Oil'      100            200
    CR1 'Heavy Oil'      150            250
    CR2 'Light Oil'      120            180
    CR2 'Heavy Oil'      150            240 ;
  
  # Cost of Cracking 1 barrel of distilled fraction. Residue disposal cost 
    param:           crack_Cost crack_Waste_Cost:= 
    CR1 'Light Oil'      20           1
    CR1 'Heavy Oil'      22           1.2
    CR2 'Light Oil'      21           1.36
    CR2 'Heavy Oil'      18           1.05  ;

  # Maximum Cracking capacity. Equipment setup period
    param: crack_Cap_Max, crack_Equipment_Setup_Period, crack_Equipment_Setup_Cost:=
    CR1     8000          0.3                               25000
    CR2     9000          0.2                               36300;

## Lubricating
  # Yield of products after Lubricating
    param: LUBRICATING: lube_Yield: 'Lube Oil':=
                  L1 'Residuum'      0.5
                  L2 'Residuum'      0.6;

  # Pollutant emissions
    param lube_Pollute: 'hydrocarbon' 'sulfur dioxide':= 
    L1 'Residuum'             60         180
    L2 'Residuum'             70         140  ;

  # Cost of Lubricating 1 barrel of distilled fraction. Residue disposal cost 
    param:           lube_Cost lube_Waste_Cost:= 
    L1 'Residuum'      21         1.2        
    L2 'Residuum'      22         1.4  ;

  # Maximum Cracking capacity. Equipment setup period
    param: lube_Cap_Max, lube_Equipment_Setup_Period, lube_Equipment_Setup_Cost:=
    L1      500             0.2                         5000
    L2      1000            0.1                         8000;

  # Volume Limitations
    param lube_limit_Min := 0 ;
    param lube_limit_Max := 1000 ;

 ## INTERMED
  # Octane numbers and Vapor pressure for each INTERMED fraction
    param:            Intermed_Octane   Intermed_VaporPressure:=
    'Light Naphta'        90                            .
    'Medium Naphta'       80                            .
    'Heavy Naphta'        70                            .
    'Light Oil'           0                             1
    'Heavy Oil'           0                             0.6
    'Residuum'            0                             0.05
    'Reformed gasoline'   115                           .
    'Cracked gasoline'    105                           .   
    'Cracked oil'         0                             1.5          
    'Lube Oil'            0                             .;

 ## Blending
  # Cost of processing 1 barrel of products
    param blending_Cost:=
    'Premium Gasoline'  2
    'Regular Gasoline'	1.9
    'Jet Fuel'	        2.2
    'Fuel Oil'	        2
    'Lube Oil'          2.4  ;
 
 ## Products
  # Minimum octane for products
    param prod_Octane_Min:=
    'Regular Gasoline'  84
    'Premium Gasoline'  94 ;

  # Vapor pressure limits of product
    param prod_VaporPressure_Max :=
    'Jet Fuel' 1 ;

  # Minimum percentage of premium gasoline production relative to regular gasoline
    param prod_Premium_Regular_Gas_Min := 0.4 ;

  # Blending ratio for fuel oil production: Light oil, Crack Oil, Fuel Oil and Residuum mix in the proportion 10:4:3:1
    param prod_FuelOil_Ratio :=
    'Light Oil'   10
    'Cracked oil' 4
    'Heavy Oil'   3
    'Residuum'    1  ;

 ## Storage
  # Storage capacity for each product. Weekly storage cost per barrel. Standard loss percentage during storage
    param:             storage_Capacity    storage_Cost  storage_Waste:=
    'Premium Gasoline'  50000               3             0.003
    'Regular Gasoline'	50000               5             0.002
    'Jet Fuel'	        30000               7             0.001
    'Fuel Oil'	        10000               2             0.0005
    'Lube Oil'          4500                4             0.0005;

  # Cost of delivering products to gas stations
    param delivery_Cost:  'Station1':=
    'Premium Gasoline'    2
    'Regular Gasoline'	  2
    'Jet Fuel'	          3
    'Fuel Oil'	          2
    'Lube Oil'            4 ; 

 ## Plant
    param plant_Shutdown_Period := 1;         # Equipment setup period    
    param plant_Shutdown_Cost = 100000;          # Equipment setup cost        
    param plant_Const_Cost := 300000;            # Plant fixed costs
    param initial_Cash := 2000000 ;               # Initial cash available

 ## Market
  # Changes in demand levels during the season.
    param seasonal_Base_Demand:
                        1       2       3       4       5       6       7       8       9       10      11      12 :=
    'Premium Gasoline'  4500    4500    4500    4500    5000    7000    7500    7000    6000    5000    4500    5000
    'Regular Gasoline'  8500    8500    8500    8800    9200    13500   14000   13500   12000   10000   9000    10000
    'Jet Fuel'          7000    7000    7000    9000    12000   10000   9000    8000    8000    9000    7000    7000
    'Fuel Oil'          4000    3800    3800    3800    2900    2500    2500    2800    3300    3400    3500    3600
    'Lube Oil'          500     500     700     700     700     500     400     500     600     700     700     700;
  
  # Changes in price levels during the season (1 barrel).
    param seasonal_Base_Price :   1   2   3   4   5   6   7   8   9   10  11  12 :=
    'Premium Gasoline'            162 165 165 165 165 165 165 167 168 168 167 165
    'Regular Gasoline'            126 126 127 127 128 128 129 129 129 129 130 130
    'Jet Fuel'                    115 115 115 115 115 116 116 116 117 117 118 118           
    'Fuel Oil'                    154 154 154 155 155 155 155 156 156 156 155 155      
    'Lube Oil'                    176 176 177 177 177 178 178 178 178 178 177 178 ;
  
 ## Price elasticity
    param nStep:= 6 ;           # Number of price steps
                            
    param:  price_nStep_Value   demand_nStep_Value:=
    1       1.06                0.8                                 
    2       1.04                0.88
    3       1.02                0.95
    4       1.0                 1
    5       0.98                1.02
    6       0.96                1.05
    7       0.94                1.09 ;

 ## Finance
  param discount_Rate := 0.05 ; # Financial parameters for discount rate
  
  # Loans
  set LOANS := 'I';
  set LOAN_param := 'term'  'interest'  'Max_Money';
  param loan: 'term'  'interest'  'Max_Money' :=
    I           1       0.065         1e7 ;

5. Solve problem

Use %%ampl_eval to evaluate AMPL commands and declarations

%%ampl_eval
option solver highs;          # Choosing a solver

# Defining Output Settings 
option show_stats 1;        # (1) Show statistical information about the size of the problem. Default 0 (statistics are not displayed)
option display_1col 0;      # Data Display Settings
option omit_zero_rows 1;    # Hide rows with 0 values. Default (0)

solve;                      # Solve the model
Presolve eliminates 15 constraints.
Substitution eliminates 107 variables.
Adjusted problem:
1259 variables:
	372 binary variables
	96 nonlinear variables
	791 linear variables
1414 constraints; 13987 nonzeros
	99 nonlinear constraints
	1315 linear constraints
	97 equality constraints
	1259 inequality constraints
	58 range constraints
1 nonlinear objective; 899 nonzeros.

HiGHS 1.6.0:HiGHS 1.6.0: optimal solution; objective 685848.772
26660 simplex iterations
19 branching nodes

6. Display the solution

Use %%ampl_eval

%%ampl_eval

display Plant_Working, Crude_Supply, Distill_to_Reforming, Distill_to_Cracking, Distill_to_Lubricating,  Blending, OctaneNumberMin, VaporPressure_Max, Storage_Fraction, Demand, Loan_In, Loan_Out, CashFlow, Waste_Pollutant;

printf {p in PROD, t in 1..nPeriod}: "Octane number for products: %u %s %6.2f\n", t, p, sum{(i,p) in BLENDING} Blending[i,p,t] * Intermed_Octane[i] / sum{(ii,p) in BLENDING}(if Blending[ii,p,t] = 0 then 1 else Blending[ii,p,t]);

printf {p in PROD, t in 1..nPeriod: p='Jet Fue'}: "Vapor pressure limits of product Jet Fuel: %u %s %6.2f\n", t, p, sum{(i,p) in BLENDING} Blending[i,p,t] * Intermed_VaporPressure[i] / (if sum{(ii,p) in BLENDING} Blending[ii,p,t] = 0 then 1 else sum{(ii,p) in BLENDING}Blending[ii,p,t]) ;

printf {t in 1..nPeriod}: "Premium / Regular percentage: %u %6.2f\n", t, 
sum{(i,p) in BLENDING: p ='Premium Gasoline'} Blending[i,p,t] / (if sum{(ii,pp) in BLENDING: pp ='Regular Gasoline'}Blending[ii,pp,t] = 0 then 1 else sum{(ii,pp) in BLENDING: pp ='Regular Gasoline'}Blending[ii,pp,t]) ;

printf {(i,p) in BLENDING, t in 1..nPeriod: p='Fuel Oil'}: "Production Fuel Oil ratio: %u   %s %6.2f\n", t, i, 
    Blending[i,p,t] / (if Blending['Residuum',p,t] = 0 then 1 else Blending['Residuum',p,t]);
Plant_Working [*] :=
 1 1    2 1    4 1    5 1    6 1    7 1    8 1    9 1   10 1   11 1   12 1
;

:                                            Crude_Supply      :=
D1                  Crude1             1     1302.75
D1                  Crude1             2    17845.2
D1                  Crude1             4     4749.83
D1                  Crude1             5     8528.38
D1                  Crude1             6     5394.44
D1                  Crude1             7     3205.38
D1                  Crude1             8     5826.38
D1                  Crude1             9     5272.35
D1                  Crude2             1    25000
D1                  Crude2             2    25000
D1                  Crude2             4    25000
D1                  Crude2             5    25000
D1                  Crude2             6    25000
D1                  Crude2             7    25000
D1                  Crude2             8    25000
D1                  Crude2             9    25000
D1                  Crude2             10   25000
D1                  Crude2             11   23582.3
D1                  Crude2             12   25000
D2                  Crude1             3        2.30926e-10
;
# $1 = Distill_to_Reforming
# $2 = Distill_to_Cracking
:                                                 $1               $2
 :=
CR1                 'Light Oil'        1           .            1.67187e-13
CR2                 'Heavy Oil'        5           .           -5.05151e-13
CR2                 'Light Oil'        1           .          940.594
CR2                 'Light Oil'        2           .         1787.13
CR2                 'Light Oil'        3           .           -2.08216e-11
CR2                 'Light Oil'        4           .          893.564
CR2                 'Light Oil'        5           .          681.931
CR2                 'Light Oil'        6           .         2506.24
CR2                 'Light Oil'        7           .         2243.56
CR2                 'Light Oil'        8           .         2440.41
CR2                 'Light Oil'        9           .         2387.48
CR2                 'Light Oil'        10          .         1433.34
CR2                 'Light Oil'        11          .          684.032
CR2                 'Light Oil'        12          .          808.559
R2                  'Heavy Naphta'     1    4184.39                 .
R2                  'Heavy Naphta'     2    5779.87                 .
R2                  'Heavy Naphta'     3       3.30119e-11          .
R2                  'Heavy Naphta'     4    4921.27                 .
R2                  'Heavy Naphta'     5    5368.11                 .
R2                  'Heavy Naphta'     6    3798.97                 .
R2                  'Heavy Naphta'     7    3533.07                 .
R2                  'Heavy Naphta'     8    3875.33                 .
R2                  'Heavy Naphta'     9    4053.69                 .
R2                  'Heavy Naphta'     10   3437.19                 .
R2                  'Heavy Naphta'     11   3427.37                 .
R2                  'Heavy Naphta'     12   3505.75                 .
;
# $1 = Distill_to_Lubricating
:                                          $1      Blending       :=
'Cracked gasoline'  'Premium Gasoline' 2    .     45.5924
'Cracked gasoline'  'Premium Gasoline' 4    .    303.812
'Cracked gasoline'  'Premium Gasoline' 5    .    231.856
'Cracked gasoline'  'Premium Gasoline' 6    .    558.735
'Cracked gasoline'  'Premium Gasoline' 7    .    462.559
'Cracked gasoline'  'Premium Gasoline' 9    .    232.723
'Cracked gasoline'  'Premium Gasoline' 11   .    232.571
'Cracked gasoline'  'Premium Gasoline' 12   .    274.91
'Cracked gasoline'  'Regular Gasoline' 1    .    319.802
'Cracked gasoline'  'Regular Gasoline' 2    .    562.031
'Cracked gasoline'  'Regular Gasoline' 3    .     -1.95174e-12
'Cracked gasoline'  'Regular Gasoline' 6    .    293.388
'Cracked gasoline'  'Regular Gasoline' 7    .    300.25
'Cracked gasoline'  'Regular Gasoline' 8    .    829.74
'Cracked gasoline'  'Regular Gasoline' 9    .    579.02
'Cracked gasoline'  'Regular Gasoline' 10   .    487.337
'Cracked oil'       'Fuel Oil'         1    .    564.356
'Cracked oil'       'Fuel Oil'         2    .   1072.28
'Cracked oil'       'Fuel Oil'         4    .    536.139
'Cracked oil'       'Fuel Oil'         5    .    409.158
'Cracked oil'       'Fuel Oil'         6    .    352.723
'Cracked oil'       'Fuel Oil'         7    .    352.723
'Cracked oil'       'Fuel Oil'         8    .    646.887
'Cracked oil'       'Fuel Oil'         9    .    613.005
'Cracked oil'       'Fuel Oil'         10   .    186.633
'Cracked oil'       'Fuel Oil'         11   .    410.419
'Cracked oil'       'Fuel Oil'         12   .    485.135
'Cracked oil'       'Jet Fuel'         6    .   1151.02
'Cracked oil'       'Jet Fuel'         7    .    993.411
'Cracked oil'       'Jet Fuel'         8    .    817.359
'Cracked oil'       'Jet Fuel'         9    .    819.483
'Cracked oil'       'Jet Fuel'         10   .    673.373
'Heavy Naphta'      'Premium Gasoline' 2    .   2289.18
'Heavy Naphta'      'Premium Gasoline' 6    .   1779.92
'Heavy Naphta'      'Premium Gasoline' 7    .   1608
'Heavy Naphta'      'Premium Gasoline' 8    .   1397.9
'Heavy Naphta'      'Regular Gasoline' 1    .    576.155
'Heavy Naphta'      'Regular Gasoline' 3    .      1.08641e-11
'Heavy Naphta'      'Regular Gasoline' 4    .    528.701
'Heavy Naphta'      'Regular Gasoline' 5    .    837.565
'Heavy Naphta'      'Regular Gasoline' 8    .    392.043
'Heavy Naphta'      'Regular Gasoline' 9    .   1500.78
'Heavy Naphta'      'Regular Gasoline' 10   .   1062.81
'Heavy Naphta'      'Regular Gasoline' 11   .    817.445
'Heavy Naphta'      'Regular Gasoline' 12   .    994.249
'Heavy Oil'         'Fuel Oil'         1    .    752.475
'Heavy Oil'         'Fuel Oil'         2    .   1429.7
'Heavy Oil'         'Fuel Oil'         4    .    714.851
'Heavy Oil'         'Fuel Oil'         5    .    545.545
'Heavy Oil'         'Fuel Oil'         6    .    470.297
'Heavy Oil'         'Fuel Oil'         7    .    470.297
'Heavy Oil'         'Fuel Oil'         8    .    862.516
'Heavy Oil'         'Fuel Oil'         9    .    817.339
'Heavy Oil'         'Fuel Oil'         10   .    248.844
'Heavy Oil'         'Fuel Oil'         11   .    547.226
'Heavy Oil'         'Fuel Oil'         12   .    646.847
'Heavy Oil'         'Jet Fuel'         1    .   4258.07
'Heavy Oil'         'Jet Fuel'         2    .   6889.34
'Heavy Oil'         'Jet Fuel'         3    .      4.39297e-11
'Heavy Oil'         'Jet Fuel'         4    .   4985.12
'Heavy Oil'         'Jet Fuel'         5    .   5910.13
'Heavy Oil'         'Jet Fuel'         6    .   5358.59
'Heavy Oil'         'Jet Fuel'         7    .   4920.78
'Heavy Oil'         'Jet Fuel'         8    .   5052.76
'Heavy Oil'         'Jet Fuel'         9    .   4987.13
'Heavy Oil'         'Jet Fuel'         10   .   4501.16
'Heavy Oil'         'Jet Fuel'         11   .   3933.41
'Heavy Oil'         'Jet Fuel'         12   .   4103.15
'Light Naphta'      'Premium Gasoline' 1    .   3880.28
'Light Naphta'      'Premium Gasoline' 2    .    955.602
'Light Naphta'      'Premium Gasoline' 3    .      2.77112e-11
'Light Naphta'      'Premium Gasoline' 4    .    598.683
'Light Naphta'      'Premium Gasoline' 5    .     36.0166
'Light Naphta'      'Premium Gasoline' 6    .    430.376
'Light Naphta'      'Premium Gasoline' 7    .    527.363
'Light Naphta'      'Premium Gasoline' 8    .   1378.44
'Light Naphta'      'Premium Gasoline' 10   .    385.528
'Light Naphta'      'Premium Gasoline' 11   .   3441.76
'Light Naphta'      'Premium Gasoline' 12   .   3377.65
'Light Naphta'      'Regular Gasoline' 2    .   4578.92
'Light Naphta'      'Regular Gasoline' 4    .   3626.3
'Light Naphta'      'Regular Gasoline' 5    .   4566.82
'Light Naphta'      'Regular Gasoline' 6    .   3859.07
'Light Naphta'      'Regular Gasoline' 7    .   3543.18
'Light Naphta'      'Regular Gasoline' 8    .   2954.2
'Light Naphta'      'Regular Gasoline' 9    .   4277.23
'Light Naphta'      'Regular Gasoline' 10   .   3364.47
'Light Naphta'      'Regular Gasoline' 11   .     95.5879
'Light Naphta'      'Regular Gasoline' 12   .    372.352
'Light Oil'         'Fuel Oil'         1    .    225.743
'Light Oil'         'Fuel Oil'         2    .    428.911
'Light Oil'         'Fuel Oil'         4    .    214.455
'Light Oil'         'Fuel Oil'         5    .    163.663
'Light Oil'         'Fuel Oil'         6    .    141.089
'Light Oil'         'Fuel Oil'         7    .    141.089
'Light Oil'         'Fuel Oil'         8    .    258.755
'Light Oil'         'Fuel Oil'         9    .    245.202
'Light Oil'         'Fuel Oil'         10   .     74.6532
'Light Oil'         'Fuel Oil'         11   .    164.168
'Light Oil'         'Fuel Oil'         12   .    194.054
'Light Oil'         'Jet Fuel'         1    .    989.993
'Light Oil'         'Jet Fuel'         2    .   1925.39
'Light Oil'         'Jet Fuel'         3    .      3.25453e-11
'Light Oil'         'Jet Fuel'         4    .   1461.96
'Light Oil'         'Jet Fuel'         5    .   2177.81
'Light Oil'         'Jet Fuel'         10   .    492.004
'Light Oil'         'Jet Fuel'         11   .   1038.39
'Light Oil'         'Jet Fuel'         12   .    997.387
'Medium Naphta'     'Premium Gasoline' 1    .    717.55
'Medium Naphta'     'Premium Gasoline' 3    .     -2.45998e-12
'Medium Naphta'     'Premium Gasoline' 4    .   3610.97
'Medium Naphta'     'Premium Gasoline' 5    .   4036.92
'Medium Naphta'     'Premium Gasoline' 9    .   3101.51
'Medium Naphta'     'Premium Gasoline' 10   .   2364.62
'Medium Naphta'     'Regular Gasoline' 1    .   5793
'Medium Naphta'     'Regular Gasoline' 2    .   9819.05
'Medium Naphta'     'Regular Gasoline' 3    .      4.6336e-11
'Medium Naphta'     'Regular Gasoline' 4    .   3589
'Medium Naphta'     'Regular Gasoline' 5    .   3918.75
'Medium Naphta'     'Regular Gasoline' 6    .   7328.89
'Medium Naphta'     'Regular Gasoline' 7    .   6891.08
'Medium Naphta'     'Regular Gasoline' 8    .   7415.28
'Medium Naphta'     'Regular Gasoline' 9    .   4202.96
'Medium Naphta'     'Regular Gasoline' 10   .   3885.38
'Medium Naphta'     'Regular Gasoline' 11   .   5895.58
'Medium Naphta'     'Regular Gasoline' 12   .   6250
'Reformed gasoline' 'Premium Gasoline' 1    .   1217.47
'Reformed gasoline' 'Premium Gasoline' 2    .   2774.34
'Reformed gasoline' 'Premium Gasoline' 3    .      3.63833e-12
'Reformed gasoline' 'Premium Gasoline' 4    .   2362.21
'Reformed gasoline' 'Premium Gasoline' 5    .   2576.69
'Reformed gasoline' 'Premium Gasoline' 6    .   1823.5
'Reformed gasoline' 'Premium Gasoline' 7    .   1695.88
'Reformed gasoline' 'Premium Gasoline' 8    .   1860.16
'Reformed gasoline' 'Premium Gasoline' 9    .   1945.77
'Reformed gasoline' 'Premium Gasoline' 10   .   1649.85
'Reformed gasoline' 'Premium Gasoline' 11   .    533.75
'Reformed gasoline' 'Premium Gasoline' 12   .    499.361
'Reformed gasoline' 'Regular Gasoline' 1    .    791.043
'Reformed gasoline' 'Regular Gasoline' 3    .      1.22074e-11
'Reformed gasoline' 'Regular Gasoline' 11   .   1111.39
'Reformed gasoline' 'Regular Gasoline' 12   .   1183.4
Residuum            'Fuel Oil'         1    .   2257.43
Residuum            'Fuel Oil'         2    .   4289.11
Residuum            'Fuel Oil'         4    .   2144.55
Residuum            'Fuel Oil'         5    .   1636.63
Residuum            'Fuel Oil'         6    .   1410.89
Residuum            'Fuel Oil'         7    .   1410.89
Residuum            'Fuel Oil'         8    .   2587.55
Residuum            'Fuel Oil'         9    .   2452.02
Residuum            'Fuel Oil'         10   .    746.532
Residuum            'Fuel Oil'         11   .   1641.68
Residuum            'Fuel Oil'         12   .   1940.54
Residuum            'Jet Fuel'         1    .    911.932
Residuum            'Jet Fuel'         2    .   1030.77
Residuum            'Jet Fuel'         3    .      3.02269e-11
Residuum            'Jet Fuel'         4    .   1472.92
Residuum            'Jet Fuel'         5    .   2472.06
Residuum            'Jet Fuel'         6    .   2290.39
Residuum            'Jet Fuel'         7    .   2005.81
Residuum            'Jet Fuel'         8    .   1169.88
Residuum            'Jet Fuel'         9    .   1233.39
Residuum            'Jet Fuel'         10   .   2253.47
Residuum            'Jet Fuel'         11   .   1188.2
Residuum            'Jet Fuel'         12   .   1059.46
;

:                     OctaneNumberMin VaporPressure_Max Storage_Fraction    :=
'Fuel Oil'         1          .                .             9.09495e-13
'Fuel Oil'         2          .                .          3608.19
'Fuel Oil'         3          .                .             5.00222e-12
'Fuel Oil'         4          .                .             4.54747e-12
'Fuel Oil'         5          .                .             4.54747e-12
'Fuel Oil'         6          .                .             5.00222e-12
'Fuel Oil'         7          .                .             3.63798e-12
'Fuel Oil'         8          .                .          1694.86
'Fuel Oil'         9          .                .          2686.93
'Fuel Oil'         10         .                .           714.576
'Fuel Oil'         11         .                .           153.347
'Fuel Oil'         12         .                .            -4.54747e-13
'Jet Fuel'         1          .                0             1.81899e-12
'Jet Fuel'         2          .                0          3681.82
'Jet Fuel'         3          .                0             3.63798e-12
'Jet Fuel'         4          .                0             3.63798e-12
'Jet Fuel'         5          .                0             3.63798e-12
'Jet Fuel'         6          .                0            -1.81899e-12
'Jet Fuel'         7          .                0             5.45697e-12
'Jet Fuel'         8          .                0             1.18234e-11
'Jet Fuel'         9          .                0             3.63798e-12
'Jet Fuel'         10         .                0             8.18545e-12
'Jet Fuel'         11         .                0            -1.81899e-12
'Jet Fuel'         12         .                0             9.09495e-12
'Premium Gasoline' 1          0                .          1849.73
'Premium Gasoline' 2          0                .          3948.12
'Premium Gasoline' 3          0                .            -4.54747e-12
'Premium Gasoline' 4          0                .          2906.92
'Premium Gasoline' 5          0                .          5380.97
'Premium Gasoline' 6          0                .          3818.21
'Premium Gasoline' 7          0                .          1518.93
'Premium Gasoline' 8          0                .            -1.00044e-11
'Premium Gasoline' 9          0                .            -1.81899e-11
'Premium Gasoline' 10         0                .             6.36646e-12
'Premium Gasoline' 11         0                .           247.336
'Premium Gasoline' 12         0                .            -5.45697e-12
'Regular Gasoline' 2          .                .          7465.04
'Regular Gasoline' 3          .                .            -9.09495e-12
'Regular Gasoline' 4          .                .            -1.09139e-11
'Regular Gasoline' 5          .                .          1224.69
'Regular Gasoline' 6          .                .           826.827
'Regular Gasoline' 7          .                .            -1.27329e-11
'Regular Gasoline' 9          .                .             1.27329e-11
'Regular Gasoline' 10         .                .            -1.45519e-11
'Regular Gasoline' 11         .                .             5.45697e-12
'Regular Gasoline' 12         .                .            -1.81899e-12
;

Demand ['Fuel Oil',Station1,*,*]
:         1          2          3         4   5   6    :=
1     0             3800    0             0   0   0
2     0             3610    0             0   0   0
3    -5.71968e-12   3610    0             0   0   0
4     0             3610    0             0   0   0
5     0             2755    0             0   0   0
6     0             2375    0             0   0   0
7     0             2375    0             0   0   0
8     0             2660   -5.16758e-13   0   0   0
9     0             3135    0             0   0   0
10    0             3230    0             0   0   0
11    0             3325    0             0   0   0
12   -1.08674e-10   3420    0             0   0   0

 ['Jet Fuel',Station1,*,*]
:       1      2   3   4   5   6    :=
1     6160     0   0   0   0   0
2     6160     0   0   0   0   0
3     3685.5   0   0   0   0   0
4     7920     0   0   0   0   0
5    10560     0   0   0   0   0
6     8800     0   0   0   0   0
7     7920     0   0   0   0   0
8     7040     0   0   0   0   0
9     7040     0   0   0   0   0
10    7920     0   0   0   0   0
11    6160     0   0   0   0   0
12    6160     0   0   0   0   0

 ['Lube Oil',Station1,*,*]
:    1   2   3   4   5   6    :=

 ['Premium Gasoline',Station1,*,*]
:     1     2   3   4   5   6    :=
1    3960   0   0   0   0   0
2    3960   0   0   0   0   0
3    3960   0   0   0   0   0
4    3960   0   0   0   0   0
5    4400   0   0   0   0   0
6    6160   0   0   0   0   0
7    6600   0   0   0   0   0
8    6160   0   0   0   0   0
9    5280   0   0   0   0   0
10   4400   0   0   0   0   0
11   3960   0   0   0   0   0
12   4400   0   0   0   0   0

 ['Regular Gasoline',Station1,*,*]
:       1      2   3   4   5   6    :=
1     7480     0   0   0   0   0
2     7480     0   0   0   0   0
3     7480     0   0   0   0   0
4     7744     0   0   0   0   0
5     8096     0   0   0   0   0
6    11880     0   0   0   0   0
7    11563     0   0   0   0   0
8    11591.3   0   0   0   0   0
9    10560     0   0   0   0   0
10    8800     0   0   0   0   0
11    7920     0   0   0   0   0
12    8800     0   0   0   0   0
;

:      Loan_In  Loan_Out    :=
I 2    80858.5       0
I 3        0     81296.5
;

CashFlow [*] :=
 1  -124454       4  -341312       7   569130      10   315474
 2 -1875550       5  -475846       8   157371      11    98679.3
 3  2204550       6   353639       9     2035.04   12   144353
;

Waste_Pollutant [*,*] (tr)
:      hydrocarbon     'sulfur dioxide'    :=
1    244348            361729
2    391231            568025
3         2.1948e-09        3.12856e-09
4    288067            427118
5    321701            474922
6    262049            379539
7    239534            347252
8    266515            386105
9    269856            393153
10   214184            313386
11   203966            299758
12   212181            310845
;

Octane number for products: 1 Premium Gasoline  93.97
Octane number for products: 2 Premium Gasoline  93.98
Octane number for products: 3 Premium Gasoline   0.00
Octane number for products: 4 Premium Gasoline  93.99
Octane number for products: 5 Premium Gasoline  93.99
Octane number for products: 6 Premium Gasoline  93.98
Octane number for products: 7 Premium Gasoline  93.98
Octane number for products: 8 Premium Gasoline  93.96
Octane number for products: 9 Premium Gasoline  93.96
Octane number for products: 10 Premium Gasoline  93.96
Octane number for products: 11 Premium Gasoline  93.96
Octane number for products: 12 Premium Gasoline  93.95
Octane number for products: 1 Regular Gasoline  83.99
Octane number for products: 2 Regular Gasoline  83.99
Octane number for products: 3 Regular Gasoline   0.00
Octane number for products: 4 Regular Gasoline  83.98
Octane number for products: 5 Regular Gasoline  83.98
Octane number for products: 6 Regular Gasoline  83.99
Octane number for products: 7 Regular Gasoline  83.98
Octane number for products: 8 Regular Gasoline  83.99
Octane number for products: 9 Regular Gasoline  83.99
Octane number for products: 10 Regular Gasoline  83.99
Octane number for products: 11 Regular Gasoline  83.99
Octane number for products: 12 Regular Gasoline  83.99
Octane number for products: 1 Jet Fuel   0.00
Octane number for products: 2 Jet Fuel   0.00
Octane number for products: 3 Jet Fuel   0.00
Octane number for products: 4 Jet Fuel   0.00
Octane number for products: 5 Jet Fuel   0.00
Octane number for products: 6 Jet Fuel   0.00
Octane number for products: 7 Jet Fuel   0.00
Octane number for products: 8 Jet Fuel   0.00
Octane number for products: 9 Jet Fuel   0.00
Octane number for products: 10 Jet Fuel   0.00
Octane number for products: 11 Jet Fuel   0.00
Octane number for products: 12 Jet Fuel   0.00
Octane number for products: 1 Fuel Oil   0.00
Octane number for products: 2 Fuel Oil   0.00
Octane number for products: 3 Fuel Oil   0.00
Octane number for products: 4 Fuel Oil   0.00
Octane number for products: 5 Fuel Oil   0.00
Octane number for products: 6 Fuel Oil   0.00
Octane number for products: 7 Fuel Oil   0.00
Octane number for products: 8 Fuel Oil   0.00
Octane number for products: 9 Fuel Oil   0.00
Octane number for products: 10 Fuel Oil   0.00
Octane number for products: 11 Fuel Oil   0.00
Octane number for products: 12 Fuel Oil   0.00
Octane number for products: 1 Lube Oil   0.00
Octane number for products: 2 Lube Oil   0.00
Octane number for products: 3 Lube Oil   0.00
Octane number for products: 4 Lube Oil   0.00
Octane number for products: 5 Lube Oil   0.00
Octane number for products: 6 Lube Oil   0.00
Octane number for products: 7 Lube Oil   0.00
Octane number for products: 8 Lube Oil   0.00
Octane number for products: 9 Lube Oil   0.00
Octane number for products: 10 Lube Oil   0.00
Octane number for products: 11 Lube Oil   0.00
Octane number for products: 12 Lube Oil   0.00
Premium / Regular percentage: 1   0.78
Premium / Regular percentage: 2   0.41
Premium / Regular percentage: 3   0.43
Premium / Regular percentage: 4   0.89
Premium / Regular percentage: 5   0.74
Premium / Regular percentage: 6   0.40
Premium / Regular percentage: 7   0.40
Premium / Regular percentage: 8   0.40
Premium / Regular percentage: 9   0.50
Premium / Regular percentage: 10   0.50
Premium / Regular percentage: 11   0.53
Premium / Regular percentage: 12   0.47
Production Fuel Oil ratio: 1   Light Oil   0.10
Production Fuel Oil ratio: 2   Light Oil   0.10
Production Fuel Oil ratio: 3   Light Oil   0.00
Production Fuel Oil ratio: 4   Light Oil   0.10
Production Fuel Oil ratio: 5   Light Oil   0.10
Production Fuel Oil ratio: 6   Light Oil   0.10
Production Fuel Oil ratio: 7   Light Oil   0.10
Production Fuel Oil ratio: 8   Light Oil   0.10
Production Fuel Oil ratio: 9   Light Oil   0.10
Production Fuel Oil ratio: 10   Light Oil   0.10
Production Fuel Oil ratio: 11   Light Oil   0.10
Production Fuel Oil ratio: 12   Light Oil   0.10
Production Fuel Oil ratio: 1   Heavy Oil   0.33
Production Fuel Oil ratio: 2   Heavy Oil   0.33
Production Fuel Oil ratio: 3   Heavy Oil   0.00
Production Fuel Oil ratio: 4   Heavy Oil   0.33
Production Fuel Oil ratio: 5   Heavy Oil   0.33
Production Fuel Oil ratio: 6   Heavy Oil   0.33
Production Fuel Oil ratio: 7   Heavy Oil   0.33
Production Fuel Oil ratio: 8   Heavy Oil   0.33
Production Fuel Oil ratio: 9   Heavy Oil   0.33
Production Fuel Oil ratio: 10   Heavy Oil   0.33
Production Fuel Oil ratio: 11   Heavy Oil   0.33
Production Fuel Oil ratio: 12   Heavy Oil   0.33
Production Fuel Oil ratio: 1   Residuum   1.00
Production Fuel Oil ratio: 2   Residuum   1.00
Production Fuel Oil ratio: 3   Residuum   0.00
Production Fuel Oil ratio: 4   Residuum   1.00
Production Fuel Oil ratio: 5   Residuum   1.00
Production Fuel Oil ratio: 6   Residuum   1.00
Production Fuel Oil ratio: 7   Residuum   1.00
Production Fuel Oil ratio: 8   Residuum   1.00
Production Fuel Oil ratio: 9   Residuum   1.00
Production Fuel Oil ratio: 10   Residuum   1.00
Production Fuel Oil ratio: 11   Residuum   1.00
Production Fuel Oil ratio: 12   Residuum   1.00
Production Fuel Oil ratio: 1   Cracked oil   0.25
Production Fuel Oil ratio: 2   Cracked oil   0.25
Production Fuel Oil ratio: 3   Cracked oil   0.00
Production Fuel Oil ratio: 4   Cracked oil   0.25
Production Fuel Oil ratio: 5   Cracked oil   0.25
Production Fuel Oil ratio: 6   Cracked oil   0.25
Production Fuel Oil ratio: 7   Cracked oil   0.25
Production Fuel Oil ratio: 8   Cracked oil   0.25
Production Fuel Oil ratio: 9   Cracked oil   0.25
Production Fuel Oil ratio: 10   Cracked oil   0.25
Production Fuel Oil ratio: 11   Cracked oil   0.25
Production Fuel Oil ratio: 12   Cracked oil   0.25

7. Further steps

Here is a list of steps required to successfully implement the results of mathematical optimization into the refinery production:

  • Verify Solution Accuracy

    • Check Constraints

    • Validate Assumptions

    • Cross-Check with Historical Data

  • Sensitivity Analysis

    • Analyze how changes in key parameters (e.g., crude oil prices, demand forecasts) affect the solution

  • Scenario Analysis

    • Evaluate the solution under different scenarios (e.g., market fluctuations, supply disruptions)

  • Risk Management

    • Identify Potential Risks: Operational, Market, External

  • Monitoring and Feedback

    • Implementation Monitoring

    • Continuous Improvement

  • Review and Update

    • Post-Implementation Review