Skip to content

Instantly share code, notes, and snippets.

@Marceeaax
Last active February 22, 2021 21:38
Show Gist options
  • Save Marceeaax/f577c9aff049c5cbb0d734ac300da468 to your computer and use it in GitHub Desktop.
Save Marceeaax/f577c9aff049c5cbb0d734ac300da468 to your computer and use it in GitHub Desktop.
Un problema de programación lineal resuelto por el método gráfico utilizando el lenguaje de programación R
# Definir restricciones
restriccion.1 <- function(x) 30 - x
# restriccion.2 x1 = 10 (Se dibuja usando geom_vline)
# restriccion.3 x2 = 10 (Se dibuja usando geom_vline)
# restriccion.4 x1 = 0 (Eje de abscisas)
# restriccion.4 x2 = 0 (Eje de ordenadas)
# Importar ggplot2 paquete
library(ggplot2)
# Construir grafica
plano <- ggplot(data = data.frame(x = 0), aes(x = x)) +
# Agregando ejes
geom_vline(xintercept = 0) +
geom_hline(yintercept = 0) +
# Agregar lineas de restricciones
stat_function(colour = "dodgerblue4", fun = restriccion.1, size = 1.5) +
geom_vline(xintercept = 10, colour = "dodgerblue3", size = 1.5) +
geom_hline(yintercept = 10, colour = "royalblue4", size = 1.5) +
# Limites del grafico
scale_x_continuous(breaks = seq(0, 100, 5), lim = c(0, 30)) +
scale_y_continuous(breaks = seq(0, 100, 5), lim = c(0, 30)) +
# Definir leyendas
labs(title = "Programación Lineal",
subtitle = "Método Gráfico",
x = expression("x"[1]),
y = expression("x"[2])) +
# Fondo oscuro
theme_dark()
regionfactible = data.frame(x = c(10,10,20), y = c(10,20,10))
# Agregar region factible
plano <- plano + geom_polygon(data = regionfactible, mapping = aes(x = x, y = y), color = "slategray") +
geom_point(data = regionfactible, aes(x = x, y = y), color = "Black", size = 2)
z1 <- function(x) -(3/2)*x + 10000/1000
plano <- plano + stat_function(colour = "yellowgreen", fun = z1, lty=2, size = 2)
z2 <- function(x) -(3/2)*x + 20000/1000
plano <- plano + stat_function(colour = "yellowgreen", fun = z2, lty=2, size = 2)
z3 <- function(x) -(3/2)*x + 30000/1000
plano <- plano + stat_function(colour = "yellowgreen", fun = z3, lty=2, size = 2)
z4 <- function(x) -(3/2)*x + 40000/1000
# punto de solucion
plano <- plano + stat_function(colour = "yellowgreen", fun = z4, lty=2, size = 2)
plano <- plano + geom_point(aes(x = 20, y = 10), color = "red", size = 4)
# Imprimir grafica
print(plano)
# Modificado de https://medium.com/swlh/operations-research-with-r-graphical-method-18f4ba34fea6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment