OR Record

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

TABLE OF CONTENTS

S. DATE NAME OF THE EXPERIMENT MARKS SIGNATURE


NO

17.08.2022 MODELLING WITH GRAPHICAL


1 SOLUTION OF LINEAR
PROGRAMMING PROBLEMS

SOLVING SIMPLEX MAXIMIZATION


2 24.08.2022 PROBLEMS.

SOLVING SIMPLEX MINIMIZATION


3 07.09.2022 PROBLEMS.

SENSITIVITY ANALYSIS AND


4 14.09.2022 DUALITY

SOLVING TRANSPORTATION
5 28.09.2022 PROBLEMS

6 12.10.2022 SOLVING ASSIGNMENT PROBLEMS

INTRODUCTION TO NETWORK
7 19.10.2022 MODELS

SOLVING INTEGER LINEAR


8 09.11.2022 PROGRAMMING

SOLVING GOAL PROGRAMMING


9 16.11.2022 MODELS

INTRODUCTION TO NON LINEAR


10
23.11.2022 PROGRAMMING
EX.NO: 1 MODELING WITH GRAPHICAL SOLUTION OF LINEAR
PROGRAMMING PROBLEMS

AIM:
To write a R program to do the Modeling with Graphical solution of Linear
programming problems

ALGORITHM:
Step 1: Install the ggplot package. In the R Console, type the following command to
install the ggplot package: install.packages (ggplot)
Step 2: plot the dataframe values in matrix Construct a graph and plot the constraint lines.
Step 3: To plot the x and y values of dataframe matrix
Step 4: Identify the feasible solution region.
Step 5: To Plot the objective function on the graph.
Step 6: Find out and display the optimum point.

PROBLEM 1a:
plot_df$x <- c(0,3,0,9/2)
plot_df$y <- c(3,0,9/3,0)

PROGRAM :
library(ggplot2)
plot_df <- as.data.frame(matrix(nrow=4,ncol=3))
colnames(plot_df) <- c("x","y","type")
plot_df$x <- c(0,3,0,9/2)
plot_df$y <- c(3,0,9/3,0)
plot_df$type <- c("line constraint","line constraint", "line objective", "line objective")
ggplot(plot_df) + geom_path(mapping = aes(x=x,y=y,color=type))

OUTPUT :

Result :

Thus the problem is implemented and executed successfully.


PROBLEM 1b:
Maximize
Z = 4X1 +5 X2
subject to
X1 +2 X2 ≤ 10
6X1 +6 X2 ≤ 36
X1 ≤ 4
X1 X2 ≥ 0

PROGRAM :

# Import ggplot2 package


library(ggplot2)
cons.1 <- function(x) 6 - x
cons.2 <- function(x) 5 - 0.5*x
# Build plot
p<- ggplot(data = data.frame(x = 0), aes(x = x)) +
geom_vline(xintercept = 0) +
geom_hline(yintercept = 0) +
stat_function(colour = "Red", fun = cons.1) +
stat_function(colour = "Blue", fun = cons.2) +
geom_vline(xintercept = 4, colour = "Green") +
scale_x_continuous(breaks = seq(0, 10, 1), lim = c(0, 10)) +
scale_y_continuous(breaks = seq(0, 10, 1), lim = c(0, 10)) +
labs(title = "Optimization Problem",
subtitle = "Graphical Method",
x = "x1",
y = "x2") +theme_bw()
# Print plot
print(p)

library(lpSolve)
# Set coefficients of the objective function
f.obj <- c(4, 5)
# Set matrix corresponding to coefficients of constraints by rows
f.con <- matrix(c(1, 2, 6, 6, 1, 0), nrow = 3, byrow = TRUE)
# Set unequality/equality signs
f.dir <- c("<=", "<=", "<=")
# Set right hand side coefficients
f.rhs <- c(10, 36, 4)
# Final value (z)
lp("max", f.obj, f.con, f.dir, f.rhs)
# Variables final values
solution <- lp("max", f.obj, f.con, f.dir, f.rhs)$solution
# Highlight optimum solution in plot
p <- p + geom_point(aes(x = solution[1], y = solution[2]), color = "red", size = 4)
# Print plot
print(p)
OUTPUT :
lp("max", f.obj, f.con, f.dir, f.rhs)
Success: the objective function is 28

Result :

Thus the problem is implemented and executed successfully


EX.NO: 2 SOLVING SIMPLEX MAXIMIZATION PROBLEMS

AIM:
To write a R program to solve simplex linear programming problems

ALGORITHM:

Step 1: Install the lpsolve package. In the R Console, type the following command to install
the lpsolve package: install.packages (lpsolve)
Step 2: Create a objective function and the constraints and to create a coefficient vector of
objective function
Step 3: Construct the values of coefficient matrix for constraint matrix
Step 4: Construct the values of direction vector
Step 5: Plot the values of the given function
Step 6: Construct the initial simplex table
Step 7: Find the most negative entry in the bottom row identifies the pivot column
Step 8: print the solution

PROBLEM 2a :
A company wants to maximize the profit for two products A and B which are sold at $ 25 and
$ 20 respectively. There are 1800 resource units available every day and product A requires
20 units while B requires 12 units. Both of these products requires a production time of 4
minutes and total available working hours are 8 in a day. What shout be the production
quantity each of the product to maximize the profit.

Solution:

The objective function of above pblm


max(sales)=max(25 x1+20 x2)

x1 is the units of product A produced


x2 is the units of product A produced
x1 and x2 are also called the decision variable

The constraints (resource and time) in the problem:

20 x1+12 x2<=1800(Resource constrain)


4 x1+4 x2<=8*60(Time constrain)
PROGRAM:

Library(lpsolve)
lp(direction=”max”,objective.in,const.mat,const.dir,const.rhs)
#set the coefficient of the decision variable
objective.in=c(25,20)
#create constraint matrix
Cont.mat=matrix(c(20,12,4,4), nrow=2,byrow=TRUE)
##define constraints
Time_constraint=(8*60)
Resource_constraint=1800
#rhs of the constraints
Const.rhs=c(resource_constraints,time_constraints)
##constrain direction
Const.dir=c(“<=”,”<=”)
##find the optimal soln:
Optimum= lp(direction=”max”,objective.in,const.mat,const.dir,const.rhs)
Display the optimum values for x1 and x2
Optimum$solution
#objective function at optimal points
Optimum$objval

OUTPUT:

print(optimum$solution)
[1] 420 580 161
>
> print(optimum$objval)
[1] 48680

Result :

Thus the problem is implemented and executed successfully.


PROBLEM 2b :
A firm produces three products A, B, and C each of which passes through three different
departments fabrication, finishing, packaging. Each unit of product A requires 3, 4 and 2
hours respectively, B requires 5, 4 and 4 hours respectively and C requires 2, 4 and 5 hours
respectively in 3 departments respectively. Every day 60 hours are available in fabrication
department, 72 hours in finishing and 100 hours in packaging department. If unit contribution
of unit A is Rs. 5, Rs. 10 for B and Rs. 3 for C. Then determine number of units of each
product so that total contribution to cost is maximized and also determine if any capacity
would remain unutilized.

Solution Max Z = 5X1 + 10X2 + 3X3


Subject to constraints: 3X1 + 5X2 + 2X3 ≤ 60
4X1 + 4X2 + 4X3 ≤ 72
2X1 + 4X2 + 5X3 ≤ 100
X1, X2, X3 ≥ 0
PROGRAM

library(lpSolve)
# Set coefficients of the objective function
f.obj <- c(5, 10, 3)
# Set matrix corresponding to coefficients of constraints by rows
f.con <- matrix(c(3, 5, 2, 4, 4, 4, 2, 4, 5), nrow = 3,byrow = TRUE)
# Set unequality/equality signs
f.dir <- c("<=", "<=", "<=")
# Set right hand side coefficients
f.rhs <- c(60, 72, 100)
# Final value (z)
lp("max", f.obj, f.con, f.dir, f.rhs)
# Variables final values
solution <- lp("max", f.obj, f.con, f.dir, f.rhs)
print(solution)
print(solution$solution)

OUTPUT

# Final value (z)


> lp("max", f.obj, f.con, f.dir, f.rhs)
Success: the objective function is 120
print(solution$solution)
[1] 0 12 0

Result :

Thus the problem is implemented and executed successfully


EX.NO: 3 SOLVING SIMPLEX MINIMIZATION PROBLEMS

AIM:
To write a R program to implement the simplex minimization problems

ALGORITHM:

Step 1: Install the lp solve package. In the R Console, type the following command to
install the lp solve package: install.packages (lpsolve)
Step 2: Create a objective function and the constraints and to create a coefficient vector of
objective function
Step 3: Construct the values of direction vector
Step 4: Plot the values of the given function
Step 5: Construct the initial simplex table
Step 6: print the solution

PROBLEM :
A company produces two models of chairs: 4P and 3P. The model 4P needs 4 legs, 1 seat and
1 back. On the other hand, the model 3P needs 3 legs and 1 seat. The company has a initial
stock of 200 legs, 500 seats and 100 backs. If the company needs more legs, seats and backs,
it can buy standard wood blocks, whose cost is 80 euro per block. The company can produce
10 seats, 20 legs and 2 backs from a standard wood block. The cost of producing the model
4P is 30 euro/chair, meanwhile the cost of the model 3P is 40 euro/chair. Finally, the
company informs that the minimum number of chairs to produce is 1000 units per month.
Define a linear programming model, which minimizes the total cost (the production costs of
the two chairs, plus the buying of new wood blocks).

SOLUTION

Therefore, the obj function is:


Cost=30∗4P+40∗3P+80∗WoodenBlocks
There is one row for each constraint, and one column for each decision variable.
 First row is for the Seat constraint. It says that:
o Each unit of 4P uses 1 seat from the seat inventory
o Each unit of 3P uses 1 seat from the seat inventory
o Each unit of wooden blocks adds 1 seat to the seat inventory
 Second row is for the Legs constraint
 Third row is for the Backs constraint
 Fourth row is for the Min production constraint
PROGRAM

library(lpSolve)
C <- c(30, 40, 80)
A <- matrix(c(1, 1, -10, 4, 3, -20, 1, 0, -2, 1, 1, 0), nrow=4, byrow=TRUE)
# Right hand side for the constraints
B <- c(500, 200, 100, 1000)
# Direction of the constraints
constranints_direction <- c("<=", "<=", "<=", ">=")
optimum <- lp(direction="min", objective.in = C, const.mat = A,
const.dir = constranints_direction, const.rhs = B, all.int = T)
str(optimum)
print(optimum$status)
# Display the optimum values for x_4p, x_3p and x_w
best_sol <- optimum$solution
names(best_sol) <- c("x_4p", "x_3p", "x_w")
print(best_sol)
print(paste("Total cost: ", optimum$objval, sep=""))

OUTPUT

List of 28
$ direction : int 0
$ x.count : int 3
$ objective : num [1:3] 30 40 80
$ const.count : int 4
$ constraints : num [1:5, 1:4] 1 1 -10 1 500 4 3 -20 1 200 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:5] "" "" "" "const.dir.num" ...
.. ..$ : NULL
$ int.count : int 3
$ int.vec : int [1:3] 1 2 3
$ bin.count : int 0
$ binary.vec : int 0
$ num.bin.solns : int 1
$ objval : num 48680
$ solution : num [1:3] 420 580 161
$ presolve : int 0
$ compute.sens : int 0
$ use.rw : int 0
$ tmp : chr "Nobody will ever look at this"
$ status : int 0
- attr(*, "class")= chr "lp"
[1] 0
x_4p x_3p x_w
420 580 161
[1] "Total cost: 48680"

Result :
EX.NO: 4 SENSITIVITY ANALYSIS AND DUALITY

AIM:
To write a program to verify duality.

ALGORITHM:

Step 1: Write the given LPP in its standard form.


Step 2: Identify the variables of dual problem which are same as the number of constraints
equation.
Step 3: Write the objective function of the dual problem by using the constants of the right
hand side of the constraints.
Step 4: To solve both Objective function using and verify that the Primal and Dual solution
are the same.
Step 5: The optimal solution is found in the bottom row of the final matrix in the columns
corresponding to the slack variables, and the minimum value of the objective function is the
same as the maximum value of the dual.
PROBLEM: Use duality to solve the following LPP
Min Z = 2X1 + 2X2
Sub to 2X1 + 4X2
- 2X2
2X1 + X2
And X1 , X2
SOLUTION

Given Primal LPP is Minimize : Z = 2X1 + 2X2


Sub to 2X1 + 4X2
+
2X2 2X1
+ X2 X1 ,
X2
Its dual is Max w = Y1 + Y2 + Y3
2Y1 + Y2 + 2Y3≤ 2
4Y1 + 2Y2 + Y3≤ 2
Y1, Y2 ,Y3 ≥ 0

PROGRAM
Primal:
require(lpSolve)
# coefficient vector of objective function
f.obj <- c(2,2)
# coefficient matrix for constraint matrix
f.c on <- matrix(c(2,4,1,2,2,1),nrow=3,byrow=TRUE)
# constraint direction vector
f.dir <- c(">=")
# constraint values
f.rhs <- c(1,1,1)
#Now let us solve the linear problem:
solution <- lp("min",f.obj,f.con,f.dir,f.rhs)
print(solution)
solution$solution

dual
require(lpSolve)
# coefficient vector of objective function
f.obj <- c(1,1,1)
# coefficient matrix for constraint matrix
f.con <- matrix(c(2,1,2,4,2,1),nrow=2,byrow=TRUE)
# constraint direction vector
f.dir <- c("<=")
# constraint values
f.rhs <- c(2,2)
#Now let us solve the linear problem:
solution <- lp("max",f.obj,f.con,f.dir,f.rhs)
print(solution)
solution$solution

Output :

Success: the objective function is 1.333333


> solution$solution
[1] 0.3333333 0.3333333

Success: the objective function is 1.333333


> solution$solution
[1] 0.0000000 0.6666667 0.6666667

Result :
EX.NO: 5 SOLVING TRANSPORTATION PROBLEMS

AIM:
To write a R program to solve Transportation problem.

ALGORITHM:

Step 1: import the library (lpsolve) package


Step 2: Construct the values of transportation matrix
Step 3: Create the col names and rows values
Step 4: Set inequality/equality signs for suppliers
Step 5: Create right hand side coefficients for suppliers
Step 6: Set un equality/equality signs for customers
Step 7: Create right hand side coefficients for customers
Step 8: print the solution

Problem 5a: Find the initial basic feasible solution of the following transportation problem:

Using Vogel’s approximation method.

PROGRAM:
# Import lpSolve package library(lpSolve)
# Set transportation costs matrix
costs <- matrix(c(1, 2, 6, 0, 4, 2,3, 1, 5), nrow = 3, byrow = TRUE)
# Set customers and suppliers' names
colnames(costs) <- c("I", "II", "III")
rownames(costs) <- c("A", "B", "C")
# Set unequality/equality signs for suppliers row.signs
<- rep("<=", 3)
# Set right hand side coefficients for suppliers row.rhs
<- c(7, 12, 11)
# Set unequality/equality signs for customers
col.signs <- rep(">=", 3)
# Set right hand side coefficients for customers
col.rhs <- c(10, 10, 10)
# Final value (z)
lp.transport(costs, "min", row.signs, row.rhs, col.signs, col.rhs)
# Variables final values
lp.transport(costs, "min", row.signs, row.rhs, col.signs, col.rhs)$solution
OUTPUT:

Success: the objective function is 40


> # Variables final values
> lp.transport(costs, "min", row.signs, row.rhs, col.signs, col.rhs)$solution
[,1] [,2] [,3]
[1,] 7 0 0
[2,] 2 0 10
[3,] 1 10 0

Result :
PROBLEM 5b:

Consider the following transportation problem

Determine initial basic feasible solution by VAM.

Program:

# Import lpSolve package


library(lpSolve)
# Set transportation costs matrix
costs <- matrix(c(5, 8, 3, 6, 4, 5, 7, 4, 6, 2, 4, 6), nrow = 3, byrow = TRUE)
# Set customers and suppliers' names
colnames(costs) <- c("D1", "D2", "D3", "D4")
rownames(costs) <- c("O1", "O2", "O3")
# Set unequality/equality signs for suppliers
row.signs <- rep("<=", 3)
# Set right hand side coefficients for suppliers
row.rhs <- c(30, 50, 20)
# Set unequality/equality signs for customers
col.signs <- rep(">=", 4)
# Set right hand side coefficients for customers
col.rhs <- c(30, 40, 20, 10)
# Final value (z)
lp.transport(costs, "min", row.signs, row.rhs, col.signs, col.rhs)
# Variables final values
lp.transport(costs, "min", row.signs, row.rhs, col.signs, col.rhs)$solution

Output:
> # Final value (z)
> lp.transport(costs, "min", row.signs, row.rhs, col.signs, col.rhs)
Success: the objective function is 370
>
> # Variables final values
> lp.transport(costs, "min", row.signs, row.rhs, col.signs, col.rhs)$solution
[,1] [,2] [,3] [,4]
[1,] 10 0 20 0
[2,] 20 20 0 10
[3,] 0 20 0 0

Result :
PROBLEM 5c:
Find the initial basic feasible solution of the following transportation problem:

Using Vogel’s approximation method.

Program:

# Import lpSolve package


library(lpSolve)
# Set transportation costs matrix
costs <- matrix(c(10, 2, 20, 11, 12, 7, 9, 20, 4, 14 , 16, 18), nrow = 3, byrow = TRUE)
# Set customers and suppliers' names
colnames(costs) <- c("Customer 1", "Customer 2", "Customer 3", "Customer 4")
rownames(costs) <- c("Supplier 1", "Supplier 2", "Supplier 3")
# Set unequality/equality signs for suppliers
row.signs <- rep("<=", 3)
# Set right hand side coefficients for suppliers
row.rhs <- c(15, 25, 10)
# Set unequality/equality signs for customers
col.signs <- rep(">=", 4)
# Set right hand side coefficients for customers
col.rhs <- c(5, 15, 15, 15)
# Final value (z)
lp.transport(costs, "min", row.signs, row.rhs, col.signs, col.rhs)
# Variables final values
lp.transport(costs, "min", row.signs, row.rhs, col.signs, col.rhs)$solution

Output :
# Final value (z)
> lp.transport(costs, "min", row.signs, row.rhs, col.signs, col.rhs)
Success: the objective function is 435
>
> # Variables final values
> lp.transport(costs, "min", row.signs, row.rhs, col.signs, col.rhs)$solution
[,1] [,2] [,3] [,4]
[1,] 0 5 0 10
[2,] 0 10 15 0
[3,] 5 0 0 5

Result :
EX.NO: 6 SOLVING ASSIGNMENT PROBLEMS

AIM:
To write a R program to implement assignment problems

ALGORITHM:

Step 1: Install the lp solve package. In the R Console, type the following command to install
the lp solve package: install.packages (lpsolve)
Step 2: Construct the values of assignment costs matrix
Step 3: Print and display the assignment costs matrix
Step 4: Formulation of assignment problem are
Min z =
m = number of sources
n = number of destinations.
The total quantity available at each source
Step 5: Print the final value (z) of assignment problem
Step 6: Print variables final value of assignment problem
Step 7: Display the result.

PROBLEM 6a:
A computer centre has got three expert programmers. The centre needs three application
programs to be developed. The head of the computer centre, after studying carefully the
programs to be developed, estimates the computer time in minutes required by the experts to
the application program as follows.
Assign the programmers to the program in such a way that the total computer time is least.

Program:

# Import lpSolve package library(lpSolve)


# Set assignment costs matrix
costs <- matrix(c(120, 100, 80, 80, 90, 110, 110, 140 ,120), nrow = 3, byrow = TRUE)
# Print assignment costs matrix
costs
# Final value (z) lp.assign(costs)
# Variables final values lp.assign(costs)$solution
Output:

> costs
[,1] [,2] [,3]
[1,] 120 100 80
[2,] 80 90 110
[3,] 110 140 120
>
> # Final value (z)
> lp.assign(costs)
Success: the objective function is 280
>
> # Variables final values
> lp.assign(costs)$solution
[,1] [,2] [,3]
[1,] 0 0 1
[2,] 0 1 0
[3,] 1 0 0

Result :
PROBLEM 6b:
A departmental head has four subordinates and four tasks to be performed. The subordinates
differ in efficiency and the tasks differ in their intrinsic difficulty. His estimates of the time
each man would take to perform each task is given below

How should the tasks be allocated to subordinates so as to minimize the total man-hours?
Program

# Import lpSolve package


library(lpSolve)
# Set assignment costs matrix
costs <- matrix(c(8, 26, 17,11, 13, 28, 4,26, 38, 19 ,18,15, 9,26,24,10),
nrow = 4, byrow = TRUE)
# Print assignment costs matrix
costs
# Final value (z)
lp.assign(costs)
# Variables final values
lp.assign(costs)$solution

Output
> costs
[,1] [,2] [,3] [,4]
[1,] 8 26 17 11
[2,] 13 28 4 26
[3,] 38 19 18 15
[4,] 9 26 24 10
>
> # Final value (z)
> lp.assign(costs)
Success: the objective function is 41
>
> # Variables final values
> lp.assign(costs)$solution
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 0 1 0
[3,] 0 1 0 0
[4,] 0 0 0 1

Result
EX.NO: 7 INTRODUCTION TO NETWORK MODELS

AIM:
To write a R program to implement Critical Path Method

ALGORITHM:

Step 1: Install the critical path package. In the R Console, type the following command to
install the critical path package: install.packages (criticalpath)
Step 2: Create an empty schedule
Step 3: add activities to the schedule
Step 4: add relations to the schedule
Step 5: plan the schedule
Step 6: Calculate duration, Critical activates and different float values.

PROBLEM 7:
Determine the critical path and project duration. Hence, calculate the total float, free float and
independent float for the project whose activities are given below.

Activity 1-2 1-3 2-4 2-5 2-6 3- 3-8 3-9 3-10


Duration 1 2 2 4 3 3 3 2 1

Activity 4-11 5-11 6-11 -12 8-13 9-14 10-15 11-16


Duration 1 2 1 1 1 1 2 1

#install.packages("criticalpath")
library(criticalpath)
sch <- sch_new() %>%
sch_add_activities( id = 1:17,
name = paste("a", as.character(1:17), sep=""),
duration =
c(1L,2L,2L,4L,3L,3L,3L,2L,1L,1L,2L,1L,1L,1L,1L,2L,1L)
) %>%
sch_add_relations(
from = c(1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L),
to = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 11L, 11L, 12L, 13L, 14L, 15L, 16L)
) %>%
sch_plan()
sch_duration(sch)
sch_critical_activities(sch)
OUTPUT

sch_duration(sch)
[1] 11
sch_critical_activities(sch)
# A tibble: 5 × 14
id name duration miles…¹ criti…² early…³ early…⁴ late_…⁵ late_…⁶ total…⁷ free_…
⁸ progr…⁹
<int> <chr> <int> <lgl> <lgl> <int> <int> <int> <int> <int> <int> <int>
1 1 a1 1 FALSE TRUE 0 1 0 1 0 0 1
2 2 a2 2 FALSE TRUE 1 3 1 3 0 0 2
3 4 a4 4 FALSE TRUE 3 7 3 7 0 0 3
4 11 a11 2 FALSE TRUE 7 9 7 9 0 0 4
5 16 a16 2 FALSE TRUE 9 11 9 11 0 0 5
# … with 2 more variables: regr_level <int>, topo_float <int>, and abbreviated variable
names
# ¹milestone, ²critical, ³early_start, ⁴early_finish, ⁵late_start, ⁶late_finish,
# ⁷total_float, ⁸free_float, ⁹progr_level
# ℹ Use `colnames()` to see all variable names
> sch_critical_relations(sch)
# A tibble: 4 × 8
from to type lag critical ord i_from i_to
<int> <int> <chr> <int> <lgl> <int> <int> <int>
1 1 2 FS 0 TRUE 1 1 2
2 2 4 FS 0 TRUE 3 2 4
3 4 11 FS 0 TRUE 10 4 11
4 11 16 FS 0 TRUE 17 11 16

Result :
EX.NO: 8 SOLVING INTEGER LINEAR PROGRAMMING

AIM:
To write a R program to implement integer linear program.

ALGORITHM:

Step1: Install the lpsolve package. In the R Console, type the following command to install
the lpsolve package: install.packages (lpsolve)
Step2: Enter the data set to read.csv
Step3: Construct the values of assignment costs matrix
Step4: Print and display the assignment costs matrix
Step5: Enter the Character string giving direction of optimization
Step 6: Numeric Vector of co efficient of objective function.

Program :
library(lpSolve)
data<- read.csv("d:\\data.exl.csv",header = T)
data
cost<- data$cost
print(cost)
pa<- data[,2:4]
pa
f.obj <- cost
cost
f.c on <-
t(pa) f.con
f.dir <- rep(">=",times =7)
f.dir
f.rhs <- rep(1,times = 7)
f.rhs
lp(direction = "min", objective.in = f.obj,const.mat = f.con,const.dir = f.dir,const.rhs = f.rhs,
all.bin = TRUE)
results <- lp(direction = "min",objective.in = f.obj,const.mat = f.con,const.dir = f.dir,
const.rhs = f.rhs,all.bin = TRUE)
str(results)

OUTPUT
Data
site s1 s2 s3 s4 s5 cost

1 1 0 1 1 0 0 10
2 2 1 1 1 0 0 12
3 3 0 0 1 1 1 15
4 4 0 0 0 1 1 14
5 5 1 0 0 0 1 6
6 6 1 0 0 1 0 8
7 7 0 1 1 1 0 11
cost<- data$cost

> print(cost)
[1] 10 12 15 14 6 8 11
> pa<- data[,2:4]
> pa
s1 s2 s3

1 0 1 1
2 1 1 1
3 0 0 1
4 0 0 0
5 1 0 0
6 1 0 0
7 0 1 1
> f.obj <- cost
> cost
[1] 10 12 15 14 6 8 11
> f.con <- t(pa)
> f.con
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
s1 0 1 0 0 1 1 0
s2 1 1 0 0 0 0 1
s3 1 1 1 0 0 0 1
> f.dir <- rep(">=",times =7)
> f.dir
[1] ">=" ">=" ">=" ">=" ">=" ">=" ">="
> f.rhs <- rep(1,times = 7)
> f.rhs
[1] 1 1 1 1 1 1 1
Success: the objective function is 12
str(results)
List of 28
$ direction : int 0
$ x.count : int 7
$ objective : int [1:7] 10 12 15 14 6 8 11
$ const.count : int 3
$ constraints : num [1:9, 1:3] 0 1 0 0 1 1 0 2 1 1 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:9] "" "" "" "" ...
.. ..$ : chr [1:3] "s1" "s2" "s3"
$ int.count : int 0
$ int.vec : int 0
$ bin.count : int 7
$ binary.vec : int [1:7] 1 2 3 4 5 6 7
$ num.bin.solns : int 1
$ objval : num 12
$ solution : num [1:7] 0 1 0 0 0 0 0
$ presolve : int 0
$ compute.sens : int 0
$ sens.coef.from : num 0
$ sens.coef.to : num 0
$ duals : num 0
$ duals.from : num 0
$ duals.to : num 0
$ scale : int 196
$ use.dense : int 0
$ dense.col : int 0
$ dense.val : num 0
$ dense.const.nrow: int 0
$ dense.ctr : num 0
$ use.rw : int 0
$ tmp : chr "Nobody will ever look at this"
$ status : int 0
- attr(*, "class")= chr "lp"

Result :
EX.NO: 9 SOLVING GOAL PROGRAMMING MODELS

AIM:
To write a R program to solve goal programming problem.

ALGORITHM:

Step1: Install the goalp package. In the R Console, type the following command to install
the goalp package: install.packages (goalp)
Step2: write our goals in a text variable, one goal per line.
Step3: Call the goalp function with our goals as an argument.
Step4: use summary on our results to see them on the screen

PROBLEM:
Imagine you want to grow some vegetables and herbs in your garden. You can grow aubergine
(A), broccoli (B), carrots (C), dill (D) or endive (E). Each vegetable requires a certain amount of
land to grow per seedling you plant, as well as a certain amount of water and fertilizer. After a bit
of research, you find out that each seedling of each vegetable needs the following amount of
resources to grow.

Aubergine Broccoli Carrot Dill Endive

Land 60 30 20 10 20

Water 0.8 0.2 0.3 0.1 0.3

Fertiliser 30 20 10 5 20

Where land is expressed in cm of a planting line, water in litres per week, and fertiliser in grams
per season. These values are only meant as an example, and do not reflect real needs of these
plants.
Now imagine you have prepared the land already, and you have 3,000 cm of planting line
available, you have 50 litres of water available during a week, and you have bought 2,000 grams
of fertiliser.
You would like to decide how much to plant of each vegetable in such a way that your land,
water, and fertiliser consumption is as close as possible to your available resources. You can go
a bit above or below each amount, as you can always extend the planting lines, get additional
water from other sources, or buy extra fertiliser, or leave any amount of them unused. But you
would like to minimise additional work or expenditure, as well as avoid any waste. How can you
decide, then, what to plant while consuming as close as possible to the available resources?
Solution:

Program :

# Load library
library(goalp)

# Write goals to a text variable


goals <- "Land : 60*A + 30*B + 20*C + 10*D + 20*E >= 3000 |
1/3000 Water: .8*A + .2*B +
.3*C + .1*D + .3*E = 50 | 1/50
Fertiliser: 30*A + 20*B + 10*C + 5*D + 20*E <= 2000 | 1/2000
A lBound 10
B lBound 10
C range [10,20]
D uBound 5"

# Solve problem
gp <- goalp(goals)
#> Solving underlying linear optimisation problem.

# Print results to screen


summary(gp)

OUTPUT
Goal programming model run by vmrma using goalp 0.3.1 on R 4.2.2 for Windows
Problem formulation:
Land : 60*A + 30*B + 20*C + 10*D + 20*E >= 3000 | 0.0003333 0 | 1# Inf#
Water : 0.8*A + 0.2*B + 0.3*C + 0.1*D + 0.3*E = 50 | 0.02 0.02 | 1# 1#
Fertiliser : 30*A + 20*B + 10*C + 5*D + 20*E <= 2000 | 0 0.0005 | Inf# 1#
A lBound 10
B lBound 10
C lBound 10
C uBound 20
D uBound 5

Objective function value: 0


Solution:
value type
A 52 integer
B 10 integer
C 20 integer
D 4 integer
E 0 integer

Deviations:
d- d+ w- w+ p- p+
Land 0 860 0.0003333333 0e+00 1 Inf
Water 0 0 0.0200000000 2e-02 1 1
Fertiliser 20 0 0.0000000000 5e-04 Inf 1
A_lBound NA 42 NA 0e+00 NA Inf
B_lBound NA 0 NA 0e+00 NA Inf
C_lBound NA 10 NA 0e+00 NA Inf
C_uBound 0 NA 0.0000000000 NA Inf NA
D_uBound 1 NA 0.0000000000 NA Inf NA

Result:
EX.NO: 10 INTRODUCTION TO NONLINEAR PROGRAMMING

AIM:
To write a R program to implement the Nonlinear Programming.

ALGORITHM:

Step1: The optimization problem is to select n decision variables x1, x2, . . . , xn from a given
feasible region in such a way as to optimize (minimize or maximize) a given objective
function f (x1, x2, . . . , xn) of the decision variables.

Step2: Create maximization form, the general nonlinear program is stated as:
Maximize f (x1, x2, . . . , xn),
subject to: g1(x1, x2, . . . , xn) ≤ b1, . . . . . .
gm(x1, x2, . . . , xn) ≤ bm

Step3: The obvious association for this case is


f (x1, x2, . . . , xn) = Xn j=1 c j x j,
and gi(x1, x2, . . . , xn) = Xn j=1 ai j x j (i = 1, 2, . . . , m).

Step4: Print and display the objective function.


Problem: Minimizing with inequality constraint without gradients
The problem to minimize is
min x∈Rn √ x2
s.t.x2 ≥ 0
x2 ≥ (a1x1 + b1) 3
x2 ≥ (a2x1 + b2) 3
with a1 = 2, b1 = 0, a2 = −1, and b2 = 1.

We re-arrange the constraints to have the form


g(x) ≤ 0:
(a1x1 + b1) 3 − x2 ≤ 0
(a2x1 + b2) 3 − x + 2 ≤ 0

Program
install.packages("nloptr")
library('nloptr')
eval_f0<- function( x, a, b ){
return( sqrt(x[2]) )
}
# constraint function
eval_g0<- function( x, a, b ) {
return( (a*x[1] + b)^3 - x[2] )
}
# define parameters
a <- c(2,-1)
b <- c(0, 1)
res1<- nloptr(x0=c(1.234,5.678),
eval_f=eval_f0,
lb = c(-Inf,0),
ub = c(Inf,Inf),
eval_g_ineq = eval_g0,
opts = list("algorithm"="NLOPT_LN_COBYLA",
"xtol_rel"=1.0e-8),
a = a,
b = b)
print( res1)

OUTPUT

Call: ## nloptr(x0 = c(1.234, 5.678), eval_f = eval_f0, lb = c(-Inf, 0),


## ub = c(Inf, Inf), eval_g_ineq = eval_g0, opts = list(algorithm = "NLOPT_LN_COBYLA",
## xtol_rel = 1e-08), a = a, b = b)
## ## ## Minimization using NLopt version 2.4.2
## ## NLopt solver status: 4 ( NLOPT_XTOL_REACHED: Optimization stopped because
## xtol_rel or xtol_abs (above) was reached. )
## ## Number of Iterations......50
## Termination conditions: xtol_rel: 1e-08
## Number of inequality constraints: 2
## Number of equality constraints: 0
## Optimal value of objective function: 0.544331053951819
## Optimal value of controls: 0.3333333 0.2962963

Result:

You might also like