PROGRAMACIÓN CON RESTRICCIONES -...
Transcript of PROGRAMACIÓN CON RESTRICCIONES -...
![Page 1: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/1.jpg)
PROGRAMACIÓN CON RESTRICCIONES
PRESENTACIÓN SOBRE COMET
Autores: Matías Sánchez-Carrasco GarcíaMariano Hernández García
![Page 2: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/2.jpg)
ÍNDICE:
•Enlaces de interés
•Breve historia
•¿Cómo funciona Comet?
• Programación lineal y entera
• Restricciones basadas en búsqueda local.
• Optimización dinámica estocástica
•Sobre el resolutor
•Instalación
•Ejemplos
•Bibliografía
![Page 3: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/3.jpg)
ENLACES DE INTERÉS:
Página de Dynadec:
http://dynadec.com/
Página Pascal Van Hentenryck:
https://dl.dropbox.com/u/62188928/site/Welcome.html
Tutorial:
http://wpweb2.tepper.cmu.edu/rlang/CPAIOR09/CPAIOR09_Tutorial.pdf
Enlace a wikipedia:
http://en.wikipedia.org/wiki/Constraint_programming
Ejemplos:
http://www.hakank.org/comet/
![Page 4: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/4.jpg)
BREVE HISTORIA:
Comet fue creado por Pascal Van Hentenryck,fundador de Dynadec y director del laboratorio de
optimización de la universidad de Brown , en Rhode
Island.
La vida de Pascal se ha centrado en el estudio de la
optimización y de los lenguajes con restricciones,
con el fin de resolver problemas de restricciones
muy complicados en el menor tiempo posible.
![Page 5: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/5.jpg)
BREVE HISTORIA:
A lo largo de su vida ha recibido
numerosos premios (NSF Young
Investigator adward, ACP adward…).
Ha escrito 5 libros y 230 publicaciones
científicas.
![Page 6: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/6.jpg)
BREVE HISTORIA:
Algunas de sus publicaciones más recientes
(año 2012) son:– A Linear-Programming Approximation of AC Power Flows.
– An Optimal Filtering Algorithm for Table Constraints.
– Constraint Satisfaction over Bit-Vectors.
– ...
Además, es uno de los principales
diseñadores e implementadores del CHIP, la
fundación de todos los sistemas de
programación con restricciones modernos.
![Page 7: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/7.jpg)
BREVE HISTORIA:
Actualmente, Pascal es profesor de
“Ciencias de la computación e información”,
de la Escuela de Ingenieros y de la
Universidad de Melbourne en Australia.
![Page 8: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/8.jpg)
¿CÓMO FUNCIONA COMET?
Comet combina– Linear and Integer
Programming
– Programación con restricciones
– Restricciones basadas en búsqueda local
– Optimización dinámica estocástica
Junto con los lenguajes más habituales (C++, XML, JAVA…)
![Page 9: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/9.jpg)
Programación lineal y entera
Técnica de optimización fundamental.
Permite:
– Generación de columnas y análisis de sensibilidad.
– Abstracciones de modelado para programas lineales
y enteros.
– Especificar procedimientos de búsqueda con
construcciones de alto nivel no deterministas.
![Page 10: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/10.jpg)
Restricciones basadas en búsqueda local
Tiene 2 componentes:
– Descripción de alto nivel basada en restricciones, combinación de
restricciones y funciones de la aplicación.
– Procedimiento de búsqueda separado
Esto permite al proceso de búsqueda encontrar
soluciones manteniendo la eficiencia.
![Page 11: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/11.jpg)
Optimización dinámica estocástica
Capacidad para tomar decisiones de forma
dinámica en un tiempo limitado.
Capacidad para utilizar datos históricos o un
modelo estocástico (comportamiento
aparentemente aleatorio de variables) para
optimizar.
Capacidad para minimizar costes en un entorno
dinámico.
![Page 12: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/12.jpg)
SOBRE EL RESOLUTOR
Tipos de datos:
– Enteros y racionales (int, double, float,real…)
– Booleanos (boolean)
– Strings (string)
– Archivos de e/s
– Arrays n-dimensionales
– Conjuntos, pilas, colas, montones…
![Page 13: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/13.jpg)
SOBRE EL RESOLUTOR
Funciones predefinidas:
– Sum y Product
– AND (&&), OR (||), igualadad (==), implicación (=>)…
– Predicado Table
– Predicado Cummulative
– Predicado Alldifferent.
– Cardinality
– Atmost
– Otras
![Page 14: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/14.jpg)
SOBRE EL RESOLUTOR
Características:– Funciones
– Clases
– Objetos
– Interfaces
– Manejo de eventos
– Control de flujo
– Hebras
– Programación concurrente
![Page 15: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/15.jpg)
SOBRE EL RESOLUTOR
Estructura de un programa de optimización en Comet:
Solver<CP> cp();//declaracion de variables
minimize<cp> //maximize<cp>//expresiones o variables
subject to {//constraints
}using {
//búsquedas no deterministas}
![Page 16: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/16.jpg)
Instalación:
Soporta:
– windows xp, Vista, 7.
– Mac OSX.
– Ubuntu, Fedora…
Entorno de trabajo:
– The Comet Hibrid Optimization Platform
– 3 licencias:
Commercial: empresas, gobiernos, agencias…
Academic: profesores.
Student: estudiantes.
![Page 17: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/17.jpg)
Instalación:
Descarga: http://dynadec.com/support/downloads/
Versión más reciente: 2.1.1
Windows: instalación sencilla por medio de un instalador.
Linux:
También puede integrarse junto con nuestro entorno de trabajo
favorito, por ejemplo eclipse, y utilizarlo junto a c++, java…
![Page 18: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/18.jpg)
Algunas capturas
![Page 19: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/19.jpg)
Algunas capturas
![Page 20: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/20.jpg)
Algunas capturas
![Page 21: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/21.jpg)
El Problema de las n reinas:“Hola mundo” en PR. Versión Comet
import cotfd;
Solver<CP> cp();
int n = 8;
range S = 1..n;
var<CP>{int} q [i in S] (cp,S);
solve<cp> {
cp.post(alldifferent(all(i in S) q[i] + i));
cp.post(alldifferent(all(i in S) q[i] - i));
cp.post(alldifferent(q));
}
using {
labelFF(q);
}
cout << q<< endl;
LabelFF(q) es equivalente a poner:
forall (i in S) by (q[i].getSize())
tryall <cp>(v in S) cp.label(q[i],v);
![Page 22: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/22.jpg)
El Problema de las n reinas:“Hola mundo” en PR. Versión Minizinc
int:n=8;
array [1..n] of var 1..n: t;
% constraints
%fila
constraint forall (i in 1..n-1)(forall(j in i+1..n)(t[i]!=t[j]));
% dos diagonales
constraint forall (i in 1..n-1)(forall(j in i+1..n)(t[i]!=t[j]+(j-i)));
constraint forall (i in 1..n-1)(forall(j in i+1..n)(t[i]!=t[j]-(j-i)));
solve satisfy;
output[ show(t[i])++“ " | i in 1..n];
![Page 23: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/23.jpg)
import cotfd;
Solver<CP> cp();
int s = 112;
range Side = 1..s;
range Square = 1..21;
int side[Square] = [50,42,37,35,33,29,27,25,24,19,18,17,16,15,11,9,8,7,6,4,2];
var<CP>{int} x[i in Square](cp,1..s-side[i]+1);
var<CP>{int} y[i in Square](cp,1..s-side[i]+1);
solve<cp> {
forall (i in Square, j in Square : i < j)
cp.post(x[i] + side[i] <= x[j]
|| x[j] + side[j] <= x[i]
|| y[i] + side[i] <= y[j]
|| y[j] + side[j] <= y[i]);
forall (p in Side) {
cp.post(sum(i in Square)
side[i] * ((x[i] <= p) && (x[i] >= p - side[i] + 1)) == s);
cp.post(sum(i in Square)
side[i] * ((y[i] <= p) && (y[i] >= p - side[i] + 1)) == s);
}
}
Ejemplo 2: Cuadrados mágicos (I)
![Page 24: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/24.jpg)
Ejemplo 2: Cuadrados mágicos (II)
using {
forall (k in Square)
selectMin(p in Square :
tryall <cp>(i in Iter(k in Square)( cp.label(x[i],r);
onFailure
cp.diff(x[i],r);
}
forall (k in Square){
selectMin (p in Square :
tryall <cp>(i in Iter(i in Square) (
cp.label(y[i],r);
onFailure
cp.diff(y[i],r);
}
cout << x << endl;
cout << y << endl;
cout << "#choices = " << cp.getNChoice() << endl;
cout << "#fail = " << cp.getNFail() << endl;
![Page 25: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/25.jpg)
import cotfd;
Solver<CP> cp();
range Cars = 1..100;
range Configs = 1..18;
range Options = 1..5;
int lb[Options] = [1,2,1,2,1];
int ub[Options] = [2,3,3,5,5];
int demand[Configs] = [5,3,7,1,10,2,11,5,4,6,12,1,1,5,9,5,12,1];
int requires[Configs,Options] = [[1,1,0,0,1],[1,1,0,1,0],...,[0,0,1,0,0]];
set{int} options[o in Options] = Iter (c in Configs)(requires[c,o] == 1);
var<CP>{int} line[Cars](cp,Configs);
solve<cp>
forall (o in Options) cp.post(sequence(line,demand,lb[o],ub[o],options[o]));
using labelFF(line);
cout << "#choices = " << cp.getNChoice() << endl;
cout << "#fail = " << cp.getNFail() << endl;
Ejemplo 3: Garaje
![Page 26: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/26.jpg)
import cotfd;
enum Country = {Belgium, Denmark, France, Germany, Netherlands, Luxembourg};
enum Colors = {blue, red, yellow, gray};
Solver<CP> m();
var<CP>{int} color[Country](m,Colors);
Integer num_solutions(0);
exploreall<m> {
m.post(color[France] != color[Belgium]);
m.post(color[France] != color[Luxembourg]);
m.post(color[France] != color[Germany]);
m.post(color[Luxembourg] != color[Germany]);
m.post(color[Luxembourg] != color[Belgium]);
m.post(color[Belgium] != color[Netherlands]);
m.post(color[Belgium] != color[Germany]);
m.post(color[Germany] != color[Netherlands]);
m.post(color[Germany] != color[Denmark]);
}
Ejemplo 4: Coloreado de mapas (I)
![Page 27: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/27.jpg)
using {
labelFF(m);
num_solutions++;
cout << num_solutions << ": " << color << endl;
}
cout << "It was " << num_solutions << " solutions." << endl;
Ejemplo 4: Coloreado de mapas (II)
![Page 28: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/28.jpg)
import cotfd;
int n = 5;
range R = 1..5;
Solver<CP> m();
var<CP>{int} nbRabbits(m,0..20);
var<CP>{int} nbPheasants(m, 0..20);
Integer num_solutions(0);
exploreall<m> {
m.post(20 == nbRabbits + nbPheasants);
m.post(56 == 4*nbRabbits + 2*nbPheasants);
}
using {
label(m);
num_solutions++;
cout << "nbRabbits: " << nbRabbits << endl;
cout << "nbPheasants: " << nbPheasants << endl;
}
Ejemplo 5: Conejos y faisanes
![Page 29: PROGRAMACIÓN CON RESTRICCIONES - UCMgpd.sip.ucm.es/rafa/docencia/pr/presenta/comet-MatiasSanchez-Car… · – Generación de columnas y análisis de sensibilidad. – Abstracciones](https://reader034.fdocuments.es/reader034/viewer/2022042215/5ebb319757822438ee3b60bc/html5/thumbnails/29.jpg)
BIBLIOGRAFÍA:
“Constraint-based local search”. Pascal Van Hentenryck & Laurent
Michael, Cambridge
– http://cisne.sim.ucm.es/search~S6*spi?/Xconstraint&searchscope=6&SO
RT=DZ/Xconstraint&searchscope=6&SORT=DZ&extended=0&SUBKEY=
constraint/1%2C166%2C166%2CB/frameset&FF=Xconstraint&searchsco
pe=6&SORT=DZ&36%2C36%2C/indexsort=-
- http://www.amazon.com/Constraint-Based-Local-Search-
PascalHentenryck/dp/026251348X