Download Programación con restricciones en Java
Document related concepts
no text concepts found
Transcript
Programación con restricciones en Java En nombre es muy explicativo por si solo: JAva COnstraint Programming. JaCoP es una librería Java de código abierto dedicada al modelado y resolución de problemas con restricciones. Su núcleo fue creado en el 2001 por Krzysztof Kuchcinski and Radoslaw Szymanek. Se distribuye con una licencia GNU Affero GPL de manera gratuita. + info : www.jacop.eu (Web oficial) Api: jacopapi.osolpro.com Contiene 3 tipos de resolutores: Variables de Dominio Finito: IntVar Conjuntos: SetVar SAT: BooleanVar (medalla de plata en Minizinc Challenge 2011) Implementa un intérprete de flatzinc Código: pr.jacop.EjemploMapas.ColorearMapas.java Alldifferent: Sintaxis: ▪ Alldifferent(ArrayList<? Extends IntVar> variables) ▪ Alldifferent(IntVar[] variables) Funcionamiento: ▪ Exige que todas las variables indicadas sean diferentes dos a dos. Ejemplo: IntVar a = new IntVar(store, "a", 2, 3); IntVar b = new IntVar(store, "b", 2, 3); IntVar c = new IntVar(store, "c", 1, 3); IntVar[] v = {a, b, c}; store.impose(new Alldifferent(v)); Código: pr.jacop.EjemploSudoku.Sudokus.java Cumulative: Sintaxis: ▪ Cumulative(inicios, duraciones, recursos, limiteRecurso) Funcionamiento: ▪ Planifica en el tiempo los inicios de tareas su duración y sus recursos requeridos con un límite de recursos. Código: pr.jacop.EjemploCumulative.Programadores.java JaCoP dispone de una serie de predicados predefinidos entre los que se encuentran: Circuit, Element, Distance, Diff2, Min, Max, Sum, SumWeight, ExtensionalSupport, ExtensionalConflict, Assignment, Count, Values, GCC, Among, AmongVar, Regular, Knapsack, Geost, NetworkFlow, Binpacking, Sequence, Stretch, Lex, Soft-Alldifferent, Soft-GCC. En JaCoP Disponemos de todas las posibles restricciones de variables enteras que se pueden esperar de un lenguaje de programación con restricciones. Restricción Especificación JaCoP X = Const X=Y X≠Const X≠Y X > Const X>Y X ≥ Const X≥Y X < Const X<Y X ≤ Const X≤Y X ⋅ Const = Z X⋅Y=Z X÷Y=Z X modY = Z X + Const = Z X+Y=Z X + Y + Const = Z X+Y+Q=Z X + Const ≤ Z X+Y≤Z X + Y > Const X + Y + Q > Const |X| = Y XY = Z XeqC(X, Const) XeqY(X, Y) XneqC(X, Const) XneqY(X, Y) XgtC(X, Const) XgtY(X, Y) XgteqC(X, Const) XgteqY(X, Y) XltC(X, Const) XltY(X, Y) XlteqC(X, Const) XlteqY(X, Y) XmulCeqZ(X, Const, Z) XmulYeqZ(X, Y, Z) XdivYeqZ(X, Y, Z) XmodYeqZ(X, Y, Z) XplusCeqZ(X, Const, Z) XplusYeqZ(X, Y, Z) XplusYplusCeqZ(X, Y, Const, Z) XplusYplusQeqZ(X, Y, Q, Z) XplusClteqZ(X, Const, Z) XplusYlteqZ(X, Y, Z) XplusYgtC(X, Y, Const) XplusYplusQgtC(X, Y, Q, Const) AbsXeqY(X, Y) XexpYeqZ(X, Y, Z) Restricción Especificación JaCoP ¬c c1 ⇔ c2 c1 ∧ c2 ∧…∧ cn Not(c); Eq(c1, c2); PrimitiveConstraint[] c = {c1, c2, …cn}; And(c); or ArrayList c = new ArrayList(); c.add(c1); c.add(c2); …c.add(cn); And(c); PrimitiveConstraint[] c = {c1, c2, …cn}; Or(c); or ArrayList c = new ArrayList(); c.add(c1); c.add(c2); …c.add(cn); Or(c); c1 ∨ c2 ∨…∨ cn Restricción Especificación JaCoP X in Dom c⇔B c ⇔¬B if c1 then c2 if c1 then c2 else c3 In(X, Dom); Reified(c, B); Xor(c, B); IfThen(c1, c2); IfThenElse(c1, c2, c3); Operaciones Booleanas result = b1 ∧ b2 ∧…∧ bn result = b1 ∨ b2 ∨…∨ bn result = b1 ⊕ b2 result = b1 → b2 BooleanVar[] b = {b1, b2, …, bn}; or ArrayList b = new ArrayList<="" span=""> b.add(b1); b.add(b2); …b.add(bn); BoolanVariable result = new BooleanVar(store, "result"); AndBool(b, result) OrBool(b, result) XorBool(b1, b2, result) IfThenBool(b1, b2, result) Restricción Especificación JaCoP e∈A S1 = S2 S1 ⊆ S2 S1 ⋃ S2 = S3 S1 ⋂ S2 = S3 S1 \ S2 = S3 S1 <> S2 Match #S1 = X Weighted sum < S, W > = X Set[X] = Y EinA(e, A) AeqB(S1, S2) AinB(S1, S2) AunionBeqC(S1, S2, S3) AintersectBeqC(S1, S2, S3) AdiffBeqC(S1, S2, S3) AdisjointB(S1, S2) Match(Set, VarArray) CardAeqX(S, X) SumWeightedSet(S, W, X) ElementSet(X, Set, Y) Código: pr.jacop.EjemploCumulative.Programadores.java 3.000 millones de razones por las que usar JaCoP: Es sencillo y rápido de usar. Es fácil de integrar en aplicaciones comerciales.