This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.

options(repos = c(CRAN = "https://cloud.r-project.org"))


install.packages("lpSolveAPI")
## 
## The downloaded binary packages are in
##  /var/folders/hz/5q2q8k394x507scjt4xk2xjw0000gn/T//RtmpPMnKyG/downloaded_packages
library(lpSolveAPI)
if (!require("lpSolveAPI", quietly = TRUE))
  install.packages("lpSolveAPI", dependencies = TRUE, repos = "https://cloud.r-project.org")
lp_model <- make.lp(0, 2)
lp_model <- make.lp(0, 2)  # 0 constraints, 2 decision variables
lp.control(lp_model, sense = "maximize")  # Set to maximize (default is minimize)
## $anti.degen
## [1] "fixedvars" "stalling" 
## 
## $basis.crash
## [1] "none"
## 
## $bb.depthlimit
## [1] -50
## 
## $bb.floorfirst
## [1] "automatic"
## 
## $bb.rule
## [1] "pseudononint" "greedy"       "dynamic"      "rcostfixing" 
## 
## $break.at.first
## [1] FALSE
## 
## $break.at.value
## [1] 1e+30
## 
## $epsilon
##       epsb       epsd      epsel     epsint epsperturb   epspivot 
##      1e-10      1e-09      1e-12      1e-07      1e-05      2e-07 
## 
## $improve
## [1] "dualfeas" "thetagap"
## 
## $infinite
## [1] 1e+30
## 
## $maxpivot
## [1] 250
## 
## $mip.gap
## absolute relative 
##    1e-11    1e-11 
## 
## $negrange
## [1] -1e+06
## 
## $obj.in.basis
## [1] TRUE
## 
## $pivoting
## [1] "devex"    "adaptive"
## 
## $presolve
## [1] "none"
## 
## $scalelimit
## [1] 5
## 
## $scaling
## [1] "geometric"   "equilibrate" "integers"   
## 
## $sense
## [1] "maximize"
## 
## $simplextype
## [1] "dual"   "primal"
## 
## $timeout
## [1] 0
## 
## $verbose
## [1] "neutral"
set.objfn(lp_model, c(9, 12.5))  # Profit coefficients


add.constraint(lp_model, c(8.5, 12.5), "<=", 4000)  # Fabric constraint
add.constraint(lp_model, c(1.5, 2), "<=", 650)      # Machine time constraint
add.constraint(lp_model, c(2, 3), "<=", 900)         # Tailor time constraint
add.constraint(lp_model, c(1, 0), "<=", 400)         # Jacket sales constraint
add.constraint(lp_model, c(0, 1), "<=", 150)         # Parka sales constraint
objective_function_coefficients <- c(9, 12.5)
constraint_coefficients <- matrix(c(8.5, 12.5, 1.5, 2, 2, 3, 1, 0, 0, 0, 0, 0, 0, 1), nrow = 7, byrow = TRUE)


cat("Objective Function Coefficients:\n")
## Objective Function Coefficients:
print(objective_function_coefficients)
## [1]  9.0 12.5
cat("\n")
print(constraint_coefficients)
##      [,1] [,2]
## [1,]  8.5 12.5
## [2,]  1.5  2.0
## [3,]  2.0  3.0
## [4,]  1.0  0.0
## [5,]  0.0  0.0
## [6,]  0.0  0.0
## [7,]  0.0  1.0
install.packages("lpSolve")
## 
## The downloaded binary packages are in
##  /var/folders/hz/5q2q8k394x507scjt4xk2xjw0000gn/T//RtmpPMnKyG/downloaded_packages
library(lpSolve)
# Define your LP model variables
obj.coef <- c(5, 3)  # Replace with the coefficients of the objective function
const.mat <- matrix(c(2, 1, 1, 2), ncol = 2, byrow = TRUE)  # Replace with the constraint matrix
const.dir <- c("<=", "<=")  # Replace with the direction of the constraints ("<=", ">=", or "==")
const.rhs <- c(8, 6)  # Replace with the right-hand side of the constraints


lp_solution <- lpSolve::lp("max", obj.coef, const.mat, const.dir, const.rhs)


cat("Objective Function Optimum Value:\n")
## Objective Function Optimum Value:
print(lp_solution$objval)
## [1] 20.66667
cat("\nDecision Variables Optimum Values:\n")
## 
## Decision Variables Optimum Values:
print(lp_solution$solution)
## [1] 3.333333 1.333333
original_obj_coef <- lp_solution$objval


perturbed_obj_coef <- original_obj_coef * 1.1


perturbed_solution <- lpSolve::lp("max", perturbed_obj_coef, const.mat, const.dir, const.rhs)


sensitivity_result <- perturbed_solution$objval - original_obj_coef
cat("Sensitivity to Coefficients of the Objective Function:\n")
## Sensitivity to Coefficients of the Objective Function:
print(sensitivity_result)
## [1] -14.98333

#A decrease in a coefficient leads to a decrease in the optimal objective function value for a maximization problem, and an increase for a minimization problem.

if (!requireNamespace("lpSolve", quietly = TRUE)) {
  install.packages("lpSolve", dependencies = TRUE, repos = "https://cloud.r-project.org")
}
library(lpSolve)


const.mat <- as.matrix(const.mat)


sensitivity_results <- lpSolve::lp("max", obj.coef, const.mat, const.dir, const.rhs)


print(sensitivity_results)
## Success: the objective function is 20.66667

#For a constraint, the slack represents the amount by which the constraint can be relaxed or increased without affecting the optimal solution. A positive slack indicates a non-binding constraint (not fully utilized), while a negative slack indicates a binding constraint (fully utilized).The dual value represents the rate of change in the objective function value per unit increase in the right-hand side of the constraint. For a binding constraint, a non-zero dual value indicates that an additional unit of resources in that constraint would result in a corresponding improvement in the optimal objective function value.If the sensitivity result for the amount of fabric has a positive slack (surplus), it means that the company is not using the entire amount of fabric allocated by the constraint. This indicates a non-binding constraint, and the surplus represents the excess amount of fabric that could be used without affecting the optimal solution.If the sensitivity result for machine time has a negative slack, it suggests that the company is currently exceeding the available machine time. This indicates a binding constraint, and the negative slack represents the amount by which the company is overusing the machine time. A non-zero dual value would indicate the marginal improvement in profit for each additional hour of machine time made available.

set.seed(123)


num_simulations <- 100


mean_return <- 0.03
std_dev_return <- 0.97


returns <- rnorm(num_simulations, mean = mean_return, sd = std_dev_return)


mean_return_simulation <- mean(returns)
std_dev_return_simulation <- sd(returns)
prob_greater_than_5_percent <- mean(returns > 0.05)

# Display the results
cat("Mean of simulated returns:", mean_return_simulation, "\n")
## Mean of simulated returns: 0.1176937
cat("Standard Deviation of simulated returns:", std_dev_return_simulation, "\n")
## Standard Deviation of simulated returns: 0.8854314
cat("Probability of occurrence where the simulation result is greater than 5%:", prob_greater_than_5_percent, "\n")
## Probability of occurrence where the simulation result is greater than 5%: 0.51
num_simulations_1 <- 1000
num_simulations_2 <- 100000


mean_return <- 0.03
std_dev_return <- 0.97

# Case 1: 1000 simulations
set.seed(123)  # Set seed for reproducibility
returns_1000 <- rnorm(num_simulations_1, mean = mean_return, sd = std_dev_return)
mean_return_1000 <- mean(returns_1000)
std_dev_return_1000 <- sd(returns_1000)
prob_greater_than_5_percent_1000 <- mean(returns_1000 > 0.05)

# Case 2: 100000 simulations
set.seed(123)  # Set seed for reproducibility
returns_100000 <- rnorm(num_simulations_2, mean = mean_return, sd = std_dev_return)
mean_return_100000 <- mean(returns_100000)
std_dev_return_100000 <- sd(returns_100000)
prob_greater_than_5_percent_100000 <- mean(returns_100000 > 0.05)


cat("Case 1 - 1000 simulations:\n")
## Case 1 - 1000 simulations:
cat("Mean:", mean_return_1000, "\n")
## Mean: 0.04564403
cat("Standard Deviation:", std_dev_return_1000, "\n")
## Standard Deviation: 0.9619441
cat("Probability of occurrence > 5%:", prob_greater_than_5_percent_1000, "\n\n")
## Probability of occurrence > 5%: 0.497
cat("Case 2 - 100000 simulations:\n")
## Case 2 - 100000 simulations:
cat("Mean:", mean_return_100000, "\n")
## Mean: 0.03094745
cat("Standard Deviation:", std_dev_return_100000, "\n")
## Standard Deviation: 0.9697414
cat("Probability of occurrence > 5%:", prob_greater_than_5_percent_100000, "\n")
## Probability of occurrence > 5%: 0.49209
results_df <- data.frame(
  Simulations = c(100, 1000, 100000),
  Mean = c(mean_return_simulation, mean_return_1000, mean_return_100000),
  Standard_Deviation = c(std_dev_return_simulation, std_dev_return_1000, std_dev_return_100000),
  Probability_Greater_Than_5_Percent = c(prob_greater_than_5_percent, prob_greater_than_5_percent_1000, prob_greater_than_5_percent_100000)
)


print(results_df)
##   Simulations       Mean Standard_Deviation Probability_Greater_Than_5_Percent
## 1       1e+02 0.11769373          0.8854314                            0.51000
## 2       1e+03 0.04564403          0.9619441                            0.49700
## 3       1e+05 0.03094745          0.9697414                            0.49209
# Interpretation
cat("\nInterpretation:\n")
## 
## Interpretation:
cat("As the number of simulations increases, the estimated mean and standard deviation converge towards the true values\n")
## As the number of simulations increases, the estimated mean and standard deviation converge towards the true values
cat("of the normal distribution. The probability of occurrence greater than 5% stabilizes as well, providing a more\n")
## of the normal distribution. The probability of occurrence greater than 5% stabilizes as well, providing a more
cat("reliable estimate with a larger number of simulations.\n")
## reliable estimate with a larger number of simulations.

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Cmd+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.