Science
Copyright © 2024 Jiri Kriz, www.nosco.ch
Snippet opt_graphics.r
Back to Overview

R script to produce the picture for 2-dim optimization



# R script to produce the picture for 2-dim optimization
# Copyright (c) 2009 Jiri Kriz @ www.nosco.ch
# For "R" see http://www.r-project.org
# The script must be sourced into the running R system (menu File/Source...).
# Then call "opt_graphics()".
# The script generates an PNG picture in the specified file.
# To produce the picture in an R window:
# - comment out: png(file="C:\\tmp\\Graphics_EN.png")
# - comment out: dev.off()

opt_graphics <- function() {

# create PNG picture in the specified file
png(file="C:\\tmp\\Graphics_EN.png")

# max. extent of the picture
x <- c(-5, 30)
y <- c(30, -5)
plot(x, y, "n", axes=FALSE, xlab = "", ylab = "")

# production &amp; transport capacity restriction 
lines(x,y, col="blue")
text(7, 22, "max capacity", col="blue")

# axes
axis(1, pos=0)
axis(2, pos=0)
title(xlab="x1", ylab="x2")

# restriction tonnage 1
lines(c(20,20), c(0, 10), col="blue")

# restriction tonnage 2
lines(c(0,20), c(10, 10), col="blue") 

# restriction: min concentration
abline(0, 1/3, col="blue")
text(10, 2, "min R%", col="blue")

# restriction: max concentration
abline(0, 1, col="blue")
text(4, 7, "max R%", col="blue")

# goal = 0
abline(0, -3, col = "red")
text(-3, 17, "goal=0", col="red")

# goal = max
abline(62.5, -3, col = "red")
text(21, 14, "goal=125=max", col="red")

# solution point
points(c(18.75), c(6.25), col="red", pch=21, bg="red")

# feasible region
x <- c(0, 18.75, 15, 10, 0)
y <- c(0, 6.25,  10, 10, 0)
polygon(x, y, col="green")
text(12, 7, "feasible", col="blue")

dev.off()

}