Current Working Directory

WorkingDirectory <- getwd()
WorkingDirectory
## [1] "C:/Users/Usuario/Desktop/LOGISTICA/Whosale Contract"

Setting the Working Directory

Here, you must to set your data directory

DataDirectory <- "C:/Users/Usuario/Desktop/LOGISTICA/Whosale Contract/"
FunctionsDirectory <- "C:/Users/Usuario/Desktop/LOGISTICA/Whosale Contract/"
source(paste(FunctionsDirectory,"Functions.R",sep=""))

Setting the libraries

#dplyr provides a flexible grammar of data manipulation.
  library(dplyr)

Reading the Data

FileName <- paste(DataDirectory,"4.1 - Wholesale Contract Data.csv",sep="")
InitialLine <- 1
ContractType <- "Wholesale"
datos <- fReadSupplyContractsData(FileName,InitialLine,ContractType)
R <- datos$R
W <- datos$W
S <- datos$S
C <- datos$C
FixedCost <- datos$FixedCost
Cu <- datos$Cu
Co <- datos$Co

Reading the Demand Distribution

InitialLine <- 4
DemandDistribution <- fReadDemandDistribution(FileName,InitialLine)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
DemandDistribution
##   Demand Probability
## 1   5000        0.15
## 2   7000        0.20
## 3   9000        0.35
## 4  11000        0.20
## 5  13000        0.10
DemandDistribution$Demand
## [1]  5000  7000  9000 11000 13000

Calculation of Mean and Standard Deviation

Parameters <- fMeanStdDev(DemandDistribution)
MeanDemand <- Parameters[1]
StdDev <- Parameters[2]
MeanDemand
## DemandxProb 
##        8800
StdDev
## DemandSquaredxProb 
##           2357.965

Calculation of Retailer Profits

RetailerProfits <- fRetailerProfits(DemandDistribution,datos,ContractType)
RetailerProfits
##       Q1     Q2     Q3     Q4     Q5
## 1 350000 270000 190000 110000  30000
## 2 350000 490000 410000 330000 250000
## 3 350000 490000 630000 550000 470000
## 4 350000 490000 630000 770000 690000
## 5 350000 490000 630000 770000 910000

Calculation of Total Retailer Profits

TotalRetailerProfits <- fTotalRetailerProfits(DemandDistribution,RetailerProfits)
TotalRetailerProfits
## [1] 350000 457000 520000 506000 448000

][] Calculation of Supplier Profits

SupplierProfits <- fSupplierProfits1(DemandDistribution, datos, ContractType)
SupplierProfits
##        [,1]   [,2]   [,3]   [,4]   [,5]
## [1,] 250000 350000 450000 550000 650000
## [2,] 250000 350000 450000 550000 650000
## [3,] 250000 350000 450000 550000 650000
## [4,] 250000 350000 450000 550000 650000
## [5,] 250000 350000 450000 550000 650000

Calculation of Total Supplier Profits

Quantities <- length(DemandDistribution$Demand)
TotalSupplierProfits <- fTotalSupplierProfits(Quantities,1,FixedCost,DemandDistribution,SupplierProfits)
TotalSupplierProfits
## [1] 150000 250000 350000 450000 550000

Calculation of Total Profits

TotalProfits <- c(rep(0,length(DemandDistribution$Demand)))
for (i in 1:length(DemandDistribution$Demand)) {
  TotalProfits[i] <- TotalRetailerProfits[i] + TotalSupplierProfits[i]
}
TotalProfits
## [1] 500000 707000 870000 956000 998000

Calculation of ProfitSelected and Q selected

RetailerProfitSelected <- max(TotalRetailerProfits)
print(paste("Retailer Profit Selected = " , RetailerProfitSelected,sep = " "))
## [1] "Retailer Profit Selected =  520000"
IndexSelected <- match(RetailerProfitSelected,TotalRetailerProfits)
print(paste("Index  Selected = " , IndexSelected, sep = " "))
## [1] "Index  Selected =  3"
QSelected <- DemandDistribution$Demand[IndexSelected]
print(paste("Q Selected =",QSelected,sep = " "))
## [1] "Q Selected = 9000"
SupplierProfitSelected <- TotalSupplierProfits[IndexSelected]
print(paste("Supplier Profit Selected = " , SupplierProfitSelected,sep = " "))
## [1] "Supplier Profit Selected =  350000"
TotalProfitSelected <- TotalProfits[IndexSelected]
print(paste("Total Profit Selected = " , TotalProfitSelected,sep = " "))
## [1] "Total Profit Selected =  870000"
MaxTotalProfitSelected <- max(TotalProfits)
MaxIndexSelected <- match(MaxTotalProfitSelected,TotalProfits)
print(paste("Maximum Total Profit Selected = " ,MaxTotalProfitSelected,sep = " "))
## [1] "Maximum Total Profit Selected =  998000"
MaxQSelected <- DemandDistribution$Demand[MaxIndexSelected]
print(paste("Q Selected =",MaxQSelected,sep = " "))
## [1] "Q Selected = 13000"

Line Graph

Titles <- c("Retailer","Wholesaler","Total")
fLineGraph(Titles, DemandDistribution$Demand, TotalRetailerProfits, TotalSupplierProfits, TotalProfits)