45
Investigación Operativa I Investigación Operativa I Tutorial SOFTWARE LINGO MSc. Ing° Ezzard Omar Alvarez Díaz Universidad Nacional Mayor de San Marcos Facultad de Ingeniería Industrial LINGO Document

Lingo Tutorial

Embed Size (px)

DESCRIPTION

tutorail competo de programacion en w software lingo.

Citation preview

Page 1: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Tutorial SOFTWARE LINGO

MSc. Ing° Ezzard Omar Alvarez Díaz

Universidad Nacional Mayor de San Marcos Facultad de Ingeniería Industrial

LINGO Document

Page 2: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

¿Qué es LINGO?• LINGO (Linear, INteractive, and General

Optimizer). Es una herramienta simple para utilizar la

optimización lineal, no-lineal y enteros.

Permite formular problemas de gran tamaño en forma concisa.

Permite resolverlos Permite analizar los resultados

Page 3: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Creando un Modelo LINGO• En general, un modelo de optimización

consiste de 3 partes : Función Objetivo

Una sola fórmula que describe exactamente que es lo que se desea optimizar.

Variables

Cantidades que pueden ser cambiadas para producir el valor óptimo de la función objetivo

Restricciones

Fórmulas que definen los límites de los valores de las variables

Page 4: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

UN EJEMPLO• Una empresa fabrica tres productos 1,2 y 3. Cada producto

requiere tiempos de producción en tres departamentos como se ilustra en la siguiente tabla :

Prod. Depart. 1 Depart. 2 Depart. 3 Benef.

1 3 hrs./unid. 2 hrs./unid. 1 hr./unid. $ 2

2 4 hrs./unid. 1 hr./unid. 3 hr./unid. $ 4

3 2 hrs./unid. 2 hr./unid. 3 hr./unid. $ 2.5

Hrs. Total

600 horas 400 horas 300 horas

Page 5: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Modelo

0,,

30033

40022

600243

.

5.242

321

321

321

321

321

xxx

xxx

xxx

xxx

as

xxxMax

Page 6: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Modelo LINGOCada Línea en LINGO debe terminarse con un punto y coma« ; ». Tu modelo no se resolverá sin ellos.

LINGO Document

Page 7: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Modelo LINGO

;30033

;40022

;600243

;5.242 max

321

321

321

321

xxx

xxx

xxx

xxx

Ya que los computadores no tienen el símbolo , LINGOAdoptó la convención de usar los caracteres <= para denotar .Sin embargo, tu puedes entrar simplemente <. Lo mismo ocurrepara >=, tu puedes entrar simplemente >.

Page 8: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Modelo LINGOTambién podemos incluir al modelo LINGO comentarios, detal manera que mejore la legibilidad de éste.

LINGO Document

Page 9: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Sintaxis general de LINGO• Una expresión puede ser escritas en muchas líneas,

pero la expresión debe ser terminada por un punto y coma. Por ejemplo, podríamos haber utilizado dos líneas para la función objetivo.

LINGO Document

LINGO no diferencia entre letras mayúsculas o minúsculas. Por lo tanto, los siguientes nombres de variables podrían ser equivalentes.

TURBO, Turbo, turbo

Page 10: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Sintaxis general de LINGO• Cuando se le dan nombres a las variables en LINGO, todos

los nombres deben comenzar con un caracter (A-Z). Los otros pueden ser alfabéticos, numéricos o el símbolo _. Los nombres pueden tener una longitud de 32 caracteres.

Page 11: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Resolviendo un modelo LINGO• Una vez que el modelo ha sido entrado en la

« ventana modelo », éste puede ser resuelto mediante :

LINGO Document

• Un click en el botón « solve »

• Seleccionando « solve » del menú LINGO

• Utilizando la tecla ctrl-s

• Si existen errores, éstos serán informados

Page 12: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Ventana de Status del Solver LINGO

• Si no se encontraron errores, la ventana del status del solver de LINGO aparece.

LINGO Document

Aparece también el informe de la solución.

Page 13: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Utilizando el Lenguaje de Modelamiento

• Una de las características más poderosas de LINGO es su lenguaje de modelamiento matemático.

El lenguaje de modelamiento de LINGO permite expresar tu problema de una manera natural que es muy similar a la notación matemática.

Page 14: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

• Powerco tiene tres plantas de generación de energía eléctrica que suministran energía requerida a cuatro ciudades. Cada planta puede suministrar las siguientes cantidades de kilowatt-hora (kwh) de energía eléctrica : la planta 1, 35 millones; la planta 2, 50 millones; la planta 3, 40 millones. Las demandas máximas de energía en estas ciudades, que se presentan al mismo momento (2 p.m.) son las siguientes (en kwh): la ciudad 1, 45 millones; la ciudad 2, 20 millones; la ciudad 3, 30 millones; la ciudad 4; 30 millones. Los costos para enviar 1 millón de kwh de energía de una planta a una ciudad depende de la distancia que la energía tiene que viajar. Formule un PL que minimice el costo para satisfacer la demanda máxima de energía de cada ciudad.

Utilizando el Lenguaje de Modelamiento

Page 15: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Utilizando el lenguaje de modelamiento

C1

(US$)

C2

(US$)

C3

(US$)

C4

(US$)

Oferta

P1 8 6 10 9 35

P2 9 12 13 7 50

P3 14 9 16 5 40

Demanda

45 20 30 30

Page 16: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Modelo

i,jx

jDx

iOx

as

xcz

ij

jj

ij

iiij

ij

0

1,2,3,4

1,2,3

.

minij

ij

Page 17: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Función Objetivo

ij

ijxc ijmin

Lenguaje modelo LINGO

MIN = @SUM(ARCOS(I,J) : C(I,J) * X(I,J));

Notación Matemática

Sintaxis LINGO

min MIN =

@SUM(ARCOS(I,J) :

cij C(I,J)

xij X(I,J));

ij

Page 18: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Las Restricciones de oferta

iOx ij

ij

@FOR(PLANTAS(I) : @SUM(CLIENTES(J):X(I,J))<=O(I));

Notación Matemática

Sintaxis LINGO

@FOR(PLANTAS(I) :

@SUM(CLIENTES(J) :

xij X(I,J)

Oi O(I));

j

i

Page 19: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Las Restricciones de demanda

jDx ji

ij

@FOR(CLIENTES(J) : @SUM(PLANTAS(I):X(I,J))>=D(J));

Notación Matemática

Sintaxis LINGO

@FOR(CLIENTES(J) :

@SUM(PLANTAS(I) :

xij X(I,J)

Dj D(J));

i

j

Page 20: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

EL MODELO LINGO ES

MODEL :

MIN = @SUM(ARCOS(I,J) : C(I,J) * X(I,J));

@FOR(PLANTAS(I) : @SUM(CLIENTES(J):X(I,J))<=O(I));

@FOR(CLIENTES(J) : @SUM(PLANTAS(I):X(I,J))>=D(J));

END

Page 21: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Definiendo los conjuntosTenemos los siguientes conjuntos a definir :

PLANTAS

CLIENTES

ARCOS

SETS:PLANTAS / P1 P2 P3/ : O;

CLIENTES / C1 C2 C3 C4/ : D;

ARCOS(PLANTAS,CLIENTES) : C,X;ENDSETS

Page 22: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

COLOCANDO LOS DATOS

DATA:O = 35 50 40;

D = 45 20 30 30;

C = 8 6 10 9 9 12 13 7 14 9 16 5;

ENDDATA

Page 23: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

MODELO LINGO

LINGO Document

Page 24: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

CARACTERISTICAS ADICIONALES

PODEMOS COLOCARLES NOMBRES A NUESTRA FUNCION OBJETIVO, RESTRICCIONES Y UN TITULOAL MODELO

Ejemplo 1: [objetivo] MIN = X;

Ejemplo 2: @FOR(PLANTAS(I) : [oferta_planta]@SUM(CLIENTES(J):X(I,J))<=O(I));

LINGO Document

Page 25: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Problema

Almacén Fábrica

1 2 3 4 Oferta

1 12 13 10 11 10

2 10 12 14 10 9

3 14 11 15 12 7

Demanda 6 5 7 8 26

Costos de transporte por unidad

Page 26: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Usando Conjuntos (Sets)

• Sets son simplemente grupos de objetos relacionados.

• Un conjunto (set) puede ser una lista de productos, camiones o empleados.

• Cada miembro del conjunto puede tener una o más características relacionadas con él.

• Estas características se conocen bajo el nombre de atributos.

• Los valores de los atributos pueden ser conocidos o desconocidos.

Page 27: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Usando Conjuntos (Sets)• LINGO reconoce 2 tipos de conjuntos :

• Primitivos

• Derivados

•Primitivos : Es un conjunto compuesto sólo de objetos que nopueden ser reducidos posteriormente. Ejemplo : PLANTAS

•Derivados : Es definido a partir de uno o más conjuntos. Ejemplo : ARCOS

Page 28: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Usando Conjuntos• Un conjunto primitivo se define de la

siguiente manera :– setname [/lista_miembros/][: lista_atributos];

PLANTAS / P1 P2 P3/ : O;

Listado explícito de la lista miembros

Page 29: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Usando Conjuntos• Un conjunto primitivo se define de la

siguiente manera :– setname [/lista_miembros/][: lista_atributos];

PLANTAS / miembro1..miembroN/ : O;

Listado implícito de la lista miembros

Page 30: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Usando Conjuntos

Lista miembros Implícito (formato)

Ejemplo Conjunto de miembros

1..n 1..5 1,2,3,4,5stringM..stringN TRUCKS3..

TRUCKS204

TRUCKS3, TRUCKS4,…,TRUCKS204

DayM..dayN MON..FRI MON,TUE,WED,THU,FRI

monthM..monthN OCT..JAN OCT,NOV,DEC,JAN

Page 31: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Usando Conjuntos• Como una ilustración, en el ejemplo

Powerco, podríamos haber definido el conjunto PLANTAS como :

– PLANTAS /P1..P3/ : O;

Page 32: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Usando Conjuntos• Una forma alternativa, cuando se utiliza el formato 1..n, tu puedes

definir la longitud del conjunto en la sección DATA y entonces realizar la referencia :– DATA:

• Número_de_plantas = 3;

– ENDDATA

– SETS:• PLANTAS /1..Número_de_plantas/ : O;

– ENDSETS

Page 33: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Usando Conjuntos : conjuntos derivados

• Para definir un conjunto derivado, tu especificas :– El nombre del conjunto– Sus conjuntos PADRES– Opcionalmente, sus miembros– Opcionalmente, sus atributos

setname(Lista_conj_padres)[/lista_miembros/][:lista_atrib]

Page 34: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Usando Conjuntos : conjuntos derivados

setname(Lista_conj_padres)[/lista_miembros/][:lista_atrib]

EJEMPLO :

SETS:PRODUCTO /A B /;MAQUINA /M N/;SEMANA /1..2/;

ASIGNACION(PRODUCTO,MAQUINA,SEMANA);ENDSETS

Page 35: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Usando Conjuntos : conjuntos derivados y filtros

EJEMPLO :

CAMIONES_PESADOS(CAMIONES) | CAPACIDAD(&1) #GT# 50000;

Operadores lógicos reconocidos por LINGO son :

#EQ# igual#NE# distinto#GE# mayor o igual que#GT# mayor#LT# menor que#LE# menor o igual que

Page 36: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Funcionesfunción uso

@FOR Es utilizado para generar conjuntos de restricciones

@SUM Calcula la suma de una expresión sobre todos los miembros de un conjunto

@MIN Calcula el mínimo de una expresión sobre todos los miembros de un conjunto

@MAX Calcula el máximo de una expresión sobre todo los miembros de un conjunto

Page 37: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Función @SUMSETS:

CLIENTES / C1 C2 C3 C4 C5/ : Demanda;ENDSETSDATA :

DEMANDA = 5 1 3 4 6;ENDDATA

Demanda_total = @SUM(CLIENTES(J):DEMANDA(J));

Demanda_total = @SUM(CLIENTES:DEMANDA);

Demanda_3 = @SUM(CLIENTES(J)|J #LE3# :DEMANDA(J));

Page 38: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Función @MIN, @MAXSETS:

CLIENTES / C1 C2 C3 C4 C5/ : Demanda;ENDSETSDATA :

DEMANDA = 5 1 3 4 6;ENDDATA

Min_demanda = @MIN(CLIENTES(J):DEMANDA(J));Max_demanda = @MAX(CLIENTES(J):DEMANDA(J));

Min_demanda = @SUM(CLIENTES:DEMANDA);Max_demanda = @SUM(CLIENTES:DEMANDA);

Page 39: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Función @FORSETS:

CAMIONES / RENAULT FORD DODGE / : CARGA;ENDSETS

@FOR(CAMIONES(T) : CARGA(T) <= 2500);

CARGA(RENAULT)<=2500;CARGA(FORD) <= 2500;CARGA(DODGE)<=2500;

Page 40: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Usando funciones de dominios para variables

Tipos variables definición

@GIN Variable entera

@BIN Variable binaria

@FREE Cualquier valor

@BND Rango para la variable

Page 41: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Sintaxis • @GIN(nombre_variable).

– @GIN(X);

LINGO Document

Page 42: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Problema de la mochila : @BINartículo peso Rating

1 1 2

2 3 9

3 4 3

4 4 8

5 4 10

6 1 6

7 5 4

8 10 10

Page 43: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Modelo LINGOMODEL:SETS:

ARTICULOS /A1..A10/: PESO, RATING, INCLUYE;

ENDSETSDATA:PESO RATING = 1 23 94 35 86 107 65 410 10;CAPACIDAD_MOCHILA = 15;ENDDATA

Page 44: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Modelo LINGOMAX = @SUM(ARTICULOS: RATINGS * INCLUYE);

@SUM(ARTICULOS:PESO*INCLUYE)<=CAPACIDAD_MOCHILA;

@FOR(ARTICULOS:@BIN(INCLUYE));

END

Page 45: Lingo Tutorial

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

Inve

stig

ació

n O

per

ativ

a I

VARIABLES ACOTADAS

@BND(cota_inferior,nombre_variable,cota_superior);