Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic...

251
Universidad Polit´ ecnica de Madrid Escuela Universitaria de Inform´ atica Trabajo f´ ın de carrera Lenguaje para implementar sistemas expertos con modelos algebraicos autor Roberto Maestre Mart´ ınez tutor Antonio Hernando Esteban departamento Sistemas Inteligentes Aplicados Diciembre, 2010

Transcript of Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic...

Page 1: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Universidad Politecnica de MadridEscuela Universitaria de Informatica

Trabajo fın de carrera

Lenguaje para implementarsistemas expertos

con modelos algebraicos

autorRoberto Maestre MartıneztutorAntonio Hernando Estebandepartamento

Sistemas Inteligentes Aplicados

Diciembre, 2010

Page 2: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

MADRID

ii

Page 3: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Agradecimientos

“The path from dreams to success does exist.May you have the vision to find it,

the courage to get on to it,and the perseverence to follow it.

Wishing you a great journey.”

Kalpana Chawla

En primer lugar querıa agradecer al profesor Antonio Hernando Esteban la confianzadepositada en mı a la hora de abordar este proyecto. Desde el primer momento me dirigio deuna manera clara y concisa, ensenandome a enfocar el esfuerzo y profundizar en losfundamentos teoricos, ajustando ası el proyecto a mis inquietudes intelectuales. En este intensoperiodo alimento mi hambre investigadora con una mezcla de conocimiento, energıa, positividade ilusion.A mi Alma mater, el Consejo Superior de Investigaciones Cientıficas (CSIC); institucion quesiempre me brindo una oportunidad y de la que he aprendido los valores del trabajo, laexcelencia y la perseverancia.A mis companeros del proyecto Dynamic Complexity of Cooperation-Based Self-OrganizingNetworks de la European Science Foundation y de la Unidad de Sistemas de InformacionGeografica del CSIC con los que he compartido los ultimos anos.A mi familia, Epifanio Angel, Mercedes, Daniel y Corina; por su apoyo incondicional en todomomento y su crıtica positiva; por ensenarme con su ejemplo a pensar libremente a traves dela razon. Por no fallar nunca. Por ser mi estrella polar.A Sol por mostrarme con su ejemplo de lucha y valentıa a no perder nunca de vista el camino

iii

Page 4: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

que lleva a la consecucion de los suenos.Al doctor Antonio Zarazaga Monzon por la informacion aportada en el diseno del sistemaexperto propuesto en este trabajo.A Jose Gabriel Perez Dıez, Jesus Lopez Sanchez y su asignatura Compiladores e Interpretesen donde aprendı los conceptos necesarios para abordar el diseno y construccion del traductorpropuesto en este trabajo.

iv

Page 5: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Abstract

According to previous mathematical results, expert systems can be quickly and easilyimplemented on specialized mathematical software (Computer Algebra Systems). Thesemathematical results provide two equivalent different approaches to implement algebraicallyexpert systems: one based on propositional Boolean logic and another based on the ‘concept-attribute-value’ representation paradigm. Both approaches are equivalent in the sense thatevery expert system implemented through one algebraic approach can be also implementedon the another algebraic approach. However, the performance of these algebraic approachesdepends on the specific expert system. Consequently, the most suitable algebraic approach canonly be determined if the specific expert system is implemented in both algebraic approachesin order to compare their performance. In this document we have designed a natural languageto implement algebraically expert systems which provides these three main advantages: wecan implement directly any expert system in both algebraic approach since our languageis independent of the algebraic approach; we can use any computer algebra system sinceour language is independent of the computer algebra system used; and we do not need tobe acquainted with specialized abstract mathematical concepts like Grobner basis or normalforms since our language is natural and does not make use of these concepts.

v

Page 6: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

vi

Page 7: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Resumen

De acuerdo con resultados matematicos previos, se puede implementar sistemas expertosde una manera sencilla y rapida usando software matematico especializado (los sistemasde algebra computacional). Estos resultados matematicos proporcionan dos aproximacionesalgebraicas equivalentes aunque diferentes: una de ellas esta basada en logica proposicionalBooleana, la otra esta basada en el paradigma de representacion ’Concepto-Atributo-Valor’.Ambas aproximaciones son equivalentes en el sentido de que cualquier sistema expertoimplementado en una aproximacion algebraica puede ser implementado tambien en la otraaproximacion algebraica. No obstante, la ejecucion de ambas aproximaciones algebraicasdepende del sistema experto especıfico y por ello, la aproximacion algebraica mas adecuadasolo puede ser determinada si se implementa el sistema experto en ambas aproximacionescon el fin de poder comparar su eficiencia. En este proyecto final de carrera se ha disenadoun lenguaje natural de implementacion algebraica de sistemas experto que proporcionalas siguientes ventajas: se puede implementar directamente cualquier sistema experto encualquiera de las dos aproximaciones algebraicas ya que nuestro lenguaje es independiente dela aproximacion algebraica; se puede usar cualquier sistema de algebra computacional puestoque nuestro lenguaje es independiente del sistema algebraico usado; y no es necesario estarfamiliarizado con conceptos abstractos como las bases de Grobner o las formas normales yaque nuestro lenguaje no utiliza esos conceptos.

vii

Page 8: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

viii

Page 9: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Obra derivada

Como fruto del presente trabajo de fın de carrera, se ha enviado un artıculo a la revista‘Expert Systems with Applications’, situada en el primer cuartil del JCR, con el siguiente tıtulo:

“A natural language for implementing algebraically expert systems”

Antonio Hernando, Roberto Maestre-Martınez, Eugenio Roanes-Lozano.

ix

Page 10: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

x

Page 11: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Indice

1. Estado de la cuestion, motivacion y objetivos 5

2. Sistemas de computacion algebraica 9

2.1. PolyBoRi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.1.1. Version utilizada . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.1.2. Definiciones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.2. Singular . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122.2.1. Version utilizada . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132.2.2. Definiciones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

2.3. CoCoA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.3.1. Version utilizada . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162.3.2. Definiciones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

3. Vision general de sistemas expertos implementados con algebra computacional 19

3.1. Formalismo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193.2. Modelo algebraico . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223.3. Ejemplo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

4. Modelo logico proposicional Booleano 25

4.1. Formalismo de representacion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254.2. Modelo algebraico . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324.3. Implementacion en el sistema de computacion algebraica Singular . . . . . . . 354.4. Implementacion en el sistema de computacion algebraica PolyBoRi . . . . . . 39

5. Modelo ‘Concepto-Atributo-Valor’ 43

5.1. Formalismo de representacion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435.2. Modelo algebraico . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

1

Page 12: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

5.3. Implementacion en el sistema de computacion algebraica Singular . . . . . . . 516. Lenguaje de implementacion algebraico de sistemas expertos 53

6.1. Descripcion del lenguaje . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536.2. Traduccion al modelo logico proposicional Booleano . . . . . . . . . . . . . . . 56

6.2.1. Traduccion en CAS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566.2.2. Tabla de sımbolos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

6.3. Traduccion del modelo ‘Concepto-Atributo-Valor’ . . . . . . . . . . . . . . . . . . 646.3.1. Traduccion en CAS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646.3.2. Tabla de sımbolos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

7. Ejemplo de Sistema Experto 69

7.1. Especificacion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697.2. Especificacion en lenguaje fuente . . . . . . . . . . . . . . . . . . . . . . . . . . . 737.3. Resultados en el modelo logico proposicional Booleano . . . . . . . . . . . . . 79

7.3.1. Traduccion en PolyBoRi . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797.3.2. Traduccion en Singular . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877.3.3. Traduccion en CoCoA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

7.4. Resultados en el modelo ‘Concepto-Atributo-Valor’ . . . . . . . . . . . . . . . . 1027.4.1. Traduccion en Singular . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1027.4.2. Traduccion en CoCoA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

8. Comparacion de modelos 111

8.1. Experimento 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1128.2. Experimento 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

9. Trabajo futuro 117

9.1. Sistemas expertos basados en logica proposicional multivaluada . . . . . . . . 1172

Page 13: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

9.1.1. Formalismo de representacion . . . . . . . . . . . . . . . . . . . . . . . . 1179.1.2. Modelo algebraico . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

10. Conclusiones 123

A. APENDICE: Especificacion del traductor en EBNF y JAVACC 125

B. APENDICE: Comparacion de sistemas de computacion algebraica 127

B.1. Mejores CAS para experimento 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 127B.2. Mejores CAS para experimento 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

C. APENDICE: Scripts para la automatizacion de pruebas 131

C.1. Generador de sistemas expertos . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131C.2. Automatizacion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

D. APENDICE: Introduccion a las bases de Grobner 141

D.1. Division (reduccion) de polinomios multivariantes . . . . . . . . . . . . . . . . . 141D.2. Definicion normal de base de Grobner . . . . . . . . . . . . . . . . . . . . . . . . 143D.3. La aplicacion de teorıa de bases de Grobner . . . . . . . . . . . . . . . . . . . . 143D.4. ¿Como puede una base de Grobner ser construida? . . . . . . . . . . . . . . . . 144

E. APENDICE: Codigo Traductor JavaCC 145

F. APENDICE: Codigo de tabla de sımbolos 205

F.1. Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205F.2. Gestor de la tabla de sımbolos . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

G. APENDICE: Modificacion, compilacion y empaquetado del traductor 237

G.1. Compilacion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237G.2. Empaquetado . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

3

Page 14: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Indice de cuadros

1. Tabla de variables. Fichero: ts mlp 1.txt . . . . . . . . . . . . . . . . . . . . . . . 632. Tabla de variables. Fichero: ts mcav 1.txt . . . . . . . . . . . . . . . . . . . . . . 673. Tabla de valores. Fichero: ts mcav 2.txt . . . . . . . . . . . . . . . . . . . . . . . 67

Indice de figuras

1. Experimento 1. Resultados. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1132. Experimento 2. Resultados. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1163. Experimento 1. Seleccion del mejor sistema para el modelo logico proposicional

booleano . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1274. Experimento 1. Seleccion del mejor sistema para el modelo ‘Concepto-Atributo-

Valor’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1285. Experimento 2, Seleccion del mejor sistema para el modelo logico proposicional

booleano . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1296. Experimento 2, Seleccion del mejor sistema para el modelo ‘Concepto-Atributo-

Valor’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

4

Page 15: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1. Estado de la cuestion, motivacion y objetivos

Los sistemas expertos son programas computacionales, que dentro de un dominio deconocimiento, tratan de simular las decisiones que expertos humanos en este dominio tomarıan.En los ultimos anos, muchos investigadores se han centrado en el desarrollo de tecnicaspara la representacion de conocimiento humano en una computadora y para el razonamientoautomatico.Una manera interesante para representar conocimiento en un sistema experto se basa endescribirlo a traves de la logica proposicional. Por medio de un resultado matematico [1]basado en trabajos previos [2, 3, 4, 5, 6], las cuestiones relativas a la inferencia del sistemaexperto pueden ser tratadas algebraicamente. Por esto, los sistemas expertos basados enlogica proposicional booleana pueden ser facilmente implementados por medio de Sistemasde Computacion Algebraica (CAS, en ingles) como CoCoA [7, 8], Singular [9] o PolyBoRi [10].Haciendo uso de estos resultados, se han implementado diversos sistemas expertos en losultimos anos [11, 12, 13, 14].El paradigma ‘Concepto-Atributo-Valor’ [15] proporciona una forma mas rapida y natural derepresentar conocimiento. Recientemente, se ha obtenido un resultado matematico similar[16]que permite implementar sistemas expertos basados en este paradigma con CAS.Aunque el metodo algebraico dado en logica proposicional booleana y el basado en elparadigma ‘Concepto-Atributo-Valor’ son diferentes maneras de implementar sistemas expertosmatematicamente, ambos son equivalentes en el sentido de que cualquier sistema expertoimplementado en un modelo puede ser implementado en el otro.En este trabajo, mostramos una comparacion entre el rendimiento de ambos enfoquesalgebraicos con el objetivo de afirmar que la cuestion sobre que enfoque algebraico es

5

Page 16: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

mejor, depende directamente de las caracterısticas del sistema experto. Debido a este motivo,solo podremos averiguar que enfoque debemos elegir para cada sistema experto si loimplementamos en ambos modelos, y esto, implica duplicar esfuerzos.Otra cuestion relacionada con la implementacion del sistema experto es la eleccion del CASsobre el que se ejecutara. Al igual que en la eleccion del metodo algebraico, cambiar el CAS enel que sera ejecutado, implica el arduo trabajo adaptar los comandos al CAS destino. Ademas,aunque ambos enfoques proporcionan una manera rapida y facil de implementar sistemasexpertos, los desarrolladores deben de estar familiarizados con abstractos y complicadosconceptos matematicos como son la ‘forma normal’ y la ‘base de Grobner’.Con el fin de evitar estos problemas, en este trabajo final de carrera desarrollaremos unnuevo lenguaje para implementar algebraicamente sistemas expertos. El uso de este lenguajeproporcionara las siguientes ventajas:

i) El lenguaje es independiente del enfoque algebraico elegido. En este trabajomostraremos como traducir un sistema experto descrito en nuestro lenguaje en ambosenfoques, de esta manera, podremos generar codigo facilmente para ambos enfoques conel fin de experimentar con ellos y elegir el que mejor rendimiento muestre.

ii) El lenguaje es independiente del CAS elegido. De esta manera, cambiar el CAS soloconllevara compilar el sistema experto especificado en nuestro lenguaje.

iii) El lenguaje no usa notacion matematica. Por lo tanto, los desarrolladores no necesitanconocer los conceptos matematicos que son comunmente utilizados en estos enfoquesalgebraicos (Forma normal y base de Grobner).

iv) Nuestro lenguaje proporciona una manera sencilla de implementar sistemas expertos.De hecho, el codigo del sistema es normalmente mas reducido y comprensible que elcodigo descrito en el CAS elegido. Nuestro lenguaje proporciona una manera muy facil

6

Page 17: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

e intuitiva de escribir reglas (las cuales, son la manera mas natural de describir la basede conocimiento de un sistema experto).

En el apartado 3, se dara una vision general de los enfoques algebraicos obtenidos paraimplementar sistemas expertos en sistemas de computacion algebraica. En el apartado 4 y5, se presentan los enfoques algebraicos relacionados con la logica Booleana proposicionaly el modelo ‘Concepto-Atributo-Valor’. En el apartado 6, se describe el nuevo lenguaje quese ha disenado para implementar sistemas expertos. Haciendo uso de este lenguaje, en elapartado 7 se desarrolla un pequeno sistema experto medico real. En el apartado 8, se haceun estudio comparativo de los diferentes enfoques algebraicos, concluyendo que la elecciondel enfoque algebraico mas adecuado depende del sistema experto en concreto. Finalmente,en el apartado 10, se presentan las conclusiones.

7

Page 18: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

8

Page 19: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

2. Sistemas de computacion algebraica

2.1. PolyBoRi

PolyBoRi esta implementado como una librerıa C++ para polinomios sobre anillosBooleanos, y proporciona un alto nivel de abstraccion de tipos de datos para polinomios ymonomios Booleanos, vectores exponente, ası como para los anillos de polinomios subyacentes.Las variables del anillo pueden ser identificadas por sus ındices o por una cadenapersonalizada. Las estructuras polinomiales y los monomios usan diagramas de decisionbinarios cero suprimido (ZDDs) como tipo de almacenaje interno, pero es transparente parael usuario. La implementacion actual utiliza el diagrama de decisiones de gestion de Cudd.Ademas, las operaciones basicas con polinomios - como, la adicion y la multiplicacion -han sido implementadas y asociadas a los operadores correspondientes. Con el objetivo deconseguir una implementacion eficiente, los operadores fueron reformulados en terminos deconjuntos de operaciones, los cuales son compatibles con el enfoque ZDD.Una completa interfaz para Python permite el analisis sintactico de los sistemas polinomicoscomplejos, tambien estrategias para el calculo de la base de Groebner. Ademas, con laherramienta de ipython las estructuras de datos PolyBoRi y sus procedimientos se puedenutilizar de forma interactiva como una herramienta de lınea de comandos.Ademas de optimizar las estructuras de datos, PolyBoRi dispone de una implementacion dereferencia para el calculo de la base Grobner utilizando una variante mejorada del algoritmode slimgb, que se aplico por primera vez en el sistema de algebra computacional Singular .El “framework” fue desarrollado y disenado conjuntamente con el autor original de slimgb.El rendimiento inicial de PolyBoRi parece ser muy prometedor. Se puede observar, que la

9

Page 20: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

ventaja de PolyBoRi crece con el numero de variables. Para muchas aplicaciones practicaseste tamano cada vez sera mayor. Tenemos mucha confianza en que sera posible abordaralgunos de estos problemas en el futuro a traves de metodos mas especializados. Este es unpunto clave en el desarrollo de PolyBoRi para facilitar soluciones de problemas especıficosy soluciones de alto rendimiento2.1.1. Version utilizada

0.52.1.2. Definiciones

1. Declaracion del anillo.Hay que destacar que PolyBoRi solo trabaja sobre anillos Booleanos, por lo queel anillo sera definido por defecto en el cuerpo Z2 y solo habra que especificarlas variables.La sintaxis para definir un anillo en PolyBoRi es la siguiente:declare_ring([Block("n",m)],globals());

donde n sera el identificador de un array de variables y m sera el tamano de esearray. Cada variable del array se nombrara de la siguiente manera:

n(0), n(1), n(2), ..., n(m− 1)2. Definicion de polinomios

El codigo es el siguiente:p0=x(0)ˆ2+x;

que es equivalente a p0 = x20 + x .10

Page 21: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

3. Definicion de idealesPara definir un ideal en PolyBoRi se hara de la siguiente manera:J=[p1,p2,...,pn];

donde p1, p2, ...pn son polinomios.4. Definicion de funciones

El codigo es el siguiente:def nombrefunction(par1,par2,...,parn):

return <expresion>

donde:• ‘def’ y ‘return’ son palabras reservadas.• nombrefunction es el nombre de la funcion y por la cual sera llamada desde

el cualquier parte del codigo.• par1, ..., parn son los parametro pasados a la funcion.• ‘Polinomial(< expresion >)’ hace referencia implıcita al que el tipo de la

expersion es un polinomio.Una < expresion > puede ser por ejemplo: 1+1, o x(0)+1, o Polynomial(x(0)2)∗0.

5. Asignacion de variablesEl codigo es el siguiente:p0=x(0);

6. Calculo de la base de GroebnerEl codigo es el siguiente:groebner_basis([p0,p1,...pn],heuristic=False);

11

Page 22: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

donde:• p1, p2, ...pn son polinomios.• heuristic = False desactivamos la heurıstica.

7. Calculo de la forma normalUtilizamos la siguiente funcion de reduccion:nf=eliminate(gb)[1];

Ya que ‘eliminate’ devuelve una funcion, la llamada para reducir un polinomio dadoes la siguiente:print nf(x(0));

donde gb es un vector con la base de Groebner previamente calculada.2.2. Singular

Singular es un sistema de computacion algebraica para calculos con polinomios, conespecial enfasis en el algebra conmutativa y no-conmutativa, geometrıa algebraica y teorıade la singularidad. Es libre y de codigo abierto bajo licencia GNU.Singular proporciona:

Algoritmos altamente eficientes.Una gran cantidad de algoritmos avanzados en los ambitos mencionados implementados,bien en el nucleo (escrito en C/C++), o como bibliotecas.Principales objetos de computo: polinomios, ideales y modulos sobre una gran variedadde anillos.Un lenguaje intuitivo, similar a C.

12

Page 23: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Maneras faciles de hacer que el usuario pueda realizar ampliaciones a traves debibliotecas.Un completo manual en lınea y ayudas de funciones.

Su objeto principal de computo son ideales, modulos y matrices sobre un gran numero deanillos base. Estos incluyen:

Anillos de polinomios sobre varios cuerpos y algunos anillos (incluyendo los enteros).Localizaciones de las anteriores.Una clase general de algebras no conmutativas.Anillos cociente de lo anterior.

Los algoritmos basicos de Singular manejan:Groebner respecto bases estandar y resoluciones libres.Factorizacion polinomial.

2.2.1. Version utilizada

3.0.42.2.2. Definiciones

1. Declaracion del anillo:El codigo es el siguiente:

13

Page 24: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

ring r=p,(n(0..m)),lp;

donde:• p es el numero entero que define el cuerpo.• n es el identificador de un array de variables.• m el numero de variables que contiene el array n. Cada variable de esearray se especifica de la siguiente manera:

n(0), n(1), n(2), ..., n(m− 1)2. Definicion de polinomios

El codigo es el siguiente:poly p0=x(0)*x(1);

3. Definicion de idealesEl codigo es el siguiente:ideal J=p0,p1,...,pn;

donde p1, p2, ...pn son polinomios.4. Definicion de funciones:

El codigo es el siguiente:proc nombrefunction(t1 par1,t2 par2,,...,tn parn) {

return(<expresion>);

}

donde:• ‘proc’ y ‘return’ son palabras reservadas.

14

Page 25: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

• nombrefunction es el nombre de la funcion y por la cual sera llamada desdeel cualquier parte del codigo.• tn es el tipo de datos de la variable vn.• < expresion > es la expresion cuyo resultado de ejecucion devuelve la

funcion.5. Asignacion de variables:

El codigo es el siguiente:poly p0=1;

6. Calculo de la base de Groebner:El codigo es el siguiente:ideal BASE=slimgb([p0,p1,...,pn]);

donde p1, p2, ...pn son polinomios.7. Calculo de la forma normal:

El codigo es el siguiente:reduce(pi,[p0,p1,...pn]);

donde p0, p2, ...pn y pi son polinomios.2.3. CoCoA

CoCoA es un programa para realizar calculos con numeros y polinomios.Funciona en varios sistemas operativos.Realiza operaciones con enteros muy grandes y numeros racionales, polinomios, basesde Groebner, sistemas de ecuaciones lineales,...

15

Page 26: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

2.3.1. Version utilizada

4.7.52.3.2. Definiciones

1. Declaracion del anillo:El codigo es el siguiente:USE Z/(p)[n[0.m]];

donde:• p es el numero entero que define el cuerpo.• n es el identificador del array de variables.• m es el numero de variables del array n. Cada variable de ese array se

especifica de la siguiente manera:n(0), n(1), n(2), ..., n(m− 1)

2. Definicion de polinomios:El codigo es el siguiente:V0:=x[0];

3. Definicion de ideales:El codigo es el siguiente:MEMORY.J:=Ideal(p0,p1,...,pn);

donde p1, p2, ...pn son polinomios.4. Definicion de funciones:

El codigo es el siguiente:16

Page 27: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Define nombrefunction(par0,par1,...,parn)

RETURN (1+M);

EndDefine;

donde:• ‘Define’, ‘RETURN’ y ‘EndDefine’ son palabras reservadas.• nombrefunction es el nombre de la funcion y por la cual sera llamada desde

el cualquier parte del codigo.• par0, par1, ..., parn son los parametros pasados a la funcion.• < expresion > es la expresion cuyo resultado de ejecucion devuelve la

funcion.5. Asignacion de variables:

El codigo es el siguiente:IC0:=nombrefunction(x[1]);

6. Calculo de la base de Groebner:El codigo es el siguiente:GBasis([p0,p1,...,pn]);

donde p1, p2, ...pn son polinomios.7. Calculo de la forma normal:

El codigo es el siguiente:NF(pi,[p0,p1,...pn]);

donde p0, p1, ...pn y pi son polinomios.

17

Page 28: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

18

Page 29: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

3. Vision general de sistemas expertos implementados con algebra

computacional

3.1. Formalismo

Los sistemas expertos son programas informaticos compuestos por estos tres elementos:Entrada La entrada de un sistema experto se refiere a la informacion relacionada con el

entorno del sistema experto. Ya que el entorno puede cambiar, esta informacion tambienesta sujeta a cambios a medida que pasa el tiempo. La informacion relativa a la entradasera denotada por I .

Salida La salida de un sistema experto se refiere a la informacion relacionada con lainformacion deducida por el sistema experto que sera util para llevar a cabo accionesen el entorno. La informacion relativa a la salida sera denotada por O.

Base de conocimiento La base de conocimiento del sistema experto se refiere a la informacioncontenida en el sistema, la cual se utiliza junto con la entrada del sistema experto parainferir la salida del sistema. Este conocimiento se suele describir por medio de reglas. Esdecir, el conocimiento con la forma “si algunas condiciones relacionadas con la entradase cumplen, entonces es necesario tomar una accion determinada”. En esta parte, esposible encontrar conocimiento relativo a restricciones que debe cumplir la entrada delsistema experto. Este tipo de conocimiento normalmente es llamado “Restricciones deIntegridad”. La informacion relacionada con la base de conocimientos sera denotada porK.

Con el objetivo de disenar programas que permitan simular el conocimiento de los humanosexpertos, los investigadores en Inteligencia Artificial han logrado desarrollar diferentesmodelos de representacion capaces de especificar formalmente el conocimiento de los humanosexpertos. En este capıtulo, los sistemas expertos usan difentes variables X1, ..., Xm para

19

Page 30: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

especificar un determinado estado de la entrada y la salida del sistema experto. Dependiendode la representacion de cada modelo, las variables pueden tomar diferentes posibles valores.Vamos a considerar diferentes tipos de variables:Variables de entrada Las variables que describen la entrada de un sistema experto. Es decir,

las variables que describe el entorno del sistema experto.Variables de salida Las variables que describen posibles acciones o consecuencias que el

sistema experto deduce.Variables auxiliares Las variables que son necesarias para especificar la base de

conocimiento del sistema experto.Un estado S , es una posible situacion en la cual cada variable toma un valor. Dado un

estado S y una variable X , la expresion S(X ) denota el valor que la variable X toma en elestado S .

Como veremos en los siguientes apartados, la informacion relacionada con la entrada,la salida y la base de conocimiento es definida a traves de formulas. Para cada modelo derepresentacion, se define formalmente el concepto de formula.

El conjunto de formulas en un modelo de representacion particular se denota por C. Laentrada y la salida son por lo general restringidas a un tipo especıfico de formulas quelimitan el posible valor de las variables de entrada y salida. Mientras que la entrada y lasalida cambian dependiendo del entorno del sistema experto, las formulas que describen labase de conocimientos se mantienen invariables.

Las formulas se pueden cumplir o no en un estado en particular. Para cada modelo derepresentacion, definimos formalmente cuando una formula A tiene un estado S . Las formulasde salida son deducidas de las formulas de entrada y de la base de conocimiento.

El problema de determinar que formulas son deducidas es formalmente definido de lasiguiente manera:

20

Page 31: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Definicion Una formula B es deducida por un sistema experto si y solo si para cada estadoS en que las formulas A ∈ K∪I cumplen el estado S , la formula B tambien cumple el estadoS .

21

Page 32: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

3.2. Modelo algebraico

Gracias a distintos resultados matematicos, en este capıtulo veremos como algunossistemas expertos pueden ser implementados en sistemas de computacion algebraica. Estosresultados matematicos estan basados en traducir cada posible formula que describaconocimiento, C, en polinomios de Zq[x1, . . . , xm] a traves de una funcion φ : C → Zq[x1, . . . , xm].Como veremos en los siguientes apartados, diferentes anillos de polinomios y funciones φ sondefinidas dependiendo del modelo de representacion utilizado para especificar el conocimientodel sistema experto.

Un concepto importante relacionado con los anillos de polinomios es el concepto deideal. El ideal generado por un conjunto de polinomios e1, ..., en (denotado por la expresion〈e1, ..., en〉), es un subconjunto de polinomios de Zq[x1, . . . , xm] los cuales incluyen loselementos e1, ..., en y cumplen algunas propiedades. Dos ideales pueden ser sumados dandocomo resultado otro ideal nuevo. Como veremos en siguientes apartados, consideraremos lossiguientes ideales cuya definicion formal depende del modelo de representacion utilizado.Ideal J Ideal definido en el anillo de polinomios Zq[x1, ..., xm] (donde q depende de la

representacion del modelo) requerido para definir la funcion φ que transforma formulasen polinomios.

Ideal I Ideal vinculado a la entrada del sistema experto.I = 〈φ(A)|A ∈ I〉

Ideal K Ideal relacionado con la base de conocimiento del sistema experto.K = 〈φ(A)|A ∈ K〉

Los ideales desempenan un papel importante ya que el problema de determinar si un22

Page 33: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

sistema experto deduce una formula puede ser traducido al problema algebraico de determinarsi un polinomio pertenece a un ideal. De hecho, como veremos en los proximos apartados, unaformula A puede ser deducida por un sistema experto, si el polinomio φ(A), pertenece al idealI + J + K . Es decir:

A ∈ O ⇔ φ(A) ∈ I + J + K

Este problema algebraico se resuelve por medio del concepto de bases de Groebner y laforma normal [17]. Aunque estos conceptos son lo suficientemente complejos para ser definidoformalmente aquı, (ver [18] para una introduccion al algebra computacional) es suficiente saberque la forma normal de un polinomio, p, modulo un ideal I + J + K (el cual es denotado porNF(p, I + J + K )) es otro polinomio que en entre otras condiciones cumple lo siguiente:

p ∈ I + J + K ⇔ NF(p, I + J + K ) = 0

23

Page 34: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

3.3. Ejemplo

Consideremos un sistema experto el cual aconseja sobre si se debe salir de casa en funcionde unas condiciones atmosfericas.La entrada de este sistema experto muestra condiciones meteorologicas como pueden serrelativas al tiempo en general o relativo a la lluvia en particular.La salida del sistema experto muestra si se debe salir de casa o no. Una de las opcionesindica que uno debe esperar en casa a que el tiempo mejore.La base de conocimiento del sistema experto describe como la entrada y la salida estanrelacionadas. Este conocimiento, K puede ser representado por reglas como la siguiente:

‘Si el tiempo es muy bueno y no hay nada de lluvia, entonces se puede salir decasa’.

‘Si el tiempo es bueno, entonces se puede salir de casa’.‘Si el tiempo es muy malo, entonces no se puede salir de casa’.‘Si no llueve, entonces el tiempo es bueno’.

La siguiente definicion nos proporcionara el ejemplo estandar utilizado en las definicionesde los modelos.

Si consideramos que la entrada, I , es que no hay lluvia y que el tiempo es muy bueno,entonces el sistema experto deducira que se puede salir de casa.

24

Page 35: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

4. Modelo logico proposicional Booleano

4.1. Formalismo de representacion

Un sistema experto basado en logica proposicional Booleana utiliza variables BooleanasX1, ..., Xm para especificar un estado particular de la entrada y salida del sistema experto.Cada variable debe tomar el valor verdadero o falso.La base de conocimiento de este tipo de sistemas expertos es descrita por medio de formulasBooleanas proposicionales. Las formulas proposicionales son descritas por medio de lasconocidas conectivas Booleanas logicas ¬, ∧, ∨, → acordes con la siguiente definicion.Definicion: Una formula A es definida recursivamente de la siguiente manera:

Xi donde Xi es una variable.¬B donde B es una formula.B ∨ C donde B y C son formulas.B ∧ C donde B y C son formulas.B → C donde B y C son formulas.

Usaremos C para indicar el conjunto de formulas. Las reglas Booleanas proposicionalesson las formulas mas naturales de enunciar conocimiento. Las reglas son formulas con lasiguiente forma

(A1 ∧ ... ∧ Ar)→ (B1 ∨ ... ∨ Bs)donde cada formula A1, ..., Ar , B1, ..., Bs es una variable o la negacion de una variable.Un estado S , es una posible situacion en la cual cada variable toma o bien el valor verdaderoo el valor falso. Dado un estado S y una variable X , la expresion S(X ) denota el valor de lavariable X tomada en el instante S . De acuerdo con la siguiente definicion, determinaremos

25

Page 36: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

si una formula A se cumple o no en un estado particular SDefinicion: Sea A una formula. Sea S un estado.La formula A se cumple en el estado S si y solo si:Caso A es Xi donde Xi es una variable.

A se cumple en S ⇔ S(Xi) es verdaderaCaso A es ¬B donde B es una formula.

A se cumple en S ⇔ B no se cumple en SCaso A es B ∨ C donde B y C son formulas.

A se cumple en S ⇔ B se cumple en S o C se cumple en SCaso A es B ∧ C donde B y C son formulas.

A se cumple en S ⇔ B se cumple en S y C se cumple en SCaso A es B → C donde B y C son formulas.

A se cumple en S ⇔ la formula ¬B ∨ C se cumple en S .Por medio de las formulas describimos la entrada, salida y la base de conocimiento de un

sistema experto.Entrada Le entrada es un conjunto de formulas con la forma X o ¬X donde X es una variable

de entrada que describe el entorno del sistema experto. Mientras que la formula X en laentrada representa que la variable de entrada X toma el valor verdadero, la formula ¬Xrepresenta que la variable de salida X toma el valor falso. Naturalmente, el conjuntode formulas que forman la entrada de los sistemas expertos cambian dependiendo delentorno del sistema experto (el cual describe la entrada del sistema experto). El sımboloI denota el conjunto de formulas que representan la entrada del sistema experto.

Base de conocimiento La base de conocimiento es un conjunto finito de formulas. A diferenciade la entrada, esas formulas son estaticas y no cambian a lo largo del tiempo. El sımbolo

26

Page 37: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

K denota el conjunto de formulas que representan la base de conocimiento del sistemaexperto.

Salida La salida es un conjunto de formulas con la forma X o ¬X donde X es una variable desalida. Estas formulas son deducidas por el sistema experto por medio de las formulasrelacionadas con la entrada y la base de conocimiento. Como en la entrada, una formulaX representa que la variable de salida X toma el valor verdadero y la formula ¬Xrepresenta que la formula X toma el valor falso. Obviamente, este conjunto de formulascambian dependiendo de la entrada del sistema experto. El sımboloO denota el conjuntode formulas que describe la salida del sistema experto

En esta apartado desarrollaremos el ejemplo de sistema experto descrito en el apartado3.3 en logica proposicional.Las variables proposicionales que vamos a utilizar son:Variables de Entrada Para el tiempo, utilizaremos las siguientes seis variables

proposicionales:x1(0): el tiempo es muy buenox1(1): el tiempo es buenox1(2): el tiempo es normalx1(3): el tiempo es regularx1(4): el tiempo es malox1(5): el tiempo es muy malo

Para comprobar lo mucho o poco que llueve, utilizaremos las siguientes cuatro variablesproposicionales:x2(0): llueve mucho

27

Page 38: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

x2(1): llueve normalx2(2): llueve pocox2(3): no llueve nada

Variables de Salida Para salir de casa, utilizaremos las dos siguientes variablesproposicionalesx0(0): salimos de casa

Ahora pasaremos a describir el conjunto de formulas, K que constituyen la base deconocimiento: En primer lugar hablaremos de las restricciones de integridad:

1. En relacion con el tiempo: no puede ocurrir que el tiempo sea a la vez muy bueno,bueno, normal, regular, malo y muy malo. Con lo cual esta idea se describe con lassiguientes restricciones de integridad:

IC1 : ¬(x1(0) ∧ x1(1))IC2 : ¬(x1(0) ∧ x1(2))IC3 : ¬(x1(0) ∧ x1(3))IC4 : ¬(x1(0) ∧ x1(4))IC5 : ¬(x1(0) ∧ x1(5))IC6 : ¬(x1(1) ∧ x1(2))IC7 : ¬(x1(1) ∧ x1(3))IC8 : ¬(x1(1) ∧ x1(4))

28

Page 39: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

IC9 : ¬(x1(1) ∧ x1(5))IC10 : ¬(x1(2) ∧ x1(3))IC11 : ¬(x1(2) ∧ x1(4))IC12 : ¬(x1(2) ∧ x1(5))IC13 : ¬(x1(3) ∧ x1(4))IC14 : ¬(x1(3) ∧ x1(5))IC15 : ¬(x1(4) ∧ x1(5))

y tambien esta la restriccion que dice el tiempo tiene que ser o muy bueno, o bueno, onormal, o regular, o malo, o muy malo:

IC16 : (x1(0) ∨ x1(1) ∨ x1(2) ∨ x1(3) ∨ x1(4) ∨ x1(5))2. En relacion con la lluvia: no puede ocurrir que llueva mucho, normal, poco o no llueva

al mismo tiempo:IC17 : ¬(x2(0) ∧ x2(1))IC18 : ¬(x2(0) ∧ x2(2))IC19 : ¬(x2(0) ∧ x2(3))IC20 : ¬(x2(1) ∧ x2(2))IC21 : ¬(x2(1) ∧ x2(3))IC22 : ¬(x2(2) ∧ x2(3))

29

Page 40: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

y tambien esta la restriccion que dice que llueve mucho, llueve normal, poco o no lluevenada:

IC23 : (x2(0) ∨ x2(1) ∨ x2(2) ∨ x2(3))

Por otro lado, la reglas que relacionan la entrada con la salida son las mostradas acontinuacion:

1. Si el tiempo es muy bueno y no hay nada de lluvia, entonces se puede salir de casaR1 : (x1(0) ∧ x2(3))→ x0(0)

2. Si el tiempo es bueno, entonces se puede salir de casaR2 : x1(1)→ x0(0)

3. Si el tiempo es muy malo, entonces no se puede salir de casaR3 : x1(5)→ ¬x0(0)

4. Si no llueve, entonces el tiempo es buenoR4 : x2(3)→ x1(1)

30

Page 41: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

La base de conocimiento serıa el conjunto de formulasK = {IC1, ..., IC23, R1, ...R4}

Si se quisiera describir como entrada ‘que no hay lluvia’ entonces la entrada serıa:I = {x2(3)}

Con esta entrada se deducirıa la proposicion x0(0) , y esto es que se puede salir de casa.

31

Page 42: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

4.2. Modelo algebraico

El problema de determinar si una formula puede ser deducida desde otras puede sertratados algebraicamente. De hecho, representando cada formula en C como un polinomioBooleano, nosotros podemos transformar el problema de determinar si una formula se puedededucir de las otras en un problema algebraico. En primer lugar, vamos a considerar comolas formulas son traducidas a polinomios Booleanos. Necesitamos definir el ideal J enZ2[x1, ..., xm]:

J = 〈x21 + x1, ..., x2m + xm〉

Una vez definido el ideal J , las formulas son transformadas en un polinomio delZ2[x1, ..., xm] por medio de la siguiente funcion:Definicion Definimos recursivamente la funcion φ : C −→ Z2[x1, ..., xm] como sigue:Caso A es Xi donde Xi es una variable.

φ(A) = φ(Xi) = xi

Caso A es ¬B donde B es una formula.φ(A) = φ(¬B) = NF(1 + φ(B), J)

Caso A es B ∨ C donde B y C son formulas.φ(A) = φ(B ∨ C ) = NF(φ(B) · φ(C ), J)

Caso A es B ∨ C donde B y C son formulas.φ(A) = φ(B ∧ C ) = NF(φ(B) + φ(C ) + φ(B) · φ(C ), J)

32

Page 43: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Caso A es B → C donde B y C son formulas.φ(A) = φ(B → C ) = NF(1 + φ(B) + φ(B) · φ(C ), J)

donde NF especifica la forma nomal de un polinomio modulo un ideal.En relacion con los sistemas expertos basados en logica Booleana proposicional, nosotros

consideraremos los siguientes ideales:Ideal J Ideal necesario para definir la funcion φ .

J = 〈x21 + x1, ..., x2m + xm〉

Ideal I Ideal relacionado con la entrada del sistema experto.I = 〈φ(A)|A ∈ I〉

Ideal K Ideal relacionado con la base de conocimiento del sistema experto.K = 〈φ(A)|A ∈ K〉

Una vez transformadas las formulas en polinomios, nosotros podemos trataralgebraicamente la cuestion de determinar si una formula puede ser deducida por el sistemaexperto.

Teorema 4.1 Una formula B puede ser deducida por el sistema experto si y solo si:

φ(B) ∈ I + J + K

33

Page 44: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Este teorema puede ser reescrito de la siguiente manera:

Teorema 4.2 Sea Xi una variable de salida.

S(Xi) es verdadero ⇔ NF (xi, I + J + K ) = 0S(Xi) es falso ⇔ NF (xi, I + J + K ) = 1

Este ultimo teorema es especialmente util ya que podemos encontrar el valor de salidacalculando la forma normal de un polinomio modulo un ideal (el cual es calculado por unsistema de computacion algebraica).

34

Page 45: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

4.3. Implementacion en el sistema de computacion algebraica Singular

En este apartado usaremos un sistema computacion algebraica llamado Singular conel objetivo de mostrar como un sistema experto puede ser implementado. Primero de todo,necesitamos especificar el anillo en el que seran traducidas las formulas. En este ejemploconsiderado en el apartado 3.3, el anillo sera definido en Singular de la siguientes manera:ring r=2,(x0(0..1),x1(0..5),x2(0..3)),lp;

poly s1=x0(0)ˆ2+x0(0);

poly s2=x1(0)ˆ2+x1(0);

poly s3=x1(1)ˆ2+x1(1);

poly s4=x1(2)ˆ2+x1(2);

poly s5=x1(3)ˆ2+x1(3);

poly s6=x1(4)ˆ2+x1(4);

poly s7=x1(5)ˆ2+x1(5);

poly s8=x2(0)ˆ2+x2(0);

poly s9=x2(1)ˆ2+x2(1);

poly s10=x2(2)ˆ2+x2(2);

poly s11=x2(3)ˆ2+x2(3);

ideal J=s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11;

J = std(J);

Tambien definimos las conectivas logicas de acuerdo con la definicion de φ, en la sintaxisde Singular:

35

Page 46: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

proc NEG(poly M) {return(reduce(1+M,J));}

proc O(poly M,poly N) {return(reduce(M*N,J));}

proc IMP(poly M,poly N) {return(O(NEG(M),N));}

proc Y(poly M,poly N) {return(NEG(O(NEG(M),NEG(N))));}

Ahora nosotros definiremos la base de conocimiento del sistema experto, los comandosnecesarios son los siguientes:

poly ic1=O(O(O(O(O(x1(5),x1(4)),x1(3)),x1(2)),x1(1)),x1(0));

poly ic2=NEG(Y(x1(0),x1(1)));

poly ic3=NEG(Y(x1(0),x1(2)));

poly ic4=NEG(Y(x1(0),x1(3)));

poly ic5=NEG(Y(x1(0),x1(4)));

poly ic6=NEG(Y(x1(0),x1(5)));

poly ic7=NEG(Y(x1(1),x1(2)));

poly ic8=NEG(Y(x1(1),x1(3)));

poly ic9=NEG(Y(x1(1),x1(4)));

poly ic10=NEG(Y(x1(1),x1(5)));

poly ic11=NEG(Y(x1(2),x1(3)));

poly ic12=NEG(Y(x1(2),x1(4)));

poly ic13=NEG(Y(x1(2),x1(5)));

poly ic14=NEG(Y(x1(3),x1(4)));

poly ic15=NEG(Y(x1(3),x1(5)));

poly ic16=NEG(Y(x1(4),x1(5)));

36

Page 47: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

poly ic17=O(O(O(x2(3),x2(2)),x2(1)),x2(0));

poly ic18=NEG(Y(x2(0),x2(1)));

poly ic19=NEG(Y(x2(0),x2(2)));

poly ic20=NEG(Y(x2(0),x2(3)));

poly ic21=NEG(Y(x2(1),x2(2)));

poly ic22=NEG(Y(x2(1),x2(3)));

poly ic23=NEG(Y(x2(2),x2(3)));

poly r1=IMP(Y((x1(0)),x2(3)),(x0(0)));

poly r2=IMP((x1(1)),(x0(0)));

poly r3=IMP(x1(5),NEG(x0(0)));

poly r4=IMP(x2(3),(x1(1)));

ideal K=ic1,ic2,ic3,ic4,ic5,ic6,ic7,ic8,ic10,ic11,ic12,ic13,ic14,

ic15,ic16,ic17,ic18,ic19,ic20,ic21,ic22,ic23,r1,r2,r3,r4;

K = std(K);

Ahora consideraremos la entrada del sistema experto ‘no hay nada de lluvia’ (I = {X2(3)})En este caso es:poly i0=x2(3);

ideal I=i0;

I = std(I);

Ahora nosotros podremos encontrar cual es el valor para la variable de salida S inferidapor el sistema experto por medio de lo siguiente:

37

Page 48: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

ideal BASE=slimgb(I+J+K);

ideal BASESTD=std(BASE);

print(reduce(x0(0),BASESTD));

Singular responde lo siguiente:0

---------------------------------------------------------

38

Page 49: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

4.4. Implementacion en el sistema de computacion algebraica PolyBoRi

En este apartado usaremos un sistema de computacion algebraica llamado PolyBoRi conel objetivo de mostrar como un sistema experto se puede implementar. En concreto utilizaremosel sistema experto descrito en el apartado anterior. Primero de todo, necesitamos realizar los‘imports’ de algunas librerias:from polybori.PyPolyBoRi import *

from polybori.gbcore import *

from polybori import *

A cotinuacion se especifica el anillo en el que seran traducidas las formulas y las conectivaslogicas de acuerdo con la definicion de φ. En este ejemplo considerado en el apartado anterior,el anillo estara definido en Polybori de la siguientes manera:declare_ring([Block("x0",2),Block("x1",6),Block("x2",4)]

,globals());

def O(M,N): return Polynomial(M)*Polynomial(N)

def NEG(M): return 1+Polynomial(M)

def IMP(M,N): return O(NEG(M),N)

def Y(M,N): return NEG(O(NEG(M),NEG(N)))

ic1=O(O(O(O(O(x1(5),x1(4)),x1(3)),x1(2)),x1(1)),x1(0));

ic2=NEG(Y(x1(0),x1(1)));

ic3=NEG(Y(x1(0),x1(2)));

ic4=NEG(Y(x1(0),x1(3)));

39

Page 50: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

ic5=NEG(Y(x1(0),x1(4)));

ic6=NEG(Y(x1(0),x1(5)));

ic7=NEG(Y(x1(1),x1(2)));

ic8=NEG(Y(x1(1),x1(3)));

ic9=NEG(Y(x1(1),x1(4)));

ic10=NEG(Y(x1(1),x1(5)));

ic11=NEG(Y(x1(2),x1(3)));

ic12=NEG(Y(x1(2),x1(4)));

ic13=NEG(Y(x1(2),x1(5)));

ic14=NEG(Y(x1(3),x1(4)));

ic15=NEG(Y(x1(3),x1(5)));

ic16=NEG(Y(x1(4),x1(5)));

ic17=O(O(O(x2(3),x2(2)),x2(1)),x2(0));

ic18=NEG(Y(x2(0),x2(1)));

ic19=NEG(Y(x2(0),x2(2)));

ic20=NEG(Y(x2(0),x2(3)));

ic21=NEG(Y(x2(1),x2(2)));

ic22=NEG(Y(x2(1),x2(3)));

ic23=NEG(Y(x2(2),x2(3)));

Ahora nosotros definiremos la base de conocimiento del sistema experto, los comandosnecesarios son los siguientes:r1=IMP(Y((x1(0)),x2(3)),(x0(0)));

40

Page 51: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

r2=IMP((x1(1)),(x0(0)));

r3=IMP(x1(5),x0(1));

r4=IMP(x2(3),(x1(1)));

K=[r1,r2,r3,r4];

Ahora consideraremos la entrada del sistema experto ‘no hay nada de lluvia’ (I = {X2(3)})En este caso es:i0=x2(3);

I=[i0];

Ahora nosotros podremos encontrar cual es el valor para la variable de salida S inferidapor el sistema experto por medio de lo siguiente:gb=groebner_basis(I+K);

nf=eliminate(gb)[1];

print nf(x0(0));

Polybori responde lo siguiente:0

---------------------------------------------------------

41

Page 52: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

42

Page 53: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

5. Modelo ‘Concepto-Atributo-Valor’

5.1. Formalismo de representacion

Un sistema experto basado en el modelo ‘Concepto-Atributo-Valor’ usa diferentes variablesX1, ..., Xm con el objetivo de especificar un estado particular de la entrada y la salidadel sistema experto. Cada variable puede tomar un subconjunto de posibles valores V ={v1, ..., vn}.Las formulas en este modelo de representacion son definidas como sigue a continuacion:Definimos primero el concepto de formula atomica positiva:Definicion Una formula atomica positiva A es una formula de la forma:

Xi = v donde Xi es una variable y v es un posible valor de Xi.Xi = Xj donde Xi and Xj son variables.

Definicion Una formula A es definida recursivamente como sigue a continuacion:A es una formula atomica positiva.¬A donde A es una formula.A1 ∨ A2 donde A1, A2 son formulas.A1 ∧ A2 donde A1, A2 son formulas.A1 → A2 donde A1, A2 son formulas.

Usaremos C para denotar el conjunto de formulas. De todas las formulas destacaremosdos tipos de formulas que son las formulas atomicas y las reglas.Definicion Una formula A es atomica si es una formula atomica positiva o tiene la forma ¬Adonde A es una formula atomica positiva.

43

Page 54: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Definicion Una formula A es una regla si es de la forma:(A1 ∧ . . . ∧ Ar)→ (B1 ∨ . . . ∨ Bs)

donde A1, . . . , Ar , B1, . . . Bs son formulas atomicas.La importancia de estos dos tipos de formulas se debe a que cualquier conjunto de formulas

puede expresarse unicamente con formulas atomicas y reglas de acuerdo con la siguientepropiedad:Proposicion 5.1 Dado un conjunto de formulas K hay un conjunto de formulas K′ integrado

unicamente por reglas y formulas atomicas que es equivalente a K.

De acuerdo con la siguiente proposicion, una regla puede ser descrita como una disyuncionde formulas atomicas. Esta proposicion sera util en un siguiente apartado.Proposicion 5.2 La regla (A1 ∧ . . . ∧ Ar)→ (B1 ∨ . . . ∨ Bs) es equivalente a la formula:

¬A1 ∨ . . . ∨ ¬Ar ∨ B1 ∨ . . . ∨ Bs

De acuerdo con la siguiente definicion, podemos determinar si una formula A se cumpleen un particular estado S .Definicion Sea A una formula. Sea S un estado.La formula A se cumple en el estado S si y solo si:Caso A es Xi = v donde Xi es una variable y v un posible valor de Xi.

A se cumple en S ⇔ S(Xi) = v

Caso A es Xi = Xj donde Xi y Xj son variables.A se cumple en S ⇔ S(Xi) = S(Xj )

44

Page 55: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Caso A es ¬B donde B es una formula.A se cumple en S ⇔ B no se cumple en S

Caso A ≡ B ∨ C donde B y C son formulas.A se cumple en S ⇔ o bien B se cumple en S o C se cumple en S

Caso A ≡ B ∧ C donde B y C son formulas.A se cumple en S ⇔ B se cumple en S y C se cumple en S

Caso A ≡ B → C donde B y C son formulas.A se cumple en S ⇔ la formula ¬B ∨ C se cumple en S .

Por medio de las formulas describimos la entrada, la salida y la base de conocimiento deun sistema experto.Entrada La entrada es un conjunto de formulas atomicas positivas. El sımbolo I denota el

conjunto de formulas que representan la entrada de un sistema experto.Base de conocimiento La base de conocimiento es un conjunto finito de formulas. A diferencia

de la entrada, estas formulas permanecen constantes e invariables a lo largo del tiempo.Como de costumbre, el sımbolo K denota un conjunto de formulas que representan labase de conocimiento del sistema experto.

Salida La salida es un conjunto de formulas atomicas positivas. El sistema experto deduceestas formulas por medio de las formulas relacionadas con la entrada y la base deconocimiento. El sımbolo O denota el conjunto de formulas que representan la salidadel sistema experto.

En este apartado desarrollaremos el ejemplo de sistema experto descrito en el apartado3.3 de acuerdo con el modelo ‘Concepto-Atributo-Valor’.

Las variables que vamos a utilizar son:45

Page 56: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Variables de entrada Dispondremos de las siguientes variables de entrada:Variable relativa al tiempo, x(1).

Esta variable puede tomar los siguientes posiblesvalores {muybueno, bueno, normal, regular,malo,muymalo} indicando eltiempo que hace.

Variable relativa a lo mucho o poco que llueve, x(2).Esta variable puede tomar los siguientes posibles valores{mucha, bastante, poca, nada}.

Variables de salida Variable que informa si se sale de casa o no. Esta variable puede tomarlos siguientes posibles valores {Verdadero, Falso} indicando si se puede salir decasa o no.

Ahora pasaremos a describir el conjunto de formulas, K que constituyen la base deconocimiento. En primer lugar hay que destacar que en este modelo de representacion, no esnecesario definir restricciones de integridad ya que se tienen en cuenta implıcitamente.

Por otro lado, la reglas que relacionan la entrada con la salida son las mostradas acontinuacion:

1. Si el tiempo es muy bueno y no hay nada de lluvia, entonces se puede salir de casaR1 : (x(1) = muybueno) ∧ (x(2) = nada)→ (x(0) = verdadero)

2. Si el tiempo es bueno, entonces se puede salir de casaR2 : (x(1) = bueno)→ (x(0) = verdadero)

46

Page 57: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

3. Si el tiempo es muy malo, entonces no se puede salir de casaR3 : (x(1) = muymalo)→ (x(0) = falso)

4. Si no llueve, entonces el tiempo es buenoR4 : (x(2) = nada)→ (x(1) = bueno)

La base de conocimiento serıa el conjunto de formulasK = {R1, R2, R3, R4}

Si se quisiera describir como entrada ‘que no hay lluvia’ entonces la entrada serıa:I = {x(2) = nada}

Con esta entrada se deducirıa la formula (x(0) = verdadero) , es decir, se puede salir decasa.

47

Page 58: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

5.2. Modelo algebraico

El problema de determinar si una formula puede ser deducida de otras puede ser tratadoalgebraicamente de acuerdo con [16].

Sea p un numero primo entero mayor o igual que el numero de valores posibles quepuede tomar cualquier variable. Primero, necesitamos codificar todos los posibles valores delas variables. Es decir, tenemos que definir una funcion, φ, entre los posibles valores de lasvariables y {0, ...p− 1}:

φ : V −→ {0, ..., p− 1}La funcion φ debe cumplir la siguiente propiedad: dado dos valores posibles de una misma

variable, v, w ∈ V , φ(v ) 6= φ(w).Dada una variable Xi, que podra tomar un posible valor {v1, ..., vn}, definimos el polinomio

si asociado a la misma:si = (x − φ(v1)) · (x − φ(v2)) · ... · (x − φ(vn))

El ideal J sobre Zp[x1, ..., xm] es definido de la siguiente manera:J = 〈s1, ..., sm〉

Una vez definido el ideal J , las formulas son transformadas en un polinonimo deZp[x1, ..., xm] por medio de la siguiente funcion:Definicion Definimos recursivamente la funcion φ : C −→ Zp[x1, ..., xm] de la siguientemanera:Caso A es Xi = v donde Xi es una variable y v un posible valor de Xi.

φ(A) = φ(Xi = v ) = xi − v

48

Page 59: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Caso A es Xi = Xj donde Xi y Xj son variables.φ(A) = φ(Xi = Xj ) = xi − xj

Caso A es ¬B donde B es una formula.φ(A) = φ(¬B) = NF(φ(B)p−1 − 1, J)

Caso A es B ∨ C donde B y C son formulas.φ(A) = φ(B ∨ C ) = NF(φ(B) · φ(C ), J)

Caso A es B ∧ C donde B y C son formulas.φ(A) = φ(B ∧ C ) = φ(¬(¬B ∨ ¬C )))

Caso A es B → C donde B y C son formulas.φ(A) = φ(B → C ) = φ(¬B ∨ C )

donde NF denota la forma normal de un polinomio modulo un ideal.La traduccion de una regla, (A1 ∧ ... ∧ Ar)→ (B1 ∨ ... ∨ Bs), es especialmente simple:φ((A1 ∧ ... ∧ Ar)→ (B1 ∨ ... ∨ Bs)) = NF(φ(¬A1) · ... · φ(¬Ar) · φ(B1)... · φ(Bs), J)

Una vez traducidas las formulas en polinomios, podemos tratar algebraicamente la cuestion49

Page 60: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

de determinar si una formula puede ser deducida por el sistema experto. Consideremos lossiguientes ideales:Ideal J Ideal para definir la funcion Zp[x1, . . . , xm] que tranforma formulas en polinomios.Ideal I Ideal relacionado con la entrada del sistema experto.

I = 〈φ(Ai)|Ai ∈ I〉Ideal K Ideal relacionados con la base de conocimiento del sistema experto.

K = 〈φ(Ai)|Ai ∈ K〉Una vez traducidas las formulas en polinomios, podemos tratar algebraicamente la cuestion

de determinar si el sistema experto deduce una formula.Teorema 5.3 Una formula B puede ser deducida por el sistema experto, si y solo si:

φ(B) ∈ I + J + K

Este teorema puede ser tambien reescrito de la siguiente manera:Teorema 5.4 Sea Xi una variable de salida y v un posible valor de Xi.

S(Xi) = v ⇔ NF (xi, I + J + K ) = φ(v )Este ultimo teorema es especialmente util ya que podemos encontrar el valor de salida

calculando la forma normal de un polinomio modulo un ideal (el cual es calculado por unsistema de computacion algebraica).

50

Page 61: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

5.3. Implementacion en el sistema de computacion algebraica Singular

En este apartado usaremos Singular con el objetivo de mostrar como un sistema expertopuede ser implementado en un sistema de computacion algebraica. Utilizaremos el ejemplodel apartado anterior. En este ejemplo p = 7. Ahora podemos especificar el anillo sobre elque las formulas seran traducidas.ring r=7,(s,x(1..2)),lp;

poly S1=(s-1)*s;

poly S2=(x(1)-5)*(x(1)-4)*(x(1)-3)*(x(1)-2)*(x(1)-1)*x(1);

poly S3=(x(2)-3)*(x(2)-2)*(x(2)-1)*x(2);

ideal J =S1,S2,S3;

J = std(J);

Tambien definiremos la conectivas de acuerdo con la definicion de φ. En sintaxis deSingular:proc NEG(poly M) {return(reduce(Mˆ6-1,J));}

proc O(poly M,poly N) {return(reduce(M*N,J));}

proc IMP(poly M,poly N) {return(O(NEG(M),N));}

proc Y(poly M,poly N) {return(NEG(O(NEG(M),NEG(N))));}

A continuacion, definiremos la base de conocimiento del sistema experto. Aquı mostraremoslos comandos necesarios para definir la base de conocimiento del sistema experto.poly R1=IMP(Y(x(1),x(2)-3),s);

poly R2=IMP(x(1)-1,s);

poly R3=IMP(x(1)-5,s-1);

51

Page 62: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

poly R4=IMP(x(2)-3,x(1)-1);

ideal K=R1,R2,R3,R4;

K = std(K);

Vamos a considerara como entrada que no hay lluvia (I = {lluvia = nada})). En sintaxisde Singular:ideal I=x(2)-3;

I = std(I);

Ahora podemos obtener cual es el valor de la salida para la variable S inferida por elsistema experto por medio de:ideal BASE=slimgb(J+I+K);

print(reduce(s,BASE));

Singular responde lo siguiente:0

---------------------------------------------------------

52

Page 63: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

6. Lenguaje de implementacion algebraico de sistemas expertos

En apartados previos de este trabajo hemos estudiado dos enfoques algebraicos paradesarrollar sistemas expertos. Tal como veremos en el siguiente aparatado, la eleccion delenfoque mas adecuado depende de la especificacion del sistema experto. En este apartado,desarrollaremos un lenguaje para crear sistemas expertos, los cuales pueden ser traducidosal modelo logico proposicional o al modelo ‘Concepto-Atributo-Valor’

Dado que el modelo ‘Concepto-Atributo-Valor’ proporciona una manera mas natural deimplementar sistemas expertos, nuestro lenguaje esta basado en este modelo. El siguientecodigo es un ejemplo de sistema experto descrito en nuestro lenguaje6.1. Descripcion del lenguaje

En este apartado describiremos la sintaxis de nuestro lenguaje. Lo siguiente muestra unejemplo de sistema experto especificado en el.BEGIN

BEGIN_VARS

A: v1, v2, v3;

B[2]: v1, v2;

C[3]: true, false;

D[2],E: w1, w2, w3;

END_VARS

BEGIN_KB

(C[1]<>C[3]);

(A=v1) AND (B[1]<>v2) AND (B[1]=B[2])-> (C[1]=C[2])

53

Page 64: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

OR (C[3]=true);

(A<>v2) AND (B[1]=B[2]) -> (C[1]=true);

END_KB

END.

Como puede verse, un sistema experto descrito en nuestro lenguaje contiene los dossiguientes apartados:Definicion de variables: Se refiere a la parte del codigo que esta entre las partes

‘BEGIN_VARS’ y ‘END_VARS’. Se define aquı cada variable, junto con los valores queesta variable puede tomar. El lenguaje proporciona tambien la posibilidad de definirarrays de variables. En el ejemplo, se han definido las siguientes variables:

La variable, A.Puede tomar un valor de el conjunto de valores posibles {v1, v2, v3}Un array de dos variables: B[1] y B[2].Cada una puede tomar un valor delconjunto de valores posibles {v1, v2}. Se definen como un array de dos variables.Un array de tres variables: C[1], C[2] y C[3].Cada una puede tomar un valordel conjunto {true, false}. Se definen como un array de tres variables.Un array de dos variables, D[1]y D[2], y una variable E.Cada una puedetomar un valor en el conjunto {w1,w2,w3}.

Definicion de la base de conocimiento . Se refiere a la parte de codigo entre las palabras‘BEGIN_KB’ y ‘END_KB’. Definiremos aquı la base de conocimiento del sistema expertopor medio de reglas y formulas atomicas.1Formulas atomicas . En nuestro lenguaje las formulas atomicas (ver Definicion 5.1) son

descritas por medio de la expresion es con la forma var1 = value, ‘var1 = var2’,‘var1 <> value’, ‘var1 <> var2’, donde ‘var1’ y ‘var2’ son variables, ‘value’

1De acuerdo con la Proposicion 5.1, se puede describir la base de conocimiento de un sistema expertorepresentado por el modelo ‘Concepto-Atributo-Valor’ a traves de reglas y formulas atomicas54

Page 65: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

es un posible valor de la variable y el sımbolo ‘<>’ denota el sımbolo ‘ 6=’ el cualpuede ser usado en las formulas atomica. En el ejemplo anterior, nosotros hemosconsiderado las formulas atomicas:

C[1] 6= C[3]

Solo consideramos formulas atomicas del tipo ‘var1 = var2’ o ‘var1 6= var2’ silas variables ‘var1’ y ‘var2’ estan declaradas en la misma lınea. De esta manera,no consideramos como correcta la formulas atomicas ‘A= B[1]’ ya que estandeclaradas en distintas lıneas. De la misma manera, considerarıamos como correctala formula atomica ’D[1]=E’.

Reglas . En nuestro lenguaje, una regla A1 ∧ ... ∧ Ar → B1 ∨ ... ∨ Bs (dondeA1, ..., Ar , B1, ..., Bs son formulas atomicas) se declara a traves del siguientecodigo:

A1 AND A2 AND . . .AND Ar -> B1 OR B2 OR . . .OR Bsdonde A1,...Ar , B1,..., Bs son formulas atomicas especificadas a traves de nuestrolenguaje.En el ejemplo anterior, hemos considerado las siguientes reglas:(A = v1)∧(B[1] 6= v2)∧(B[1] = B[2])→ (C[1] = C[2])∨(C[3] = true)

(A 6= v2) ∧ (B[1] = B[2])→ (C[1] = true)

55

Page 66: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

6.2. Traduccion al modelo logico proposicional Booleano

6.2.1. Traduccion en CAS

Un sistema experto especificado en nuestro lenguaje (llamado sistema experto original eneste apartado) puede ser traducido a un sistema experto equivalente especificado en logicaproposicional Booleana (llamado sistema experto traducido en este apartado) la cual estadescrita en el apartado 4.

A continuacion, se tendran en cuenta:Variables El sistema experto traducido contiene solo variables Booleanas. Cada variable

Booleana en el sistema experto traducido indica si una variable especificada en elsistema experto original toma un valor determinado.En el ejemplo anterior, definimos las variables Booleanas X1, X2 en el sistema expertotraducido indicando si la variable A en el sistema experto original toma respectivamenteel valor v1 o el valor v2.Definir otro valor Booleano para el caso de que la variableA tome el valor v3 no es necesario debido a que la informacion puede ser deducida delas variables X1 y X2. De hecho, cuando una formula Booleana (¬X1 ∧¬X2) se cumple,la variable A ni toma el valor v1 ni el valor v2, y consecuentemente A toma el valorv3.En resumen, tenemos que:X1: La variable A toma el valor v1.X2: La variable A toma el valor v2.(¬X1 ∧ ¬X2): La variable A toma el valor v3.Del mismo modo, definiremos diferentes variables Booleanas para el resto de variablesy el resto de valores del sistema experto original.Con el fin de definir estas variables Booleanas, consideraremos las siguientes funciones:

n(Z ) donde Z es una variable. Indica el numero de valores que puede tomar esavariable.

56

Page 67: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

ψ(Z, v ), donde Z y v son respectivamente una variable y un valor definidos enel sistema experto original. Es un numero natural entre 0 y m − 1 donde m esel numero de variables Booleanas definidas en el sistema experto traducido. Unvalor a = ψ(Z, v ) ≥ 0 informa de que la variable Booleana Xa indica si la variableZ toma el valor v . Un valor a = ψ(Z, v ) = −1 informa de que no hay variablesBooleanas que indiquen si la variable Z toma el valor v . La funcion ψ se definede tal forma que para cualquier array de variables, W , se cumple lo siguiente:

ψ(W [i], v ) = ψ(W [1], v ) + (i− 1) · (n(W [1])− 1).En el ejemplo anterior, necesitamos definir en el sistema experto traducido lasvariables Booleanas {X1, ..., X7} . La funcion ψ se define como sigue:ψ(A,v1) = 0; ψ(A,v2) = 1; ψ(A,v3) = −1;ψ(B[1],v1) = 2; ψ(B[1],v2) = −1;ψ(B[2],v1) = 3; ψ(B[2],v2) = −1;ψ(C[1],true) = 4; ψ(C[1],false) = −1;ψ(C[2],true) = 5; ψ(C[2],false) = −1;ψ(C[3],true) = 6; ψ(C[3],false) = −1;ψ(D[1],w1) = 7; ψ(D[1],w2) = 8; ψ(D[1],w3) = −1;ψ(D[2],w1) = 9; ψ(D[2],w2) = 10; ψ(D[2],w3) = −1;ψ(E,w1) = 11 ψ(E,w2) = 12; ψ(E,w3) = −1;τ1(Z, v ) y τ2(Z, v ), donde Z y v son respectivamente una variable y un valordefinidos en el sistema experto original. Son numeros naturales entre 0 y elnumero de variables Booleanas menos 1. Cuando a = ψ(Z, v ) = −1, los valoresb1 = τ1(Z, v ) y b2 = τ2(Z, v ) indican que se puede detectar si la variable Z toma

57

Page 68: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

el valor v a traves de la formula Booleana:¬Xb1 ∧ ¬Xb1+1 ∧ ... ∧ ¬Xb2−1 ∧ ¬Xb2

En el ejemplo anterior, tenemos que se definen las funciones τ1 and τ2 de lasiguiente manera:τ1(A,v3) = 0; τ2(A,v3) = 1;τ1(B[1],v2) = 2; τ2(B[1],v2) = 2;τ1(B[2],v2) = 3; τ2(B[2],v2) = 3;τ1(C[1],false) = 4; τ2(C[1],false) = 4;τ1(C[2],false) = 5; τ2(C[2],false) = 5;τ1(C[3],false) = 6; τ2(C[3],false) = 6;τ1(D[1],false) = 7; τ2(D[1],false) = 8;τ1(D[2],false) = 9; τ2(D[2],false) = 10;τ1(E,false) = 11; τ2(E,false) = 12;

Formulas Se debe especificar cada formula en el sistema experto original a traves de lasvariables Booleanas que acabamos de definir. Nosotros definimos una funcion Φ que loefectua.Caso A es la formula atomica positiva Z = v .

Dado a = ψ(Z, v ). Dado b1 = τ1(Z, v ) y b2 = τ2(Z, v )Φ(A) =

Xa si a > 0¬Xb1 ∧ ¬Xb1+1... ∧ ¬Xb2−1 ∧ ¬Xb2 si a = 0

Caso A es la formula atomica positiva Z = W .Sea {v1, ..., vn} el conjunto de valores que pueden ser tomados por la variable Z

58

Page 69: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

y la variable W . Tenemos que:Φ(A) = (Φ(Z = v1) ∧ Φ(W = v1)) ∨ . . . ∨ (Φ(Z = vn) ∧ Φ(W = vn))

Caso A es ¬B donde B es una formula.Φ(A) = Φ(¬B) = ¬Φ(B)

Caso A es B ∧ C donde B y C son formulas.Φ(A) = Φ(B ∧ C ) = Φ(B) ∧ Φ(C )

Caso A es B ∨ C donde B y C son formulas.Φ(A) = Φ(B ∨ C ) = Φ(B) ∨ Φ(C )

Caso A es B → C donde B y C son formulas.Φ(A) = Φ(B → C ) = Φ(B)→ Φ(C )

Restricciones de integridad A parte de la traduccion de las formulas especificadas en elsistema experto original en terminos Booleanos, necesitamos definir restricciones deintegridad para que el modelo proposicional Booleano pueda ser equivalente al descritoen nuestro lenguaje. En el ejemplo anterior, necesitamos especificar que la variable Ano puede tomar los valores v1 y v2 al mismo tiempo. Es decir, necesitamos especificarla restriccion de integridad ¬(X1 ∧ X2).De esta manera, por cada variable Z y cada dos variables v y w (especificados en

59

Page 70: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

el sistema experto original) de tal manera que a = ψ(Z, v ) ≥ 0 y b = ψ(Z,w) ≥ 0necesitamos definir la siguiente restriccion de integridad, la cual especifica que lavariable Z no puede tomar los valores a y b al mismo tiempo:

¬(Xa ∧ Xb)Para el ejemplo anterior, solo tenemos la siguiente restriccion de integridad:

IC1 ≡ ¬(X1 ∧ X2)A continuacion, especificaremos los pasos necesarios para implementar un sistema experto

basado en logica proposicional Booleana en un sistema de computacion algebraica.i) Definir las funciones ψ , n, τ1 y τ2. Sea m el numero de variables Booleanas necesarias.ii) Definir el anillo de polinomios Z2[x1, . . . , xm] y el ideal J (mirar apartado 5.2). Para el

ejemplo anterior en sintaxis de Singular:ring r=2,(x(0..12)),lp;

poly s0=(x(0)ˆ2) + x(0);

poly s1=(x(1)ˆ2) + x(1);

poly s2=(x(2)ˆ2) + x(2);

poly s3=(x(3)ˆ2) + x(3);

poly s4=(x(4)ˆ2) + x(4);

poly s5=(x(5)ˆ2) + x(5);

poly s6=(x(6)ˆ2) + x(6);

60

Page 71: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

poly s7=(x(7)ˆ2) + x(7);

poly s8=(x(8)ˆ2) + x(8);

poly s9=(x(9)ˆ2) + x(9);

poly s10=(x(10)ˆ2) + x(10);

poly s11=(x(11)ˆ2) + x(11);

poly s11=(x(12)ˆ2) + x(12);

ideal J=s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12;

J = std(J);

iii) Definir las conectivas ¬ y ∧, ∨, → de acuerdo a la definicion 4.2. No es necesariodefinir las conectivas → y ∧. En sintaxis de CoCoA:proc NEG(poly M) {return(reduce(1+M,J));}

proc O(poly M,poly N) {return(reduce(M*N,J));}

proc Y(poly M,poly N) {return(reduce(M*N+M+N,J));}

proc IMP(poly M,poly N) {return(reduce(M*N+N,J));}

iv) Definir las restricciones de integridad necesarias para que el modelo proposicionalBooleano pueda ser equivalente al especificado en nuestro lenguaje. Para el ejemploanterior en sintaxis de CoCoA:poly ic1=NEG(Y(x(0),x(1)));

v) Traducir las formulas especificadas en nuestro lenguaje en formulas Booleanas tal comohemos visto anteriormente. En el ejemplo anterior, en sintaxis de CoCoA:

61

Page 72: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

poly r1:= NEG(O(Y(x(4),x(6)),Y(NEG(x(4)),NEG(x(6)))));

poly r2:= IMP(Y(Y(x(0),NEG(NEG(x(2)))),

O(Y(x(2),x(3)),Y(NEG(x(2)),NEG(x(3))))),

O(O(Y(x(4),x(5)),Y(NEG(x(4)),NEG(x(5)))),x(6)));

poly r3:= IMP(Y(NEG(x(1)),O(Y(x(2),x(3)),Y(NEG(x(2))

,NEG(x(3))))),x(4));

vi) Definir el ideal K como los polinomios asociados a las formulas traducido en terminosBooleanos junto con los polinomios asociados a las restricciones de integridad. Para elejemplo anterior en sintaxis de Singular:ideal K=slimgb(IC1,R1,R2,R3);

6.2.2. Tabla de sımbolos

Cuando el traductor genera un codigo para un sistema experto dado en el modelo logicoproposicional Booleano, genera un fichero que forma la tabla de sımbolos de variables yvalores. A partir de este fichero podemos traducir una variable y un valor desde el sistemaexperto original al sistema experto traducido.

A continuacion se muestra el fichero correspondiente a la tabla anterior. Las columnas sedistinguen por tabuladores y las filas por saltos de lınea.

62

Page 73: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Cuadro 1: Tabla de variables. Fichero: ts mlp 1.txtVariable Valor ψ(Variable, Valor) n τ1 τ2A v1 0 3 - -A v2 1 - - -A v3 -1 - 0 1B v1 2 2 - -B v2 -1 - 2 2C true 4 2 - -C false -1 - 4 4D w1 7 3 - -D w2 8 - - -D w3 -1 - 7 8E w1 11 3 - -E w2 12 - - -E w3 -1 - 11 12A v1 0 3

A v2 1

A v3 -1 0 1

B v1 2 2

B v2 -1 2 2

C true 4 2

C false -1 4 4

D w1 7 3

D w2 8

D w3 -1 7 8

E w1 11 3

E w2 12

E w3 -1 11 12

63

Page 74: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

6.3. Traduccion del modelo ‘Concepto-Atributo-Valor’

6.3.1. Traduccion en CAS

En este apartado, trataremos de generar las sentencias para implementar el sistemaexperto descrito en nuestro lenguaje en un sistema de computacion algebraica de acuerdocon el enfoque algebraico del apartado 5.

Dado que nuestro lenguaje se basa en el formalismo de representacion ‘Concepto-Atributo-Valor’, estas sentencias son faciles de generar. Conllevan los siguientes pasos:

i) Calcular un primo p mayor o igual que el numero de posibles valores que puede tomarcualquier variable. En el ejemplo previo, el numero de posibles valores de cualquiervariable es como maximo tres. En consecuencia, tenemos que p = 3.

ii) Codificaremos cada variable y cada valor en nuestro lenguaje con un numero natural.En consecuencia, definiremos las siguientes funciones:• ψ(X ) indica el codigo de la variable X . Dado un array de variables, X , se cumple

lo siguiente:ψ(X [i]) = ψ(X [1]) + i− 1

• φ(X, v ) indica el codigo del valor v que puede tomar la variable X . El codigo decualquier valor es un numero natural entre 0 y p− 1. Dados dos valores v, w deuna misma variable X , tenemos que

φ(X, v ) 6= φ(X,w)En el ejemplo anterior, codificamos las variables de la siguiente manera:

64

Page 75: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

ψ(A) = 1; ψ(B[1]) = 2; ψ(B[2]) = 3;ψ(C[1]) = 4; ψ(C[2]) = 5; ψ(C[3]) = 6;ψ(D[1]) = 7; ψ(D[2]) = 8;ψ(E) = 9;y codificamos los valores de la siguiente manera:φ(A,v1) = 0; φ(A,v2) = 1; φ(A,v3) = 2;φ(B,v1) = 0; φ(B,v2) = 1;φ(C,true) = 0; φ(C,false) = 1;φ(D,w1) = 0; φ(D,w2) = 1; φ(D,w3) = 2;φ(E,w1) = 0; φ(E,w2) = 1; φ(E,w3) = 2;

iii) Definir el anillo de polinomios Zp[x1, . . . , xm] (ver apartado 5.2) donde m es el numerode variables especificadas en nuestro lenguaje. Para el ejemplo anterior en sintaxis deCoCoA:ring r=3,(x(0..9)),lp;

iv) Definir cada polinomio si asociado a cada variable y al ideal J (ver apartado 5.2). Parael ejemplo anterior en sintaxis de Singular:poly s0=(x(0)-2) * (x(0)-1) * (x(0)-0);

poly s1=(x(1)-1) * (x(1)-0);

poly s2=(x(2)-1) * (x(2)-0);

poly s3=(x(3)-1) * (x(3)-0);

poly s4=(x(4)-1) * (x(4)-0);

poly s5=(x(5)-1) * (x(5)-0);

poly s6=(x(6)-2) * (x(6)-1) * (x(6)-0);

65

Page 76: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

poly s7=(x(7)-2) * (x(7)-1) * (x(7)-0);

poly s8=(x(8)-2) * (x(8)-1) * (x(8)-0);

poly s9=(x(9)-2) * (x(9)-1) * (x(9)-0);

ideal J=s0,s1,s2,s3,s4,s5,s6,s7,s8,s9;

J = std(J);

v) Necesitamos definir las conectivas ¬ y ∨ de acuerdo con la Definicion 5.2. Las conectivas→ o ∧no son necesarias de ser definidas. En sintaxys de Singular:proc NEG(poly M) {return(reduce(Mˆ2-1,J));}

proc O(poly M,poly N) {return(reduce(M*N,J));}

vi) Necesitamos definir el ideal asociado a la base de conocimiento del sistema experto.De acuerdo con la Proposicion 5.2, las reglas son definidas usando solo las conectivas¬ y ∨.Para el ejemplo anterior en sintaxis de Singular:poly r0=NEG(x(2)-x(4));

poly r1=O(NEG((x(0)-0)),O(O((x(1)-1),NEG(x(1)-x(2)))

,O(x(3)-x(4),(x(5)-0))));

poly r2=O((x(0)-1),O(NEG(x(1)-x(2)),(x(3)-0)));

ideal K=r0,r1,r2;

ideal BASE=slimgb(J+K);

66

Page 77: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

6.3.2. Tabla de sımbolos

Cuando el traductor genera un codigo para un sistema experto dado en el modelo‘Concepto-Atributo-Valor’, genera dos ficheros que forman la tabla de sımbolos de variablesy valores. A partir de estos dos ficheros podemos traducir una variable y un valor desde elsistema experto original al sistema experto traducido.

En la tabla de variables definimos el codigo que tiene asociado cada variable.Cuadro 2: Tabla de variables. Fichero: ts mcav 1.txtNombre variable Codigo Variablea 0b 1c 3

En la tabla de valores definimos el codigo que tiene asociado cada valor de cada variable.Cuadro 3: Tabla de valores. Fichero: ts mcav 2.txtNombre valor Codigo Variable Indicev1 0 0v2 0 1v3 0 2v1 1 0v2 1 1true 3 0false 3 1

67

Page 78: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

68

Page 79: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

7. Ejemplo de Sistema Experto

En este apartado desarrollaremos un ejemplo de sistema experto real. Se trata de unsistema experto que recomienda el mejor tratamiento que un paciente con cancer de colonen funcion de los resultados de unas pruebas clınicas al que el paciente previamente se hasometido. Este sistema experto se va a desarrollar en el lenguaje descrito en este trabajo finalde carrera. A continuacion, detallamos el sistema experto.7.1. Especificacion

Algunos terminos utilizados en el lenguaje del sistema experto.Tratamiento : Tratamiento que se debe aplicar al paciente. Consideramos los siguientestratamientos definidos en [19].• Quimioterapia: Quimioterapia para la enfermedad avanzada o metastasica (ver pag.

25 en [19])• Tratamiento 3: Tratamiento y terapia adyuvante definida en (ver pag. 9 en [19])• Tratamiento 6: Tratamiento y terapia adyuvante definida en (ver pag. 12 en [19])• Tratamiento 7: Tratamiento y terapia adyuvante definida en (ver pag. 13 en [19]) ]• Tratamiento 8: Tratamiento primario definido en (ver pag. 14 en [19])

Cirugia : En caso de que haya que someter con cirugıa al paciente, consideraremos lossiguientes tipos:• Supervision: No se practica cirugıa al paciente, se recomienda la observacion y

seguimiento del mismo• Bloc removal.• One stage bloc removal.• Reserction with diversion.

69

Page 80: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

• Stent.• Diversion.

Pruebas. En ocasiones es recomendable que el paciente se someta a nuevas pruebas.Consideraremos las siguientes:• Prueba 7.1 (ver pag. 7 en [19]). Consiste en efectuar lo siguiente:◦ Pathology review◦ Colonoscopy◦ Marking of cancerous polyp site

El resultado de efectuar la prueba 7.1 es una de las siguientes posibilidades:◦ single specimen◦ fragmented specimen◦ negativo◦ desconocido

• Prueba 7.2 (ver pag. 7 en [19]). Consiste en efectuar lo siguiente:◦ Pathology review◦ Colonoscopy◦ Marking of cancerous polyp site

El resultado de efectuar la prueba 7.2 es una de las siguientes posibilidades:◦ single specimen◦ fragmented specimen◦ negativo◦ desconocido

• Prueba 8 (ver pag. 8 en [19]). Consiste en efectuar lo siguiente:◦ Pathology review

70

Page 81: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

◦ Colonoscopy◦ CBC,platelets, chemistry profile◦ CEA◦ Chest/abdominal/pelvit CT◦ PET-CT scan is not routinely indicated.

El resultado de efectuar la prueba 8 es una de las siguientes posibilidades:◦ resecable obstruyendo◦ resecable no obstruyendo◦ localmente no resecable◦ medicamente inoperable◦ desconocido

• Prueba 15.1 (ver pag. 15 en [19]). Consiste en efectuar lo siguiente:◦ Physical exam◦ Colonoscopy◦ Chest/abdominal/pelvit CT◦ consider PET-CT scan

El resultado de efectuar la prueba 15.1 es una de las siguientes posibilidades:◦ si◦ no◦ desconocido

• Prueba 15.2 (ver pag. 15 en [19]). Consiste en efectuar lo siguiente:◦ Consider PET-CT scan◦ Rreevaluate chest/abdominal/pelvit CT in 3 mo

El resultado de efectuar la prueba 15.2 es una de las siguientes posibilidades:◦ si

71

Page 82: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

◦ no◦ desconocido

• Prueba 11 (ver pag. 11 en [19]). Consiste en efectuar lo siguiente:◦ Colonoscopy◦ Chest/addominal/pelvic CT◦ CBC,plateltes,chemistry profile◦ CEA◦ Determination of tumor KRAS genestatus◦ Needle biopsy, if clinically indicated◦ PET-CT scan only if potentially surgically curable M1 disease◦ Multidisciplinary team evaluation, including a surgeon experienced in the

resection of hepatobiliary and lung metastases.El resultado de efectuar la prueba 11 es una de las siguientes posibilidades:◦ sincronos liver resecable◦ sincronos liver no resecable◦ sincronos abdominal◦ desconocido

• Prueba 13 (ver pag. 15 en [19]). Consiste en efectuar lo siguiente:◦ Reevaluate for conversion to resectable every 2 mo if conversion to

resectability is a reasonable goalEl resultado de efectuar la prueba 13 es una de las siguientes posibilidades:◦ convertida a resecable◦ restos no resecables◦ desconocido

72

Page 83: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Presentacion Clinica (ver pag. 3 en [19]). El paciente puede presentar los siguientessıntomas:• Colon cancer: Colon cancer appropiate for resection.• Suspect: Suspected or proven metastatic adenocarcicoma from the large bowel.• Pendunculated poly: Pendunculated polyp (adenoma [tubular, tubulovillous or

villous]) with invasive cancer.• Sessile polyp: Sessile polyp (adenoma [tubular, tubulovillous or villous]) with

invasive cancer.7.2. Especificacion en lenguaje fuente

1 BEGIN23 BEGIN VARS45 (∗OUTPUTS∗ )6 t r a t am i e n t o : q u im i o t e r a p i a , t r a t am i e n t o 3 , t r a t am i e n t o 6 ,

t r a t am i e n t o 7 , t r a t am i e n t o 8 ;7 c i r u g i a : s u p e r v i s i o n , c o n b l o c r emo v a l , o n e s t a g e b l o c r emo v a l ,

r e s e r c t i o n w i t h d i v e r s i o n , s t e n t , d i v e r s i o n ;8 ha ce r p r u eba : prueba7 1 , prueba7 2 , prueba 8 , p rueba 15 1 ,

p rueba 15 2 , prueba 11 , prueba 15 , prueba 13 , r e h a c e r p r u e b a 15 2 ;9

1011 (∗ INPUTS ∗ )

73

Page 84: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

12 p r e s e n t a c i o n c l i n i c a : c a n c e r c o l o n , sospechosa , p enduncu l a t ed po l yp, s e s s i l e p o l y p ;

13 v p r u eba7 1 : s i n g l e s p e c imen , f r agmen t ed spec imen , n ega t i v o ,d e s c ono c i d o ;

14 v p r u eba7 2 : s i n g l e s p e c imen , f r agmen t ed spec imen , n ega t i v o ,d e s c ono c i d o ;

15 v p r u eba 8 : r e s e c a b l e o b s t r u y e nd o , r e s e c a b l e n o o b s t r u y e nd o ,l o c a lm e n t e n o r e s e c a b l e , med i c amen t e i nope rab l e , d e s c ono c i d o ;

16 v p r u eba 15 1 : s i , no , d e s c ono c i d o ;17 v p r u eba 15 2 : s i , no ;18 v p r u eba 11 : s i n c r o n o s l i v e r r e s e c a b l e ,

s i n c r o n o s l i v e r n o r e s e c a b l e , s i n c r o n o s abdom i na l , d e s c ono c i d o ;19 v p r u eba 15 : s i n c r o n o s l i v e r r e s e c a b l e ,

s i n c r o n o s l i v e r n o r e s e c a b l e , s i n c r o n o s abdom i na l , d e s c ono c i d o ;20 v p r u eba 13 : c o n v e r t i d a a r e s e c a b l e , r e s t o s n o r e s e c a b l e s ,

d e s c ono c i d o ;21 r e c u r r e n c i a : s e r i a l c e a , d o cumen t ed me ta ch r onus me ta s t a se s , no ;22 t r a t a m i e n t o 3 a p l i c a d o : s i , no ;23 t r a t a m i e n t o 7 a p l i c a d o : s i , no ;24 t r a t a m i e n t o 8 a p l i c a d o : s i , no ;2526 (∗ AUXILIARES ∗ )27 c o l 1 : s i , no ;28 c o l 2 : s i , no ;29 c o l 3 : s i , no ;

74

Page 85: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

30 c o l 5 : s i , no ;31 c o l 6 : s i , no ;32 c o l 7 : s i , no ;33 c o l 8 : s i , no ;34 c o l 9 : s i , no ;35 c o l 1 0 : s i , no ;36 c o l c : s i , no ;3738 END VARS3940 BEGIN KB4142 (∗COL−1∗)43 ( p r e s e n t a c i o n c l i n i c a=pendun cu l a t e d po l y p ) AND ( v p r ueba7 1=

des cono c i d o ) −> ( h a c e r p r u eba=prueba7 1 ) ;44 ( p r e s e n t a c i o n c l i n i c a=pendun cu l a t e d po l y p ) AND ( v p r ueba7 1=

s i n g l e s p e c im e n ) −> ( c i r u g i a=s u p e r v i s i o n ) ;45 ( p r e s e n t a c i o n c l i n i c a=pendun cu l a t e d po l y p ) AND ( v p r ueba7 1=

f r agmen t ed spe c imen ) −> ( c i r u g i a=c o n b l o c r em o v a l ) ;46 ( p r e s e n t a c i o n c l i n i c a=s e s s i l e p o l y p ) AND ( v p r ueba7 2=des cono c i d o )

−> ( h a c e r p r u eba=prueba7 2 ) ;47 ( p r e s e n t a c i o n c l i n i c a=s e s s i l e p o l y p ) AND ( v p r ueba7 2=

s i n g l e s p e c im e n ) −> ( c i r u g i a=s u p e r v i s i o n ) OR ( c i r u g i a=c o n b l o c r em o v a l ) ;

75

Page 86: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

48 ( p r e s e n t a c i o n c l i n i c a=s e s s i l e p o l y p ) AND ( v p r ueba7 2=f r agmen t ed spe c imen ) −> ( c i r u g i a=c o n b l o c r em o v a l ) ;

49 ( p r e s e n t a c i o n c l i n i c a=s e s s i l e p o l y p ) −> ( c o l 3=s i ) ;50 ( p r e s e n t a c i o n c l i n i c a=pendun cu l a t e d po l y p ) −> ( c o l 3=s i ) ;5152 (∗COL−2∗)53 ( p r e s e n t a c i o n c l i n i c a=c a n c e r c o l o n ) AND ( v p r u eba 8=des cono c i d o )

−> ( h a c e r p r u eba=prueba 8 ) ;54 ( p r e s e n t a c i o n c l i n i c a=c a n c e r c o l o n ) AND ( v p r u eba 8=

r e s e c a b l e n o o b s t r u y e n d o ) −> ( c i r u g i a=c o n b l o c r em o v a l ) ;55 ( p r e s e n t a c i o n c l i n i c a=c a n c e r c o l o n ) AND ( v p r u eba 8=

r e s e c a b l e o b s t r u y e n d o ) −> ( c i r u g i a=o n e s t a g e b l o c r em o v a l ) OR (c i r u g i a=r e s e r c t i o n w i t h d i v e r s i o n ) OR ( c i r u g i a=d i v e r s i o n ) ;

56 ( p r e s e n t a c i o n c l i n i c a=c a n c e r c o l o n ) AND ( v p r u eba 8=l o c a l m e n t e n o r e s e c a b l e ) −> ( c i r u g i a=o n e s t a g e b l o c r em o v a l ) ;

57 ( p r e s e n t a c i o n c l i n i c a=c a n c e r c o l o n ) AND ( v p r u eba 8=l o c a l m e n t e n o r e s e c a b l e ) −> ( c o l c=s i ) ;

58 ( v p r u eba 8=med i c amen t e i n ope r ab l e ) −> ( c o l c=s i ) ;59 ( p r e s e n t a c i o n c l i n i c a=c a n c e r c o l o n ) AND ( v p r u eba 8=

r e s e c a b l e n o o b s t r u y e n d o ) −> ( c o l 3=s i ) ;60 ( v p r u eba 8=r e s e c a b l e o b s t r u y e n d o ) −> ( c o l 3=s i ) ;61 ( p r e s e n t a c i o n c l i n i c a=sospechosa ) −> ( c o l 5=s i ) ;6263 (∗ COL 5 ∗ )

76

Page 87: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

64 ( p r e s e n t a c i o n c l i n i c a=sospechosa ) AND ( v p r ueba 11=des cono c i d o ) −>( h a c e r p r u eba=prueba 11 ) ;

65 ( p r e s e n t a c i o n c l i n i c a=sospechosa ) AND ( v p r ueba 11=s i n c r o n o s l i v e r r e s e c a b l e ) −> ( c o l 6=s i ) ;

66 ( p r e s e n t a c i o n c l i n i c a=sospechosa ) AND ( v p r ueba 11=s i n c r o n o s l i v e r n o r e s e c a b l e ) −> ( c o l 7=s i ) ;

67 ( p r e s e n t a c i o n c l i n i c a=sospechosa ) AND ( v p r ueba 11=s i n c r o n o s a b d om i n a l ) −> ( c o l 8=s i ) ;

6869 (∗ COL 3 ∗ )70 ( c o l 3=s i ) AND ( t r a t a m i e n t o 3 a p l i c a d o=no ) −> ( t r a t am i e n t o=

t r a t am i e n t o 3 ) ;71 ( c o l 3=s i ) AND ( t r a t a m i e n t o 3 a p l i c a d o=s i ) −> ( c o l 9=s i ) ;7273 (∗ COL 6 ∗ )74 ( c o l 6=s i ) −> ( c o l 9=s i ) ;7576 (∗ COL 7 ∗ )77 ( c o l 7=s i ) AND ( v p r ueba 13=des cono c i d o ) −> ( h a c e r p r u eba=

prueba 13 ) ;78 ( c o l 7=s i ) AND ( v p r ueba 13=c o n v e r t i d a a r e s e c a b l e ) AND (

t r a t a m i e n t o 7 a p l i c a d o=no ) −> ( t r a t am i e n t o=t r a t am i e n t o 7 ) ;79 ( c o l 7=s i ) AND ( v p r ueba 13=r e s t o s n o r e s e c a b l e s ) −> ( t r a t am i e n t o=

qu im i o t e r a p i a ) ;80

77

Page 88: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

81 (∗ COL 8 ∗ )82 ( c o l 8=s i ) −> ( c o l c=s i ) ;83 (∗ COL 9 ∗ )84 ( c o l 9=s i ) AND ( r e c u r r e n c i a=s e r i a l c e a ) AND ( v p r u eba 15 1=

des cono c i d o ) −> ( h a c e r p r u eba=prueba 15 1 ) ;85 ( c o l 9=s i ) AND ( r e c u r r e n c i a=s e r i a l c e a ) AND ( v p r u eba 15 1=no ) −>

( h a c e r p r u eba=prueba 15 2 ) ;86 ( c o l 9=s i ) AND ( r e c u r r e n c i a=s e r i a l c e a ) AND ( v p r u eba 15 1=no ) AND

( v p r u eba 15 2=no ) −> ( h a c e r p r u eba=r e h a c e r p r u e b a 15 2 ) ;87 ( c o l 9=s i ) AND ( r e c u r r e n c i a=s e r i a l c e a ) AND ( v p r u eba 15 1=no ) AND

( v p r u eba 15 2=no ) AND ( v p r u eba 15 2=s i ) −> ( c o l 1 0=s i ) ;88 ( c o l 9=s i ) AND ( r e c u r r e n c i a=s e r i a l c e a ) AND ( v p r u eba 15 1=s i ) −>

( c o l 1 0=s i ) ;89 ( c o l 9=s i ) AND ( r e c u r r e n c i a=documen t ed me t a ch r onu s me t a s t a s e s ) −>

( c o l 1 0=s i ) ;9091 (∗ COL 10 ∗ )92 ( c o l 1 0=s i ) −> ( c o l c=s i ) ;93 (∗COL C ∗ )94 ( c o l c=s i ) −> ( t r a t am i e n t o=qu im i o t e r a p i a ) ;9596 END KB9798 END.

78

Page 89: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

7.3. Resultados en el modelo logico proposicional Booleano

Una vez descrito el sistema experto en nuestro lenguaje podemos obtener un sistemaexperto basado en logica proposicional Booleana para los sistemas de computacion algebraica:CoCoA, PolyBoRi o Singular.7.3.1. Traduccion en PolyBoRi

1 f r om p o l y b o r i . PyPolyBoRi impo r t ∗

2 f r om p o l y b o r i . gbco re impo r t ∗

3 f r om p o l y b o r i impo r t ∗

45 de f O(M,N) : r e t u r n Polynomia l (M) ∗Polynomia l (N)6 de f NEG(M) : r e t u r n 1+Polynomia l (M)7 de f Y (M,N) : r e t u r n NEG(O(NEG(M) ,NEG(N) ) )89 d e c l a r e r i n g ( [ B lock ( ” x ” , 5 6 ) ] , g l o b a l s ( ) ) ;

1011 s0=pow ( x ( 0 ) , 2 ) + x ( 0 ) ;12 s1=pow ( x ( 1 ) , 2 ) + x ( 1 ) ;13 s2=pow ( x ( 2 ) , 2 ) + x ( 2 ) ;14 s3=pow ( x ( 3 ) , 2 ) + x ( 3 ) ;15 s4=pow ( x ( 4 ) , 2 ) + x ( 4 ) ;16 s5=pow ( x ( 5 ) , 2 ) + x ( 5 ) ;17 s6=pow ( x ( 6 ) , 2 ) + x ( 6 ) ;18 s7=pow ( x ( 7 ) , 2 ) + x ( 7 ) ;

79

Page 90: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

19 s8=pow ( x ( 8 ) , 2 ) + x ( 8 ) ;20 s9=pow ( x ( 9 ) , 2 ) + x ( 9 ) ;21 s10=pow ( x ( 1 0 ) , 2 ) + x ( 1 0 ) ;22 s11=pow ( x ( 1 1 ) , 2 ) + x ( 1 1 ) ;23 s12=pow ( x ( 1 2 ) , 2 ) + x ( 1 2 ) ;24 s13=pow ( x ( 1 3 ) , 2 ) + x ( 1 3 ) ;25 s14=pow ( x ( 1 4 ) , 2 ) + x ( 1 4 ) ;26 s15=pow ( x ( 1 5 ) , 2 ) + x ( 1 5 ) ;27 s16=pow ( x ( 1 6 ) , 2 ) + x ( 1 6 ) ;28 s17=pow ( x ( 1 7 ) , 2 ) + x ( 1 7 ) ;29 s18=pow ( x ( 1 8 ) , 2 ) + x ( 1 8 ) ;30 s19=pow ( x ( 1 9 ) , 2 ) + x ( 1 9 ) ;31 s20=pow ( x ( 2 0 ) , 2 ) + x ( 2 0 ) ;32 s21=pow ( x ( 2 1 ) , 2 ) + x ( 2 1 ) ;33 s22=pow ( x ( 2 2 ) , 2 ) + x ( 2 2 ) ;34 s23=pow ( x ( 2 3 ) , 2 ) + x ( 2 3 ) ;35 s24=pow ( x ( 2 4 ) , 2 ) + x ( 2 4 ) ;36 s25=pow ( x ( 2 5 ) , 2 ) + x ( 2 5 ) ;37 s26=pow ( x ( 2 6 ) , 2 ) + x ( 2 6 ) ;38 s27=pow ( x ( 2 7 ) , 2 ) + x ( 2 7 ) ;39 s28=pow ( x ( 2 8 ) , 2 ) + x ( 2 8 ) ;40 s29=pow ( x ( 2 9 ) , 2 ) + x ( 2 9 ) ;41 s30=pow ( x ( 3 0 ) , 2 ) + x ( 3 0 ) ;42 s31=pow ( x ( 3 1 ) , 2 ) + x ( 3 1 ) ;43 s32=pow ( x ( 3 2 ) , 2 ) + x ( 3 2 ) ;

80

Page 91: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

44 s33=pow ( x ( 3 3 ) , 2 ) + x ( 3 3 ) ;45 s34=pow ( x ( 3 4 ) , 2 ) + x ( 3 4 ) ;46 s35=pow ( x ( 3 5 ) , 2 ) + x ( 3 5 ) ;47 s36=pow ( x ( 3 6 ) , 2 ) + x ( 3 6 ) ;48 s37=pow ( x ( 3 7 ) , 2 ) + x ( 3 7 ) ;49 s38=pow ( x ( 3 8 ) , 2 ) + x ( 3 8 ) ;50 s39=pow ( x ( 3 9 ) , 2 ) + x ( 3 9 ) ;51 s40=pow ( x ( 4 0 ) , 2 ) + x ( 4 0 ) ;52 s41=pow ( x ( 4 1 ) , 2 ) + x ( 4 1 ) ;53 s42=pow ( x ( 4 2 ) , 2 ) + x ( 4 2 ) ;54 s43=pow ( x ( 4 3 ) , 2 ) + x ( 4 3 ) ;55 s44=pow ( x ( 4 4 ) , 2 ) + x ( 4 4 ) ;56 s45=pow ( x ( 4 5 ) , 2 ) + x ( 4 5 ) ;57 s46=pow ( x ( 4 6 ) , 2 ) + x ( 4 6 ) ;58 s47=pow ( x ( 4 7 ) , 2 ) + x ( 4 7 ) ;59 s48=pow ( x ( 4 8 ) , 2 ) + x ( 4 8 ) ;60 s49=pow ( x ( 4 9 ) , 2 ) + x ( 4 9 ) ;61 s50=pow ( x ( 5 0 ) , 2 ) + x ( 5 0 ) ;62 s51=pow ( x ( 5 1 ) , 2 ) + x ( 5 1 ) ;63 s52=pow ( x ( 5 2 ) , 2 ) + x ( 5 2 ) ;64 s53=pow ( x ( 5 3 ) , 2 ) + x ( 5 3 ) ;65 s54=pow ( x ( 5 4 ) , 2 ) + x ( 5 4 ) ;66 s55=pow ( x ( 5 5 ) , 2 ) + x ( 5 5 ) ;6768 i c 0=NEG( Y ( x ( 0 ) , x ( 1 ) ) ) ;

81

Page 92: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

69 i c 1=NEG( Y ( x ( 0 ) , x ( 2 ) ) ) ;70 i c 2=NEG( Y ( x ( 0 ) , x ( 3 ) ) ) ;71 i c 3=NEG( Y ( x ( 1 ) , x ( 2 ) ) ) ;72 i c 4=NEG( Y ( x ( 1 ) , x ( 3 ) ) ) ;73 i c 5=NEG( Y ( x ( 2 ) , x ( 3 ) ) ) ;74 i c 6=NEG( Y ( x ( 4 ) , x ( 5 ) ) ) ;75 i c 7=NEG( Y ( x ( 4 ) , x ( 6 ) ) ) ;76 i c 8=NEG( Y ( x ( 4 ) , x ( 7 ) ) ) ;77 i c 9=NEG( Y ( x ( 4 ) , x ( 8 ) ) ) ;78 i c 1 0=NEG( Y ( x ( 5 ) , x ( 6 ) ) ) ;79 i c 1 1=NEG( Y ( x ( 5 ) , x ( 7 ) ) ) ;80 i c 1 2=NEG( Y ( x ( 5 ) , x ( 8 ) ) ) ;81 i c 1 3=NEG( Y ( x ( 6 ) , x ( 7 ) ) ) ;82 i c 1 4=NEG( Y ( x ( 6 ) , x ( 8 ) ) ) ;83 i c 1 5=NEG( Y ( x ( 7 ) , x ( 8 ) ) ) ;84 i c 1 6=NEG( Y ( x ( 9 ) , x ( 1 0 ) ) ) ;85 i c 1 7=NEG( Y ( x ( 9 ) , x ( 1 1 ) ) ) ;86 i c 1 8=NEG( Y ( x ( 9 ) , x ( 1 2 ) ) ) ;87 i c 1 9=NEG( Y ( x ( 9 ) , x ( 1 3 ) ) ) ;88 i c 2 0=NEG( Y ( x ( 9 ) , x ( 1 4 ) ) ) ;89 i c 2 1=NEG( Y ( x ( 9 ) , x ( 1 5 ) ) ) ;90 i c 2 2=NEG( Y ( x ( 9 ) , x ( 1 6 ) ) ) ;91 i c 2 3=NEG( Y ( x ( 1 0 ) , x ( 1 1 ) ) ) ;92 i c 2 4=NEG( Y ( x ( 1 0 ) , x ( 1 2 ) ) ) ;93 i c 2 5=NEG( Y ( x ( 1 0 ) , x ( 1 3 ) ) ) ;

82

Page 93: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

94 i c 2 6=NEG( Y ( x ( 1 0 ) , x ( 1 4 ) ) ) ;95 i c 2 7=NEG( Y ( x ( 1 0 ) , x ( 1 5 ) ) ) ;96 i c 2 8=NEG( Y ( x ( 1 0 ) , x ( 1 6 ) ) ) ;97 i c 2 9=NEG( Y ( x ( 1 1 ) , x ( 1 2 ) ) ) ;98 i c 3 0=NEG( Y ( x ( 1 1 ) , x ( 1 3 ) ) ) ;99 i c 3 1=NEG( Y ( x ( 1 1 ) , x ( 1 4 ) ) ) ;

100 i c 3 2=NEG( Y ( x ( 1 1 ) , x ( 1 5 ) ) ) ;101 i c 3 3=NEG( Y ( x ( 1 1 ) , x ( 1 6 ) ) ) ;102 i c 3 4=NEG( Y ( x ( 1 2 ) , x ( 1 3 ) ) ) ;103 i c 3 5=NEG( Y ( x ( 1 2 ) , x ( 1 4 ) ) ) ;104 i c 3 6=NEG( Y ( x ( 1 2 ) , x ( 1 5 ) ) ) ;105 i c 3 7=NEG( Y ( x ( 1 2 ) , x ( 1 6 ) ) ) ;106 i c 3 8=NEG( Y ( x ( 1 3 ) , x ( 1 4 ) ) ) ;107 i c 3 9=NEG( Y ( x ( 1 3 ) , x ( 1 5 ) ) ) ;108 i c 4 0=NEG( Y ( x ( 1 3 ) , x ( 1 6 ) ) ) ;109 i c 4 1=NEG( Y ( x ( 1 4 ) , x ( 1 5 ) ) ) ;110 i c 4 2=NEG( Y ( x ( 1 4 ) , x ( 1 6 ) ) ) ;111 i c 4 3=NEG( Y ( x ( 1 5 ) , x ( 1 6 ) ) ) ;112 i c 4 4=NEG( Y ( x ( 1 7 ) , x ( 1 8 ) ) ) ;113 i c 4 5=NEG( Y ( x ( 1 7 ) , x ( 1 9 ) ) ) ;114 i c 4 6=NEG( Y ( x ( 1 8 ) , x ( 1 9 ) ) ) ;115 i c 4 7=NEG( Y ( x ( 2 0 ) , x ( 2 1 ) ) ) ;116 i c 4 8=NEG( Y ( x ( 2 0 ) , x ( 2 2 ) ) ) ;117 i c 4 9=NEG( Y ( x ( 2 1 ) , x ( 2 2 ) ) ) ;118 i c 5 0=NEG( Y ( x ( 2 3 ) , x ( 2 4 ) ) ) ;

83

Page 94: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

119 i c 5 1=NEG( Y ( x ( 2 3 ) , x ( 2 5 ) ) ) ;120 i c 5 2=NEG( Y ( x ( 2 4 ) , x ( 2 5 ) ) ) ;121 i c 5 3=NEG( Y ( x ( 2 6 ) , x ( 2 7 ) ) ) ;122 i c 5 4=NEG( Y ( x ( 2 6 ) , x ( 2 8 ) ) ) ;123 i c 5 5=NEG( Y ( x ( 2 6 ) , x ( 2 9 ) ) ) ;124 i c 5 6=NEG( Y ( x ( 2 7 ) , x ( 2 8 ) ) ) ;125 i c 5 7=NEG( Y ( x ( 2 7 ) , x ( 2 9 ) ) ) ;126 i c 5 8=NEG( Y ( x ( 2 8 ) , x ( 2 9 ) ) ) ;127 i c 5 9=NEG( Y ( x ( 3 0 ) , x ( 3 1 ) ) ) ;128 i c 6 0=NEG( Y ( x ( 3 3 ) , x ( 3 4 ) ) ) ;129 i c 6 1=NEG( Y ( x ( 3 3 ) , x ( 3 5 ) ) ) ;130 i c 6 2=NEG( Y ( x ( 3 4 ) , x ( 3 5 ) ) ) ;131 i c 6 3=NEG( Y ( x ( 3 6 ) , x ( 3 7 ) ) ) ;132 i c 6 4=NEG( Y ( x ( 3 6 ) , x ( 3 8 ) ) ) ;133 i c 6 5=NEG( Y ( x ( 3 7 ) , x ( 3 8 ) ) ) ;134 i c 6 6=NEG( Y ( x ( 3 9 ) , x ( 4 0 ) ) ) ;135 i c 6 7=NEG( Y ( x ( 4 1 ) , x ( 4 2 ) ) ) ;136137 J =[ s0 , s1 , s2 , s3 , s4 , s5 , s6 , s7 , s8 , s9 , s10 , s11 , s12 , s13 , s14 , s15 , s16 , s17 , s18 ,

s19 , s20 , s21 , s22 , s23 , s24 , s25 , s26 , s27 , s28 , s29 , s30 , s31 , s32 , s33 , s34 ,s35 , s36 , s37 , s38 , s39 , s40 , s41 , s42 , s43 , s44 , s45 , s46 , s47 , s48 , s49 , s50 ,s51 , s52 , s53 , s54 , s55 , i c0 , i c1 , i c2 , i c3 , i c4 , i c5 , i c6 , i c7 , i c8 , i c9 , i c10 ,i c11 , i c12 , i c13 , i c14 , i c15 , i c16 , i c17 , i c18 , i c19 , i c20 , i c21 , i c22 , i c23 ,i c24 , i c25 , i c26 , i c27 , i c28 , i c29 , i c30 , i c31 , i c32 , i c33 , i c34 , i c35 , i c36 ,i c37 , i c38 , i c39 , i c40 , i c41 , i c42 , i c43 , i c44 , i c45 , i c46 , i c47 , i c48 , i c49 ,

84

Page 95: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

i c50 , i c51 , i c52 , i c53 , i c54 , i c55 , i c56 , i c57 , i c58 , i c59 , i c60 , i c61 , i c62 ,i c63 , i c64 , i c65 , i c66 , i c 67 ] ;

138139 r0=O(NEG( x ( 1 9 ) ) ,O(NEG(1+x ( 2 0 ) ∗ x ( 2 1 ) ∗ x ( 2 2 ) ) , x ( 9 ) ) ) ;140 r1=O(NEG( x ( 1 9 ) ) ,O(NEG( x ( 2 0 ) ) , x ( 4 ) ) ) ;141 r2=O(NEG( x ( 1 9 ) ) ,O(NEG( x ( 2 1 ) ) , x ( 5 ) ) ) ;142 r3=O(NEG(1+x ( 1 7 ) ∗ x ( 1 8 ) ∗ x ( 1 9 ) ) ,O(NEG(1+x ( 2 3 ) ∗ x ( 2 4 ) ∗ x ( 2 5 ) ) , x ( 1 0 ) ) ) ;143 r4=O(NEG(1+x ( 1 7 ) ∗ x ( 1 8 ) ∗ x ( 1 9 ) ) ,O(NEG( x ( 2 3 ) ) ,O( x ( 4 ) , x ( 5 ) ) ) ) ;144 r5=O(NEG(1+x ( 1 7 ) ∗ x ( 1 8 ) ∗ x ( 1 9 ) ) ,O(NEG( x ( 2 4 ) ) , x ( 5 ) ) ) ;145 r6=O(NEG(1+x ( 1 7 ) ∗ x ( 1 8 ) ∗ x ( 1 9 ) ) , x ( 4 8 ) ) ;146 r7=O(NEG( x ( 1 9 ) ) , x ( 4 8 ) ) ;147 r8=O(NEG( x ( 1 7 ) ) ,O(NEG(1+x ( 2 6 ) ∗ x ( 2 7 ) ∗ x ( 2 8 ) ∗ x ( 2 9 ) ) , x ( 1 1 ) ) ) ;148 r9=O(NEG( x ( 1 7 ) ) ,O(NEG( x ( 2 7 ) ) , x ( 5 ) ) ) ;149 r10=O(NEG( x ( 1 7 ) ) ,O(NEG( x ( 2 6 ) ) ,O( x ( 6 ) ,O( x ( 7 ) ,1+ x ( 4 ) ∗ x ( 5 ) ∗ x ( 6 ) ∗ x ( 7 ) ∗ x

( 8 ) ) ) ) ) ;150 r11=O(NEG( x ( 1 7 ) ) ,O(NEG( x ( 2 8 ) ) , x ( 6 ) ) ) ;151 r12=O(NEG( x ( 1 7 ) ) ,O(NEG( x ( 2 8 ) ) , x ( 5 5 ) ) ) ;152 r13=O(NEG( x ( 2 9 ) ) , x ( 5 5 ) ) ;153 r14=O(NEG( x ( 1 7 ) ) ,O(NEG( x ( 2 7 ) ) , x ( 4 8 ) ) ) ;154 r15=O(NEG( x ( 2 6 ) ) , x ( 4 8 ) ) ;155 r16=O(NEG( x ( 1 8 ) ) , x ( 4 9 ) ) ;156 r17=O(NEG( x ( 1 8 ) ) ,O(NEG(1+x ( 3 3 ) ∗ x ( 3 4 ) ∗ x ( 3 5 ) ) , x ( 1 4 ) ) ) ;157 r18=O(NEG( x ( 1 8 ) ) ,O(NEG( x ( 3 3 ) ) , x ( 5 0 ) ) ) ;158 r19=O(NEG( x ( 1 8 ) ) ,O(NEG( x ( 3 4 ) ) , x ( 5 1 ) ) ) ;159 r20=O(NEG( x ( 1 8 ) ) ,O(NEG( x ( 3 5 ) ) , x ( 5 2 ) ) ) ;

85

Page 96: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

160 r21=O(NEG( x ( 4 8 ) ) ,O(NEG(1+x ( 4 3 ) ) , x ( 1 ) ) ) ;161 r22=O(NEG( x ( 4 8 ) ) ,O(NEG( x ( 4 3 ) ) , x ( 5 3 ) ) ) ;162 r23=O(NEG( x ( 5 0 ) ) , x ( 5 3 ) ) ;163 r24=O(NEG( x ( 5 1 ) ) ,O(NEG(1+x ( 3 9 ) ∗ x ( 4 0 ) ) , x ( 1 6 ) ) ) ;164 r25=O(NEG( x ( 5 1 ) ) ,O(O(NEG( x ( 3 9 ) ) ,NEG(1+x ( 4 4 ) ) ) , x ( 3 ) ) ) ;165 r26=O(NEG( x ( 5 1 ) ) ,O(NEG( x ( 4 0 ) ) , x ( 0 ) ) ) ;166 r27=O(NEG( x ( 5 2 ) ) , x ( 5 5 ) ) ;167 r28=O(NEG( x ( 5 3 ) ) ,O(O(NEG( x ( 4 1 ) ) ,NEG(1+x ( 3 0 ) ∗ x ( 3 1 ) ) ) , x ( 1 2 ) ) ) ;168 r29=O(NEG( x ( 5 3 ) ) ,O(O(NEG( x ( 4 1 ) ) ,NEG( x ( 3 1 ) ) ) , x ( 1 3 ) ) ) ;169 r30=O(NEG( x ( 5 3 ) ) ,O(O(NEG( x ( 4 1 ) ) ,O(NEG( x ( 3 1 ) ) ,NEG(1+x ( 3 2 ) ) ) ) ,1+ x ( 9 ) ∗ x

( 1 0 ) ∗ x ( 1 1 ) ∗ x ( 1 2 ) ∗ x ( 1 3 ) ∗ x ( 1 4 ) ∗ x ( 1 5 ) ∗ x ( 1 6 ) ) ) ;170 r31=O(NEG( x ( 5 3 ) ) ,O(O(NEG( x ( 4 1 ) ) ,O(NEG( x ( 3 1 ) ) ,O(NEG(1+x ( 3 2 ) ) ,NEG( x ( 3 2 )

) ) ) ) , x ( 5 4 ) ) ) ;171 r32=O(NEG( x ( 5 3 ) ) ,O(O(NEG( x ( 4 1 ) ) ,NEG( x ( 3 0 ) ) ) , x ( 5 4 ) ) ) ;172 r33=O(NEG( x ( 5 3 ) ) ,O(NEG( x ( 4 2 ) ) , x ( 5 4 ) ) ) ;173 r34=O(NEG( x ( 5 4 ) ) , x ( 5 5 ) ) ;174 r35=O(NEG( x ( 5 5 ) ) , x ( 0 ) ) ;175176 K=[ r0 , r1 , r2 , r3 , r4 , r5 , r6 , r7 , r8 , r9 , r10 , r11 , r12 , r13 , r14 , r15 , r16 , r17 , r18 ,

r19 , r20 , r21 , r22 , r23 , r24 , r25 , r26 , r27 , r28 , r29 , r30 , r31 , r32 , r33 , r34 ,r35 ] ;

177178179 gb=g r o e b n e r b a s i s ( J+K , h e u r i s t i c=False ) ;

86

Page 97: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

7.3.2. Traduccion en Singular

1 r i n g r =2 , ( x ( 0 . . 5 5 ) ) , l p ;23 po ly s0=( x ( 0 ) ˆ2 ) + x ( 0 ) ;4 po ly s1=( x ( 1 ) ˆ2 ) + x ( 1 ) ;5 po ly s2=( x ( 2 ) ˆ2 ) + x ( 2 ) ;6 po ly s3=( x ( 3 ) ˆ2 ) + x ( 3 ) ;7 po ly s4=( x ( 4 ) ˆ2 ) + x ( 4 ) ;8 po ly s5=( x ( 5 ) ˆ2 ) + x ( 5 ) ;9 po ly s6=( x ( 6 ) ˆ2 ) + x ( 6 ) ;

10 po ly s7=( x ( 7 ) ˆ2 ) + x ( 7 ) ;11 po ly s8=( x ( 8 ) ˆ2 ) + x ( 8 ) ;12 po ly s9=( x ( 9 ) ˆ2 ) + x ( 9 ) ;13 po ly s10=( x ( 1 0 ) ˆ2 ) + x ( 1 0 ) ;14 po ly s11=( x ( 1 1 ) ˆ2 ) + x ( 1 1 ) ;15 po ly s12=( x ( 1 2 ) ˆ2 ) + x ( 1 2 ) ;16 po ly s13=( x ( 1 3 ) ˆ2 ) + x ( 1 3 ) ;17 po ly s14=( x ( 1 4 ) ˆ2 ) + x ( 1 4 ) ;18 po ly s15=( x ( 1 5 ) ˆ2 ) + x ( 1 5 ) ;19 po ly s16=( x ( 1 6 ) ˆ2 ) + x ( 1 6 ) ;20 po ly s17=( x ( 1 7 ) ˆ2 ) + x ( 1 7 ) ;21 po ly s18=( x ( 1 8 ) ˆ2 ) + x ( 1 8 ) ;22 po ly s19=( x ( 1 9 ) ˆ2 ) + x ( 1 9 ) ;23 po ly s20=( x ( 2 0 ) ˆ2 ) + x ( 2 0 ) ;

87

Page 98: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

24 po ly s21=( x ( 2 1 ) ˆ2 ) + x ( 2 1 ) ;25 po ly s22=( x ( 2 2 ) ˆ2 ) + x ( 2 2 ) ;26 po ly s23=( x ( 2 3 ) ˆ2 ) + x ( 2 3 ) ;27 po ly s24=( x ( 2 4 ) ˆ2 ) + x ( 2 4 ) ;28 po ly s25=( x ( 2 5 ) ˆ2 ) + x ( 2 5 ) ;29 po ly s26=( x ( 2 6 ) ˆ2 ) + x ( 2 6 ) ;30 po ly s27=( x ( 2 7 ) ˆ2 ) + x ( 2 7 ) ;31 po ly s28=( x ( 2 8 ) ˆ2 ) + x ( 2 8 ) ;32 po ly s29=( x ( 2 9 ) ˆ2 ) + x ( 2 9 ) ;33 po ly s30=( x ( 3 0 ) ˆ2 ) + x ( 3 0 ) ;34 po ly s31=( x ( 3 1 ) ˆ2 ) + x ( 3 1 ) ;35 po ly s32=( x ( 3 2 ) ˆ2 ) + x ( 3 2 ) ;36 po ly s33=( x ( 3 3 ) ˆ2 ) + x ( 3 3 ) ;37 po ly s34=( x ( 3 4 ) ˆ2 ) + x ( 3 4 ) ;38 po ly s35=( x ( 3 5 ) ˆ2 ) + x ( 3 5 ) ;39 po ly s36=( x ( 3 6 ) ˆ2 ) + x ( 3 6 ) ;40 po ly s37=( x ( 3 7 ) ˆ2 ) + x ( 3 7 ) ;41 po ly s38=( x ( 3 8 ) ˆ2 ) + x ( 3 8 ) ;42 po ly s39=( x ( 3 9 ) ˆ2 ) + x ( 3 9 ) ;43 po ly s40=( x ( 4 0 ) ˆ2 ) + x ( 4 0 ) ;44 po ly s41=( x ( 4 1 ) ˆ2 ) + x ( 4 1 ) ;45 po ly s42=( x ( 4 2 ) ˆ2 ) + x ( 4 2 ) ;46 po ly s43=( x ( 4 3 ) ˆ2 ) + x ( 4 3 ) ;47 po ly s44=( x ( 4 4 ) ˆ2 ) + x ( 4 4 ) ;48 po ly s45=( x ( 4 5 ) ˆ2 ) + x ( 4 5 ) ;

88

Page 99: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

49 po ly s46=( x ( 4 6 ) ˆ2 ) + x ( 4 6 ) ;50 po ly s47=( x ( 4 7 ) ˆ2 ) + x ( 4 7 ) ;51 po ly s48=( x ( 4 8 ) ˆ2 ) + x ( 4 8 ) ;52 po ly s49=( x ( 4 9 ) ˆ2 ) + x ( 4 9 ) ;53 po ly s50=( x ( 5 0 ) ˆ2 ) + x ( 5 0 ) ;54 po ly s51=( x ( 5 1 ) ˆ2 ) + x ( 5 1 ) ;55 po ly s52=( x ( 5 2 ) ˆ2 ) + x ( 5 2 ) ;56 po ly s53=( x ( 5 3 ) ˆ2 ) + x ( 5 3 ) ;57 po ly s54=( x ( 5 4 ) ˆ2 ) + x ( 5 4 ) ;58 po ly s55=( x ( 5 5 ) ˆ2 ) + x ( 5 5 ) ;5960 i d e a l J=s0 , s1 , s2 , s3 , s4 , s5 , s6 , s7 , s8 , s9 , s10 , s11 , s12 , s13 , s14 , s15 , s16 , s17

, s18 , s19 , s20 , s21 , s22 , s23 , s24 , s25 , s26 , s27 , s28 , s29 , s30 , s31 , s32 , s33 ,s34 , s35 , s36 , s37 , s38 , s39 , s40 , s41 , s42 , s43 , s44 , s45 , s46 , s47 , s48 , s49 ,s50 , s51 , s52 , s53 , s54 , s55 ;

61 J = s t d ( J ) ;6263 p ro c NEG( po ly M) { r e t u r n ( r educe (1+M, J ) ) ; }64 p ro c O( po ly M, po ly N) { r e t u r n ( r educe (M∗N, J ) ) ; }65 p ro c Y ( po ly M, po ly N) { r e t u r n (NEG(O(NEG(M) ,NEG(N) ) ) ) ; }6667 po ly i c 0=NEG( Y ( x ( 0 ) , x ( 1 ) ) ) ;68 po ly i c 1=NEG( Y ( x ( 0 ) , x ( 2 ) ) ) ;69 po ly i c 2=NEG( Y ( x ( 0 ) , x ( 3 ) ) ) ;70 po ly i c 3=NEG( Y ( x ( 1 ) , x ( 2 ) ) ) ;

89

Page 100: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

71 po ly i c 4=NEG( Y ( x ( 1 ) , x ( 3 ) ) ) ;72 po ly i c 5=NEG( Y ( x ( 2 ) , x ( 3 ) ) ) ;73 po ly i c 6=NEG( Y ( x ( 4 ) , x ( 5 ) ) ) ;74 po ly i c 7=NEG( Y ( x ( 4 ) , x ( 6 ) ) ) ;75 po ly i c 8=NEG( Y ( x ( 4 ) , x ( 7 ) ) ) ;76 po ly i c 9=NEG( Y ( x ( 4 ) , x ( 8 ) ) ) ;77 po ly i c 10=NEG( Y ( x ( 5 ) , x ( 6 ) ) ) ;78 po ly i c 11=NEG( Y ( x ( 5 ) , x ( 7 ) ) ) ;79 po ly i c 12=NEG( Y ( x ( 5 ) , x ( 8 ) ) ) ;80 po ly i c 13=NEG( Y ( x ( 6 ) , x ( 7 ) ) ) ;81 po ly i c 14=NEG( Y ( x ( 6 ) , x ( 8 ) ) ) ;82 po ly i c 15=NEG( Y ( x ( 7 ) , x ( 8 ) ) ) ;83 po ly i c 16=NEG( Y ( x ( 9 ) , x ( 1 0 ) ) ) ;84 po ly i c 17=NEG( Y ( x ( 9 ) , x ( 1 1 ) ) ) ;85 po ly i c 18=NEG( Y ( x ( 9 ) , x ( 1 2 ) ) ) ;86 po ly i c 19=NEG( Y ( x ( 9 ) , x ( 1 3 ) ) ) ;87 po ly i c 20=NEG( Y ( x ( 9 ) , x ( 1 4 ) ) ) ;88 po ly i c 21=NEG( Y ( x ( 9 ) , x ( 1 5 ) ) ) ;89 po ly i c 22=NEG( Y ( x ( 9 ) , x ( 1 6 ) ) ) ;90 po ly i c 23=NEG( Y ( x ( 1 0 ) , x ( 1 1 ) ) ) ;91 po ly i c 24=NEG( Y ( x ( 1 0 ) , x ( 1 2 ) ) ) ;92 po ly i c 25=NEG( Y ( x ( 1 0 ) , x ( 1 3 ) ) ) ;93 po ly i c 26=NEG( Y ( x ( 1 0 ) , x ( 1 4 ) ) ) ;94 po ly i c 27=NEG( Y ( x ( 1 0 ) , x ( 1 5 ) ) ) ;95 po ly i c 28=NEG( Y ( x ( 1 0 ) , x ( 1 6 ) ) ) ;

90

Page 101: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

96 po ly i c 29=NEG( Y ( x ( 1 1 ) , x ( 1 2 ) ) ) ;97 po ly i c 30=NEG( Y ( x ( 1 1 ) , x ( 1 3 ) ) ) ;98 po ly i c 31=NEG( Y ( x ( 1 1 ) , x ( 1 4 ) ) ) ;99 po ly i c 32=NEG( Y ( x ( 1 1 ) , x ( 1 5 ) ) ) ;

100 po ly i c 33=NEG( Y ( x ( 1 1 ) , x ( 1 6 ) ) ) ;101 po ly i c 34=NEG( Y ( x ( 1 2 ) , x ( 1 3 ) ) ) ;102 po ly i c 35=NEG( Y ( x ( 1 2 ) , x ( 1 4 ) ) ) ;103 po ly i c 36=NEG( Y ( x ( 1 2 ) , x ( 1 5 ) ) ) ;104 po ly i c 37=NEG( Y ( x ( 1 2 ) , x ( 1 6 ) ) ) ;105 po ly i c 38=NEG( Y ( x ( 1 3 ) , x ( 1 4 ) ) ) ;106 po ly i c 39=NEG( Y ( x ( 1 3 ) , x ( 1 5 ) ) ) ;107 po ly i c 40=NEG( Y ( x ( 1 3 ) , x ( 1 6 ) ) ) ;108 po ly i c 41=NEG( Y ( x ( 1 4 ) , x ( 1 5 ) ) ) ;109 po ly i c 42=NEG( Y ( x ( 1 4 ) , x ( 1 6 ) ) ) ;110 po ly i c 43=NEG( Y ( x ( 1 5 ) , x ( 1 6 ) ) ) ;111 po ly i c 44=NEG( Y ( x ( 1 7 ) , x ( 1 8 ) ) ) ;112 po ly i c 45=NEG( Y ( x ( 1 7 ) , x ( 1 9 ) ) ) ;113 po ly i c 46=NEG( Y ( x ( 1 8 ) , x ( 1 9 ) ) ) ;114 po ly i c 47=NEG( Y ( x ( 2 0 ) , x ( 2 1 ) ) ) ;115 po ly i c 48=NEG( Y ( x ( 2 0 ) , x ( 2 2 ) ) ) ;116 po ly i c 49=NEG( Y ( x ( 2 1 ) , x ( 2 2 ) ) ) ;117 po ly i c 50=NEG( Y ( x ( 2 3 ) , x ( 2 4 ) ) ) ;118 po ly i c 51=NEG( Y ( x ( 2 3 ) , x ( 2 5 ) ) ) ;119 po ly i c 52=NEG( Y ( x ( 2 4 ) , x ( 2 5 ) ) ) ;120 po ly i c 53=NEG( Y ( x ( 2 6 ) , x ( 2 7 ) ) ) ;

91

Page 102: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

121 po ly i c 54=NEG( Y ( x ( 2 6 ) , x ( 2 8 ) ) ) ;122 po ly i c 55=NEG( Y ( x ( 2 6 ) , x ( 2 9 ) ) ) ;123 po ly i c 56=NEG( Y ( x ( 2 7 ) , x ( 2 8 ) ) ) ;124 po ly i c 57=NEG( Y ( x ( 2 7 ) , x ( 2 9 ) ) ) ;125 po ly i c 58=NEG( Y ( x ( 2 8 ) , x ( 2 9 ) ) ) ;126 po ly i c 59=NEG( Y ( x ( 3 0 ) , x ( 3 1 ) ) ) ;127 po ly i c 60=NEG( Y ( x ( 3 3 ) , x ( 3 4 ) ) ) ;128 po ly i c 61=NEG( Y ( x ( 3 3 ) , x ( 3 5 ) ) ) ;129 po ly i c 62=NEG( Y ( x ( 3 4 ) , x ( 3 5 ) ) ) ;130 po ly i c 63=NEG( Y ( x ( 3 6 ) , x ( 3 7 ) ) ) ;131 po ly i c 64=NEG( Y ( x ( 3 6 ) , x ( 3 8 ) ) ) ;132 po ly i c 65=NEG( Y ( x ( 3 7 ) , x ( 3 8 ) ) ) ;133 po ly i c 66=NEG( Y ( x ( 3 9 ) , x ( 4 0 ) ) ) ;134 po ly i c 67=NEG( Y ( x ( 4 1 ) , x ( 4 2 ) ) ) ;135136 po ly r0=O(NEG( x ( 1 9 ) ) ,O(NEG(1+x ( 2 0 ) ∗ x ( 2 1 ) ∗ x ( 2 2 ) ) , x ( 9 ) ) ) ;137 po ly r1=O(NEG( x ( 1 9 ) ) ,O(NEG( x ( 2 0 ) ) , x ( 4 ) ) ) ;138 po ly r2=O(NEG( x ( 1 9 ) ) ,O(NEG( x ( 2 1 ) ) , x ( 5 ) ) ) ;139 po ly r3=O(NEG(1+x ( 1 7 ) ∗ x ( 1 8 ) ∗ x ( 1 9 ) ) ,O(NEG(1+x ( 2 3 ) ∗ x ( 2 4 ) ∗ x ( 2 5 ) ) , x ( 1 0 ) ) )

;140 po ly r4=O(NEG(1+x ( 1 7 ) ∗ x ( 1 8 ) ∗ x ( 1 9 ) ) ,O(NEG( x ( 2 3 ) ) ,O( x ( 4 ) , x ( 5 ) ) ) ) ;141 po ly r5=O(NEG(1+x ( 1 7 ) ∗ x ( 1 8 ) ∗ x ( 1 9 ) ) ,O(NEG( x ( 2 4 ) ) , x ( 5 ) ) ) ;142 po ly r6=O(NEG(1+x ( 1 7 ) ∗ x ( 1 8 ) ∗ x ( 1 9 ) ) , x ( 4 8 ) ) ;143 po ly r7=O(NEG( x ( 1 9 ) ) , x ( 4 8 ) ) ;144 po ly r8=O(NEG( x ( 1 7 ) ) ,O(NEG(1+x ( 2 6 ) ∗ x ( 2 7 ) ∗ x ( 2 8 ) ∗ x ( 2 9 ) ) , x ( 1 1 ) ) ) ;

92

Page 103: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

145 po ly r9=O(NEG( x ( 1 7 ) ) ,O(NEG( x ( 2 7 ) ) , x ( 5 ) ) ) ;146 po ly r10=O(NEG( x ( 1 7 ) ) ,O(NEG( x ( 2 6 ) ) ,O( x ( 6 ) ,O( x ( 7 ) ,1+ x ( 4 ) ∗ x ( 5 ) ∗ x ( 6 ) ∗ x

( 7 ) ∗ x ( 8 ) ) ) ) ) ;147 po ly r11=O(NEG( x ( 1 7 ) ) ,O(NEG( x ( 2 8 ) ) , x ( 6 ) ) ) ;148 po ly r12=O(NEG( x ( 1 7 ) ) ,O(NEG( x ( 2 8 ) ) , x ( 5 5 ) ) ) ;149 po ly r13=O(NEG( x ( 2 9 ) ) , x ( 5 5 ) ) ;150 po ly r14=O(NEG( x ( 1 7 ) ) ,O(NEG( x ( 2 7 ) ) , x ( 4 8 ) ) ) ;151 po ly r15=O(NEG( x ( 2 6 ) ) , x ( 4 8 ) ) ;152 po ly r16=O(NEG( x ( 1 8 ) ) , x ( 4 9 ) ) ;153 po ly r17=O(NEG( x ( 1 8 ) ) ,O(NEG(1+x ( 3 3 ) ∗ x ( 3 4 ) ∗ x ( 3 5 ) ) , x ( 1 4 ) ) ) ;154 po ly r18=O(NEG( x ( 1 8 ) ) ,O(NEG( x ( 3 3 ) ) , x ( 5 0 ) ) ) ;155 po ly r19=O(NEG( x ( 1 8 ) ) ,O(NEG( x ( 3 4 ) ) , x ( 5 1 ) ) ) ;156 po ly r20=O(NEG( x ( 1 8 ) ) ,O(NEG( x ( 3 5 ) ) , x ( 5 2 ) ) ) ;157 po ly r21=O(NEG( x ( 4 8 ) ) ,O(NEG(1+x ( 4 3 ) ) , x ( 1 ) ) ) ;158 po ly r22=O(NEG( x ( 4 8 ) ) ,O(NEG( x ( 4 3 ) ) , x ( 5 3 ) ) ) ;159 po ly r23=O(NEG( x ( 5 0 ) ) , x ( 5 3 ) ) ;160 po ly r24=O(NEG( x ( 5 1 ) ) ,O(NEG(1+x ( 3 9 ) ∗ x ( 4 0 ) ) , x ( 1 6 ) ) ) ;161 po ly r25=O(NEG( x ( 5 1 ) ) ,O(O(NEG( x ( 3 9 ) ) ,NEG(1+x ( 4 4 ) ) ) , x ( 3 ) ) ) ;162 po ly r26=O(NEG( x ( 5 1 ) ) ,O(NEG( x ( 4 0 ) ) , x ( 0 ) ) ) ;163 po ly r27=O(NEG( x ( 5 2 ) ) , x ( 5 5 ) ) ;164 po ly r28=O(NEG( x ( 5 3 ) ) ,O(O(NEG( x ( 4 1 ) ) ,NEG(1+x ( 3 0 ) ∗ x ( 3 1 ) ) ) , x ( 1 2 ) ) ) ;165 po ly r29=O(NEG( x ( 5 3 ) ) ,O(O(NEG( x ( 4 1 ) ) ,NEG( x ( 3 1 ) ) ) , x ( 1 3 ) ) ) ;166 po ly r30=O(NEG( x ( 5 3 ) ) ,O(O(NEG( x ( 4 1 ) ) ,O(NEG( x ( 3 1 ) ) ,NEG(1+x ( 3 2 ) ) ) ) ,1+ x

( 9 ) ∗ x ( 1 0 ) ∗ x ( 1 1 ) ∗ x ( 1 2 ) ∗ x ( 1 3 ) ∗ x ( 1 4 ) ∗ x ( 1 5 ) ∗ x ( 1 6 ) ) ) ;

93

Page 104: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

167 po ly r31=O(NEG( x ( 5 3 ) ) ,O(O(NEG( x ( 4 1 ) ) ,O(NEG( x ( 3 1 ) ) ,O(NEG(1+x ( 3 2 ) ) ,NEG(x ( 3 2 ) ) ) ) ) , x ( 5 4 ) ) ) ;

168 po ly r32=O(NEG( x ( 5 3 ) ) ,O(O(NEG( x ( 4 1 ) ) ,NEG( x ( 3 0 ) ) ) , x ( 5 4 ) ) ) ;169 po ly r33=O(NEG( x ( 5 3 ) ) ,O(NEG( x ( 4 2 ) ) , x ( 5 4 ) ) ) ;170 po ly r34=O(NEG( x ( 5 4 ) ) , x ( 5 5 ) ) ;171 po ly r35=O(NEG( x ( 5 5 ) ) , x ( 0 ) ) ;172 i d e a l K=ic0 , i c1 , i c2 , i c3 , i c4 , i c5 , i c6 , i c7 , i c8 , i c9 , i c10 , i c11 , i c12 , i c13 ,

i c14 , i c15 , i c16 , i c17 , i c18 , i c19 , i c20 , i c21 , i c22 , i c23 , i c24 , i c25 , i c26 ,i c27 , i c28 , i c29 , i c30 , i c31 , i c32 , i c33 , i c34 , i c35 , i c36 , i c37 , i c38 , i c39 ,i c40 , i c41 , i c42 , i c43 , i c44 , i c45 , i c46 , i c47 , i c48 , i c49 , i c50 , i c51 , i c52 ,i c53 , i c54 , i c55 , i c56 , i c57 , i c58 , i c59 , i c60 , i c61 , i c62 , i c63 , i c64 , i c65 ,i c66 , i c67 , r0 , r1 , r2 , r3 , r4 , r5 , r6 , r7 , r8 , r9 , r10 , r11 , r12 , r13 , r14 , r15 ,r16 , r17 , r18 , r19 , r20 , r21 , r22 , r23 , r24 , r25 , r26 , r27 , r28 , r29 , r30 , r31 ,r32 , r33 , r34 , r35 ;

173174 i d e a l BASE=s l imgb ( J+K) ;

7.3.3. Traduccion en CoCoA

1 USE Z / ( 2 ) [ x [ 0 . . 5 5 ] ] ;23 S0 :=( x [ 0 ] ˆ 2 ) + x [ 0 ] ;4 S1 :=( x [ 1 ] ˆ 2 ) + x [ 1 ] ;5 S2 :=( x [ 2 ] ˆ 2 ) + x [ 2 ] ;6 S3 :=( x [ 3 ] ˆ 2 ) + x [ 3 ] ;

94

Page 105: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

7 S4 :=( x [ 4 ] ˆ 2 ) + x [ 4 ] ;8 S5 :=( x [ 5 ] ˆ 2 ) + x [ 5 ] ;9 S6 :=( x [ 6 ] ˆ 2 ) + x [ 6 ] ;

10 S7 :=( x [ 7 ] ˆ 2 ) + x [ 7 ] ;11 S8 :=( x [ 8 ] ˆ 2 ) + x [ 8 ] ;12 S9 :=( x [ 9 ] ˆ 2 ) + x [ 9 ] ;13 S10 :=( x [ 1 0 ] ˆ 2 ) + x [ 1 0 ] ;14 S11 :=( x [ 1 1 ] ˆ 2 ) + x [ 1 1 ] ;15 S12 :=( x [ 1 2 ] ˆ 2 ) + x [ 1 2 ] ;16 S13 :=( x [ 1 3 ] ˆ 2 ) + x [ 1 3 ] ;17 S14 :=( x [ 1 4 ] ˆ 2 ) + x [ 1 4 ] ;18 S15 :=( x [ 1 5 ] ˆ 2 ) + x [ 1 5 ] ;19 S16 :=( x [ 1 6 ] ˆ 2 ) + x [ 1 6 ] ;20 S17 :=( x [ 1 7 ] ˆ 2 ) + x [ 1 7 ] ;21 S18 :=( x [ 1 8 ] ˆ 2 ) + x [ 1 8 ] ;22 S19 :=( x [ 1 9 ] ˆ 2 ) + x [ 1 9 ] ;23 S20 :=( x [ 2 0 ] ˆ 2 ) + x [ 2 0 ] ;24 S21 :=( x [ 2 1 ] ˆ 2 ) + x [ 2 1 ] ;25 S22 :=( x [ 2 2 ] ˆ 2 ) + x [ 2 2 ] ;26 S23 :=( x [ 2 3 ] ˆ 2 ) + x [ 2 3 ] ;27 S24 :=( x [ 2 4 ] ˆ 2 ) + x [ 2 4 ] ;28 S25 :=( x [ 2 5 ] ˆ 2 ) + x [ 2 5 ] ;29 S26 :=( x [ 2 6 ] ˆ 2 ) + x [ 2 6 ] ;30 S27 :=( x [ 2 7 ] ˆ 2 ) + x [ 2 7 ] ;31 S28 :=( x [ 2 8 ] ˆ 2 ) + x [ 2 8 ] ;

95

Page 106: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

32 S29 :=( x [ 2 9 ] ˆ 2 ) + x [ 2 9 ] ;33 S30 :=( x [ 3 0 ] ˆ 2 ) + x [ 3 0 ] ;34 S31 :=( x [ 3 1 ] ˆ 2 ) + x [ 3 1 ] ;35 S32 :=( x [ 3 2 ] ˆ 2 ) + x [ 3 2 ] ;36 S33 :=( x [ 3 3 ] ˆ 2 ) + x [ 3 3 ] ;37 S34 :=( x [ 3 4 ] ˆ 2 ) + x [ 3 4 ] ;38 S35 :=( x [ 3 5 ] ˆ 2 ) + x [ 3 5 ] ;39 S36 :=( x [ 3 6 ] ˆ 2 ) + x [ 3 6 ] ;40 S37 :=( x [ 3 7 ] ˆ 2 ) + x [ 3 7 ] ;41 S38 :=( x [ 3 8 ] ˆ 2 ) + x [ 3 8 ] ;42 S39 :=( x [ 3 9 ] ˆ 2 ) + x [ 3 9 ] ;43 S40 :=( x [ 4 0 ] ˆ 2 ) + x [ 4 0 ] ;44 S41 :=( x [ 4 1 ] ˆ 2 ) + x [ 4 1 ] ;45 S42 :=( x [ 4 2 ] ˆ 2 ) + x [ 4 2 ] ;46 S43 :=( x [ 4 3 ] ˆ 2 ) + x [ 4 3 ] ;47 S44 :=( x [ 4 4 ] ˆ 2 ) + x [ 4 4 ] ;48 S45 :=( x [ 4 5 ] ˆ 2 ) + x [ 4 5 ] ;49 S46 :=( x [ 4 6 ] ˆ 2 ) + x [ 4 6 ] ;50 S47 :=( x [ 4 7 ] ˆ 2 ) + x [ 4 7 ] ;51 S48 :=( x [ 4 8 ] ˆ 2 ) + x [ 4 8 ] ;52 S49 :=( x [ 4 9 ] ˆ 2 ) + x [ 4 9 ] ;53 S50 :=( x [ 5 0 ] ˆ 2 ) + x [ 5 0 ] ;54 S51 :=( x [ 5 1 ] ˆ 2 ) + x [ 5 1 ] ;55 S52 :=( x [ 5 2 ] ˆ 2 ) + x [ 5 2 ] ;56 S53 :=( x [ 5 3 ] ˆ 2 ) + x [ 5 3 ] ;

96

Page 107: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

57 S54 :=( x [ 5 4 ] ˆ 2 ) + x [ 5 4 ] ;58 S55 :=( x [ 5 5 ] ˆ 2 ) + x [ 5 5 ] ;5960 MEMORY. J := I d e a l ( S0 , S1 , S2 , S3 , S4 , S5 , S6 , S7 , S8 , S9 , S10 , S11 , S12 , S13 , S14 , S15

, S16 , S17 , S18 , S19 , S20 , S21 , S22 , S23 , S24 , S25 , S26 , S27 , S28 , S29 , S30 , S31 ,S32 , S33 , S34 , S35 , S36 , S37 , S38 , S39 , S40 , S41 , S42 , S43 , S44 , S45 , S46 , S47 ,S48 , S49 , S50 , S51 , S52 , S53 , S54 , S55 ) ;

6162 De f i ne NEG(M) RETURN NF(1+M,MEMORY. J ) ; EndDef ine ;63 De f i ne O(M,N) RETURN NF(M∗N,MEMORY. J ) ; EndDef ine ;64 De f i ne Y (M,N) RETURN (NEG(O(NEG(M) ,NEG(N) ) ) ) ; EndDef ine ;6566 IC0 :=NEG( Y ( x [ 0 ] , x [ 1 ] ) ) ;67 IC1 :=NEG( Y ( x [ 0 ] , x [ 2 ] ) ) ;68 IC2 :=NEG( Y ( x [ 0 ] , x [ 3 ] ) ) ;69 IC3 :=NEG( Y ( x [ 1 ] , x [ 2 ] ) ) ;70 IC4 :=NEG( Y ( x [ 1 ] , x [ 3 ] ) ) ;71 IC5 :=NEG( Y ( x [ 2 ] , x [ 3 ] ) ) ;72 IC6 :=NEG( Y ( x [ 4 ] , x [ 5 ] ) ) ;73 IC7 :=NEG( Y ( x [ 4 ] , x [ 6 ] ) ) ;74 IC8 :=NEG( Y ( x [ 4 ] , x [ 7 ] ) ) ;75 IC9 :=NEG( Y ( x [ 4 ] , x [ 8 ] ) ) ;76 IC10 :=NEG( Y ( x [ 5 ] , x [ 6 ] ) ) ;77 IC11 :=NEG( Y ( x [ 5 ] , x [ 7 ] ) ) ;78 IC12 :=NEG( Y ( x [ 5 ] , x [ 8 ] ) ) ;

97

Page 108: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

79 IC13 :=NEG( Y ( x [ 6 ] , x [ 7 ] ) ) ;80 IC14 :=NEG( Y ( x [ 6 ] , x [ 8 ] ) ) ;81 IC15 :=NEG( Y ( x [ 7 ] , x [ 8 ] ) ) ;82 IC16 :=NEG( Y ( x [ 9 ] , x [ 1 0 ] ) ) ;83 IC17 :=NEG( Y ( x [ 9 ] , x [ 1 1 ] ) ) ;84 IC18 :=NEG( Y ( x [ 9 ] , x [ 1 2 ] ) ) ;85 IC19 :=NEG( Y ( x [ 9 ] , x [ 1 3 ] ) ) ;86 IC20 :=NEG( Y ( x [ 9 ] , x [ 1 4 ] ) ) ;87 IC21 :=NEG( Y ( x [ 9 ] , x [ 1 5 ] ) ) ;88 IC22 :=NEG( Y ( x [ 9 ] , x [ 1 6 ] ) ) ;89 IC23 :=NEG( Y ( x [ 1 0 ] , x [ 1 1 ] ) ) ;90 IC24 :=NEG( Y ( x [ 1 0 ] , x [ 1 2 ] ) ) ;91 IC25 :=NEG( Y ( x [ 1 0 ] , x [ 1 3 ] ) ) ;92 IC26 :=NEG( Y ( x [ 1 0 ] , x [ 1 4 ] ) ) ;93 IC27 :=NEG( Y ( x [ 1 0 ] , x [ 1 5 ] ) ) ;94 IC28 :=NEG( Y ( x [ 1 0 ] , x [ 1 6 ] ) ) ;95 IC29 :=NEG( Y ( x [ 1 1 ] , x [ 1 2 ] ) ) ;96 IC30 :=NEG( Y ( x [ 1 1 ] , x [ 1 3 ] ) ) ;97 IC31 :=NEG( Y ( x [ 1 1 ] , x [ 1 4 ] ) ) ;98 IC32 :=NEG( Y ( x [ 1 1 ] , x [ 1 5 ] ) ) ;99 IC33 :=NEG( Y ( x [ 1 1 ] , x [ 1 6 ] ) ) ;

100 IC34 :=NEG( Y ( x [ 1 2 ] , x [ 1 3 ] ) ) ;101 IC35 :=NEG( Y ( x [ 1 2 ] , x [ 1 4 ] ) ) ;102 IC36 :=NEG( Y ( x [ 1 2 ] , x [ 1 5 ] ) ) ;103 IC37 :=NEG( Y ( x [ 1 2 ] , x [ 1 6 ] ) ) ;

98

Page 109: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

104 IC38 :=NEG( Y ( x [ 1 3 ] , x [ 1 4 ] ) ) ;105 IC39 :=NEG( Y ( x [ 1 3 ] , x [ 1 5 ] ) ) ;106 IC40 :=NEG( Y ( x [ 1 3 ] , x [ 1 6 ] ) ) ;107 IC41 :=NEG( Y ( x [ 1 4 ] , x [ 1 5 ] ) ) ;108 IC42 :=NEG( Y ( x [ 1 4 ] , x [ 1 6 ] ) ) ;109 IC43 :=NEG( Y ( x [ 1 5 ] , x [ 1 6 ] ) ) ;110 IC44 :=NEG( Y ( x [ 1 7 ] , x [ 1 8 ] ) ) ;111 IC45 :=NEG( Y ( x [ 1 7 ] , x [ 1 9 ] ) ) ;112 IC46 :=NEG( Y ( x [ 1 8 ] , x [ 1 9 ] ) ) ;113 IC47 :=NEG( Y ( x [ 2 0 ] , x [ 2 1 ] ) ) ;114 IC48 :=NEG( Y ( x [ 2 0 ] , x [ 2 2 ] ) ) ;115 IC49 :=NEG( Y ( x [ 2 1 ] , x [ 2 2 ] ) ) ;116 IC50 :=NEG( Y ( x [ 2 3 ] , x [ 2 4 ] ) ) ;117 IC51 :=NEG( Y ( x [ 2 3 ] , x [ 2 5 ] ) ) ;118 IC52 :=NEG( Y ( x [ 2 4 ] , x [ 2 5 ] ) ) ;119 IC53 :=NEG( Y ( x [ 2 6 ] , x [ 2 7 ] ) ) ;120 IC54 :=NEG( Y ( x [ 2 6 ] , x [ 2 8 ] ) ) ;121 IC55 :=NEG( Y ( x [ 2 6 ] , x [ 2 9 ] ) ) ;122 IC56 :=NEG( Y ( x [ 2 7 ] , x [ 2 8 ] ) ) ;123 IC57 :=NEG( Y ( x [ 2 7 ] , x [ 2 9 ] ) ) ;124 IC58 :=NEG( Y ( x [ 2 8 ] , x [ 2 9 ] ) ) ;125 IC59 :=NEG( Y ( x [ 3 0 ] , x [ 3 1 ] ) ) ;126 IC60 :=NEG( Y ( x [ 3 3 ] , x [ 3 4 ] ) ) ;127 IC61 :=NEG( Y ( x [ 3 3 ] , x [ 3 5 ] ) ) ;128 IC62 :=NEG( Y ( x [ 3 4 ] , x [ 3 5 ] ) ) ;

99

Page 110: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

129 IC63 :=NEG( Y ( x [ 3 6 ] , x [ 3 7 ] ) ) ;130 IC64 :=NEG( Y ( x [ 3 6 ] , x [ 3 8 ] ) ) ;131 IC65 :=NEG( Y ( x [ 3 7 ] , x [ 3 8 ] ) ) ;132 IC66 :=NEG( Y ( x [ 3 9 ] , x [ 4 0 ] ) ) ;133 IC67 :=NEG( Y ( x [ 4 1 ] , x [ 4 2 ] ) ) ;134135 R0:=O(NEG( x [ 1 9 ] ) ,O(NEG(1+x [ 2 0 ] ∗ x [ 2 1 ] ∗ x [ 2 2 ] ) , x [ 9 ] ) ) ;136 R1:=O(NEG( x [ 1 9 ] ) ,O(NEG( x [ 2 0 ] ) , x [ 4 ] ) ) ;137 R2:=O(NEG( x [ 1 9 ] ) ,O(NEG( x [ 2 1 ] ) , x [ 5 ] ) ) ;138 R3:=O(NEG(1+x [ 1 7 ] ∗ x [ 1 8 ] ∗ x [ 1 9 ] ) ,O(NEG(1+x [ 2 3 ] ∗ x [ 2 4 ] ∗ x [ 2 5 ] ) , x [ 1 0 ] ) ) ;139 R4:=O(NEG(1+x [ 1 7 ] ∗ x [ 1 8 ] ∗ x [ 1 9 ] ) ,O(NEG( x [ 2 3 ] ) ,O( x [ 4 ] , x [ 5 ] ) ) ) ;140 R5:=O(NEG(1+x [ 1 7 ] ∗ x [ 1 8 ] ∗ x [ 1 9 ] ) ,O(NEG( x [ 2 4 ] ) , x [ 5 ] ) ) ;141 R6:=O(NEG(1+x [ 1 7 ] ∗ x [ 1 8 ] ∗ x [ 1 9 ] ) , x [ 4 8 ] ) ;142 R7:=O(NEG( x [ 1 9 ] ) , x [ 4 8 ] ) ;143 R8:=O(NEG( x [ 1 7 ] ) ,O(NEG(1+x [ 2 6 ] ∗ x [ 2 7 ] ∗ x [ 2 8 ] ∗ x [ 2 9 ] ) , x [ 1 1 ] ) ) ;144 R9:=O(NEG( x [ 1 7 ] ) ,O(NEG( x [ 2 7 ] ) , x [ 5 ] ) ) ;145 R10:=O(NEG( x [ 1 7 ] ) ,O(NEG( x [ 2 6 ] ) ,O( x [ 6 ] , O( x [ 7 ] , 1+ x [ 4 ] ∗ x [ 5 ] ∗ x [ 6 ] ∗ x [ 7 ] ∗ x

[ 8 ] ) ) ) ) ;146 R11:=O(NEG( x [ 1 7 ] ) ,O(NEG( x [ 2 8 ] ) , x [ 6 ] ) ) ;147 R12:=O(NEG( x [ 1 7 ] ) ,O(NEG( x [ 2 8 ] ) , x [ 5 5 ] ) ) ;148 R13:=O(NEG( x [ 2 9 ] ) , x [ 5 5 ] ) ;149 R14:=O(NEG( x [ 1 7 ] ) ,O(NEG( x [ 2 7 ] ) , x [ 4 8 ] ) ) ;150 R15:=O(NEG( x [ 2 6 ] ) , x [ 4 8 ] ) ;151 R16:=O(NEG( x [ 1 8 ] ) , x [ 4 9 ] ) ;152 R17:=O(NEG( x [ 1 8 ] ) ,O(NEG(1+x [ 3 3 ] ∗ x [ 3 4 ] ∗ x [ 3 5 ] ) , x [ 1 4 ] ) ) ;

100

Page 111: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

153 R18:=O(NEG( x [ 1 8 ] ) ,O(NEG( x [ 3 3 ] ) , x [ 5 0 ] ) ) ;154 R19:=O(NEG( x [ 1 8 ] ) ,O(NEG( x [ 3 4 ] ) , x [ 5 1 ] ) ) ;155 R20:=O(NEG( x [ 1 8 ] ) ,O(NEG( x [ 3 5 ] ) , x [ 5 2 ] ) ) ;156 R21:=O(NEG( x [ 4 8 ] ) ,O(NEG(1+x [ 4 3 ] ) , x [ 1 ] ) ) ;157 R22:=O(NEG( x [ 4 8 ] ) ,O(NEG( x [ 4 3 ] ) , x [ 5 3 ] ) ) ;158 R23:=O(NEG( x [ 5 0 ] ) , x [ 5 3 ] ) ;159 R24:=O(NEG( x [ 5 1 ] ) ,O(NEG(1+x [ 3 9 ] ∗ x [ 4 0 ] ) , x [ 1 6 ] ) ) ;160 R25:=O(NEG( x [ 5 1 ] ) ,O(O(NEG( x [ 3 9 ] ) ,NEG(1+x [ 4 4 ] ) ) , x [ 3 ] ) ) ;161 R26:=O(NEG( x [ 5 1 ] ) ,O(NEG( x [ 4 0 ] ) , x [ 0 ] ) ) ;162 R27:=O(NEG( x [ 5 2 ] ) , x [ 5 5 ] ) ;163 R28:=O(NEG( x [ 5 3 ] ) ,O(O(NEG( x [ 4 1 ] ) ,NEG(1+x [ 3 0 ] ∗ x [ 3 1 ] ) ) , x [ 1 2 ] ) ) ;164 R29:=O(NEG( x [ 5 3 ] ) ,O(O(NEG( x [ 4 1 ] ) ,NEG( x [ 3 1 ] ) ) , x [ 1 3 ] ) ) ;165 R30:=O(NEG( x [ 5 3 ] ) ,O(O(NEG( x [ 4 1 ] ) ,O(NEG( x [ 3 1 ] ) ,NEG(1+x [ 3 2 ] ) ) ) ,1+ x [ 9 ] ∗ x

[ 1 0 ] ∗ x [ 1 1 ] ∗ x [ 1 2 ] ∗ x [ 1 3 ] ∗ x [ 1 4 ] ∗ x [ 1 5 ] ∗ x [ 1 6 ] ) ) ;166 R31:=O(NEG( x [ 5 3 ] ) ,O(O(NEG( x [ 4 1 ] ) ,O(NEG( x [ 3 1 ] ) ,O(NEG(1+x [ 3 2 ] ) ,NEG( x

[ 3 2 ] ) ) ) ) , x [ 5 4 ] ) ) ;167 R32:=O(NEG( x [ 5 3 ] ) ,O(O(NEG( x [ 4 1 ] ) ,NEG( x [ 3 0 ] ) ) , x [ 5 4 ] ) ) ;168 R33:=O(NEG( x [ 5 3 ] ) ,O(NEG( x [ 4 2 ] ) , x [ 5 4 ] ) ) ;169 R34:=O(NEG( x [ 5 4 ] ) , x [ 5 5 ] ) ;170 R35:=O(NEG( x [ 5 5 ] ) , x [ 0 ] ) ;171 K:= I d e a l ( IC0 , IC1 , IC2 , IC3 , IC4 , IC5 , IC6 , IC7 , IC8 , IC9 , IC10 , IC11 , IC12 , IC13 ,

IC14 , IC15 , IC16 , IC17 , IC18 , IC19 , IC20 , IC21 , IC22 , IC23 , IC24 , IC25 , IC26 ,IC27 , IC28 , IC29 , IC30 , IC31 , IC32 , IC33 , IC34 , IC35 , IC36 , IC37 , IC38 , IC39 ,IC40 , IC41 , IC42 , IC43 , IC44 , IC45 , IC46 , IC47 , IC48 , IC49 , IC50 , IC51 , IC52 ,IC53 , IC54 , IC55 , IC56 , IC57 , IC58 , IC59 , IC60 , IC61 , IC62 , IC63 , IC64 , IC65 ,

101

Page 112: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

IC66 , IC67 , R0 , R1 , R2 , R3 , R4 , R5 , R6 , R7 , R8 , R9 , R10 , R11 , R12 , R13 , R14 , R15 ,R16 , R17 , R18 , R19 , R20 , R21 , R22 , R23 , R24 , R25 , R26 , R27 , R28 , R29 , R30 , R31 ,R32 , R33 , R34 , R35 ) ;

172173 GBasis (MEMORY. J+K) ;

7.4. Resultados en el modelo ‘Concepto-Atributo-Valor’

Una vez descrito el sistema experto en nuestro lenguaje podemos obtener un sistemaexperto basado en el modelo ‘Concepto-Atributo-Valor’ para los sistemas de computacionalgebraica: CoCoA o Singular.7.4.1. Traduccion en Singular

1 r i n g r =11 , ( x ( 0 . . 2 5 ) ) , l p ;23 po ly s0=( x ( 0 ) −4) ∗ ( x ( 0 ) −3) ∗ ( x ( 0 ) −2) ∗ ( x ( 0 ) −1) ∗ ( x ( 0 ) −0) ;4 po ly s1=( x ( 1 ) −5) ∗ ( x ( 1 ) −4) ∗ ( x ( 1 ) −3) ∗ ( x ( 1 ) −2) ∗ ( x ( 1 ) −1) ∗ ( x ( 1 )

−0) ;5 po ly s2=( x ( 2 ) −8) ∗ ( x ( 2 ) −7) ∗ ( x ( 2 ) −6) ∗ ( x ( 2 ) −5) ∗ ( x ( 2 ) −4) ∗ ( x ( 2 )

−3) ∗ ( x ( 2 ) −2) ∗ ( x ( 2 ) −1) ∗ ( x ( 2 ) −0) ;6 po ly s3=( x ( 3 ) −3) ∗ ( x ( 3 ) −2) ∗ ( x ( 3 ) −1) ∗ ( x ( 3 ) −0) ;7 po ly s4=( x ( 4 ) −3) ∗ ( x ( 4 ) −2) ∗ ( x ( 4 ) −1) ∗ ( x ( 4 ) −0) ;8 po ly s5=( x ( 5 ) −3) ∗ ( x ( 5 ) −2) ∗ ( x ( 5 ) −1) ∗ ( x ( 5 ) −0) ;9 po ly s6=( x ( 6 ) −4) ∗ ( x ( 6 ) −3) ∗ ( x ( 6 ) −2) ∗ ( x ( 6 ) −1) ∗ ( x ( 6 ) −0) ;

10 po ly s7=( x ( 7 ) −2) ∗ ( x ( 7 ) −1) ∗ ( x ( 7 ) −0) ;102

Page 113: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

11 po ly s8=( x ( 8 ) −1) ∗ ( x ( 8 ) −0) ;12 po ly s9=( x ( 9 ) −3) ∗ ( x ( 9 ) −2) ∗ ( x ( 9 ) −1) ∗ ( x ( 9 ) −0) ;13 po ly s10=( x ( 1 0 ) −3) ∗ ( x ( 1 0 ) −2) ∗ ( x ( 1 0 ) −1) ∗ ( x ( 1 0 ) −0) ;14 po ly s11=( x ( 1 1 ) −2) ∗ ( x ( 1 1 ) −1) ∗ ( x ( 1 1 ) −0) ;15 po ly s12=( x ( 1 2 ) −2) ∗ ( x ( 1 2 ) −1) ∗ ( x ( 1 2 ) −0) ;16 po ly s13=( x ( 1 3 ) −1) ∗ ( x ( 1 3 ) −0) ;17 po ly s14=( x ( 1 4 ) −1) ∗ ( x ( 1 4 ) −0) ;18 po ly s15=( x ( 1 5 ) −1) ∗ ( x ( 1 5 ) −0) ;19 po ly s16=( x ( 1 6 ) −1) ∗ ( x ( 1 6 ) −0) ;20 po ly s17=( x ( 1 7 ) −1) ∗ ( x ( 1 7 ) −0) ;21 po ly s18=( x ( 1 8 ) −1) ∗ ( x ( 1 8 ) −0) ;22 po ly s19=( x ( 1 9 ) −1) ∗ ( x ( 1 9 ) −0) ;23 po ly s20=( x ( 2 0 ) −1) ∗ ( x ( 2 0 ) −0) ;24 po ly s21=( x ( 2 1 ) −1) ∗ ( x ( 2 1 ) −0) ;25 po ly s22=( x ( 2 2 ) −1) ∗ ( x ( 2 2 ) −0) ;26 po ly s23=( x ( 2 3 ) −1) ∗ ( x ( 2 3 ) −0) ;27 po ly s24=( x ( 2 4 ) −1) ∗ ( x ( 2 4 ) −0) ;28 po ly s25=( x ( 2 5 ) −1) ∗ ( x ( 2 5 ) −0) ;2930 i d e a l J=s0 , s1 , s2 , s3 , s4 , s5 , s6 , s7 , s8 , s9 , s10 , s11 , s12 , s13 , s14 , s15 , s16 , s17

, s18 , s19 , s20 , s21 , s22 , s23 , s24 , s25 ;31 J = s t d ( J ) ;3233 p ro c NEG( po ly M) { r e t u r n ( r educe (Mˆ10−1 , J ) ) ; }34 p ro c O( po ly M, po ly N) { r e t u r n ( r educe (M∗N, J ) ) ; }

103

Page 114: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

3536 po ly r0=O(NEG( ( x ( 3 ) −2) ) ,O(NEG( ( x ( 4 ) −3) ) , ( x ( 2 ) −0) ) ) ;37 po ly r1=O(NEG( ( x ( 3 ) −2) ) ,O(NEG( ( x ( 4 ) −0) ) , ( x ( 1 ) −0) ) ) ;38 po ly r2=O(NEG( ( x ( 3 ) −2) ) ,O(NEG( ( x ( 4 ) −1) ) , ( x ( 1 ) −1) ) ) ;39 po ly r3=O(NEG( ( x ( 3 ) −3) ) ,O(NEG( ( x ( 5 ) −3) ) , ( x ( 2 ) −1) ) ) ;40 po ly r4=O(NEG( ( x ( 3 ) −3) ) ,O(NEG( ( x ( 5 ) −0) ) ,O ( ( x ( 1 ) −0) , ( x ( 1 ) −1) ) ) ) ;41 po ly r5=O(NEG( ( x ( 3 ) −3) ) ,O(NEG( ( x ( 5 ) −1) ) , ( x ( 1 ) −1) ) ) ;42 po ly r6=O(NEG( ( x ( 3 ) −3) ) , ( x ( 1 8 ) −0) ) ;43 po ly r7=O(NEG( ( x ( 3 ) −2) ) , ( x ( 1 8 ) −0) ) ;44 po ly r8=O(NEG( ( x ( 3 ) −0) ) ,O(NEG( ( x ( 6 ) −4) ) , ( x ( 2 ) −2) ) ) ;45 po ly r9=O(NEG( ( x ( 3 ) −0) ) ,O(NEG( ( x ( 6 ) −1) ) , ( x ( 1 ) −1) ) ) ;46 po ly r10=O(NEG( ( x ( 3 ) −0) ) ,O(NEG( ( x ( 6 ) −0) ) ,O ( ( x ( 1 ) −2) ,O ( ( x ( 1 ) −3) , ( x ( 1 )

−5) ) ) ) ) ;47 po ly r11=O(NEG( ( x ( 3 ) −0) ) ,O(NEG( ( x ( 6 ) −2) ) , ( x ( 1 ) −2) ) ) ;48 po ly r12=O(NEG( ( x ( 3 ) −0) ) ,O(NEG( ( x ( 6 ) −2) ) , ( x ( 2 5 ) −0) ) ) ;49 po ly r13=O(NEG( ( x ( 6 ) −3) ) , ( x ( 2 5 ) −0) ) ;50 po ly r14=O(NEG( ( x ( 3 ) −0) ) ,O(NEG( ( x ( 6 ) −1) ) , ( x ( 1 8 ) −0) ) ) ;51 po ly r15=O(NEG( ( x ( 6 ) −0) ) , ( x ( 1 8 ) −0) ) ;52 po ly r16=O(NEG( ( x ( 3 ) −1) ) , ( x ( 1 9 ) −0) ) ;53 po ly r17=O(NEG( ( x ( 3 ) −1) ) ,O(NEG( ( x ( 9 ) −3) ) , ( x ( 2 ) −5) ) ) ;54 po ly r18=O(NEG( ( x ( 3 ) −1) ) ,O(NEG( ( x ( 9 ) −0) ) , ( x ( 2 0 ) −0) ) ) ;55 po ly r19=O(NEG( ( x ( 3 ) −1) ) ,O(NEG( ( x ( 9 ) −1) ) , ( x ( 2 1 ) −0) ) ) ;56 po ly r20=O(NEG( ( x ( 3 ) −1) ) ,O(NEG( ( x ( 9 ) −2) ) , ( x ( 2 2 ) −0) ) ) ;57 po ly r21=O(NEG( ( x ( 1 8 ) −0) ) ,O(NEG( ( x ( 1 3 ) −1) ) , ( x ( 0 ) −1) ) ) ;58 po ly r22=O(NEG( ( x ( 1 8 ) −0) ) ,O(NEG( ( x ( 1 3 ) −0) ) , ( x ( 2 3 ) −0) ) ) ;

104

Page 115: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

59 po ly r23=O(NEG( ( x ( 2 0 ) −0) ) , ( x ( 2 3 ) −0) ) ;60 po ly r24=O(NEG( ( x ( 2 1 ) −0) ) ,O(NEG( ( x ( 1 1 ) −2) ) , ( x ( 2 ) −7) ) ) ;61 po ly r25=O(NEG( ( x ( 2 1 ) −0) ) ,O(O(NEG( ( x ( 1 1 ) −0) ) ,NEG( ( x ( 1 4 ) −1) ) ) , ( x ( 0 ) −3)

) ) ;62 po ly r26=O(NEG( ( x ( 2 1 ) −0) ) ,O(NEG( ( x ( 1 1 ) −1) ) , ( x ( 0 ) −0) ) ) ;63 po ly r27=O(NEG( ( x ( 2 2 ) −0) ) , ( x ( 2 5 ) −0) ) ;64 po ly r28=O(NEG( ( x ( 2 3 ) −0) ) ,O(O(NEG( ( x ( 1 2 ) −0) ) ,NEG( ( x ( 7 ) −2) ) ) , ( x ( 2 ) −3) )

) ;65 po ly r29=O(NEG( ( x ( 2 3 ) −0) ) ,O(O(NEG( ( x ( 1 2 ) −0) ) ,NEG( ( x ( 7 ) −1) ) ) , ( x ( 2 ) −4) )

) ;66 po ly r30=O(NEG( ( x ( 2 3 ) −0) ) ,O(O(NEG( ( x ( 1 2 ) −0) ) ,O(NEG( ( x ( 7 ) −1) ) ,NEG( ( x

( 8 ) −1) ) ) ) , ( x ( 2 ) −8) ) ) ;67 po ly r31=O(NEG( ( x ( 2 3 ) −0) ) ,O(O(NEG( ( x ( 1 2 ) −0) ) ,O(NEG( ( x ( 7 ) −1) ) ,O(NEG( ( x

( 8 ) −1) ) ,NEG( ( x ( 8 ) −0) ) ) ) ) , ( x ( 2 4 ) −0) ) ) ;68 po ly r32=O(NEG( ( x ( 2 3 ) −0) ) ,O(O(NEG( ( x ( 1 2 ) −0) ) ,NEG( ( x ( 7 ) −0) ) ) , ( x ( 2 4 ) −0)

) ) ;69 po ly r33=O(NEG( ( x ( 2 3 ) −0) ) ,O(NEG( ( x ( 1 2 ) −1) ) , ( x ( 2 4 ) −0) ) ) ;70 po ly r34=O(NEG( ( x ( 2 4 ) −0) ) , ( x ( 2 5 ) −0) ) ;71 po ly r35=O(NEG( ( x ( 2 5 ) −0) ) , ( x ( 0 ) −0) ) ;7273 i d e a l K=r0 , r1 , r2 , r3 , r4 , r5 , r6 , r7 , r8 , r9 , r10 , r11 , r12 , r13 , r14 , r15 , r16 , r17

, r18 , r19 , r20 , r21 , r22 , r23 , r24 , r25 , r26 , r27 , r28 , r29 , r30 , r31 , r32 , r33 ,r34 , r35 ;

7475 i d e a l BASE=s l imgb ( J+K) ;

105

Page 116: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

7.4.2. Traduccion en CoCoA

1 USE Z / ( 1 1 ) [ x [ 0 . . 2 5 ] ] ;23 S0 :=( x [ 0 ] −4 ) ∗ ( x [ 0 ] −3 ) ∗ ( x [ 0 ] −2 ) ∗ ( x [ 0 ] −1 ) ∗ ( x [ 0 ] −0 ) ;4 S1 :=( x [ 1 ] −5 ) ∗ ( x [ 1 ] −4 ) ∗ ( x [ 1 ] −3 ) ∗ ( x [ 1 ] −2 ) ∗ ( x [ 1 ] −1 ) ∗ ( x [ 1 ] −0 ) ;5 S2 :=( x [ 2 ] −8 ) ∗ ( x [ 2 ] −7 ) ∗ ( x [ 2 ] −6 ) ∗ ( x [ 2 ] −5 ) ∗ ( x [ 2 ] −4 ) ∗ ( x [ 2 ] −3 ) ∗

( x [ 2 ] −2 ) ∗ ( x [ 2 ] −1 ) ∗ ( x [ 2 ] −0 ) ;6 S3 :=( x [ 3 ] −3 ) ∗ ( x [ 3 ] −2 ) ∗ ( x [ 3 ] −1 ) ∗ ( x [ 3 ] −0 ) ;7 S4 :=( x [ 4 ] −3 ) ∗ ( x [ 4 ] −2 ) ∗ ( x [ 4 ] −1 ) ∗ ( x [ 4 ] −0 ) ;8 S5 :=( x [ 5 ] −3 ) ∗ ( x [ 5 ] −2 ) ∗ ( x [ 5 ] −1 ) ∗ ( x [ 5 ] −0 ) ;9 S6 :=( x [ 6 ] −4 ) ∗ ( x [ 6 ] −3 ) ∗ ( x [ 6 ] −2 ) ∗ ( x [ 6 ] −1 ) ∗ ( x [ 6 ] −0 ) ;

10 S7 :=( x [ 7 ] −2 ) ∗ ( x [ 7 ] −1 ) ∗ ( x [ 7 ] −0 ) ;11 S8 :=( x [ 8 ] −1 ) ∗ ( x [ 8 ] −0 ) ;12 S9 :=( x [ 9 ] −3 ) ∗ ( x [ 9 ] −2 ) ∗ ( x [ 9 ] −1 ) ∗ ( x [ 9 ] −0 ) ;13 S10 :=( x [ 1 0 ] −3 ) ∗ ( x [ 1 0 ] −2 ) ∗ ( x [ 1 0 ] −1 ) ∗ ( x [ 1 0 ] −0 ) ;14 S11 :=( x [ 1 1 ] −2 ) ∗ ( x [ 1 1 ] −1 ) ∗ ( x [ 1 1 ] −0 ) ;15 S12 :=( x [ 1 2 ] −2 ) ∗ ( x [ 1 2 ] −1 ) ∗ ( x [ 1 2 ] −0 ) ;16 S13 :=( x [ 1 3 ] −1 ) ∗ ( x [ 1 3 ] −0 ) ;17 S14 :=( x [ 1 4 ] −1 ) ∗ ( x [ 1 4 ] −0 ) ;18 S15 :=( x [ 1 5 ] −1 ) ∗ ( x [ 1 5 ] −0 ) ;19 S16 :=( x [ 1 6 ] −1 ) ∗ ( x [ 1 6 ] −0 ) ;20 S17 :=( x [ 1 7 ] −1 ) ∗ ( x [ 1 7 ] −0 ) ;21 S18 :=( x [ 1 8 ] −1 ) ∗ ( x [ 1 8 ] −0 ) ;22 S19 :=( x [ 1 9 ] −1 ) ∗ ( x [ 1 9 ] −0 ) ;

106

Page 117: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

23 S20 :=( x [ 2 0 ] −1 ) ∗ ( x [ 2 0 ] −0 ) ;24 S21 :=( x [ 2 1 ] −1 ) ∗ ( x [ 2 1 ] −0 ) ;25 S22 :=( x [ 2 2 ] −1 ) ∗ ( x [ 2 2 ] −0 ) ;26 S23 :=( x [ 2 3 ] −1 ) ∗ ( x [ 2 3 ] −0 ) ;27 S24 :=( x [ 2 4 ] −1 ) ∗ ( x [ 2 4 ] −0 ) ;28 S25 :=( x [ 2 5 ] −1 ) ∗ ( x [ 2 5 ] −0 ) ;293031 MEMORY. J := I d e a l ( S0 , S1 , S2 , S3 , S4 , S5 , S6 , S7 , S8 , S9 , S10 , S11 , S12 , S13 , S14 , S15

, S16 , S17 , S18 , S19 , S20 , S21 , S22 , S23 , S24 , S25 ) ;3233 De f i ne NEG(M) RETURN NF(Mˆ10−1 ,MEMORY. J ) ; EndDef ine ;34 De f i ne O(M,N) RETURN NF(M∗N,MEMORY. J ) ; EndDef ine ;3536 R0:=O(NEG( ( x [ 3 ] −2 ) ) ,O(NEG( ( x [ 4 ] −3 ) ) , ( x [ 2 ] −0 ) ) ) ;37 R1:=O(NEG( ( x [ 3 ] −2 ) ) ,O(NEG( ( x [ 4 ] −0 ) ) , ( x [ 1 ] −0 ) ) ) ;38 R2:=O(NEG( ( x [ 3 ] −2 ) ) ,O(NEG( ( x [ 4 ] −1 ) ) , ( x [ 1 ] −1 ) ) ) ;39 R3:=O(NEG( ( x [ 3 ] −3 ) ) ,O(NEG( ( x [ 5 ] −3 ) ) , ( x [ 2 ] −1 ) ) ) ;40 R4:=O(NEG( ( x [ 3 ] −3 ) ) ,O(NEG( ( x [ 5 ] −0 ) ) ,O ( ( x [ 1 ] −0 ) , ( x [ 1 ] −1 ) ) ) ) ;41 R5:=O(NEG( ( x [ 3 ] −3 ) ) ,O(NEG( ( x [ 5 ] −1 ) ) , ( x [ 1 ] −1 ) ) ) ;42 R6:=O(NEG( ( x [ 3 ] −3 ) ) , ( x [ 1 8 ] −0 ) ) ;43 R7:=O(NEG( ( x [ 3 ] −2 ) ) , ( x [ 1 8 ] −0 ) ) ;44 R8:=O(NEG( ( x [ 3 ] −0 ) ) ,O(NEG( ( x [ 6 ] −4 ) ) , ( x [ 2 ] −2 ) ) ) ;45 R9:=O(NEG( ( x [ 3 ] −0 ) ) ,O(NEG( ( x [ 6 ] −1 ) ) , ( x [ 1 ] −1 ) ) ) ;

107

Page 118: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

46 R10:=O(NEG( ( x [ 3 ] −0 ) ) ,O(NEG( ( x [ 6 ] −0 ) ) ,O ( ( x [ 1 ] −2 ) ,O ( ( x [ 1 ] −3 ) , ( x [ 1 ] −5 ) ) )) ) ;

47 R11:=O(NEG( ( x [ 3 ] −0 ) ) ,O(NEG( ( x [ 6 ] −2 ) ) , ( x [ 1 ] −2 ) ) ) ;48 R12:=O(NEG( ( x [ 3 ] −0 ) ) ,O(NEG( ( x [ 6 ] −2 ) ) , ( x [ 2 5 ] −0 ) ) ) ;49 R13:=O(NEG( ( x [ 6 ] −3 ) ) , ( x [ 2 5 ] −0 ) ) ;50 R14:=O(NEG( ( x [ 3 ] −0 ) ) ,O(NEG( ( x [ 6 ] −1 ) ) , ( x [ 1 8 ] −0 ) ) ) ;51 R15:=O(NEG( ( x [ 6 ] −0 ) ) , ( x [ 1 8 ] −0 ) ) ;52 R16:=O(NEG( ( x [ 3 ] −1 ) ) , ( x [ 1 9 ] −0 ) ) ;53 R17:=O(NEG( ( x [ 3 ] −1 ) ) ,O(NEG( ( x [ 9 ] −3 ) ) , ( x [ 2 ] −5 ) ) ) ;54 R18:=O(NEG( ( x [ 3 ] −1 ) ) ,O(NEG( ( x [ 9 ] −0 ) ) , ( x [ 2 0 ] −0 ) ) ) ;55 R19:=O(NEG( ( x [ 3 ] −1 ) ) ,O(NEG( ( x [ 9 ] −1 ) ) , ( x [ 2 1 ] −0 ) ) ) ;56 R20:=O(NEG( ( x [ 3 ] −1 ) ) ,O(NEG( ( x [ 9 ] −2 ) ) , ( x [ 2 2 ] −0 ) ) ) ;57 R21:=O(NEG( ( x [ 1 8 ] −0 ) ) ,O(NEG( ( x [ 1 3 ] −1 ) ) , ( x [ 0 ] −1 ) ) ) ;58 R22:=O(NEG( ( x [ 1 8 ] −0 ) ) ,O(NEG( ( x [ 1 3 ] −0 ) ) , ( x [ 2 3 ] −0 ) ) ) ;59 R23:=O(NEG( ( x [ 2 0 ] −0 ) ) , ( x [ 2 3 ] −0 ) ) ;60 R24:=O(NEG( ( x [ 2 1 ] −0 ) ) ,O(NEG( ( x [ 1 1 ] −2 ) ) , ( x [ 2 ] −7 ) ) ) ;61 R25:=O(NEG( ( x [ 2 1 ] −0 ) ) ,O(O(NEG( ( x [ 1 1 ] −0 ) ) ,NEG( ( x [ 1 4 ] −1 ) ) ) , ( x [ 0 ] −3 ) ) ) ;62 R26:=O(NEG( ( x [ 2 1 ] −0 ) ) ,O(NEG( ( x [ 1 1 ] −1 ) ) , ( x [ 0 ] −0 ) ) ) ;63 R27:=O(NEG( ( x [ 2 2 ] −0 ) ) , ( x [ 2 5 ] −0 ) ) ;64 R28:=O(NEG( ( x [ 2 3 ] −0 ) ) ,O(O(NEG( ( x [ 1 2 ] −0 ) ) ,NEG( ( x [ 7 ] −2 ) ) ) , ( x [ 2 ] −3 ) ) ) ;65 R29:=O(NEG( ( x [ 2 3 ] −0 ) ) ,O(O(NEG( ( x [ 1 2 ] −0 ) ) ,NEG( ( x [ 7 ] −1 ) ) ) , ( x [ 2 ] −4 ) ) ) ;66 R30:=O(NEG( ( x [ 2 3 ] −0 ) ) ,O(O(NEG( ( x [ 1 2 ] −0 ) ) ,O(NEG( ( x [ 7 ] −1 ) ) ,NEG( ( x [ 8 ] −1 )

) ) ) , ( x [ 2 ] −8 ) ) ) ;67 R31:=O(NEG( ( x [ 2 3 ] −0 ) ) ,O(O(NEG( ( x [ 1 2 ] −0 ) ) ,O(NEG( ( x [ 7 ] −1 ) ) ,O(NEG( ( x

[ 8 ] −1 ) ) ,NEG( ( x [ 8 ] −0 ) ) ) ) ) , ( x [ 2 4 ] −0 ) ) ) ;108

Page 119: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

68 R32:=O(NEG( ( x [ 2 3 ] −0 ) ) ,O(O(NEG( ( x [ 1 2 ] −0 ) ) ,NEG( ( x [ 7 ] −0 ) ) ) , ( x [ 2 4 ] −0 ) ) ) ;69 R33:=O(NEG( ( x [ 2 3 ] −0 ) ) ,O(NEG( ( x [ 1 2 ] −1 ) ) , ( x [ 2 4 ] −0 ) ) ) ;70 R34:=O(NEG( ( x [ 2 4 ] −0 ) ) , ( x [ 2 5 ] −0 ) ) ;71 R35:=O(NEG( ( x [ 2 5 ] −0 ) ) , ( x [ 0 ] −0 ) ) ;7273 K:= I d e a l ( R0 , R1 , R2 , R3 , R4 , R5 , R6 , R7 , R8 , R9 , R10 , R11 , R12 , R13 , R14 , R15 , R16 ,

R17 , R18 , R19 , R20 , R21 , R22 , R23 , R24 , R25 , R26 , R27 , R28 , R29 , R30 , R31 , R32 ,R33 , R34 , R35 ) ;

7475 GBasis (MEMORY. J+K) ;

109

Page 120: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

110

Page 121: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

8. Comparacion de modelos

En este trabajo, consideraremos diferentes modelos de representacion para implementarsistemas expertos en sistemas de computacion algebraica. Cada modelo de representaciontiene ventajas e inconvenientes. El modelo mas adecuado depende de la especificacion de laaplicacion y por lo tanto, debe ser elegido dependiendo de cada caso particular.

El modelo ‘Concepto-Atributo-Valor’ y el modelo basado en logica proposicional Booleana,pueden ser usados para representar conocimiento. Ambos modelos son equivalentes en elsentido de que cada sistema experto representado bajo un modelo puede ser representadobajo el otro modelo. El modelo ‘Concepto-Atributo-Valor’ presenta la ventaja sobre la logicaproposicional Booleana de que generalmente implica un menor numero de variables y un menornumero de formulas en la base de conocimiento por lo que se obtiene un sistema experto maspequeno. Sin embargo, los sistemas expertos implementados en logica proposicional puedenser implementados en un sistema de computacion algebraica, PolyBoRi, el cual realiza elcalculo muy rapido. En general, hemos observado que cuando un sistema experto trata conun gran numero de variables, las cuales pueden tomar un gran numero de valores posibles,el modelo ‘Concepto-Atributo-Valor’ es mas adecuado que el modelo logico proposicionalBooleano. Por el contrario, cuando un gran numero de variables solo pueden tomar dos posiblesvalores, la logica proposicional Booleana proporciona un mejor rendimiento.

Hemos disenado dos experimentos: uno que comprueba que un sistema expertodesarrollado en el modelo ‘Concepto-Atributo-Valor’ puede ser mejor que el mismodesarrollado en el modelo logico proposicional Booleano; y otro, que comprueba lo contrario,es decir, que un sistema experto desarrollado en el modelo logico proposicional Booleanopuede ser mejor que el mismo desarrollado en el modelo ‘Concepto-Atributo-Valor’.

111

Page 122: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

8.1. Experimento 1

Dado un numero natural, m, consideraremos un sistema experto con tresvariables,X1, X2, X3 de tal modo que cada variable puede tomar un valor de un conjunto de mposibles valores {v1, ..., vm}. La base de conocimiento de este sistema experto esta compuestopor tan solo una regla:

(X1 = X2)→ (X2 6= X3)Por ejemplo, si m = 6, especificaremos nuestro sistema experto en nuestro lenguaje de la

siguiente manera:BEGIN

BEGIN_VARS

X[3]: v1, v2, v3, v4, v5, v6;

END_VARS

BEGIN_KB

(X[1]=X[2]) -> (X[2]<>X[3] )

END_KB

END

Desde nuestro lenguaje hemos traducido el sistema experto en ambos enfoques algebraicos:Sentencias para nuestro sistema de computacion algebraica, PolyBoRi, de acuerdocon el enfoque basado en logica proposicional Booleana. Hemos elegido PolyBoRi

como el sistema de computacion algebraica ya que es el mas rapido para implementaralgebraicamente sistemas expertos basados en logica proposicional Booleana.

112

Page 123: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Sentencias para el sistema de computacion algebraica, Singular, de acuerdo con elenfoque algebraico basado en el modelo ‘Concepto-Atributo-Valor’.

En la figura 8.1, mostramos una grafica en la que estudiamos el rendimiento de ambosenfoques algebraicos. Como se puede observar, el enfoque algebraico basado en el modelo‘Concepto-Atributo-Valor’ proporciona mejores resultados.

0 5 10 15 20 25 30

01

23

4

m

tiem

po

●●●●●●●●●

● ●

Singular cavPolybori lpb

Figura 1: Experimento 1Comparacion de modelos con los sistemas de computacion algebraica de mejor rendimiento

113

Page 124: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

8.2. Experimento 2

Dado un numero natural, n, consideraremos un sistema experto con n variables,X1, X2, . . . , Xn, de tal modo que cada variable puede tomar un valor de un conjunto de5 posibles valores {v1, v2, . . . , v5}. La base de conocimiento de este sistema experto estacompuesto por n− 1 reglas:

(X1 = v1)→ (X2 6= v2)(X2 = v1)→ (X3 6= v2)

. . .

(Xn−1 = v1)→ (Xn 6= v2)Por ejemplo, si n = 6, especificaremos nuestro sistema experto en nuestro lenguaje de lasiguiente manera:BEGIN

BEGIN_VARS

X[6]: v1, v2, v3, v4, v5;

END_VARS

BEGIN_KB

(X[1]=v1) -> (X[2]<>v2);

(X[2]=v1) -> (X[3]<>v2);

(X[3]=v1) -> (X[4]<>v2);

(X[4]=v1) -> (X[5]<>v2);

114

Page 125: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

(X[5]=v1) -> (X[6]<>v2);

END_KB

END.

Desde nuestro lenguaje hemos traducido el sistema experto en ambos enfoques algebraicos:Sentencias para nuestro sistema de computacion algebraica, PolyBoRi, de acuerdocon el enfoque basado en logica proposicional Booleana. Hemos elegido PolyBoRi

como el sistema de computacion algebraica ya que es el mas rapido para implementaralgebraicamente sistemas expertos basados en logica proposicional Booleana.Sentencias para el sistema de computacion algebraica, Singular, de acuerdo con elenfoque algebraico basado en el modelo ‘Concepto-Atributo-Valor’.

115

Page 126: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

En la figura 8.2, mostramos una grafica en la que estudiamos el rendimiento de ambosenfoques algebraicos. Como se puede observar, el enfoque algebraico basado en el modelologico proposicional Booleano proporciona mejores resultados.

2 4 6 8 10

01

23

4

n

tiem

po

● ● ● ● ● ● ● ● ● ● ● ● ●

Singular cavPolybori lpb

Figura 2: Experimento 2Comparacion de modelos con los sistemas de computacion algebraica de mejor rendimiento

116

Page 127: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

9. Trabajo futuro

En este proyecto final de carrera hemos desarrollado un lenguaje para implementarsistemas expertos por medio de sistemas de computacion algebraica. El lenguaje que hemosdesarrollado podrıa ampliarse en el futuro para que permita lo siguiente:

1. Definir para cada variable el numero de valores tanto discretos como continuos.2. Definir formulas atomicas en las que aparezcan las relaciones ≥ y ≤.3. Definir funciones auxiliares disenadas por el usuario.Ademas, creemos que serıa relevante que el lenguaje sirviera para manejar sistemas

expertos que manejan incertidumbre en el conocimiento. A continuacion, describimos el modeloalgebraico basado en logica multivaluada para manejar incertidumbre.9.1. Sistemas expertos basados en logica proposicional multivaluada

La logica proposicional multivaluada puede ser usada para representar incertidumbre enel conocimiento.9.1.1. Formalismo de representacion

En logica proposicional multivaluada, las variables X1, ...Xm pueden tomar diferentesgrados de verdad. Los posibles valores de verdad seran nombrados por numeros naturalesentre 0 y q−1, donde 0 significa, como es habitual, ‘falso’ y q−1 ‘verdadero’. Los logicos handesarrollado diferentes logicas proposicionales multivaluadas con diferentes grados de verdad.Cada logica no solo implementa de manera diferente las conectivas logicas ∧, ∨, ¬, → , sinoque tambien definen nuevas conectivas. Cada conectiva unaria, fu (respectivamente conectivabinaria, fb), tiene asociada una funcion Fu : {0, ..., q− 1} −→ {0, ..., q− 1} (respectivamenteuna funcion Fb : {0, ..., q− 1} × {0, ..., q− 1} −→ {0, ..., q− 1}).

117

Page 128: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Definicion Una formula A es definida recursivamente de la siguiente manera:Xi donde Xi es una variable.fu(B) donde B es una formula y fu es un conectivo unario de la logica.fb(B, C ) donde B y C son formulas, and fb es un conectivo binario de la logica.

Usaremos C para denotar un conjunto de formulas. Llamaremos estado, S , a una posiblesituacion en la cual cada variable toma el grado de verdad entre 0 y q− 1. Dado un estadoS y una variable X , la expresion S(X ) denota el valor que la variable X toma en el estadoS . Dado una formula A y un estado S , la expresion S(A) denota un valor entre 0 y q − 1asociado a la formula A en el estado S .Definicion Sea A una formula. Sea S un estado. S(A) es definida de la siguiente manera:Caso A es Xi donde Xi es una variable.

S(A) = S(Xi)Caso A es fu(B) donde B es una formula y fu un conectivo unario asociado a la funcion

Fu : {0, ..., q− 1} → {0, ..., q− 1}.S(A) = Fu(S(B))

Caso A es fb(B, C ) donde B y C son formulas.S(A) = Fb(S(B), S(C ))

Definicion Sea A una formula y sea S un estado. La formula A se cumple en S sı y solosı S(A) = q− 1Por medio de las formulas describimos la entrada, la salida y la base de conocimiento de unsistema experto.Entrada La entrada es un conjunto de formulas con la forma X o fu(X ) donde X es una

variable de entrada y fu es un conectivo unario. El sımbolo I denota un conjunto deformulas representando la entrada del sistema experto.

118

Page 129: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Base de conocimiento La base de conocimiento es un conjunto finito de formulas. El sımboloK denota el conjunto de formulas que representan la base de conocimiento del sistemaexperto.

Salida La salida es un conjunto de formulas con la forma X or fu(X ) donde X es una variablede salida. El sımbolo O denota el conjunto de formulas que representan la salida deconocimiento del sistema experto.

9.1.2. Modelo algebraico

El problema de determinar si una formula puede ser deducida de otras puede ser tratadoalgebraicamente cuando q es una potencia de un numero primo [1] (esto es q = pn donde pes un primo y n un numero natural) .

En primer lugar, vamos a considerar como las formulas son traducidas a polinomios.Primero de todo, necesitamos definir el siguiente ideal J en Zp[x1, ..., xm, t]:

J = 〈xq1 − x1, ..., xqm − xm, r〉donde p es un primo, q = pn y r es un polinomio irreducible en Zp de grado n.

Una vez definido el ideal J , las formulas son transformadas en un polinomio Zq[x1, ..., xm]a traves de la siguiente funcion:Definicion Definimos recursivamente la funcion φ : C −→ A de la manera siguiente:Caso A es Xi donde Xi es una variable.

φ(A) = φ(Xi) = xi

119

Page 130: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Caso A es fu(B) donde B es una formula.φ(A) = NF(pu(φ(B)), J)

donde pu(x) es un polinomio particular que traduce la conectiva fu.Caso A es fb(B, C ) donde B y C son formulas.

φ(A) = NF(pb(φ(B), φ(C )), J)donde pb(x, y) es un polinomio particular que traduce la conectiva fb.

En relacion con los sistemas expertos, nosotros consideraremos los siguientes ideales:Ideal J Ideal necesario para definir el anillo A en el que las formulas son representadas.

J = 〈xq1 − x1, ..., xqm − xm〉Ideal I Ideal realacionado con la entrada del sistema experto.

I = 〈φ(Ai)|Ai ∈ I〉Ideal K Ideal realacionado con la base de conocimiento del sistema experto.

K = 〈φ(Ai)|Ai ∈ K〉Una vez traducidas las formulas en polinomios, podremos tratar algebraicamente el

problema de determinar si una formula puede ser deducida por un sistema experto.120

Page 131: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Teorema 9.1 Una formula B puede ser deducida por un sistema experto sı y solo sı:

φ(B) ∈ I + J + K

Este teorema puede ser tambien reescrito de la siguiente manera:Teorema 9.2 Una formula B puede ser deducida por un sistema experto sı y solo sı:

NF (φ(B), I + J + K ) = 0

121

Page 132: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

122

Page 133: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

10. Conclusiones

En este trabajo final de carrera hemos disenado un lenguaje para implementar rapida ycomodamente sistemas expertos a traves de sistemas de computacion algebraica. Este lenguajecontempla las siguientes ventajas:

i) El lenguaje es independiente del enfoque algebraico elegido para implementar elsistema experto. En este trabajo mostraremos como traducir un sistema experto descritoen nuestro lenguaje en ambos enfoques algebraicos. De esta manera podemos generarfacilmente el codigo del sistema experto en ambos enfoques algebraicos con el fin deexperimentar con ellos y elegir el que mejor rendimiento muestre.

ii) El lenguaje es independiente del sistema de computacion algebraica usado paraimplementar el sistema experto. De esta manera, cambiar el sistema de algebracomputacional solo conlleva volver a compilar el sistema experto descrito en nuestrolenguaje.

iii) El lenguaje no usa notacion matematica. Por lo tanto, los desarrolladores no necesitanconocer conceptos matematicos como bases de Groebner o formas normales, que soncomunmente utilizados en estos enfoques algebraicos.

iv) Nuestro lenguaje proporciona una manera sencilla de implementar sistemas expertos.De hecho, el codigo de un sistema experto es normalmente mucho mas reducido y muchomas comprensible que el codigo descrito en los sistemas de computacion algebraica.Nuestro lenguaje proporciona una manera muy intuitiva y facil de escribir reglas (lascuales son la manera mas natural de describir la base de conocimiento de un sistemaexperto).

123

Page 134: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

124

Page 135: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

A. APENDICE: Especificacion del traductor en EBNF y JAVACC

La gramatica en EBNF [20], predictiva recursiva por la izquierda y LL(1), es la siguiente:unprograma ::= ‘BEGIN’

‘BEGIN VARS’ decvariable {decvariable} ‘END VARS’‘BEGIN KB’ {regla} ‘END KB’[ ‘BEGIN INPUT’ [input] ‘END INPUT’ ]‘END’ .

decvariable ::= id [‘[’ numero ‘]’] { ‘,’ variable} ‘:’ id {‘,’ id} ‘;’variable ::= id [‘[’ numero ‘]’]regla ::= expresion [restoregla] ‘;’restoregla ::=consecuente {consecuente}

| {antecedente} “→” expresion {consecuente}consecuente ::= ‘OR‘ expresionantecedente::= ‘AND’ expresionexpresion ::= ‘(’ variable operador variable ‘)’operador::= ‘=’ | ‘<>’variable ::= id [‘[’ <numero> ‘]’]input ::= expresion {‘,’ expresion} ‘;’id ::= caracter {caracter | “0” |’ numero | “ ” }numero ::= caracternumerico{[“0” | caracternumerico]}caracter ::= restocaracter { “ ”| restocaracter}restocaracter ::= “a” | “b” | “c” | “d” | “e” | “f” | “g” | “h” | “i” | “j” | “k” | “l” | “m” | “n” | “n” | “o” |

“p” |“q” | “r” | “s” | “t” | “u” | “w” | “x” | “y” | “z”caracternumerico ::= “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9”

125

Page 136: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Se ha elegido JavaCC como plataforma para generar el analizador lexico-sintactico por laposibilidad de obtener un traductor que pueda ser ejecutado en distintas plataformas siempreque se tenga instalada la maquina virtual de Java. Ademas es comodo introducir la generacionde codigo directamente en el traductor en lenguaje JAVA.A partir de la definicion en EBNF es muy sencillo conseguir la gramatica para JavaCC [21]es:unprograma ::= “BEGIN”

“BEGIN VARS” (decvariable())+ “END VARS”“BEGIN KB” (regla())* “END KB”[ “BEGIN INPUT” [input()] “END INPUT” ]“END”.

decvariable ::= <id> [“[” numero “]”] ( ‘,’ <id> [‘[’ <numero> ‘]’] )* “:” <id> (“,” <id>)* “;”regla ::= expresion() [restoregla()] “;”restoregla ::=consecuente() (consecuente())*

| (antecedente() )* “→” expresion() (consecuente())*consecuente ::= “OR“ expresion()antecedente ::= “AND” expresion()expresion ::= “(” variable() operador() variable() “)”operador ::= “=” | “<>”variable ::= <id> [“[” <numero> “]”]input ::= expresion() (“,” expresion() )* “;”id ::= [“A”-“Z”, “a”-“z” , “ ” ]([“A”-“Z”, “a”-“z”,“0”-“9”’ , “ ” ,“\”])*numero ::= [“1”-“9”]([“0”-“9”])*

126

Page 137: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

B. APENDICE: Comparacion de sistemas de computacion

algebraica

B.1. Mejores CAS para experimento 1

1 2 3 4 5 6 7 8

02

46

810

m

tiem

po

● ● ● ● ● ● ● ● ●

SingularCocoaPolybori

Figura 3: Experimento 1Resultados de PolyBoRi, Singular y CoCoA y el modelo logico proposcional booleano

127

Page 138: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

0 5 10 15 20 25 30

05

1015

m

tiem

po

SingularCocoa

Figura 4: Experimento 1Resultados de Singular y CoCoA y el modelo ‘Concepto-Atributo-Valor’

128

Page 139: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

B.2. Mejores CAS para experimento 2

La figura que se muestra a continuacion, demuestra que el sistema algebraico de computomas acertado para representar un sistema experto con muchos valores para pocas variableses el sistema PolyBoRi.

0 10 20 30 40 50 60

0.0

0.5

1.0

1.5

2.0

n

tiem

po

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

SingularCocoaPolybori

Figura 5: Experimento 2Resultados de PolyBoRi, Singular y CoCoA y el modelo logico proposcional booleano

129

Page 140: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

2 4 6 8 10

05

1015

n

tiem

po

SingularCocoa

Figura 6: Experimento 2Resultados de Singular y CoCoA y el modelo ‘Concepto-Atributo-Valor

130

Page 141: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

C. APENDICE: Scripts para la automatizacion de pruebas

Se han generado dos programas para generar un sistema experto cuyas caracterısticas quese corresponden a las especificaciones de los experimentos de los apartados 8.1 y 8.2. Tambiense han creado tres scripts para ejecutar estos sistemas expertos de manera sistematica enlos sistemas de computacion algebraica PolyBoRi, Singular y CoCoA.C.1. Generador de sistemas expertos

Codigo necesario para generar sistemas expertos especificados en el lenguaje fuentepropuesto en este proyecto. Se tiene en cuenta dos parametros n y m, que son, numerode variables y numeros de valores por variable correspondientemente.Experimento1

1 /∗2 ∗ A t t r i b u t i o n −Noncommerc ia l 3 . 0 Unpor ted − h t t p : / / c r e a t i v e c ommons .

o rg / l i c e n s e s / by−nc / 3 . 0 /3 ∗ /4 package gene rado r ;56 impo r t j a v a . i o . B u f f e r e dW r i t e r ;7 impo r t j a v a . i o . F i l eW r i t e r ;89 /∗ ∗

10 ∗

11 ∗ @author Robe r t o Maestre Mar t i ne z131

Page 142: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

12 ∗ /13 pub l i c c l a s s Generador {141516 pub l i c s t a t i c vo i d s t r i n g T o F i l e ( S t r i n g msg , boolean append ) {17 t r y {

18 S t r i n g FILE CODE=” f u e n t e e x p 1 . t x t ” ;19 F i l eW r i t e r f s t r e am = new F i l eW r i t e r ( FILE CODE , append ) ;20 Bu f f e r e dW r i t e r ou t = new Bu f f e r e dW r i t e r ( f s t r e am ) ;21 ou t . w r i t e ( msg ) ;22 ou t . c l o s e ( ) ;23 } ca t ch ( E x c ep t i o n e ) {24 System . e r r . p r i n t l n ( ” E r r o r : ” + e . getMessage ( ) ) ;25 }

26 }

2728 /∗ ∗29 ∗ @param args t he command l i n e arguments30 ∗ /31 pub l i c s t a t i c vo i d main ( S t r i n g [ ] a r g s ) th rows Ex cep t i o n {3233 i f ( a rg s . l e n g t h !=2) {34 throw new Ex cep t i o n ( ” Numero de pa rame t r o s e r r o n e o s ” ) ;35 }

36 i n t n=I n t e g e r . p a r s e I n t ( a rg s [ 0 ] ) ;132

Page 143: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

37 i n t m=I n t e g e r . p a r s e I n t ( a rg s [ 1 ] ) ;38 i f ( n<2) throw new Ex cep t i o n ( ” Deben e x i s t i r a l menos dos

v a r i a b l e s ” ) ;39 i f (m<2) throw new Ex cep t i o n ( ” Deben e x i s t i r a l menos dos

v a l o r e s ” ) ;4041 System . ou t . p r i n t l n ( ” Generando ”+n+” v a r i a b l e s con ”+m+”

v a l o r e s cada una ” ) ;4243 s t r i n g T o F i l e ( ”BEGIN\n ” , f a l s e ) ;4445 s t r i n g T o F i l e ( ” \ tBEGIN VARS\n ” , t r u e ) ;46 s t r i n g T o F i l e ( ” \ t \ t v a r [ ” +(n−1)+” ] : \ t ” , t r u e ) ;47 f o r ( i n t x=1; x<m ; x++){48 s t r i n g T o F i l e ( ” v a l ”+x , t r u e ) ;49 i f ( x<m−1) s t r i n g T o F i l e ( ” , ” , t r u e ) ;50 e l se s t r i n g T o F i l e ( ” ; \ n ” , t r u e ) ;51 }

52 s t r i n g T o F i l e ( ” \tEND VARS\n\n ” , t r u e ) ;5354 s t r i n g T o F i l e ( ” \ tBEGIN KB\n ” , t r u e ) ;55 f o r ( i n t i =1; i<n−2; i++){56 s t r i n g T o F i l e ( ” \ t \ t ( v a r [ ”+i+” ]= v a r [ ” +( i +1)+” ] ) \ t

−> \ t ( v a r [ ” +( i +1)+” ]<> v a r [ ” +( i +2)+” ] ) ; \ n ” ,t r u e ) ;

133

Page 144: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

57 }

58 s t r i n g T o F i l e ( ” \n\tEND KB\n\n ” , t r u e ) ;5960 s t r i n g T o F i l e ( ”END. ” , t r u e ) ;61 }

6263 }

Experimento 2

1 /∗2 ∗ A t t r i b u t i o n −Noncommerc ia l 3 . 0 Unpor ted − h t t p : / / c r e a t i v e c ommons .

o rg / l i c e n s e s / by−nc / 3 . 0 /3 ∗ /4 package gene rado r ;56 impo r t j a v a . i o . B u f f e r e dW r i t e r ;7 impo r t j a v a . i o . F i l eW r i t e r ;89 /∗ ∗

10 ∗

11 ∗ @author Robe r t o Maestre Mar t i ne z12 ∗ /13 pub l i c c l a s s Generador {1415

134

Page 145: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

16 pub l i c s t a t i c vo i d s t r i n g T o F i l e ( S t r i n g msg , boolean append ) {17 t r y {

18 S t r i n g FILE CODE=” f u e n t e e x p 2 . t x t ” ;19 F i l eW r i t e r f s t r e am = new F i l eW r i t e r ( FILE CODE , append ) ;20 Bu f f e r e dW r i t e r ou t = new Bu f f e r e dW r i t e r ( f s t r e am ) ;21 ou t . w r i t e ( msg ) ;22 ou t . c l o s e ( ) ;23 } ca t ch ( E x c ep t i o n e ) {24 System . e r r . p r i n t l n ( ” E r r o r : ” + e . getMessage ( ) ) ;25 }

26 }

2728 /∗ ∗29 ∗ @param args t he command l i n e arguments30 ∗ /31 pub l i c s t a t i c vo i d main ( S t r i n g [ ] a r g s ) th rows Ex cep t i o n {3233 i f ( a rg s . l e n g t h !=2) {34 throw new Ex cep t i o n ( ” Numero de pa rame t r o s e r r o n e o s ” ) ;35 }

36 i n t n=I n t e g e r . p a r s e I n t ( a rg s [ 0 ] ) ;37 i n t m=I n t e g e r . p a r s e I n t ( a rg s [ 1 ] ) ;38 i f ( n<2) throw new Ex cep t i o n ( ” Deben e x i s t i r a l menos dos

v a r i a b l e s ” ) ;39 i f (m<2) throw new Ex cep t i o n ( ” Deben e x i s t i r a l menos dos

135

Page 146: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

v a l o r e s ” ) ;4041 System . ou t . p r i n t l n ( ” Generando ”+n+” v a r i a b l e s con ”+m+”

v a l o r e s cada una ” ) ;4243 s t r i n g T o F i l e ( ”BEGIN\n ” , f a l s e ) ;4445 s t r i n g T o F i l e ( ” \ tBEGIN VARS\n ” , t r u e ) ;46 f o r ( i n t i =1; i<n ; i++){47 s t r i n g T o F i l e ( ” \ t \ t v a r ”+i+” : \ t ” , t r u e ) ;48 f o r ( i n t x=0; x<m ; x++){49 s t r i n g T o F i l e ( ” v a l ”+x , t r u e ) ;50 i f ( x<m−1) s t r i n g T o F i l e ( ” , ” , t r u e ) ;51 e l se s t r i n g T o F i l e ( ” ; \ n ” , t r u e ) ;52 }

53 }

54 s t r i n g T o F i l e ( ” \tEND VARS\n\n ” , t r u e ) ;5556 s t r i n g T o F i l e ( ” \ tBEGIN KB\n ” , t r u e ) ;57 f o r ( i n t i =1; i<n−1; i++){58 s t r i n g T o F i l e ( ” \ t \ t ( v a r ”+i+”=va l 1 ) \ t −> \ t ( v a r ”

+( i +1)+”<>v a l 2 ) ; \ n ” , t r u e ) ;59 }

60 s t r i n g T o F i l e ( ” \n\tEND KB\n\n ” , t r u e ) ;61

136

Page 147: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

62 s t r i n g T o F i l e ( ”END. ” , t r u e ) ;63 }

6465 }

C.2. Automatizacion

Los siguientes scripts permitiran automatizar las pruebas a traves de los siguientes pasos:Generar el codigo especıfico del sistema experto en nuestro lenguaje teniendo en cuentalos parametros n y m que son el numero de variables y el numero de valores por variablecorrespondientemente.Ejecutar el traductor para traducir el sistema experto generado con la opcion “-b” paratener en cuenta el tiempo de calculo de la base de Groebner.Ejecutar en el sistema de computacion algebraica Singular, CoCoA o PolyBoRi elsistema experto, volcando la salida de la ejecucion en el fichero de resultados.

El fichero de resultados sera utilizado para comparar analıticamente ambos experimentos.PolyBoRi

1 #! / b in / bash2 SALIDA=” r e s u l t a d o s . t x t ”3 N=20;45 #Voy a l d i r e c t o r i o donde e s t an t odo s l o s s c r i p t s6 cd / home / r o b e r t o / t r a d u c t o r

137

Page 148: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

78 #Re i n i c i o l a s a l i d a9 echo ” ” > $SALIDA

10 f o r ( ( c=2; c<=N; c++ ) )11 do

12 #Compi lo l a p rueb te , ese de c i r , e l c od i go f u e n t e13 j a v a − j a r Expe r imen to1 . j a r 4 $c14 #Creo e l s i s t ema e x p e r t o15 j a v a T r a du c t o r 1 1 −b < f u e n t e e x p 1 . t x t16 #e j e c u t o e l s i s t ema e x p e r t o ob t en i endo e l t i empo de s a l i d a17 python s em l p b p o l y b o r i . t x t >> $SALIDA18 done

Singular

1 #! / b in / bash2 SALIDA=” r e s u l t a d o s . t x t ”3 N=35;45 #Voy a l d i r e c t o r i o donde e s t an t odo s l o s s c r i p t s6 cd / home / r o b e r t o / t r a d u c t o r78 #Re i n i c i o l a s a l i d a9 echo ” ” > $SALIDA

10 f o r ( ( c=3; c<=N; c++ ) )11 do

138

Page 149: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

12 #Compi lo l a p rueb te , ese de c i r , e l c od i go f u e n t e13 j a v a − j a r Expe r imen to1 . j a r 4 $c14 #Creo e l s i s t ema e x p e r t o15 j a v a T r a du c t o r 2 1 −b < f u e n t e e x p 1 . t x t16 echo ” q u i t ( ) ; ” >> s em c v a s i n g u l a r . t x t17 #e j e c u t o e l s i s t ema e x p e r t o ob t en i endo e l t i empo de s a l i d a18 S ingu l a r s em c v a s i n g u l a r . t x t > tmp19 #sOlo n e c e s i t a l a l i n e a 5 que es l a que t i e n e e l t i empo20 c oun t=1 # coun t i n g l i n e s f r om 121 whi le read −r l i n e ; do

22 i f [ $count == 6 ] ; then

23 echo $ l i n e >> $SALIDA24 f i

25 l e t c oun t=$count+126 done < tmp27 done

CoCoA

1 #! / b in / bash2 SALIDA=” r e s u l t a d o s . t x t ”3 N=35;45 #Voy a l d i r e c t o r i o donde e s t an t odo s l o s s c r i p t s6 cd / home / r o b e r t o / t r a d u c t o r7

139

Page 150: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

8 #Re i n i c i o l a s a l i d a9 echo ” ” > $SALIDA

10 f o r ( ( c=3; c<=N; c++ ) )11 do

12 #Compi lo l a p rueb te , ese de c i r , e l c od i go f u e n t e13 j a v a − j a r Expe r imen to1 . j a r 4 $c14 #Creo e l s i s t ema e x p e r t o15 j a v a T r a du c t o r 2 2 −b < f u e n t e e x p 1 . t x t16 echo ” QUIT ; ” >> semcva cocoa . t x t17 #e j e c u t o e l s i s t ema e x p e r t o ob t en i endo e l t i empo de s a l i d a18 cocoa semcva cocoa . t x t > tmp19 #sOlo n e c e s i t a l a l i n e a 5 que es l a que t i e n e e l t i empo20 c oun t=1 # coun t i n g l i n e s f r om 121 whi le read −r l i n e ; do

22 i f [ $count == 18 ] ; then

23 echo ${ l i n e : 1 1 : 4 } >> $SALIDA24 f i

25 l e t c oun t=$count+126 done < tmp27 done

140

Page 151: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

D. APENDICE: Introduccion a las bases de Grobner

(Traduccion del artıculo ‘Grobner Bases: A Short Introduction for Systems Theorists’ [22])El metodo de las bases de Grobner proporciona un enfoque uniforme para solucionar un amplioabanico de problemas expresados en terminos de conjuntos de polinomios multivariantes.Un polinomio mutlivariante es un polinomio en mas de una variable, como por ejemploP(x, y) = a22x2y2 + a21x2y+ a11xy2 + a10x + a01y+ a00Se deben definir varios conceptos que ayudaran a entender mejor que son las bases deGrobner.D.1. Division (reduccion) de polinomios multivariantes

Definimos los siguientes polinomios bivariantes,g = x2y3 + 3xy2 − 5xf1 = xy− 2y,f2 = 2y2 − x2,

y el siguiente con junto de polinomiosF = {f1, f2}

sus monomios estan ordenados lexicograficamente teniendo y mayor peso que x y presentadosen orden descendente de izquierda a derecha. El mayor monomio (es decir, el de mas a laizquierda) es llamado el monomio lıder en el polinomio. Hay infinitas maneras de ordenar

los polinomios que son admisibles en la teorıa de las bases de Grobner

Un posible paso para dividir (“reducir”), puede ser, “reducir el polinomio g modulo f1”:h = g− (3y)f1 = −5x + 6y2 + x2y3,

141

Page 152: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

observamos que uno de los monomios de g se cancela con el monomio lıder de −(3y)f1. Enesta situacion podemos escribir la operacion de la siguiente manera (se lee: “g se reduce a hmodulo f1”):

g→f1 h

En general muchas reducciones son posibles. Dado un conjunto de polinomios F y un polinomiog, muchas reducciones de g modulo polinomios en F son posibles, en nuestro ejemplo podemostener:

g→f1 h2, donde h2 = g− (xy2)f1g→f2 h3, donde h3 = g− (12x2y)f2

La division de polinomios multivariantes siempre termina pero no es unica.Escribimos g→F h sı

g→f h para algun f ∈ F

y tambien escribimos, g→∗F h si g es reducido a h en un numero finito de pasos.Tambien escribimos hF si h no puede ser reducido mas. Dos puntos fundamentales para elconcepto de reduccion son:

Terminacion: Para cualquier g Y F , no hay infinitos pasos de reduccion modulo F

empezando desde gLa reduccion es algorıtmica: Hay un algoritmo RF el cual produce una forma reducidade F para cualquier polinomio dado g, por ejemplo, para todo g y F . g→F∗ RF (F, g)FNo-singularidad Dado g y F pueden existir H y K , de manera que:hF ←F ∗ g→F ∗ kF ,pero h 6= k

142

Page 153: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

D.2. Definicion normal de base de Grobner

F es una base de Grobner :⇐⇒→F es unica∨g,h,k

(hF ←F ∗ g→F ∗ kF ) =⇒ h = k

D.3. La aplicacion de teorıa de bases de Grobner

A primera vista, uno no puede ver por que la propiedad definitoria de las bases de Grobnerdeberıa jugar un papel fundamental. La importancia de esta propiedad se deriva del siguientehecho:Hecho: Las bases de Grobner tienen ‘muchas buenas propiedades’ y por lo tanto, para basesde Groebner, muchos problemas fundamentales pueden ser resueltos por algoritmos ‘sencillos’.Ejemplo (El problema principal de la teoria de ideales de polinomios):

Sea F un conjunto de polinomiosSi F es una base de Grobner, entonces:f ∈ Ideal(F )⇐⇒ f →∗F 0Aquı, Ideal(F ) es un ideal generado por F , es decir, el conjunto de todos los polinomios

de la forma ∑mi=1 pifi, con fi en F y polinomios aleatorios pi. Como una consecuencia de la

propiedad anterior, la cuestion relacionada con la pertenencia o no f ∈ Ideal(F ), para lasbases de Grobner F , puede ser deducida solamente reduciendo f modulo F y comprobandosi el resultado es o no 0. Para un F general, esta pregunta es muy difıcil de decidir y, dehecho, en la literatura antigua sobre teoria de ideales de polinomios fue llamada ‘El problemaprincipal de la teoria de ideales de polinomios’Ejemplo (El problema de ‘La eliminacion’): Dado F un conjunto de polinomios en lasindeterminadas x1, . . . , xn y dado i ≤ n:

143

Page 154: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Si F es una base de Grobner, entonces:Ideal(F ) ∩ K [x1, . . . , xn] = Ideal(F ∩ K [x1, . . . , xn])Como consecuencia, una base para la ”i-esima eliminacion ideal”Ideal(F )∩K [x1, . . . , xn] de

unas bases de Grobner finitas F pueden ser obtenidos solamnete cogiendo aquellos polinomiosen F que dependen solo de los primeros i indeterminados. De nuevo, este problema es muydificil para un F general.D.4. ¿Como puede una base de Grobner ser construida?

El principal problema ahora es como, dado un conjunto F arbitrario y finito de polinomios(multivariantes), uno puede encontrar un conjunto de polinomios G tal que Ideal(F ) = Ideal(G)y G es una base de Grobner.El problema puede ser solucionado por el siguiente algoritmo:Empieza con G := F

Para cada par de polinomios f1, f2 ∈ G

Computa el ”S-Polinomio”de f1, f2Reduce forma h w.r.t. G .Si h = 0, considera el siguiente parSi h 6= 0, anade h a G e itera.

El algoritmo anterior necesita de la computacion de ”S-Polinomios”. De nuevo, damos ladefinicion con un ejemplo:

f1 := xy− 2y, f2 := 2y2 − x2,S-Polinomio[f1, f2] = yf1 − 12xf2 = x32 − 2y2

144

Page 155: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

E. APENDICE: Codigo Traductor JavaCC

1 /∗2 ∗ A t t r i b u t i o n −Noncommerc ia l 3 . 0 Unpor ted − h t t p : / / c r e a t i v e c ommons .

o rg / l i c e n s e s / by−nc / 3 . 0 /3 ∗ /4 o p t i o n s { I g n o r e c a s e = f a l s e ;5 Common Token Act ion = t r u e ;6 E r r o r R e p o r t i n g = t r u e ;7 Ou t p u t d i r e c t o r y=”R :\\TFC−DATOS\\ j a v a c c −4.0\\ t r a d u c t o r ” ;8 }9 PARSER BEGIN ( T r a du c t o r )

1011 impo r t j a v a . u t i l . ∗ ;12 impo r t j a v a . i o . ∗ ;13 impo r t t a b l a s i m b o l o s c omp i l a d o r . ∗ ;1415 pub l i c c l a s s T r a du c t o r {1617 / / Tabla de s imbo l o s de l s i s t ema18 p r i v a t e s t a t i c TS t s ;19 / / F lags g l o b a l e s para i n d i c a r e l t i p o de c omp i l a c i o n20 p r i v a t e s t a t i c S t r i n g MODELO=” ” ;21 p r i v a t e s t a t i c S t r i n g SALIDA=” ” ;22 / / Guardara donde se genera e l c od i go

145

Page 156: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

23 p r i v a t e s t a t i c S t r i n g OUTPUT FILE ;24 / / E s t r u c t u r a s g l o b a l e s para gua rda r p o l i n om i o s y ope r ado r e s25 p r i v a t e s t a t i c Vec to r<St r i ng> p o l i n om i o s ;26 p r i v a t e s t a t i c Vec to r<Boolean> ope r ado r e s ;27 / / No s i n d i c a s i l a p r ime ra e x p r e s i o n de una r e g l a c o r r e s ponde a un28 / / c o n s e cuen t e o a un an t e c eden t e29 p r i v a t e s t a t i c boolean FLAGANTECEDENTE ;30 p r i v a t e s t a t i c boolean FLAGNEGARTODO;31 p r i v a t e s t a t i c boolean FLAGEXPRESIONVALORCOMPLEJO ;32 p r i v a t e s t a t i c boolean FLAGEXPRESIONVALORCOMPLEJONEGADO ;33 p r i v a t e s t a t i c boolean FLAGEXISTEENTRADA ;34 / / Guardamos e l numero de p o l i n om i o s de r e s t r i c c i o n , p o l i n om i o s de

v a r i a b l e s35 / / p o l i n om i o s de r e g l a s y p o l i n om i o s de en t r ada para luego gene ra r

l a s l l amadas36 / / para e l c a l c u l o de i d e a l e s y de l a base de g roebne r37 p r i v a t e s t a t i c i n t NUMPOLRES=0;38 p r i v a t e s t a t i c i n t NUMPOLVARS=0;39 p r i v a t e s t a t i c i n t NUMREGLAS=0;40 p r i v a t e s t a t i c i n t NUMREGLASINPUT=0;41 / / En l a gene r a c i o n de cod igo , s i e s t a a c t i v a d o e s t e f l a g , se

i n c o r p o r a n42 / / marcas de t i empo43 p r i v a t e s t a t i c boolean BENCHMARK;44

146

Page 157: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

45 / / Crea un pun t e r o que apunta a l a e s t r u c t u r a g l o b a l po l i n om i o s ,d e v u e l v e

46 / / e l p un t e r o y bo r r a l a e s t r u c t u r a g l o b a l47 pub l i c s t a t i c Vec to r<St r i ng> r e p o l s ( ) {48 Vec to r<St r i ng> aux=( Ve c t o r ) p o l i n om i o s . c l o n e ( ) ;49 p o l i n om i o s . c l e a r ( ) ;50 r e t u r n aux ;5152 }

5354 / / Crea un pun t e r o que apunta a l a e s t r u c t u r a g l o b a l ope rado res ,

d e v u e l v e55 / / e l p un t e r o y bo r r a l a e s t r u c t u r a g l o b a l56 pub l i c s t a t i c Vec to r<Boolean> r eops ( ) {57 Vec to r<Boolean> aux=( Ve c t o r ) ope r ado r e s . c l o n e ( ) ;58 ope r ado r e s . c l e a r ( ) ;59 r e t u r n aux ;6061 }

6263 / / Annade l o s p o l i n om i o s de l a e s t r u c t u r a g l o b a l ” p o l i n om i o s ”64 / / a un a u x i l i a r y v a c i a l a e s t r u c t u r a g l o b a l65 pub l i c s t a t i c vo i d an i a d epo l s ( Vec to r<St r i ng> v ) {66 f o r ( i n t i =0; i<p o l i n om i o s . s i z e ( ) ; i++){67 v . add ( p o l i n om i o s . ge t ( i ) ) ;

147

Page 158: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

68 }

69 p o l i n om i o s . c l e a r ( ) ;70 }

7172 / / Annade l o s p o l i n om i o s de l a e s t r u c t u r a g l o b a l ” ope r ado r e s ”73 / / a un a u x i l i a r y v a c i a l a e s t r u c t u r a g l o b a l74 pub l i c s t a t i c vo i d an iadeops ( Vec to r<Boolean> v ) {75 f o r ( i n t i =0; i<ope r ado r e s . s i z e ( ) ; i++){76 v . add ( ope r ado r e s . g e t ( i ) ) ;77 }

78 ope r ado r e s . c l e a r ( ) ;79 }

8081 / / Impr ime un v e c t o r con v a r i a b l e s de un po l i n om i o .82 / / Func ion usada para DEBUG83 pub l i c s t a t i c vo i d impr ime ( Vec to r<St r i ng> v ) {84 System . ou t . p r i n t ( ” ” ) ;85 f o r ( i n t i =0; i<v . s i z e ( ) ; i++){86 System . ou t . p r i n t ( v . g e t ( i )+” ” ) ;87 }

88 System . ou t . p r i n t l n ( ” ” ) ;89 }

9091 / / Devue l ve un v e c t o r de p o l i n om i o s en una cadena

148

Page 159: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

92 pub l i c s t a t i c S t r i n g acadena ( Vec to r<St r i ng> v , Vec to r<Boolean> op ,boolean negado ) {

93 / / Si s o l o es un po l i nom io , l o d e v u e l v o d i r e c t amen t e94 i f ( v . s i z e ( ) ==1) {95 r e t u r n i s n eg ( v . g e t ( 0 ) , negado , op . ge t ( 0 ) ) ;96 } e l se {

97 / / Si no , t engo que hace r pa re s para d e v o l v e r O( a ,O( b , c ) )98 r e t u r n encadenaOrs ( v , op , 0 , negado ) ;99 }

100 }

101 / / Func ion r e c u r s i v a para gene ra r l o s OR( a , b ) encadenados a p a r t i rde un v e c t o r

102 / / de p o l i n om i o s103 pub l i c s t a t i c S t r i n g encadenaOrs ( Vec to r<St r i ng> v , Vec to r<Boolean>

op , i n t i ndex , boolean negado ) {104 i f ( i n d e x==v . s i z e ( ) −2){105 r e t u r n ”O( ”+i s neg ( v . g e t ( i n de x ) , negado , op . ge t ( i n de x ) )+” , ”+i s neg ( v .

g e t ( i n de x +1) , negado , op . ge t ( i n de x +1) )+” ) ” ;106 } e l se {

107 r e t u r n ”O( ”+i s neg ( v . g e t ( i n de x ) , negado , op . ge t ( i n de x ) )+” , ”+encadenaOrs ( v , op , i n d e x +1 , negado )+” ) ” ;

108 }

109 }

110111

149

Page 160: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

112 / / Func ion r e c u r s i v a para gene ra r l o s OR( a , b ) encadenados a p a r t i rde un v e c t o r

113 / / de p o l i n om i o s114 pub l i c s t a t i c S t r i n g encadenaOrSimple ( Vec to r<St r i ng> v , i n t i n d e x ) {115 i f ( i n d e x==v . s i z e ( ) −2){116 r e t u r n ”O( ”+v . ge t ( i n de x )+” , ”+v . ge t ( i n de x +1)+” ) ” ;117 } e l se {

118 r e t u r n ”O( ”+v . ge t ( i n de x )+” , ”+encadenaOrSimple ( v , i n d e x +1)+” ) ” ;119 }

120 }

121122123 / / Annade neg ( s ) a una v a r i a b l e ” s ” pasada pos pa rame t r o comprobando

e l ope rado r124 / / y comprobando125 pub l i c s t a t i c S t r i n g i s neg ( S t r i n g s , boolean negado , boolean op ) {126 / / Si es an t e c eden t e => negado==t r u e −> l a o p e r a c i o n negada127 / / negado==f a l s e −> l a o p e r a c i o n normal128 boolean f l a g=f a l s e ;129 i f ( ! negado ) f l a g =!op ;130 e l se f l a g=op ;131 / / Devue l vo e l v a l o r c o r r e c t o132 i f ( f l a g )133 r e t u r n ”NEG( ”+s+” ) ” ;134 e l se

150

Page 161: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

135 r e t u r n s ;136 }

137138 / / Muestra un mensa je de e r r o r i n d i c a ndo que l o s pa rame t r o s son

i n c o r r e c t o s139 / / po r c o n s o l a140 pub l i c s t a t i c vo i d mo s t r a r e r r o r d e p a r am e t r o s ( ) {141 System . ou t . p r i n t l n ( ” \nUSO DEL TRADUCTOR : ” ) ;142 System . ou t . p r i n t l n ( ”> j a v a T r a du c t o r \ ”MODELO\ ” \ ” SALIDA \ ” [− t s ] [ −

b ] < f u e n t e . t x t \n ” ) ;143 System . ou t . p r i n t l n ( ” \ t − t s : Muestra l a t a b l a de s imbo l o s ” ) ;144 System . ou t . p r i n t l n ( ” \ t −b : Annade marcas de benchmark \n ” ) ;145 System . ou t . p r i n t l n ( ” Po s i b l e s Usos : ” ) ;146 System . ou t . p r i n t l n ( ”> j a v a T r a du c t o r 1 1 < f u e n t e . t x t ( Modelo LPB

y s i s t ema Po l ybo r i ) ” ) ;147 System . ou t . p r i n t l n ( ”> j a v a T r a du c t o r 1 2 < f u e n t e . t x t ( Modelo LPB

y s i s t ema S i ngu l a r ) ” ) ;148 System . ou t . p r i n t l n ( ”> j a v a T r a du c t o r 1 3 < f u e n t e . t x t ( Modelo LPB

y s i s t ema Cocoa ) ” ) ;149 System . ou t . p r i n t l n ( ”> j a v a T r a du c t o r 2 1 < f u e n t e . t x t ( Modelo CAV

y s i s t ema S i ngu l a r ) ” ) ;150 System . ou t . p r i n t l n ( ”> j a v a T r a du c t o r 2 2 < f u e n t e . t x t ( Modelo CAV

y s i s t ema Cocoa ) ” ) ;151 System . ou t . p r i n t l n ( ” \n [ LPB]=Modelo l o g i c o p r o p o s i c i o n a l boo leano ”

) ;151

Page 162: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

152 System . ou t . p r i n t l n ( ” [ CAV]=Modelo ’ Concepto−A t r i b u t o−Va lo r ’ \n ” ) ;153 System . e x i t ( 0 ) ;154 }

155156157158159 pub l i c s t a t i c vo i d g e n e r a r P o l i n om i o s R e s t r i c c i o n ( ) {160 t r y {

161 i f (MODELO . equa l s ( ”PROPOSICIONAL ” ) ) {162 / / Obtengo l o s pa re s con e l f o rma t o p r o p o s i c i o n a l e i n d i c o l o s163 / / s e pa r ado r e s para cada modelo164 i f ( SALIDA . equa l s ( ”POLYBORI ” ) ) {165 / / De c l a r a r l o s imp o r t s y l a s f u n c i o n e s166 s t r i n g T o F i l e ( ” f r om p o l y b o r i . PyPolyBoRi impo r t ∗ ” , t r u e ) ;167 s t r i n g T o F i l e ( ” f r om p o l y b o r i . gbco re impo r t ∗ ” , t r u e ) ;168 s t r i n g T o F i l e ( ” f r om p o l y b o r i imp o r t ∗ ” , t r u e ) ;169 i f (BENCHMARK) s t r i n g T o F i l e ( ” impo r t t ime ” , t r u e ) ;170 s t r i n g T o F i l e ( ” ” , t r u e ) ;171 s t r i n g T o F i l e ( ” d e f O(M,N) : r e t u r n Po lynomia l (M) ∗Polynomia l (N) ” ,

t r u e ) ;172 s t r i n g T o F i l e ( ” d e f NEG(M) : r e t u r n 1+Polynomia l (M) ” , t r u e ) ;173 s t r i n g T o F i l e ( ” d e f Y (M,N) : r e t u r n NEG(O(NEG(M) ,NEG(N) ) ) ” , t r u e ) ;174 s t r i n g T o F i l e ( ” ” , t r u e ) ;175 / / Dec la ro e l a n i l l o

152

Page 163: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

176 i n t t o t a l v a r i a b l e s =5;177 s t r i n g T o F i l e ( ” d e c l a r e r i n g ( [ B lock ( \ ” x \ ” , ”+t s .

g e t T o t a l V a r i a b l e s R e s t r i c c i o n L o g i c a ( )+” ) ] , g l o b a l s ( ) ) ; ” , t r u e ) ;178 s t r i n g T o F i l e ( ” ” , t r u e ) ;179 / / Genero l o s p o l i n om i o s de v a r i a b l e180 S t r i n g p o l v a r s=” ” ;181 Vec to r<St r i ng> pa re s = t s . g e t R e s t r i c c i o n e s V a r i a b l e s ( ” ( ” , ” ) ” ) ;182 NUMPOLVARS=0;183 f o r ( i n t i =0; i<pa re s . s i z e ( ) ; i++){184 s t r i n g T o F i l e ( ” s ”+i+”=pow ( ”+pa res . ge t ( i )+” , 2 ) + ”+pa res . ge t ( i )+

” ; ” , t r u e ) ;185 p o l v a r s+=” s ”+i ;186 i f ( i<pa re s . s i z e ( ) −1) p o l v a r s+=” , ” ;187 / / I n d i c o e l numero de p o l i n om i o s de v a r i a b l e s188 NUMPOLVARS++;189 }

190 s t r i n g T o F i l e ( ” ” , t r u e ) ;191 / / Dec la ro e l i d e a l con l o s p o l i n om i o s de r e s t r i c c i o n192 / / s t r i n g T o F i l e ( ” ” , t r u e ) ;193 / / s t r i n g T o F i l e ( ” i d e a l i=”+p o l v a r s + ” ; ” , t r u e ) ;194 / / s t r i n g T o F i l e ( ” ” , t r u e ) ;195 / / Genero l o s p o l i n om i o s de r e s t r i c c i o n196 S t r i n g p o l r e s=” ” ;197 pa re s = t s . g e t R e s t r i c c i o n e s P r o p o s i c i o n a l ( ” ( ” , ” ) ” ) ;198 i n t c on t =0;

153

Page 164: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

199 NUMPOLRES=0;200 f o r ( i n t i =1; i<pa re s . s i z e ( ) ; i=i +2){201 / / System . ou t . p r i n t l n ( ” p o l r e s ”+ con t+”=”+pares . ge t ( i −1)+”∗”+

pa re s . ge t ( i ) + ” ; ” ) ;202 s t r i n g T o F i l e ( ” i c ”+con t+”=NEG( Y ( ”+pa res . ge t ( i −1)+” , ”+pa res . ge t (

i )+” ) ) ; ” , t r u e ) ;203 p o l r e s+=” i c ”+con t ;204 i f ( i<pa re s . s i z e ( ) −2) p o l r e s+=” , ” ;205 c on t++;206 NUMPOLRES++; / / I n d i c o e l numero de p o l i n om i o s de v a r i a b l e s207 }

208 s t r i n g T o F i l e ( ” ” , t r u e ) ;209 s t r i n g T o F i l e ( ” J =[ ”+p o l v a r s+” , ”+p o l r e s+” ] ; ” , t r u e ) ;210 s t r i n g T o F i l e ( ” ” , t r u e ) ;211212 } e l se i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {213 Vec to r<St r i ng> pa re s = t s . g e t R e s t r i c c i o n e s P r o p o s i c i o n a l ( ” [ ” , ” ] ”

) ;214 / / Dec la ro e l a n i l l o215 i n t t o t a l v a r i a b l e s =5;216 s t r i n g T o F i l e ( ” r i n g r =2 , ( x ( 0 . . ” +( t s .

g e t T o t a l V a r i a b l e s R e s t r i c c i o n L o g i c a ( ) −1)+” ) ) , l p ; ” , t r u e ) ;217 s t r i n g T o F i l e ( ” ” , t r u e ) ;218 / / Genero l o s p o l i n om i o s de v a r i a b l e219 S t r i n g p o l v a r s=” ” ;

154

Page 165: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

220 pa re s = t s . g e t R e s t r i c c i o n e s V a r i a b l e s ( ” ( ” , ” ) ” ) ;221 NUMPOLVARS=0;222 f o r ( i n t i =0; i<pa re s . s i z e ( ) ; i++){223 s t r i n g T o F i l e ( ” po ly s ”+i+” =( ”+pa res . ge t ( i )+” ˆ2 ) + ”+pa res . ge t ( i

)+” ; ” , t r u e ) ;224 p o l v a r s+=” s ”+i ;225 i f ( i<pa re s . s i z e ( ) −1) p o l v a r s+=” , ” ;226 / / I n d i c o e l numero de p o l i n om i o s de v a r i a b l e s227 NUMPOLVARS++;228 }

229 s t r i n g T o F i l e ( ” ” , t r u e ) ;230 / / Genero e l i d e a l i231 s t r i n g T o F i l e ( ” i d e a l J=”+p o l v a r s+” ; ” , t r u e ) ;232 s t r i n g T o F i l e ( ” J = s t d ( J ) ; ” , t r u e ) ;233 s t r i n g T o F i l e ( ” ” , t r u e ) ;234 / / I n c l u y o l a s f u n c i o n e s235 s t r i n g T o F i l e ( ” p r o c NEG( po ly M) { r e t u r n ( r educe (1+M, J ) ) ; } ” , t r u e )

;236 s t r i n g T o F i l e ( ” p r o c O( po ly M, po ly N) { r e t u r n ( r educe (M∗N, J ) ) ; } ” ,

t r u e ) ;237 s t r i n g T o F i l e ( ” p r o c Y ( po ly M, po ly N) { r e t u r n (NEG(O(NEG(M) ,NEG(N)

) ) ) ; } ” , t r u e ) ;238 s t r i n g T o F i l e ( ” ” , t r u e ) ;239 / / Genero l o s p o l i n om i o s de r e s t r i c c i o n240 S t r i n g p o l r e s=” ” ;

155

Page 166: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

241 pa re s = t s . g e t R e s t r i c c i o n e s P r o p o s i c i o n a l ( ” ( ” , ” ) ” ) ;242 i n t c on t =0;243 NUMPOLRES=0;244 f o r ( i n t i =1; i<pa re s . s i z e ( ) ; i=i +2){245 / / System . ou t . p r i n t l n ( ” p o l r e s ”+ con t+”=”+pares . ge t ( i −1)+”∗”+

pa re s . ge t ( i ) + ” ; ” ) ;246 s t r i n g T o F i l e ( ” po ly i c ”+con t+”=NEG( Y ( ”+pa res . ge t ( i −1)+” , ”+pa res

. ge t ( i )+” ) ) ; ” , t r u e ) ;247 p o l r e s+=” i c ”+con t ;248 i f ( i<pa re s . s i z e ( ) −2) p o l r e s+=” , ” ;249 c on t++;250 NUMPOLRES++; / / I n d i c o e l numero de p o l i n om i o s de v a r i a b l e s251 }

252 s t r i n g T o F i l e ( ” ” , t r u e ) ;253254 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {255 Vec to r<St r i ng> pa re s = t s . g e t R e s t r i c c i o n e s P r o p o s i c i o n a l ( ” [ ” , ” ] ”

) ;256 / / Dec la ro e l a n i l l o257 s t r i n g T o F i l e ( ”USE Z / ( 2 ) [ x [ 0 . . ” +( t s .

g e t T o t a l V a r i a b l e s R e s t r i c c i o n L o g i c a ( ) −1)+” ] ] ; ” , t r u e ) ;258 s t r i n g T o F i l e ( ” ” , t r u e ) ;259 / / Genero l o s p o l i n om i o s de v a r i a b l e260 S t r i n g p o l v a r s=” ” ;261 pa re s = t s . g e t R e s t r i c c i o n e s V a r i a b l e s ( ” [ ” , ” ] ” ) ;

156

Page 167: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

262 NUMPOLVARS=0;263 f o r ( i n t i =0; i<pa re s . s i z e ( ) ; i++){264 s t r i n g T o F i l e ( ”S ”+i+” :=( ”+pa res . ge t ( i )+” ˆ2 ) + ”+pa res . ge t ( i )+” ;

” , t r u e ) ;265 p o l v a r s+=”S”+i ;266 i f ( i<pa re s . s i z e ( ) −1) p o l v a r s+=” , ” ;267 / / I n d i c o e l numero de p o l i n om i o s de v a r i a b l e s268 NUMPOLVARS++;269 }

270 s t r i n g T o F i l e ( ” ” , t r u e ) ;271 / / Genereo e l i d e a l i272 s t r i n g T o F i l e ( ”MEMORY. J := I d e a l ( ”+p o l v a r s+” ) ; ” , t r u e ) ;273 / / I n c l u y o l a s f u n c i o n e s274 s t r i n g T o F i l e ( ” ” , t r u e ) ;275276 / / I n c l u y o l a s f u n c i o n e s277 s t r i n g T o F i l e ( ” De f i ne NEG(M) RETURN NF(1+M,MEMORY. J ) ; EndDef ine ;

” , t r u e ) ;278 s t r i n g T o F i l e ( ” De f i ne O(M,N) RETURN NF(M∗N,MEMORY. J ) ; EndDef ine ;

” , t r u e ) ;279 s t r i n g T o F i l e ( ” De f i ne Y (M,N) RETURN (NEG(O(NEG(M) ,NEG(N) ) ) ) ;

EndDef ine ; ” , t r u e ) ;280 s t r i n g T o F i l e ( ” ” , t r u e ) ;281 / / Genero l o s p o l i n om i o s de r e s t r i c c i o n282 S t r i n g p o l r e s=” ” ;

157

Page 168: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

283 pa re s = t s . g e t R e s t r i c c i o n e s P r o p o s i c i o n a l ( ” [ ” , ” ] ” ) ;284 i n t c on t =0;285 NUMPOLRES=0;286 f o r ( i n t i =1; i<pa re s . s i z e ( ) ; i=i +2){287 / / System . ou t . p r i n t l n ( ” p o l r e s ”+ con t+”=”+pares . ge t ( i −1)+”∗”+

pa re s . ge t ( i ) + ” ; ” ) ;288 s t r i n g T o F i l e ( ” IC ”+con t+” :=NEG( Y ( ”+pa res . ge t ( i −1)+” , ”+pa res . ge t

( i )+” ) ) ; ” , t r u e ) ;289 p o l r e s+=” IC ”+con t ;290 i f ( i<pa re s . s i z e ( ) −2) p o l r e s+=” , ” ;291 c on t++;292 NUMPOLRES++; / / I n d i c o e l numero de p o l i n om i o s de v a r i a b l e s293 }

294 s t r i n g T o F i l e ( ” ” , t r u e ) ;295 }

296297 } e l se i f (MODELO . equa l s ( ”NUEVOALGEBRAICO” ) ) {298 i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {299300 s t r i n g T o F i l e ( ” r i n g r=”+t s . getMayorPrimo ( )+” , ( x ( 0 . . ” +( t s .

getNumVars ( ) −1)+” ) ) , l p ; ” , t r u e ) ;301 s t r i n g T o F i l e ( ” ” , t r u e ) ;302 / / Genero l o s p o l v a r s303 S t r i n g a u x p o l v a r s=” ” , p o l v a r s=” ” ;304 NUMPOLVARS=0;

158

Page 169: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

305 f o r ( i n t i =0; i<t s . getNumVars ( ) ; i++){306 a u x p o l v a r s+=” po ly s ”+i+”=” ;307 p o l v a r s+=” s ”+i ;308 i f ( i<t s . getNumVars ( ) −1) p o l v a r s+=” , ” ;309 f o r ( i n t v=t s . ge tNumValo resDeVar iab le ( i ) −1; v>=0;v−−){310 a u x p o l v a r s+=” ( x ( ”+i+” )−”+v+” ) ” ;311 i f ( v>0) a u x p o l v a r s+=” ∗ ” ;312 }

313 a u x p o l v a r s+=” ; \ n ” ;314 NUMPOLVARS++;315 }

316 s t r i n g T o F i l e ( a u x po l v a r s , t r u e ) ;317 / / Genero e l i d e a l J318 s t r i n g T o F i l e ( ” i d e a l J=”+p o l v a r s+” ; ” , t r u e ) ;319 s t r i n g T o F i l e ( ” J = s t d ( J ) ; ” , t r u e ) ;320 / / I n c l u y o l a s f u n c i o n e s321 s t r i n g T o F i l e ( ” ” , t r u e ) ;322 s t r i n g T o F i l e ( ” p r o c NEG( po ly M) { r e t u r n ( r educe (Mˆ ” +( t s .

getMayorPrimo ( ) −1)+” −1 , J ) ) ; } ” , t r u e ) ;323 s t r i n g T o F i l e ( ” p r o c O( po ly M, po ly N) { r e t u r n ( r educe (M∗N, J ) ) ; } ” ,

t r u e ) ;324 s t r i n g T o F i l e ( ” ” , t r u e ) ;325326327 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {

159

Page 170: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

328329 s t r i n g T o F i l e ( ”USE Z / ( ”+t s . getMayorPrimo ( )+” ) [ x [ 0 . . ” +( t s .

getNumVars ( ) −1)+” ] ] ; ” , t r u e ) ;330 s t r i n g T o F i l e ( ” ” , t r u e ) ;331 / / Genero l o s p o l v a r s332 S t r i n g a u x p o l v a r s=” ” , p o l v a r s=” ” ;333 NUMPOLVARS=0;334 f o r ( i n t i =0; i<t s . getNumVars ( ) ; i++){335 a u x p o l v a r s+=”S”+i+” := ” ;336 p o l v a r s+=”S”+i ;337 i f ( i<t s . getNumVars ( ) −1) p o l v a r s+=” , ” ;338 f o r ( i n t v=t s . ge tNumValo resDeVar iab le ( i ) −1; v>=0;v−−){339 a u x p o l v a r s+=” ( x [ ”+i+” ]− ”+v+” ) ” ;340 i f ( v>0) a u x p o l v a r s+=” ∗ ” ;341 }

342 a u x p o l v a r s+=” ; \ n ” ;343 NUMPOLVARS++;344 }

345 s t r i n g T o F i l e ( a u x po l v a r s , t r u e ) ;346 s t r i n g T o F i l e ( ” ” , t r u e ) ;347 / / Genereo e l i d e a l i348 s t r i n g T o F i l e ( ”MEMORY. J := I d e a l ( ”+p o l v a r s+” ) ; ” , t r u e ) ;349 / / I n c l u y o l a s f u n c i o n e s350 s t r i n g T o F i l e ( ” ” , t r u e ) ;351

160

Page 171: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

352 s t r i n g T o F i l e ( ” De f i ne NEG(M) RETURN NF(Mˆ ” +( t s . getMayorPrimo ( )−1)+” −1 ,MEMORY. J ) ; EndDef ine ; ” , t r u e ) ;

353 s t r i n g T o F i l e ( ” De f i ne O(M,N) RETURN NF(M∗N,MEMORY. J ) ; EndDef ine ;” , t r u e ) ;

354 s t r i n g T o F i l e ( ” ” , t r u e ) ;355 }

356 }

357 } ca t ch ( E x c ep t i o n e ) {358 / / e . p r i n t S t a c k T r a c e ( ) ;359 System . e x i t ( 0 ) ;360 }

361362 }

363364365366367 pub l i c s t a t i c vo i d s t r i n g T o F i l e ( S t r i n g msg , boolean append ) {368 t r y {

369 S t r i n g FILE CODE=” ” ;370371 S t r i n g LOG POLYBORI=” s em l p b p o l y b o r i . t x t ” ;372 S t r i n g LOG SINGULAR=” s em l p b s i n g u l a r . t x t ” ;373 S t r i n g LOG COCOA=” sem lpb cocoa . t x t ” ;374 S t r i n g NMA SINGULAR=” s em c v a s i n g u l a r . t x t ” ;

161

Page 172: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

375 S t r i n g NMA COCOA=” semcva cocoa . t x t ” ;376377 i f (MODELO . equa l s ( ”PROPOSICIONAL ” ) ) {378 i f ( SALIDA . equa l s ( ”POLYBORI ” ) ) {379 FILE CODE=LOG POLYBORI ;380 } e l se i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {381 FILE CODE=LOG SINGULAR ;382 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {383 FILE CODE=LOG COCOA ;384 }

385 } e l se i f (MODELO . equa l s ( ”NUEVOALGEBRAICO” ) ) {386 i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {387 FILE CODE=NMA SINGULAR ;388 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {389 FILE CODE=NMA COCOA ;390 }

391 }

392 / / I n d i c o e l f i c h e r o de s a l i d a en una v a r i a b l e g l o b a l para luego393 / / i n d i c a r donde se genero e l c od i go a l u s u a r i o394 OUTPUT FILE=FILE CODE ;395 F i l eW r i t e r f s t r e am = new F i l eW r i t e r ( FILE CODE , append ) ;396 Bu f f e r e dW r i t e r ou t = new Bu f f e r e dW r i t e r ( f s t r e am ) ;397 ou t . w r i t e ( msg+” \n ” ) ;398 ou t . c l o s e ( ) ;399 } ca t ch ( E x c ep t i o n e ) {

162

Page 173: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

400 System . e r r . p r i n t l n ( ” E r r o r : ” + e . getMessage ( ) ) ;401 }

402 }

403404405406 pub l i c s t a t i c vo i d c a l c u l a r b a s e g r o e b n e r ( ) {407 i f (MODELO . equa l s ( ”PROPOSICIONAL ” ) ) {408 i f ( SALIDA . equa l s ( ”POLYBORI ” ) ) {409 / / Genero l o s p o l v a r s410 S t r i n g p o l v a r s=” ” ;411 f o r ( i n t i =0; i<NUMPOLVARS; i++){412 p o l v a r s+=” v ”+i ;413 i f ( i<NUMPOLVARS−1) p o l v a r s+=” , ” ;414 e l se i f ( i==NUMPOLVARS−1) p o l v a r s+=” , ” ;415 }

416 / / Genero l o s p o l r e s417 S t r i n g p o l r e s=” ” ;418 f o r ( i n t i =0; i<NUMPOLRES; i++){419 p o l r e s+=” i c ”+i ;420 i f ( i<NUMPOLRES−1) p o l r e s+=” , ” ;421 e l se i f ( i==NUMPOLRES−1) p o l r e s+=” , ” ;422 }

423 / / Genero l o s p o l i n p u t s424 S t r i n g p o l i n p u t s=” ” ;

163

Page 174: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

425 f o r ( i n t i =0; i<NUMREGLASINPUT ; i++){426 p o l i n p u t s+=” i ”+i ;427 i f ( i<NUMREGLASINPUT−1) p o l i n p u t s+=” , ” ;428 e l se i f ( i==NUMREGLASINPUT−1) p o l i n p u t s+=” ” ;429 }

430 / / Genero l o s p o l i n om i o s de r e g l a s431 S t r i n g p o l r e g l a s=” ” ;432 f o r ( i n t i =0; i<NUMREGLAS; i++){433 p o l r e g l a s+=” r ”+i ;434 i f ( i<NUMREGLAS−1) p o l r e g l a s+=” , ” ;435 }

436 s t r i n g T o F i l e ( ” ” , t r u e ) ;437 i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ” I =[ ”+p o l i n p u t s+” ] ; ” , t r u e ) ;438 s t r i n g T o F i l e ( ”K=[ ”+p o l r e g l a s+” ] ; ” , t r u e ) ;439 s t r i n g T o F i l e ( ” ” , t r u e ) ;440 / / Creo l a l lamada441 i f (BENCHMARK) s t r i n g T o F i l e ( ” a=t ime . t ime ( ) ; ” , t r u e ) ;442443 i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ” gb=g r o e b n e r b a s i s ( J+K+I ,

h e u r i s t i c=False ) ; ” , t r u e ) ;444 e l se s t r i n g T o F i l e ( ” gb=g r o e b n e r b a s i s ( J+K , h e u r i s t i c=False ) ; ” ,

t r u e ) ;445446 i f (BENCHMARK)447 s t r i n g T o F i l e ( ” p r i n t t ime . t ime ( )−a ; ” , t r u e ) ;

164

Page 175: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

448 e l se {

449 / / t s . ge tNumSoluc iones ( )−1 => deb ido a l a o p t i m i z a c i o n e lu l t i m o v a l o r

450 / / se s u s t i t u y e por e l negado de l r e s t o451 s t r i n g T o F i l e ( ” n f=e l i m i n a t e ( gb ) [ 1 ] ; ” , t r u e ) ;452 f o r ( i n t s=0; s<t s . ge tNumSoluc iones ( ) −1; s++)453 s t r i n g T o F i l e ( ” p r i n t n f ( x ( ”+s+” ) ) ; ” , t r u e ) ;454 }

455456 } e l se i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {457 / / Genero l o s p o l v a r s458 S t r i n g p o l v a r s=” ” ;459 f o r ( i n t i =0; i<NUMPOLVARS; i++){460 p o l v a r s+=” v ”+i ;461 i f ( i<NUMPOLVARS−1) p o l v a r s+=” , ” ;462 e l se i f ( i==NUMPOLVARS−1) p o l v a r s+=” , ” ;463 }

464 / / Genero l o s p o l r e s465 S t r i n g p o l r e s=” ” ;466 f o r ( i n t i =0; i<NUMPOLRES; i++){467 p o l r e s+=” i c ”+i ;468 i f ( i<NUMPOLRES−1) p o l r e s+=” , ” ;469 e l se i f ( i==NUMPOLRES−1) p o l r e s+=” , ” ;470 }

471 / / Genero l o s p o l i n p u t s165

Page 176: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

472 S t r i n g p o l i n p u t s=” ” ;473 f o r ( i n t i =0; i<NUMREGLASINPUT ; i++){474 p o l i n p u t s+=” i ”+i ;475 i f ( i<NUMREGLASINPUT−1) p o l i n p u t s+=” , ” ;476 e l se i f ( i==NUMREGLASINPUT−1) p o l i n p u t s+=” ” ;477 }

478 / / Genero l o s p o l i n om i o s de r e g l a s479 S t r i n g p o l r e g l a s=” ” ;480 f o r ( i n t i =0; i<NUMREGLAS; i++){481 p o l r e g l a s+=” r ”+i ;482 i f ( i<NUMREGLAS−1) p o l r e g l a s+=” , ” ;483 }

484 s t r i n g T o F i l e ( ” i d e a l K=”+p o l r e s+p o l r e g l a s+” ; ” , t r u e ) ;485 / / s t r i n g T o F i l e ( ” K = s t d (K) ; ” , t r u e ) ;486 i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ” i d e a l I=”+p o l i n p u t s+” ; ” ,

t r u e ) ;487 / / i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ” I = s t d ( I ) ; ” , t r u e ) ;488 s t r i n g T o F i l e ( ” ” , t r u e ) ;489 / / Creao l a l lamada490 i f (BENCHMARK) {491 s t r i n g T o F i l e ( ” i n t t =0;\ n t ime r =0; ” , t r u e ) ;492 s t r i n g T o F i l e ( ” sys tem (\”−− t i c k s −per−sec \ ” , 1 0 0 0 ) ; ” , t r u e ) ;493 s t r i n g T o F i l e ( ” t=t im e r ; ” , t r u e ) ;494 }

166

Page 177: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

495 i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ” i d e a l BASE=s l imgb ( J+K+I ) ; ” ,t r u e ) ;

496 e l se s t r i n g T o F i l e ( ” i d e a l BASE=s l imgb ( J+K) ; ” , t r u e ) ;497 i f (BENCHMARK)498 s t r i n g T o F i l e ( ” p r i n t ( t ime r−t ) ; ” , t r u e ) ;499 e l se {

500 s t r i n g T o F i l e ( ” i d e a l BASESTD=s t d (BASE) ; ” , t r u e ) ;501 / / t s . ge tNumSoluc iones ( )−1 => deb ido a l a o p t i m i z a c i o n e l

u l t i m o v a l o r502 / / se s u s t i t u y e por e l negado de l r e s t o503 f o r ( i n t s=0; s<t s . ge tNumSoluc iones ( ) −1; s++)504 s t r i n g T o F i l e ( ” p r i n t ( r educe ( x ( ”+s+” ) ,BASESTD) ) ; ” , t r u e ) ;505 }

506 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {507 / / Genero l o s p o l v a r s508 S t r i n g p o l v a r s=” ” ;509 f o r ( i n t i =0; i<NUMPOLVARS; i++){510 p o l v a r s+=”V ”+i ;511 i f ( i<NUMPOLVARS−1) p o l v a r s+=” , ” ;512 e l se i f ( i==NUMPOLVARS−1) p o l v a r s+=” , ” ;513 }

514 / / Genero l o s p o l r e s515 S t r i n g p o l r e s=” ” ;516 f o r ( i n t i =0; i<NUMPOLRES; i++){517 p o l r e s+=” IC ”+i ;

167

Page 178: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

518 i f ( i<NUMPOLRES−1) p o l r e s+=” , ” ;519 }

520 / / Genero l o s p o l i n p u t s521 S t r i n g p o l i n p u t s=” ” ;522 f o r ( i n t i =0; i<NUMREGLASINPUT ; i++){523 p o l i n p u t s+=” I ”+i ;524 i f ( i<NUMREGLASINPUT−1) p o l i n p u t s+=” , ” ;525 }

526 / / Genero l o s p o l i n om i o s de r e g l a s527 S t r i n g p o l r e g l a s=” ” ;528 f o r ( i n t i =0; i<NUMREGLAS; i++){529 p o l r e g l a s+=”R”+i ;530 i f ( i<NUMREGLAS−1) p o l r e g l a s+=” , ” ;531 }

532 / / Creao l a l lamada533 i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ” I := I d e a l ( ”+p o l i n p u t s+” ) ; ” ,

t r u e ) ;534 i f ( p o l r e s . l e n g t h ( ) >0)535 s t r i n g T o F i l e ( ”K:= I d e a l ( ”+p o l r e s+” , ”+p o l r e g l a s+” ) ; ” , t r u e ) ;536 e l se

537 s t r i n g T o F i l e ( ”K:= I d e a l ( ”+p o l r e g l a s+” ) ; ” , t r u e ) ;538 / / Obtengo t oda s l a s ” s a l i d a s s o l u c i o n ”539 / / t s . ge tNumSoluc iones ( )−1 => deb ido a l a o p t i m i z a c i o n e l

u l t i m o v a l o r540 / / se s u s t i t u y e por e l negado de l r e s t o

168

Page 179: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

541 i f (BENCHMARK) {542 i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ” Time T:=GBasis (MEMORY. J+K+

I ) ; ” , t r u e ) ;543 e l se s t r i n g T o F i l e ( ” Time T:=GBasis (MEMORY. J+K) ; ” , t r u e ) ;544 } e l se {

545 f o r ( i n t s=0; s<t s . ge tNumSoluc iones ( ) −1; s++)546 i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ”NF( x [ ”+s+” ] ,MEMORY. J+K+I )

; ” , t r u e ) ;547 e l se s t r i n g T o F i l e ( ”NF( x [ ”+s+” ] ,MEMORY. J+K) ; ” , t r u e ) ;548 }

549550 }

551 } e l se i f (MODELO . equa l s ( ”NUEVOALGEBRAICO” ) ) {552 i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {553 / / Genero l o s p o l v a r s554 S t r i n g p o l v a r s=” ” ;555 f o r ( i n t i =0; i<NUMPOLVARS; i++){556 p o l v a r s+=” v ”+i ;557 i f ( i<NUMPOLVARS−1) p o l v a r s+=” , ” ;558 e l se i f ( i==NUMPOLVARS−1) p o l v a r s+=” , ” ;559 }

560 / / Genero l o s p o l i n p u t s561 S t r i n g p o l i n p u t s=” ” ;562 f o r ( i n t i =0; i<NUMREGLASINPUT ; i++){563 p o l i n p u t s+=” i ”+i ;

169

Page 180: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

564 i f ( i<NUMREGLASINPUT−1) p o l i n p u t s+=” , ” ;565 e l se i f ( i==NUMREGLASINPUT−1) p o l i n p u t s+=” ” ;566 }

567 / / Genero l o s p o l i n om i o s de r e g l a s568 S t r i n g p o l r e g l a s=” ” ;569 f o r ( i n t i =0; i<NUMREGLAS; i++){570 p o l r e g l a s+=” r ”+i ;571 i f ( i<NUMREGLAS−1) p o l r e g l a s+=” , ” ;572 }

573 s t r i n g T o F i l e ( ” ” , t r u e ) ;574 s t r i n g T o F i l e ( ” i d e a l K=”+p o l r e g l a s+” ; ” , t r u e ) ;575 / / s t r i n g T o F i l e ( ” K = s t d (K) ; ” , t r u e ) ;576 i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ” i d e a l I=”+p o l i n p u t s+” ; ” ,

t r u e ) ;577 / / i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ” I = s t d ( I ) ; ” , t r u e ) ;578 / / Creao l a l lamada579 s t r i n g T o F i l e ( ” ” , t r u e ) ;580 / / Creao l a l lamada581 i f (BENCHMARK) {582 s t r i n g T o F i l e ( ” i n t t =0;\ n t ime r =0; ” , t r u e ) ;583 s t r i n g T o F i l e ( ” sys tem (\”−− t i c k s −per−sec \ ” , 1 0 0 0 ) ; ” , t r u e ) ;584 s t r i n g T o F i l e ( ” t=t im e r ; ” , t r u e ) ;585 }

586 i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ” i d e a l BASE=s l imgb ( J+K+I ) ; ” ,t r u e ) ;

170

Page 181: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

587 e l se s t r i n g T o F i l e ( ” i d e a l BASE=s l imgb ( J+K) ; ” , t r u e ) ;588 i f (BENCHMARK) {589 s t r i n g T o F i l e ( ” p r i n t ( t ime r−t ) ; ” , t r u e ) ;590 } e l se {

591 s t r i n g T o F i l e ( ” i d e a l BASESTD=s t d (BASE) ; ” , t r u e ) ;592 s t r i n g T o F i l e ( ” ” , t r u e ) ;593 s t r i n g T o F i l e ( ” p r i n t ( r educe ( x ( 0 ) ,BASESTD) ) ; ” , t r u e ) ;594 }

595 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {596 / / Genero l o s p o l v a r s597 S t r i n g p o l v a r s=” ” ;598 f o r ( i n t i =0; i<NUMPOLVARS; i++){599 p o l v a r s+=”V ”+i ;600 i f ( i<NUMPOLVARS−1) p o l v a r s+=” , ” ;601 e l se i f ( i==NUMPOLVARS−1) p o l v a r s+=” , ” ;602 }

603 / / Genero l o s p o l i n p u t s604 S t r i n g p o l i n p u t s=” ” ;605 f o r ( i n t i =0; i<NUMREGLASINPUT ; i++){606 p o l i n p u t s+=” I ”+i ;607 i f ( i<NUMREGLASINPUT−1) p o l i n p u t s+=” , ” ;608 }

609 / / Genero l o s p o l i n om i o s de r e g l a s610 S t r i n g p o l r e g l a s=” ” ;611 f o r ( i n t i =0; i<NUMREGLAS; i++){

171

Page 182: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

612 p o l r e g l a s+=”R”+i ;613 i f ( i<NUMREGLAS−1) p o l r e g l a s+=” , ” ;614 }

615 / / Creao l a l lamada616 s t r i n g T o F i l e ( ” ” , t r u e ) ;617 s t r i n g T o F i l e ( ”K:= I d e a l ( ”+p o l r e g l a s+” ) ; ” , t r u e ) ;618 i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ” I := I d e a l ( ”+p o l i n p u t s+” ) ; ” ,

t r u e ) ;619 s t r i n g T o F i l e ( ” ” , t r u e ) ;620 i f (BENCHMARK) {621 i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ” Time T:=GBasis (MEMORY. J+K+

I ) ; ” , t r u e ) ;622 e l se s t r i n g T o F i l e ( ” Time T:=GBasis (MEMORY. J+K) ; ” , t r u e ) ;623 } e l se

624 i f ( FLAGEXISTEENTRADA ) s t r i n g T o F i l e ( ”NF( x [ 0 ] ,MEMORY. J+K+I ) ; ” ,t r u e ) ;

625 e l se s t r i n g T o F i l e ( ”NF( x [ 0 ] ,MEMORY. J+K) ; ” , t r u e ) ;626 }

627 }

628 }

629630631 pub l i c s t a t i c vo i d mos t ra rWarn ings ( ) {632 Vec to r<St r i ng> warn ings = t s . ge tWarn ingVar iab lesNoUsadas ( ) ;633 i f ( warn ings . s i z e ( ) >0) {

172

Page 183: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

634 System . ou t . p r i n t l n ( ” Warning ! V a r i a b l e s no usadas : ”+warn ings ) ;635 }

636 }

637638 /∗639 BEGIN

======================================================================640 ∗ /641 / / I n i c o de l programa642 pub l i c s t a t i c vo i d main ( S t r i n g [ ] a r g s ) {

643644 t r y {

645646 / / Compruebo e l numero de pa rame t r o s que se l e pasan a l c omp i l a do r647 i f ( a rg s . l eng th >4 | | a rgs . l eng th <2) {648 mo s t r a r e r r o r d e p a r am e t r o s ( ) ;649 }

650 / / C l a s i f i c o l a s opc i ones , i n d i c a ndo e r r o r s i no e s t an den t r o de l a sop c i o ne s

651 / / v a l i d a d a s652 i f ( a rg s [ 0 ] . e qua l s ( ” 1 ” ) ) {653 MODELO=”PROPOSICIONAL ” ;654 i f ( a rg s [ 1 ] . e qua l s ( ” 1 ” ) ) {655 SALIDA=”POLYBORI ” ;

173

Page 184: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

656 } e l se i f ( a rg s [ 1 ] . e qua l s ( ” 2 ” ) ) {657 SALIDA=”SINGULAR ” ;658 } e l se i f ( a rg s [ 1 ] . e qua l s ( ” 3 ” ) ) {659 SALIDA=”COCOA” ;660 } e l se {

661 mo s t r a r e r r o r d e p a r am e t r o s ( ) ;662 }

663 }

664 e l se i f ( a rg s [ 0 ] . e qua l s ( ” 2 ” ) ) {665 MODELO=”NUEVOALGEBRAICO” ;666 i f ( a rg s [ 1 ] . e qua l s ( ” 1 ” ) ) {667 SALIDA=”SINGULAR ” ;668 } e l se i f ( a rg s [ 1 ] . e qua l s ( ” 2 ” ) ) {669 SALIDA=”COCOA” ;670 } e l se {

671 mo s t r a r e r r o r d e p a r am e t r o s ( ) ;672 }

673 } e l se {

674 mo s t r a r e r r o r d e p a r am e t r o s ( ) ;675 }

676677 boolean most ra rTS=f a l s e ;678 BENCHMARK=f a l s e ;679680 i f ( a rg s . l e n g t h==3) {

174

Page 185: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

681 i f ( a rg s [ 2 ] . e qua l s ( ”− t s ” ) ) {682 most ra rTS=t r u e ;683 } e l se i f ( a rg s [ 2 ] . e qua l s ( ”−b ” ) ) {684 BENCHMARK=t r u e ;685 } e l se {

686 mo s t r a r e r r o r d e p a r am e t r o s ( ) ;687 }

688 }

689690 i f ( a rg s . l e n g t h==4) {691 i f ( a rg s [ 2 ] . e qua l s ( ”− t s ” ) && a rgs [3 ]== n u l l ) {692 most ra rTS=t r u e ;693 } e l se i f ( a rg s [ 2 ] . e qua l s ( ”−b ” ) && a rgs [3 ]== n u l l ) {694 BENCHMARK=t r u e ;695 }

696 e l se i f ( a rg s [ 2 ] . e qua l s ( ”−b ” ) && a rgs [ 3 ] . e qua l s ( ”−b ” ) ) {697 BENCHMARK=t r u e ;698 } e l se i f ( a rg s [ 2 ] . e qua l s ( ”− t s ” ) && a rgs [ 3 ] . e qua l s ( ”−b ” ) ) {699 BENCHMARK=t r u e ;700 most ra rTS=t r u e ;701 } e l se i f ( a rg s [ 2 ] . e qua l s ( ”−b ” ) && a rgs [ 3 ] . e qua l s ( ”− t s ” ) ) {702 BENCHMARK=t r u e ;703 most ra rTS=t r u e ;704 }

705 e l se {

175

Page 186: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

706 mo s t r a r e r r o r d e p a r am e t r o s ( ) ;707 }

708 }

709710 / / Creo l a t a b l a de s imbo l o s711 t s = new TS ( ) ;712 / / I n i c i o l a e s t r u c t u r a en donde i r e guardando l o s p o l i n om i o s713 p o l i n om i o s = new Vec to r<St r i ng > ( ) ;714 / / I n i c i o l a e s t r u c t u r a en donde i r e guardando l o s ope r ado r e s de l o s

p o l i n omo i s715 ope r ado r e s = new Vec to r<Boolean > ( ) ;716 / / Lanzo e l p r o c e s o de c omp i l a c i o n717 T r a du c t o r anLS = new T r a du c t o r ( System . i n ) ;718719 anLS . unPrograma ( t s ) ;720 i f ( mos t ra rTS )721 System . ou t . p r i n t l n ( t s . imp r im i rTS ( ) ) ;722 mos t ra rWarn ings ( ) ;723724725 / / Genero l o s f i c h e r o s de l a t a b l a de s imbo l o s c o r r e s p o n d i e n t e s726 / / a l a op c i on de l modelo e l e g i d o727728 i f (MODELO . equa l s ( ”PROPOSICIONAL ” ) ) {729 t s . s a l i d aAF i c h e r o ( 0 , ” t s m l p 1 . t x t ” , n u l l ) ;

176

Page 187: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

730 } e l se i f (MODELO . equa l s ( ”NUEVOALGEBRAICO” ) ) {731 t s . s a l i d aAF i c h e r o ( 1 , ” t s m c v a 1 . t x t ” , ” t s m c v a 2 . t x t ” ) ;732 }

733734 S t r i n g msg mode l o s a l i d a=” ” ;735 i f (MODELO . equa l s ( ”PROPOSICIONAL ” ) ) m sg mode l o s a l i d a=” Log i c o

p r o p o s i c i o n a l boo leano ” ;736 e l se i f (MODELO . equa l s ( ”NUEVOALGEBRAICO” ) ) m sg mode l o s a l i d a=” ’

Concepto−A t r i b u t o−Va lo r ’ ” ;737738 S t r i n g m s g s i s t ema s a l i d a=” ” ;739 i f ( SALIDA . equa l s ( ”POLYBORI ” ) ) m s g s i s t ema s a l i d a=” Po l ybo r i ” ;740 e l se i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) m s g s i s t ema s a l i d a=” S i ngu l a r ” ;741 e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) m s g s i s t ema s a l i d a=” Cocoa ” ;742743 System . ou t . p r i n t l n ( ” \nEl cod i go se ha generado en e l f i c h e r o ”+

OUTPUT FILE . toUpperCase ( ) ) ;744 System . ou t . p r i n t l n ( ” MODELO: ”+msg mode l o s a l i d a ) ;745 System . ou t . p r i n t l n ( ” SISTEMA : ”+ms g s i s t ema s a l i d a ) ;746747 } ca t ch ( TokenMgrError tme ) {748 / / Para p e r s o n l i z a r e l e r r o r t o c a r en TokenMgrError . j a v a749 System . ou t . p r i n t l n ( tme ) ;750 System . e x i t ( 0 ) ;751 } ca t ch ( Pa r s eEx cep t i on pee ) {

177

Page 188: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

752 / / Para p e r s o n l i z a r e l e r r o r t o c a r en Pa r s eEx cep t i on . j a v a753 System . ou t . p r i n t l n ( pee ) ;754 System . e x i t ( 0 ) ;755 } ca t ch ( E x c ep t i o n e ) {756 System . ou t . p r i n t l n ( e ) ;757 System . e x i t ( 0 ) ;758 }

759760 }

761 }

762763764 PARSER END ( T r a du c t o r )765766 vo i d unPrograma ( TS t s ) :767 {}

768 {

769 ”BEGIN” ”BEGIN VARS ” ( d e c v a r i a b l e ( t s ) )+ { t r y { t s . g e n e r a r I n d i c e s ( ); } ca t ch ( E x c ep t i o n e ) {System . ou t . p r i n t l n ( e ) ; System . e x i t ( 0 ) ; }s t r i n g T o F i l e ( ” ” , f a l s e ) ; } ”END VARS” {g e n e r a r P o l i n om i o s R e s t r i c c i o n ( ) ; } ” BEGIN KB ” ( r e g l a ( ) ) ∗ ”END KB” {FLAGEXISTEENTRADA=f a l s e ; } [ ” BEGIN INPUT ” i n p u t ( ) ”END INPUT” ]{ c a l c u l a r b a s e g r o e b n e r ( ) ; } ”END. ”

770 }

771

178

Page 189: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

772 TOKEN MGR DECLS :773 {

774 s t a t i c i n t numLin = 0 ;775 s t a t i c S t r i n g l i n e a = ” ” ;776 s t a t i c vo i d CommonTokenAction ( Token p i e z a ) {777 l i n e a = l i n e a + p i e z a . image ;778 }

779 }

780 vo i d d e c v a r i a b l e ( TS t s ) :781 {

782 Token p i e z a v a r i a b l e=nu l l , p i e zanumero=n u l l ; S t r i n g v a l o r=” ” ;783 Vec to r<St r i ng> v a r i a b l e sEncadenada s = new Vec to r<St r i ng > ( ) ;784 }

785 {

786 p i e z a v a r i a b l e=<id> [ ” [ ” p i e zanumero=<numero> ” ] ” ]787 {

788 / / I n s e r t o l a v a r i a b l e789 t r y {

790 / / T r a t o e l numero de v a r i a b l e s791 i n t numva r i a b l e s =1;792 i f ( p i e zanumero != n u l l )793 numva r i a b l e s=I n t e g e r . p a r s e I n t ( p iezanumero . t o S t r i n g ( ) ) ;794 / / I n s e r t o l a v a r i a b l e795 / / Encadeno l a p r ime ra v a r i a b l e796 v a r i a b l e sEncadenada s . add ( p i e z a v a r i a b l e . t o S t r i n g ( ) ) ;

179

Page 190: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

797 t s . i n s e r t a V a r i a b l e ( p i e z a v a r i a b l e . t o S t r i n g ( ) , p i e z a v a r i a b l e .t o S t r i n g ( ) , n umva r i a b l e s ) ;

798 / / Ahora i n s e r t o t odo s l o s v a l o r e s para l a v a r i a b l e799800 } ca t ch ( E x c ep t i o n e ) {801 System . ou t . p r i n t l n ( ” E r r o r en l i n e a ”+p i e z a v a r i a b l e . b eg i nL i n e+” \n

” ) ;802 System . ou t . p r i n t l n ( e ) ;803 / / e . p r i n t S t a c k T r a c e ( ) ;804 System . e x i t ( 0 ) ;805 }

806 }

807 / /NUEVO, i n s e r t o l a s v a r i a b l e s con l o s v a l o r e s de l a a n t e r i o r s iesque e x i s t e n encadenadas

808 / /−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

809 { / / Flag para sabe r s i en l a s d e f i n i c i o n e s de v a r i a b l e s se hanencadenado

810 / / s i es a s i gua rda re c ua l es l a padre para mas t a r d e sabe r s ipuedo compa r a r l a s

811 boolean encadenadas=f a l s e ;812 }

813 (814 ” , ” p i e z a v a r i a b l e=<id> [ ” [ ” p i e zanumero=<numero> ” ] ” ]

180

Page 191: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

815 / / I n s e r t o l a v a r i a b l e816 {

817 v a r i a b l e sEncadenada s . add ( p i e z a v a r i a b l e . t o S t r i n g ( ) ) ;818 encadenadas=t r u e ;819 t r y {

820 / / T r a t o e l numero de v a r i a b l e s821 i n t numva r i a b l e s =1;822 i f ( p i e zanumero != n u l l )823 numva r i a b l e s=I n t e g e r . p a r s e I n t ( p iezanumero . t o S t r i n g ( ) ) ;824 / / I n s e r t o l a v a r i a b l e825 t s . i n s e r t a V a r i a b l e ( p i e z a v a r i a b l e . t o S t r i n g ( ) , v a r i ab l e sEncadenada s

. ge t ( 0 ) , n umva r i a b l e s ) ;826 / / Ahora i n s e r t o t o do s l o s v a l o r e s para l a v a r i a b l e827 } ca t ch ( E x c ep t i o n e ) {828 System . ou t . p r i n t l n ( ” E r r o r en l i n e a ”+p i e z a v a r i a b l e . b eg i nL i n e+” \

n ” ) ;829 System . ou t . p r i n t l n ( e ) ;830 / / e . p r i n t S t a c k T r a c e ( ) ;831 System . e x i t ( 0 ) ;832 }

833 }

834 ) ∗835836 ” : ” v a l o r=v a l o r ( )837 {

181

Page 192: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

838 / / I n s e r t o e l v a l o r de l a v a r i a b l e839 t r y {

840 / / I n s e r t o l o s v a l o r e s en cada p i e z a encadenada841 f o r ( i n t i =0; i<v a r i a b l e sEncadenada s . s i z e ( ) ; i++){842 t s . i n s e r t a V a l o r E nVa r i a b l e ( v a r i a b l e sEncadenada s . ge t ( i ) . t o S t r i n g ( )

, v a l o r ) ;843 }

844 } ca t ch ( E x c ep t i o n e ) {845 System . ou t . p r i n t l n ( ” E r r o r en l i n e a ”+p i e z a v a r i a b l e . b eg i nL i n e+” \n

” ) ;846 System . ou t . p r i n t l n ( e ) ;847 / / e . p r i n t S t a c k T r a c e ( ) ;848 System . e x i t ( 0 ) ;849 }

850 }

851 ( ” , ” v a l o r=v a l o r ( )852 {

853 / / I n s e r t o e l v a l o r de l a v a r i a b l e854 t r y {

855 / / I n s e r t o l o s v a l o r e s en cada p i e z a encadenada856 f o r ( i n t i =0; i<v a r i a b l e sEncadenada s . s i z e ( ) ; i++){857 t s . i n s e r t a V a l o r E nVa r i a b l e ( v a r i a b l e sEncadenada s . ge t ( i ) . t o S t r i n g ( )

, v a l o r ) ;858 }

859 } ca t ch ( E x c ep t i o n e ) {182

Page 193: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

860 System . ou t . p r i n t l n ( ” E r r o r en l i n e a ”+p i e z a v a r i a b l e . b eg i nL i n e+” \n” ) ;

861 System . ou t . p r i n t l n ( e ) ;862 / / e . p r i n t S t a c k T r a c e ( ) ;863 System . e x i t ( 0 ) ;864 }

865 }

866 ) ∗ ” ; ”867 }

868 S t r i n g v a l o r ( ) :869 {Token p i e z a ; }870 {

871 p i e z a=<id>872 {

873 r e t u r n p i e z a . t o S t r i n g ( ) ;874 }

875 }

876 vo i d r e g l a ( ) :877 {

878 S t r i n g r e s t o=” ” ;879 Vec to r<St r i ng> p0 ;880 Vec to r<Boolean> p0ops ;881 boolean i sE xp r e s i o nComp l e j a LaP r ime r a=f a l s e ;882 p0 = new Vec to r<St r i ng > ( ) ;883 p0ops = new Vec to r<Boolean > ( ) ;

183

Page 194: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

884 }

885 {

886 t r y {

887 e x p r e s i o n ( )888 {

889 p0=r e p o l s ( ) ;890 p0ops=reops ( ) ;891 i sE xp r e s i o nComp l e j a LaP r ime r a=FLAGEXPRESIONVALORCOMPLEJO ;892 }

893 [ r e s t o=r e s t o r e g l a ( ) ] ” ; ”894 {

895 / / T r a t o l a p r ime ra e x p r e s i o n896 S t r i n g ex1=” ” ;897 / / Miro s i l a p r ime ra e x p r e s i o n es c omp l e j a o no l o es para a s i

a p l i c a r l e898 / / l a negac i on a cada r e g l a por s e r an t e ceden t e , o no a p l i c a r e s t a

r e g l a .899 i f ( i sE xp r e s i o nComp l e j a LaP r ime r a ) {900 / / Llamamos a l a cadena con e l FLAG de an t e c eden t e a

FLAGANTECEDENTE901 / / porque se negara todo depend i en t e s i es an t e c eden t e o

c on se cuen t e902 / / en l a f u n c i o n acadena903 ex1=acadena ( p0 , p0ops , FLAGANTECEDENTE) ;904 } e l se {

184

Page 195: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

905 / / Generamos l a cadena ap l i c a nd o l a negac i on normal906 ex1=acadena ( p0 , p0ops , FLAGANTECEDENTE) ;907 }

908 / / Aqui tamb ien m i r a r e l t i p o de modelo y e l t i p o de v a r i a b l e909 i f (MODELO . equa l s ( ”PROPOSICIONAL ” ) ) {910911 i f ( SALIDA . equa l s ( ”POLYBORI ” ) ) {912 i f ( ! r e s t o . equa l s ( ” ” ) ) {913 s t r i n g T o F i l e ( ” r ”+NUMREGLAS+”=O( ”+ex1+” , ”+r e s t o+” ) ; ” , t r u e ) ;914 } e l se {

915 s t r i n g T o F i l e ( ” r ”+NUMREGLAS+”=”+ex1+” ; ” , t r u e ) ;916 }

917 } e l se i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {918 i f ( ! r e s t o . equa l s ( ” ” ) ) {919 s t r i n g T o F i l e ( ” po ly r ”+NUMREGLAS+”=O( ”+ex1+” , ”+r e s t o+” ) ; ” , t r u e )

;920 } e l se {

921 s t r i n g T o F i l e ( ” po ly r ”+NUMREGLAS+”=”+ex1+” ; ” , t r u e ) ;922 }

923 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {924 i f ( ! r e s t o . equa l s ( ” ” ) ) {925 s t r i n g T o F i l e ( ”R ”+NUMREGLAS+” :=O( ”+ex1+” , ”+r e s t o+” ) ; ” , t r u e ) ;926 } e l se {

927 s t r i n g T o F i l e ( ”R ”+NUMREGLAS+” := ”+ex1+” ; ” , t r u e ) ;928 }

185

Page 196: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

929 }

930931 } e l se i f (MODELO . equa l s ( ”NUEVOALGEBRAICO” ) ) {932933 i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {934 i f ( ! r e s t o . equa l s ( ” ” ) ) {935 s t r i n g T o F i l e ( ” po ly r ”+NUMREGLAS+”=O( ”+ex1+” , ”+r e s t o+” ) ; ” , t r u e )

;936 } e l se {

937 s t r i n g T o F i l e ( ” po ly r ”+NUMREGLAS+”=”+ex1+” ; ” , t r u e ) ;938 }

939 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {940 i f ( ! r e s t o . equa l s ( ” ” ) ) {941 s t r i n g T o F i l e ( ”R ”+NUMREGLAS+” :=O( ”+ex1+” , ”+r e s t o+” ) ; ” , t r u e ) ;942 } e l se {

943 s t r i n g T o F i l e ( ”R ”+NUMREGLAS+” := ”+ex1+” ; ” , t r u e ) ;944 }

945 }

946947 }

948 NUMREGLAS++;949 }

950 }

951 ca t ch ( E x c ep t i o n e ) {952 System . ou t . p r i n t l n ( e ) ;

186

Page 197: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

953 System . e x i t ( 0 ) ;954 }

955 }

956 S t r i n g r e s t o r e g l a ( ) :957 {

958 Vec to r<St r i ng> p1 , p2 , p3 ;959 p1 = new Vec to r<St r i ng > ( ) ;960 p2 = new Vec to r<St r i ng > ( ) ;961 p3 = new Vec to r<St r i ng > ( ) ;962 Vec to r<Boolean> p1ops , p2ops , p3ops ;963 p1ops = new Vec to r<Boolean > ( ) ;964 p2ops = new Vec to r<Boolean > ( ) ;965 p3ops = new Vec to r<Boolean > ( ) ;966 }

967 {

968969 / / PRIMERA FORMA DE LAS REGLAS970 {FLAGANTECEDENTE=f a l s e ; }971 c on s e cuen t e ( ) {p1=r e p o l s ( ) ; p1ops=reops ( ) ; } ( c o n s e cuen t e ( ) {

an i a d epo l s ( p1 ) ; an iadeops ( p1ops ) ; } ) ∗972 { r e t u r n acadena ( p1 , p1ops , f a l s e ) ; }973974975 / / SEGUNDA FORMA DE LA REGLAS976 |

187

Page 198: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

977 {FLAGANTECEDENTE=t r u e ; }978 ( a n t e c eden t e ( ) { an i a d epo l s ( p2 ) ; an iadeops ( p2ops ) ; } ) ∗979 ”−>” e x p r e s i o n ( ) {p3=r e p o l s ( ) ; p3ops=reops ( ) ; } ( c o n s e cuen t e ( ) {

an i a d epo l s ( p3 ) ; an iadeops ( p3ops ) ; } ) ∗980 {

981 i f ( p2 . s i z e ( ) >0) {982 r e t u r n ”O( ”+acadena ( p2 , p2ops , t r u e )+” , ”+acadena ( p3 , p3ops , f a l s e )+”

) ” ;983 } e l se {

984 r e t u r n acadena ( p3 , p3ops , f a l s e ) ;985 }

986987 }

988 }

989 vo i d c on s e cuen t e ( ) :990 { }

991 {

992 / / ( ”OR” | ”O ” ) e x p r e s i o n ( )993 ( ”OR” ) e x p r e s i o n ( )994 }

995 vo i d an t e c eden t e ( ) :996 { }

997 {

998 / / ( ”AND” | ” Y ” ) e x p r e s i o n ( )999 ( ”AND” ) e x p r e s i o n ( )

188

Page 199: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1000 }

1001 vo i d e x p r e s i o n ( ) :1002 {Token p i e z a v a r i a b l e=n u l l ; S t r i n g [ ] v1 , v2 ; S t r i n g aux=” ” ; boolean

ope rado r ; }1003 {

1004 ” ( ” v1=v a r i a b l e ( ) ope rado r=ope rado r ( ) v2=v a r i a b l e ( ) ” ) ”1005 / / En e s t e pun to tengo l a v a r i a b l e y e l v a l o r o v a r i a b l e a s i gnados .1006 / / Obtengo l a p r ime ra v a r i a b l e y mi ro s i e x i s t e ” e l segundo ” como

v a l o r1007 / / o como v a r i a b l e y ob tengo l a i n f o rm a c i o n .1008 {

1009 t r y {

10101011 / / ASIGNACION VARIABLE−VALOR1012 i f ( ! t s . e x i s t e V a r i a b l e ( v2 [ 0 ] ) ) {1013 / / marco l a v a r i a b l e para luego mo s t r a r warn ings s i no ha s i d o

usada1014 t s . ma r caVa r i ab l e ( v1 [ 0 ]+ ” [ ”+v1 [ 1 ]+ ” ] ” ) ;10151016 FLAGEXPRESIONVALORCOMPLEJO=f a l s e ;1017 / / Tenemos que comprobar que se e s t e a c ced i endo a l a v a r i a b l e

c o r r e c t amen t e1018 / / s i no no podremos p r o s e g u i r . Es una medida de r e s t r i c c i o n para

c l a r i f i c a r1019 / / e l c od i go .

189

Page 200: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

10201021 / / INFORMACION ESTRUCTURA DE DATOS1022 / / v [ 0 ] Con t i ene e l nombre de l a v a r i a b l e1023 / / v [ 1 ] Con t i ene e l i n d i c e de acceso a e l l a1024 / / Comprobamos que s i l a v a r i a b l e e s t a de c l a r ada como ar ray , s o l o

se pe rm i t a e l a c c c e s o a e l l a1025 / / desde nombrevar [ i n d i c e ] y no como : nombrevar1026 t s . c omp r ubaa c c e s o a v a r i a b l e ( v1 [ 0 ] , v1 [ 1 ] ) ;10271028 i f (MODELO . equa l s ( ”PROPOSICIONAL ” ) ) {10291030 i f ( SALIDA . equa l s ( ”POLYBORI ” ) | | ( SALIDA . equa l s ( ” SINGULAR ” ) ) ) {1031 i n t [ ] v a r i a b l e = t s . g e t I d e n t i f i c a d o r R e a l ( v1 [ 0 ] , I n t e g e r . p a r s e I n t

( v1 [ 1 ] ) , v2 [ 0 ] ) ;1032 / / Op t im i z a c i o n de l o s u l t i m o s v a l o r e s1033 S t r i n g v a r i a b l eOp t im i z a d a=” x ( ”+v a r i a b l e [ 0 ]+ ” ) ” ;1034 / / Compruebo s i a lguna de l a s dos v a r i a b l e s es e l u l t i m o v a l o r1035 i f ( v a r i a b l e [2 ]==1) {1036 v a r i a b l eOp t im i z a d a=”1+” ;1037 Vec to r<St r i ng> i d s 1=t s . g e t I d e n t i f i c a d o r e s R e a lO p t i m i z a d o ( v1 [ 0 ] ,

I n t e g e r . p a r s e I n t ( v1 [ 1 ] ) ) ;1038 f o r ( i n t c=0; c<i d s 1 . s i z e ( ) ; c++){1039 v a r i a b l eOp t im i z a d a +=” x ( ”+i d s1 . ge t ( c )+” ) ” ;1040 i f ( c<i d s 1 . s i z e ( ) −1) v a r i a b l eOp t im i z a d a+=” ∗ ” ;1041 }

190

Page 201: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1042 }

1043 p o l i n om i o s . add ( v a r i a b l eOp t im i z a d a ) ;1044 / / FLAGNEGARTODO a f a l s e s i emp re en e s t a p o s i c i o n1045 FLAGNEGARTODO=f a l s e ;1046 ope r ado r e s . add ( ope rado r ) ;1047 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {1048 i n t [ ] v a r i a b l e = t s . g e t I d e n t i f i c a d o r R e a l ( v1 [ 0 ] , I n t e g e r . p a r s e I n t

( v1 [ 1 ] ) , v2 [ 0 ] ) ;1049 / / Op t im i z a c i o n de l o s u l t i m o s v a l o r e s1050 S t r i n g v a r i a b l eOp t im i z a d a=” x [ ”+v a r i a b l e [ 0 ]+ ” ] ” ;1051 / / Compruebo s i a lguna de l a s dos v a r i a b l e s es e l u l t i m o v a l o r1052 i f ( v a r i a b l e [2 ]==1) {1053 v a r i a b l eOp t im i z a d a=”1+” ;1054 Vec to r<St r i ng> i d s 1=t s . g e t I d e n t i f i c a d o r e s R e a lO p t i m i z a d o ( v1 [ 0 ] ,

I n t e g e r . p a r s e I n t ( v1 [ 1 ] ) ) ;1055 f o r ( i n t c=0; c<i d s 1 . s i z e ( ) ; c++){1056 v a r i a b l eOp t im i z a d a +=” x [ ”+i d s1 . ge t ( c )+” ] ” ;1057 i f ( c<i d s 1 . s i z e ( ) −1) v a r i a b l eOp t im i z a d a+=” ∗ ” ;1058 }

1059 }

1060 p o l i n om i o s . add ( v a r i a b l eOp t im i z a d a ) ;1061 / / FLAGNEGARTODO a f a l s e s i emp re en e s t a p o s i c i o n1062 FLAGNEGARTODO=f a l s e ;1063 ope r ado r e s . add ( ope rado r ) ;1064 }

191

Page 202: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

10651066 } e l se i f (MODELO . equa l s ( ”NUEVOALGEBRAICO” ) ) {10671068 i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {1069 i n t [ ] v a r i a b l e = t s . g e t I d e n t i f i c a d o r R e a l A l g e b r a i c o ( v1 [ 0 ] ,

I n t e g e r . p a r s e I n t ( v1 [ 1 ] ) , v2 [ 0 ] ) ;1070 p o l i n om i o s . add ( ” ( x ( ”+v a r i a b l e [ 0 ]+ ” )−”+v a r i a b l e [ 1 ]+ ” ) ” ) ;1071 ope r ado r e s . add ( ope rado r ) ;1072 / / FLAGNEGARTODO a f a l s e s i emp re en e s t a p o s i c i o n1073 FLAGNEGARTODO=f a l s e ;1074 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {1075 i n t [ ] v a r i a b l e = t s . g e t I d e n t i f i c a d o r R e a l A l g e b r a i c o ( v1 [ 0 ] ,

I n t e g e r . p a r s e I n t ( v1 [ 1 ] ) , v2 [ 0 ] ) ;1076 p o l i n om i o s . add ( ” ( x [ ”+v a r i a b l e [ 0 ]+ ” ]− ”+v a r i a b l e [ 1 ]+ ” ) ” ) ;1077 ope r ado r e s . add ( ope rado r ) ;1078 / / FLAGNEGARTODO a f a l s e s i emp re en e s t a p o s i c i o n1079 FLAGNEGARTODO=f a l s e ;1080 }

10811082 }

108310841085 / / ASIGNACION VARIABLE−VARIABLE1086 } e l se {

192

Page 203: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1087 / / Ya que l a e x p r e s i o n t r a b a j a con dos v a r i a b l e s , t e n d r e quer e a l i z a r l a comprobac ion seman t i c a

1088 / / que s o l o p e rm i t e comparar v a r i a b l e s cuando han s i d o d e f i n i d a sen l a misma l i n e a de l f i c h e r o

1089 / / f u e n t e , s i no , pararemos l a e j e c u c i o n y mos t ra remos un e r r o rs eman t i c o .

1090 / / Para e s t e p r o p o s i t o usaremos un a t r i b u t o de l a c l a v e Va r i a b l ede l paque te Tab laS imbo losCompi lado r ,

1091 / / en e s t e a t r i b u t o tenemos e l v a l o r de l a v a r i a b l e padre , s ic o i n c i d en , s i g n i f i c a que podemos comparar

1092 / / ambas v a r i a b l e s ya que e s t an d e f i n i d a s en l a misma l i n e a .1093 / / La f u n c i o n t s . i sCompa rab l e s l a n z a r a una e x c e p c i o n s i l a s

v a r i a b l e s no pueden s e r comparadas .1094 t s . i sCompa rab l e s ( v1 [ 0 ] , v2 [ 0 ] ) ;10951096 / / marco l a s v a r i a b l e s para luego mo s t r a r warn ings s i no han s i d o

usadas1097 t s . ma r caVa r i ab l e ( v1 [ 0 ]+ ” [ ”+v1 [ 1 ]+ ” ] ” ) ;1098 t s . ma r caVa r i ab l e ( v2 [ 0 ]+ ” [ ”+v2 [ 1 ]+ ” ] ” ) ;10991100 FLAGEXPRESIONVALORCOMPLEJO=t r u e ;11011102 i f (MODELO . equa l s ( ”PROPOSICIONAL ” ) ) {1103 i f ( SALIDA . equa l s ( ”POLYBORI ” ) | | SALIDA . equa l s ( ” SINGULAR ” ) ) {1104 S t r i n g a u x v a l o r c omp l e j o=” ” ;

193

Page 204: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1105 Vec to r<St r i ng> pa raen cadena r eno r s=new Vec to r<St r i ng > ( ) ;1106 Vec to r<St r i ng> v a l o r e s comun = t s . getValoresEnComun ( v1 [ 0 ] , v2 [ 0 ] )

;1107 f o r ( i n t i =0; i<v a l o r e s comun . s i z e ( ) ; i++){1108 i n t [ ] v a r i a b l e 1 = t s . g e t I d e n t i f i c a d o r R e a l ( v1 [ 0 ] , I n t e g e r .

p a r s e I n t ( v1 [ 1 ] ) , v a l o r e s comun . ge t ( i ) ) ;1109 i n t [ ] v a r i a b l e 2 = t s . g e t I d e n t i f i c a d o r R e a l ( v2 [ 0 ] , I n t e g e r .

p a r s e I n t ( v2 [ 1 ] ) , v a l o r e s comun . ge t ( i ) ) ;1110 / / Op t im i z a c i o n de l o s u l t i m o s v a l o r e s1111 S t r i n g v a r i a b l e 1Op t im i z a d a=” x ( ”+v a r i a b l e 1 [ 0 ]+ ” ) ” ;1112 S t r i n g v a r i a b l e 2Op t im i z a d a=” x ( ”+v a r i a b l e 2 [ 0 ]+ ” ) ” ;1113 / / Compruebo s i a lguna de l a s dos v a r i a b l e s es e l u l t i m o v a l o r1114 i f ( v a r i a b l e 1 [2 ]==1) {1115 v a r i a b l e 1Op t im i z a d a=”1+” ;1116 Vec to r<St r i ng> i d s 1=t s . g e t I d e n t i f i c a d o r e s R e a lO p t i m i z a d o ( v1

[ 0 ] , I n t e g e r . p a r s e I n t ( v1 [ 1 ] ) ) ;1117 f o r ( i n t c=0; c<i d s 1 . s i z e ( ) ; c++){1118 v a r i a b l e 1Op t im i z a d a +=” x ( ”+i d s1 . ge t ( c )+” ) ” ;1119 i f ( c<i d s 1 . s i z e ( ) −1) v a r i a b l e 1Op t im i z a d a+=” ∗ ” ;1120 }

1121 }

1122 i f ( v a r i a b l e 2 [2 ]==1) {1123 v a r i a b l e 2Op t im i z a d a=”1+” ;1124 Vec to r<St r i ng> i d s 2=t s . g e t I d e n t i f i c a d o r e s R e a lO p t i m i z a d o ( v2

[ 0 ] , I n t e g e r . p a r s e I n t ( v2 [ 1 ] ) ) ;194

Page 205: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1125 f o r ( i n t c=0; c<i d s 2 . s i z e ( ) ; c++){1126 v a r i a b l e 2Op t im i z a d a +=” x ( ”+i d s2 . ge t ( c )+” ) ” ;1127 i f ( c<i d s 2 . s i z e ( ) −1) v a r i a b l e 2Op t im i z a d a+=” ∗ ” ;1128 }

1129 }

1130 pa raen cadena r eno r s . add ( ” Y ( ”+v a r i a b l e 1Op t im i z a d a+” ”+” , ”+v a r i a b l e 2Op t im i z a d a+” ) ” ) ;

1131 }

1132 / / EXPRESIONES COMPUESTAS1133 / / Si no t i e n e v a l o r e s en comun , d e v u e l v o un 1 ( f a l s e )1134 i f ( p a r aen cadena r eno r s . s i z e ( ) ==0){1135 p o l i n om i o s . add ( ” 1 ” ) ;1136 ope r ado r e s . add ( ope rado r ) ;1137 } e l se {

1138 / / Si t i e n e v a l o r e s en comun l o s encadeno r e c u r s i v am e n t e en o ( y( a , { o ( . . ) } ) ) y d e v u e l v o

1139 / / l a e x p r e s i o n c omp l e j a1140 i f ( p a r aen cadena r eno r s . s i z e ( ) >1)1141 a u x v a l o r c omp l e j o=encadenaOrSimple ( pa raencadena reno r s , 0 ) ;1142 e l se

1143 a u x v a l o r c omp l e j o=pa raen cadena r eno r s . g e t ( 0 ) ;1144 p o l i n om i o s . add ( a u x v a l o r c omp l e j o ) ;1145 / / Fuerzo e l ope rado r a t r ue , es d e c i r ==, e l negado se l o

pongo1146 / / l uego en e l n i e v e l r e g l a ( ) s i e l FLAGNEGARTODO es t a a c t i v o

195

Page 206: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1147 ope r ado r e s . add ( ope rado r ) ;1148 }

1149 i f ( ope rado r ) FLAGNEGARTODO=f a l s e ;1150 e l se FLAGNEGARTODO=t r u e ;115111521153 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {1154 S t r i n g a u x v a l o r c omp l e j o=” ” ;1155 Vec to r<St r i ng> pa raen cadena r eno r s=new Vec to r<St r i ng > ( ) ;1156 Vec to r<St r i ng> v a l o r e s comun = t s . getValoresEnComun ( v1 [ 0 ] , v2 [ 0 ] )

;1157 f o r ( i n t i =0; i<v a l o r e s comun . s i z e ( ) ; i++){1158 i n t [ ] v a r i a b l e 1 = t s . g e t I d e n t i f i c a d o r R e a l ( v1 [ 0 ] , I n t e g e r .

p a r s e I n t ( v1 [ 1 ] ) , v a l o r e s comun . ge t ( i ) ) ;1159 i n t [ ] v a r i a b l e 2 = t s . g e t I d e n t i f i c a d o r R e a l ( v2 [ 0 ] , I n t e g e r .

p a r s e I n t ( v2 [ 1 ] ) , v a l o r e s comun . ge t ( i ) ) ;1160 / / Op t im i z a c i o n de l o s u l t i m o s v a l o r e s1161 S t r i n g v a r i a b l e 1Op t im i z a d a=” x [ ”+v a r i a b l e 1 [ 0 ]+ ” ] ” ;1162 S t r i n g v a r i a b l e 2Op t im i z a d a=” x [ ”+v a r i a b l e 2 [ 0 ]+ ” ] ” ;1163 / / Compruebo s i a lguna de l a s dos v a r i a b l e s es e l u l t i m o v a l o r1164 i f ( v a r i a b l e 1 [2 ]==1) {1165 v a r i a b l e 1Op t im i z a d a=”1+” ;1166 Vec to r<St r i ng> i d s 1=t s . g e t I d e n t i f i c a d o r e s R e a lO p t i m i z a d o ( v1

[ 0 ] , I n t e g e r . p a r s e I n t ( v1 [ 1 ] ) ) ;1167 f o r ( i n t c=0; c<i d s 1 . s i z e ( ) ; c++){

196

Page 207: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1168 v a r i a b l e 1Op t im i z a d a +=” x [ ”+i d s1 . ge t ( c )+” ] ” ;1169 i f ( c<i d s 1 . s i z e ( ) −1) v a r i a b l e 1Op t im i z a d a+=” ∗ ” ;1170 }

1171 }

1172 i f ( v a r i a b l e 2 [2 ]==1) {1173 v a r i a b l e 2Op t im i z a d a=”1+” ;1174 Vec to r<St r i ng> i d s 2=t s . g e t I d e n t i f i c a d o r e s R e a lO p t i m i z a d o ( v2

[ 0 ] , I n t e g e r . p a r s e I n t ( v2 [ 1 ] ) ) ;1175 f o r ( i n t c=0; c<i d s 2 . s i z e ( ) ; c++){1176 v a r i a b l e 2Op t im i z a d a +=” x [ ”+i d s2 . ge t ( c )+” ] ” ;1177 i f ( c<i d s 2 . s i z e ( ) −1) v a r i a b l e 2Op t im i z a d a+=” ∗ ” ;1178 }

1179 }

1180 pa raen cadena r eno r s . add ( ” Y ( ”+v a r i a b l e 1Op t im i z a d a+” ”+” , ”+v a r i a b l e 2Op t im i z a d a+” ) ” ) ;

1181 }

1182 / / EXPRESIONES COMPUESTAS1183 / / Si no t i e n e v a l o r e s en comun , d e v u e l v o un 1 ( f a l s e )1184 i f ( p a r aen cadena r eno r s . s i z e ( ) ==0){1185 p o l i n om i o s . add ( ” 1 ” ) ;1186 ope r ado r e s . add ( ope rado r ) ;1187 } e l se {

1188 / / Si t i e n e v a l o r e s en comun l o s encadeno r e c u r s i v am e n t e en o ( y( a , { o ( . . ) } ) ) y d e v u e l v o

1189 / / l a e x p r e s i o n c omp l e j a197

Page 208: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1190 i f ( p a r aen cadena r eno r s . s i z e ( ) >1)1191 a u x v a l o r c omp l e j o=encadenaOrSimple ( pa raencadena reno r s , 0 ) ;1192 e l se

1193 a u x v a l o r c omp l e j o=pa raen cadena r eno r s . g e t ( 0 ) ;1194 p o l i n om i o s . add ( a u x v a l o r c omp l e j o ) ;1195 / / Fuerzo e l ope rado r a t r ue , es d e c i r ==, e l negado se l o

pongo1196 / / l uego en e l n i e v e l r e g l a ( ) s i e l FLAGNEGARTODO es t a a c t i v o1197 ope r ado r e s . add ( ope rado r ) ;1198 }

1199 i f ( ope rado r ) FLAGNEGARTODO=f a l s e ;1200 e l se FLAGNEGARTODO=t r u e ;1201 }

12021203 } e l se i f (MODELO . equa l s ( ”NUEVOALGEBRAICO” ) ) {1204 i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {1205 i n t [ ] aux1=t s . g e t I d e n t i f i c a d o r R e a l A l g e b r a i c o S i n V a l o r ( v1 [ 0 ] ,

I n t e g e r . p a r s e I n t ( v1 [ 1 ] ) ) ;1206 i n t [ ] aux2=t s . g e t I d e n t i f i c a d o r R e a l A l g e b r a i c o S i n V a l o r ( v2 [ 0 ] ,

I n t e g e r . p a r s e I n t ( v2 [ 1 ] ) ) ;1207 S t r i n g po ly1=” x ( ”+aux1 [ 0 ]+ ” ) ” ;1208 S t r i n g po ly2=” x ( ”+aux2 [ 0 ]+ ” ) ” ;1209 p o l i n om i o s . add ( po ly1+”−”+po ly2 ) ;1210 ope r ado r e s . add ( ope rado r ) ;1211 / / FLAGNEGARTODO a f a l s e s i emp re en e s t a p o s i c i o n

198

Page 209: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1212 FLAGNEGARTODO=f a l s e ;1213 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {1214 i n t [ ] aux1=t s . g e t I d e n t i f i c a d o r R e a l A l g e b r a i c o S i n V a l o r ( v1 [ 0 ] ,

I n t e g e r . p a r s e I n t ( v1 [ 1 ] ) ) ;1215 i n t [ ] aux2=t s . g e t I d e n t i f i c a d o r R e a l A l g e b r a i c o S i n V a l o r ( v2 [ 0 ] ,

I n t e g e r . p a r s e I n t ( v2 [ 1 ] ) ) ;1216 S t r i n g po ly1=” x [ ”+aux1 [ 0 ]+ ” ] ” ;1217 S t r i n g po ly2=” x [ ”+aux2 [ 0 ]+ ” ] ” ;1218 p o l i n om i o s . add ( po ly1+”−”+po ly2 ) ;1219 ope r ado r e s . add ( ope rado r ) ;1220 / / FLAGNEGARTODO a f a l s e s i emp re en e s t a p o s i c i o n1221 FLAGNEGARTODO=f a l s e ;1222 }

1223 }

12241225 }

1226 } ca t ch ( E x c ep t i o n e ) {1227 System . ou t . p r i n t l n ( e ) ;1228 / / e . p r i n t S t a c k T r a c e ( ) ;1229 System . e x i t ( 0 ) ;1230 }

1231 }

1232 }

1233 boolean ope rado r ( ) :1234 {}

199

Page 210: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1235 {

1236 ”=” { r e t u r n t r u e ; }1237 / / | ( ” ! = ” | ”<>”) { r e t u r n f a l s e ; }1238 | ”<>” { r e t u r n f a l s e ; }1239 }

1240 S t r i n g [ ] v a r i a b l e ( ) :1241 {Token i d e n t i f i c a d o r=n u l l ; Token idnumero=n u l l ; }1242 {

1243 i d e n t i f i c a d o r=<id> [ ” [ ” idnumero=<numero> ” ] ” ]1244 {

1245 / / Devue l vo e l i d e n t i f i c a d o y e l numero de l i n d i c e s i l o t u v i e r a1246 S t r i n g [ ] r e s = new S t r i n g [ 2 ] ;1247 S t r i n g numero=” 0 ” ;1248 i f ( idnumero != n u l l ) numero=idnumero . t o S t r i n g ( ) ;1249 r e s [ 0 ]= i d e n t i f i c a d o r . t o S t r i n g ( ) ;1250 / / Si se ha de c l a r a do l a v a r i a b l e como ” v a r ” y no como ” v a r [ 1 ] ” , l o

i nd i c amos1251 / / po r cod igo , es d e c i r , e s t ab l e c emos a 1 e l i n d i c e de ac ceso .1252 i f ( idnumero==n u l l ) numero=” 1 ” ;1253 r e s [ 1 ]= numero ;1254 r e t u r n r e s ;1255 }

1256 }

1257 vo i d i n p u t ( ) :1258 { Vec to r<St r i ng> p1 , p2 , p3 ;

200

Page 211: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1259 p1 = new Vec to r<St r i ng > ( ) ;1260 Vec to r<Boolean> p1ops , p2ops , p3ops ;1261 p1ops = new Vec to r<Boolean > ( ) ;1262 }

1263 {

1264 {FLAGEXISTEENTRADA=t r u e ; }1265 e x p r e s i o n ( )1266 {

1267 p1=r e p o l s ( ) ;1268 p1ops=reops ( ) ;1269 s t r i n g T o F i l e ( ” ” , t r u e ) ;1270 i f (MODELO . equa l s ( ”PROPOSICIONAL ” ) ) {1271 i f ( SALIDA . equa l s ( ”POLYBORI ” ) ) {1272 s t r i n g T o F i l e ( ” i ”+NUMREGLASINPUT+”=”+acadena ( p1 , p1ops , f a l s e )+” ; ”

, t r u e ) ;1273 } e l se i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {1274 s t r i n g T o F i l e ( ” po ly i ”+NUMREGLASINPUT+”=”+acadena ( p1 , p1ops , f a l s e

)+” ; ” , t r u e ) ;1275 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {1276 s t r i n g T o F i l e ( ” I ”+NUMREGLASINPUT+” := ”+acadena ( p1 , p1ops , f a l s e )+” ;

” , t r u e ) ;1277 }

1278 } e l se i f (MODELO . equa l s ( ”NUEVOALGEBRAICO” ) ) {1279 i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {

201

Page 212: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1280 s t r i n g T o F i l e ( ” po ly i ”+NUMREGLASINPUT+”=”+acadena ( p1 , p1ops , f a l s e

)+” ; ” , t r u e ) ;1281 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {1282 s t r i n g T o F i l e ( ” I ”+NUMREGLASINPUT+” := ”+acadena ( p1 , p1ops , f a l s e )+” ;

” , t r u e ) ;1283 }

1284 }

1285 NUMREGLASINPUT++;1286 }

1287 ( ” , ” e x p r e s i o n ( )1288 {

1289 p1=r e p o l s ( ) ;1290 p1ops=reops ( ) ;1291 i f (MODELO . equa l s ( ”PROPOSICIONAL ” ) ) {1292 i f ( SALIDA . equa l s ( ”POLYBORI ” ) ) {1293 s t r i n g T o F i l e ( ” i ”+NUMREGLASINPUT+”=”+acadena ( p1 , p1ops , f a l s e )+” ; ”

, t r u e ) ;1294 } e l se i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {1295 s t r i n g T o F i l e ( ” po ly i ”+NUMREGLASINPUT+”=”+acadena ( p1 , p1ops , f a l s e

)+” ; ” , t r u e ) ;1296 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {1297 s t r i n g T o F i l e ( ” I ”+NUMREGLASINPUT+” := ”+acadena ( p1 , p1ops , f a l s e )+” ;

” , t r u e ) ;1298 }

1299 } e l se i f (MODELO . equa l s ( ”NUEVOALGEBRAICO” ) ) {202

Page 213: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1300 i f ( SALIDA . equa l s ( ” SINGULAR ” ) ) {1301 s t r i n g T o F i l e ( ” po ly i ”+NUMREGLASINPUT+”=”+acadena ( p1 , p1ops , f a l s e

)+” ; ” , t r u e ) ;1302 } e l se i f ( SALIDA . equa l s ( ”COCOA” ) ) {1303 s t r i n g T o F i l e ( ” I ”+NUMREGLASINPUT+” := ”+acadena ( p1 , p1ops , f a l s e )+” ;

” , t r u e ) ;1304 }

1305 }

1306 NUMREGLASINPUT++;1307 }

1308 ) ∗ ” ; ”1309 }

1310 TOKEN:1311 {

1312 < i d : [ ” A ”−” Z ” , ” a ”−” z ” , ” a ” , ” ” ] ( [ ” A ”−” Z ” , ” a ”−” z ” , ” 0 ”−” 9 ” , ” ” , ” \ ” ”] ) ∗ >

1313 }

1314 TOKEN:1315 {

1316 < numero : [ ” 1 ”−” 9 ” ] ( [ ” 0 ”−” 9 ” ] ) ∗ >1317 }

1318 SKIP :1319 {

1320 < ” / / ” ( ˜ [ ” \n ” , ” \ r ” ] ) ∗ >

1321 { l i n e a = l i n e a + image ; }203

Page 214: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

1322 }

1323 SKIP :1324 {

1325 < ” /∗ ” ( ˜ [ ] ) ∗ ” ∗ / ” >

1326 { l i n e a = l i n e a + image ; }1327 }

1328 SKIP :1329 {

1330 < ” (∗ ” ( ˜ [ ” \n ” , ” \ r ” ] ) ∗ ” ∗ ) ” >

1331 { l i n e a = l i n e a + image ; }1332 }

1333 SKIP :1334 {

1335 < ” ” | ” \ t ” | ” \ r ” >

1336 { l i n e a = l i n e a + image ; }1337 }

1338 SKIP :1339 {

1340 ” \n ”1341 {

1342 numLin++;1343 / / System . ou t . p r i n t l n ( numLin + ” : ” + l i n e a ) ;1344 l i n e a = ” ” ;1345 }

1346 }

204

Page 215: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

F. APENDICE: Codigo de tabla de sımbolos

F.1. Variable

1 /∗2 ∗ A t t r i b u t i o n −Noncommerc ia l 3 . 0 Unpor ted − h t t p : / / c r e a t i v e c ommons .

o rg / l i c e n s e s / by−nc / 3 . 0 /3 ∗ /4 package t a b l a s i m b o l o s c omp i l a d o r ;56 impo r t j a v a . u t i l . V e c t o r ;78 /∗ ∗9 ∗

10 ∗ @author Robe r t o Maestre Mar t i ne z11 ∗ /12 pub l i c c l a s s Va r i a b l e {1314 pub l i c Vec to r<St r i ng> v a l o r e s ;15 p r i v a t e S t r i n g v a r i a b l e ;16 pub l i c i n t numva r i a b l e s ;17 pub l i c i n t i n d i c e 2 ;1819 pub l i c Va r i a b l e ( ) {20 t h i s . v a l o r e s = new Vec to r<St r i ng > ( ) ;

205

Page 216: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

21 }

2223 pub l i c S t r i n g ge tNombreVar iab le ( ) {24 r e t u r n t h i s . v a r i a b l e ;25 }

2627 Va r i a b l e ( S t r i n g v a r i a b l e , i n t numva r i a b l e s ) {28 t h i s . v a l o r e s = new Vec to r<St r i ng > ( ) ;29 t h i s . v a r i a b l e=v a r i a b l e ;30 t h i s . n um va r i a b l e s=numva r i a b l e s ;31 }

323334 }

206

Page 217: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

F.2. Gestor de la tabla de sımbolos

1 /∗2 ∗ A t t r i b u t i o n −Noncommerc ia l 3 . 0 Unpor ted − h t t p : / / c r e a t i v e c ommons .

o rg / l i c e n s e s / by−nc / 3 . 0 /3 ∗ /4 package t a b l a s i m b o l o s c omp i l a d o r ;56 impo r t e x c e p c i o n e s . T SE x c e p t i o n i n d i c e d e v a r i a b l e n o e s p e c i f i c a d o ;7 impo r t e x c e p c i o n e s . TSE x c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o ;8 impo r t e x c e p c i o n e s . T SE x c e p t i o n i n d i c e v a r i a b l e f u e r a d e r a n g o ;9 impo r t e x c e p c i o n e s . TSEx c e p t i o n n omb r e v a r i a b l e d up l i c a d a ;

10 impo r t e x c e p c i o n e s . T SE x c e p t i o n v a l o r d e v a r i a b l e y a e x i s t e ;11 impo r t e x c e p c i o n e s . TSEx c e p t i o n v a l o r n o e n c o n t r a d o ;12 impo r t e x c e p c i o n e s . T SE x c e p t i o n v a l o r y v a r i b a l e c o i n c i d e n ;13 impo r t e x c e p c i o n e s . TSEx c e p t i o n v a r i a b l e n o e n c o n t r a d a ;14 impo r t j a v a . u t i l . I t e r a t o r ;15 impo r t j a v a . u t i l . Set ;16 impo r t j a v a . u t i l . T reeSet ;17 impo r t j a v a . u t i l . V e c t o r ;1819 /∗ ∗20 ∗

21 ∗ @author Robe r t o Maestre Mar t i ne z22 ∗ /

207

Page 218: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

23 pub l i c c l a s s TS {2425 / / E s t r u c t u r a s g l o b a l e s para s o p o r t a r l a t a b l a de s imbo l o s26 p r i v a t e Vec to r<Var i ab l e> v a r s ;27 p r i v a t e Vec to r<St r i ng> pa l ab r a sRe s e r v ada s ;28 / / E s t r u c t u r a s a u x i l i a r e s para marcar l a s v a r i a b l e s que se

u t i l i z a n29 / / Se u t i l i z a n TreeSet po r su a l t a v e l o c i d a d en l a s c o n s u l t a s30 Set <St r i ng> marcas ;313233 pub l i c TS ( ) {34 / / I n i c i am o s e l v e c t o r de pun t e r o s a v a r i a b l e s35 t h i s . v a r s=new Vec to r<Var i ab l e > ( ) ;36 / / I n i c amo s l a s e s t r u c t u r a s de marcas37 marcas = new TreeSet <St r i ng > ( ) ;38 / / I n i c i am o s e l v e c t o r de pa l a b r a s r e s e r v a d a s de l l engua j e

, es d e c i r , no se podran d e f i n i r39 / / v a r i a b l e s n i v a l o r e s con e s t a s pa l a b r a s40 t h i s . p a l ab r a sRe se r v ada s=new Vec to r<St r i ng > ( ) ;41 t h i s . p a l ab r a sRe se r v ada s . add ( ”BEGIN” ) ;42 t h i s . p a l ab r a sRe se r v ada s . add ( ”BEGINVARS” ) ;43 t h i s . p a l ab r a sRe se r v ada s . add ( ”ENDVARS” ) ;44 t h i s . p a l ab r a sRe se r v ada s . add ( ”BEGINRULES” ) ;45 t h i s . p a l ab r a sRe se r v ada s . add ( ”ENDRULES” ) ;

208

Page 219: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

46 t h i s . p a l ab r a sRe se r v ada s . add ( ”BEGININPUT ” ) ;47 t h i s . p a l ab r a sRe se r v ada s . add ( ”ENDINPUT” ) ;48 t h i s . p a l ab r a sRe se r v ada s . add ( ”OR” ) ;49 t h i s . p a l ab r a sRe se r v ada s . add ( ”O” ) ;50 t h i s . p a l ab r a sRe se r v ada s . add ( ”AND” ) ;51 t h i s . p a l ab r a sRe se r v ada s . add ( ”A ” ) ;52 t h i s . p a l ab r a sRe se r v ada s . add ( ” Y ” ) ;53 }

5455 pub l i c vo i d mar caVa r i ab l e ( S t r i n g v a r i a b l e ) {56 t h i s . marcas . add ( v a r i a b l e ) ;57 }

5859 pub l i c Vec to r<St r i ng> ge tWarn ingVar iab lesNoUsadas ( ) {60 Vec to r<St r i ng> aux=new Vec to r<St r i ng > ( ) ;61 f o r ( i n t v =0; v<t h i s . v a r s . s i z e ( ) ; v++){62 f o r ( i n t i =1; i<=t h i s . v a r s . g e t ( v ) . n umva r i a b l e s ; i++){63 / / Si e l i n d i c e de acceso es 1 , se ha pod ido

l l ama r a l a v a r a i b l e por ” v a r ” en ve z depor ” v a r [ 1 ] ”

64 S t r i n g check = t h i s . v a r s . g e t ( v ) .ge tNombreVar iab le ( )+” [ ”+i+” ] ” ;

65 i f ( ! t h i s . marcas . c o n t a i n s ( check ) ) {66 aux . add ( check ) ;67 }

209

Page 220: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

68 }

69 }

70 r e t u r n aux ;71 }

727374 pub l i c boolean esPrimo ( i n t numero ) {75 i n t c o n t a d o r = 2 ;76 boolean pr imo=t r u e ;77 whi le ( ( p r imo ) && ( c o n t a d o r !=numero ) ) {78 i f ( numero % c on t a d o r == 0 )79 pr imo = f a l s e ;80 c o n t a d o r++;81 }

82 r e t u r n pr imo ;83 }

848586 pub l i c i n t getNumVars ( ) {87 i n t aux =0;88 f o r ( i n t i =0; i<t h i s . v a r s . s i z e ( ) ; i++){89 i f ( t h i s . v a r s . g e t ( i ) . n umva r i ab l e s >1)90 aux+=t h i s . v a r s . g e t ( i ) . n umva r i ab l e s −1;91 }

92 r e t u r n t h i s . v a r s . s i z e ( )+aux ;210

Page 221: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

93 }

9495 pub l i c i n t getNumSoluc iones ( ) {96 r e t u r n t h i s . v a r s . g e t ( 0 ) . v a l o r e s . s i z e ( ) ;97 }

9899 pub l i c i n t getMayorPrimo ( ) {

100 i n t mayor=I n t e g e r . MIN VALUE ;101 f o r ( i n t i =0; i<t h i s . v a r s . s i z e ( ) ; i++) {102 i f ( t h i s . v a r s . g e t ( i ) . v a l o r e s . s i z e ( )>mayor )103 mayor=t h i s . v a r s . g e t ( i ) . v a l o r e s . s i z e ( ) ;104 }

105 mayor++;106 whi le ( ! t h i s . esPr imo ( mayor ) ) mayor++;107 r e t u r n mayor ;108 }

109110 pub l i c i n t g e t T o t a l V a r i a b l e s R e s t r i c c i o n L o g i c a ( ) {111 i n t aux =0;112 f o r ( i n t a=0;a<t h i s . v a r s . s i z e ( ) ; a++) {113 aux+=( t h i s . v a r s . g e t ( a ) . n umva r i a b l e s ∗ ( t h i s . v a r s .

g e t ( a ) . v a l o r e s . s i z e ( ) −1) ) ;114 }

115 r e t u r n aux ;116 }

211

Page 222: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

117118 pub l i c vo i d c omp r ubaa c c e s o a v a r i a b l e ( S t r i n g v a r i a b l e , S t r i n g

i n d i c e ) th rows TSEx c ep t i o n v a r i a b l e n o en c o n t r a d a ,TSEx c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o ,T SE x c e p t i o n i n d i c e d e v a r i a b l e n o e s p e c i f i c a d o {

119 boolean enc=f a l s e ;120 i n t i =0;121 i n t maxva rs=t h i s . v a r s . s i z e ( ) ;122 whi le ( i<maxvars && ! enc ) {123 i f ( t h i s . v a r s . g e t ( i ) . ge tNombreVar iab le ( ) . e qua l s (

v a r i a b l e ) ) {124 enc=t r u e ;125 i f ( t h i s . v a r s . g e t ( i ) . n umva r i ab l e s >0 && i n d i c e .

equa l s ( ” 0 ” ) ) {126 throw new

TSE x c e p t i o n i n d i c e d e v a r i a b l e n o e s p e c i f i c a d o( v a r i a b l e ) ;

127 }

128 }

129 i ++;130 }

131 i f ( ! enc ) {132 throw new TSEx c e p t i o n v a r i a b l e n o e n c o n t r a d a ( v a r i a b l e ) ;133 }

134 }

212

Page 223: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

135136 pub l i c Vec to r<St r i ng> getValoresEnComun ( S t r i n g var1 , S t r i n g

va r2 ) th rows TSEx c e p t i o n v a r i a b l e n o e n c o n t r a d a {137 Vec to r<St r i ng> v a l o r e s = new Vec to r<St r i ng > ( ) ;138 i n t i n d i c e v a r 1 =0 , i n d i c e v a r 2 =0;139 boolean en c va r1=f a l s e , e n c v a r 2=f a l s e ;140 i n t maxva rs=t h i s . v a r s . s i z e ( ) ;141 i n t i =0;142 whi le ( i<maxvars && ( ! en c va r 1 | | ! e n c va r 2 ) ) {143 i f ( t h i s . v a r s . g e t ( i ) . ge tNombreVar iab le ( ) . e qua l s ( v a r 1 )

) {144 en c va r1=t r u e ;145 i n d i c e v a r 1=i ;146 }

147 i f ( t h i s . v a r s . g e t ( i ) . ge tNombreVar iab le ( ) . e qua l s ( v a r 2 )) {

148 en c va r2=t r u e ;149 i n d i c e v a r 2=i ;150 }

151 i ++;152 }

153 / / Si no en cuen t r a a lguna de l a s v a r i a b l e s , lanzamos l ae x c e p c i o n c o r r e s p o n d i e n t e

154 i f ( ! e n c v a r 1 )155 throw new TSEx c e p t i o n v a r i a b l e n o e n c o n t r a d a ( v a r1 ) ;

213

Page 224: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

156 i f ( ! e n c v a r 2 )157 throw new TSEx c e p t i o n v a r i a b l e n o e n c o n t r a d a ( v a r2 ) ;158 / / Si ha en con t r ado l a s v a r i a b l e s , chequeamos l o s v a l o r e s

que t i e n e n en comun y devo l v emos159 / / l a l i s t a con d i c h o s v a l o r e s comunes a ambas v a r i a b l e s160 f o r ( i n t a=0;a<t h i s . v a r s . g e t ( i n d i c e v a r 1 ) . v a l o r e s . s i z e ( ) ; a

++){161 f o r ( i n t b=0;b<t h i s . v a r s . g e t ( i n d i c e v a r 2 ) . v a l o r e s . s i z e

( ) ; b++){162 / / System . ou t . p r i n t l n ( t h i s . v a r s . g e t ( i n d i c e v a r 1 ) .

v a l o r e s . g e t ( a ) + ” : : : : : ” + t h i s . v a r s . g e t (i n d i c e v a r 2 ) . v a l o r e s . g e t ( b ) ) ;

163 i f ( t h i s . v a r s . g e t ( i n d i c e v a r 1 ) . v a l o r e s . g e t ( a ) .e qua l s ( t h i s . v a r s . g e t ( i n d i c e v a r 2 ) . v a l o r e s . g e t ( b) ) ) {

164 v a l o r e s . add ( t h i s . v a r s . g e t ( i n d i c e v a r 1 ) . v a l o r e s. g e t ( a ) ) ;

165 }

166 }

167 }

168 r e t u r n v a l o r e s ;169 }

170171 pub l i c vo i d g e n e r a r I n d i c e s ( ) {172 / / Genero e l i n d i c e 2 para e l modelo p o l i n om i c o

214

Page 225: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

173 i n t aux =0;174 f o r ( i n t i =0; i<t h i s . v a r s . s i z e ( ) ; i++){175 t h i s . v a r s . g e t ( i ) . i n d i c e 2 = aux ;176 i n t nume r o v a r i a b l e s=t h i s . v a r s . g e t ( i ) . n umva r i a b l e s ;177 i f ( n ume r o v a r i a b l e s==0) n ume r o v a r i a b l e s =1;178 aux +=( t h i s . v a r s . g e t ( i ) . v a l o r e s . s i z e ( ) ) ∗

nume r o v a r i a b l e s ;179 }

180 }

181182183 / / Para e l modelo a l g e b r a i c o184 pub l i c i n t [ ] g e t I d e n t i f i c a d o r R e a l A l g e b r a i c o ( S t r i n g v a r i a b l e ,

i n t i n d i c e a r r a y , S t r i n g v a l o r ) th rows

TSEx c ep t i o n v a r i a b l e n o en c o n t r a d a ,TSEx c ep t i o n v a l o r n o en c on t r a d o ,TSE x c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o {

185 / / I n i c i o l a r e s p u e s t a186 i n t [ ] aux=n u l l ;187 i n t i =0;188 i n t maxva rs=t h i s . v a r s . s i z e ( ) ;189 boolean enc=f a l s e ;190 whi le ( i<maxvars && ! enc ) {191 i f ( t h i s . v a r s . g e t ( i ) . ge tNombreVar iab le ( ) . e qua l s (

v a r i a b l e ) ) {215

Page 226: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

192 / / Una ve z en con t r ada l a v a r i a b l e compruebo que e li n d i c e de acceso

193 / / no sob repase a l o d e c l a r a do194 i f ( i n d i c e a r r a y>t h i s . v a r s . g e t ( i ) . n umva r i a b l e s ) {195 throw new TSEx c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o

( i n d i c e a r r a y , t h i s . v a r s . g e t ( i ) . n umva r i a b l e s) ;

196 }

197 enc=t r u e ;198 boolean e n c v a l o r=f a l s e ;199 i n t c=0;200 i n t max va l o r e s=t h i s . v a r s . g e t ( i ) . v a l o r e s . s i z e ( ) ;201 whi le ( c<max va l o r e s && ! e n c v a l o r ) {202 i f ( t h i s . v a r s . g e t ( i ) . v a l o r e s . g e t ( c ) . e qua l s (

v a l o r ) ) {203 e n c v a l o r=t r u e ;204 aux = new i n t [ 3 ] ;205 / / Devue l vo e l i d e n t i f i c a d o r r e a l de l a

v a r i a b l e , es d e c i r , v a r i a b l e206 / / t e n i e ndo en cuen ta e l v a l o r de l a r r ay

s i e s t a d e c l a r ado y l a p o s i c i o n de l av a r i a b l e

207 i n t numva r s conmasdedos va l o r e s =0;208 f o r ( i n t x=0; x<i ; x++){

216

Page 227: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

209 i f ( ( t h i s . v a r s . g e t ( x ) . n umva r i a b l e s )>1)

210 numva r s conmasdedos va l o r e s+=( t h i s .v a r s . g e t ( x ) . n umva r i ab l e s −1) ;

211 }

212 / / Ahora sumar l o s i n d i c e s que se de j anpor d e t r a s en l a misma v a r i a b l e

213 aux [ 0 ] = ( i+i n d i c e a r r a y+numva r s conmasdedos va l o r e s ) −1;

214 aux [ 1 ]= c ;215 }

216 c++;217 }

218 i f ( ! e n c v a l o r ) {219 throw new TSEx c e p t i o n v a l o r n o e n c o n t r a d o ( v a l o r

) ;220 }

221 }

222 i ++;223 }

224 i f ( ! enc ) {225 throw new TSEx c e p t i o n v a r i a b l e n o e n c o n t r a d a ( v a r i a b l e ) ;226 }

227 r e t u r n aux ;228 }

217

Page 228: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

229230231232233 / / Para e l modelo a l g e b r a i c o234 pub l i c i n t [ ] g e t I d e n t i f i c a d o r R e a l A l g e b r a i c o S i n V a l o r ( S t r i n g

v a r i a b l e , i n t i n d i c e a r r a y ) th rows

TSEx c ep t i o n v a r i a b l e n o en c o n t r a d a ,TSEx c ep t i o n v a l o r n o en c on t r a d o ,TSE x c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o {

235 / / I n i c i o l a r e s p u e s t a236 i n t [ ] aux=n u l l ;237 i n t i =0;238 i n t maxva rs=t h i s . v a r s . s i z e ( ) ;239 boolean enc=f a l s e ;240 whi le ( i<maxvars && ! enc ) {241 i f ( t h i s . v a r s . g e t ( i ) . ge tNombreVar iab le ( ) . e qua l s (

v a r i a b l e ) ) {242 / / Una ve z en con t r ada l a v a r i a b l e compruebo que e l

i n d i c e de acceso243 / / no sob repase a l o d e c l a r a do244 i f ( i n d i c e a r r a y>t h i s . v a r s . g e t ( i ) . n umva r i a b l e s ) {245 throw new TSEx c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o

( i n d i c e a r r a y , t h i s . v a r s . g e t ( i ) . n umva r i a b l e s) ;

218

Page 229: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

246 }

247 enc=t r u e ;248 i n t c=0;249 / / Ahora sumar l o s i n d i c e s que se de j an por d e t r a s

en l a misma v a r i a b l e250 aux = new i n t [ 2 ] ;251 i n t numva r s conmasdedos va l o r e s =0;252 f o r ( i n t x=0; x<i ; x++){253 i f ( ( t h i s . v a r s . g e t ( x ) . n umva r i a b l e s )

>1)254 numva r s conmasdedos va l o r e s+=( t h i s .

v a r s . g e t ( x ) . n umva r i ab l e s −1) ;255 }

256 aux [ 0 ] = ( i+i n d i c e a r r a y+numva r s conmasdedos va l o r e s )−1;

257 aux [ 1 ]= c ;258 }

259 i ++;260 }

261 i f ( ! enc ) {262 throw new TSEx c e p t i o n v a r i a b l e n o e n c o n t r a d a ( v a r i a b l e ) ;263 }

264 r e t u r n aux ;265 }

266

219

Page 230: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

267268269270271272273 / / Para e l modelo p r o p o s i c i o n a l274 pub l i c i n t [ ] g e t I d e n t i f i c a d o r R e a l ( S t r i n g v a r i a b l e , i n t

i n d i c e a r r a y , S t r i n g v a l o r ) th rows

TSEx c ep t i o n v a r i a b l e n o en c o n t r a d a ,TSEx c ep t i o n v a l o r n o en c on t r a d o ,TSE x c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o {

275 / / I n i c i o l a r e s p u e s t a276 i n t [ ] aux=n u l l ;277 i n t i =0;278 i n t maxva rs=t h i s . v a r s . s i z e ( ) ;279 boolean enc=f a l s e ;280 whi le ( i<maxvars && ! enc ) {281 i f ( t h i s . v a r s . g e t ( i ) . ge tNombreVar iab le ( ) . e qua l s (

v a r i a b l e ) ) {282 / / Una ve z en con t r ada l a v a r i a b l e compruebo que e l

i n d i c e de acceso283 / / no sob repase a l o d e c l a r a do284 i f ( i n d i c e a r r a y>t h i s . v a r s . g e t ( i ) . n umva r i a b l e s ) {

220

Page 231: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

285 throw new TSEx c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o( i n d i c e a r r a y , t h i s . v a r s . g e t ( i ) . n umva r i a b l e s) ;

286 }

287 enc=t r u e ;288 boolean e n c v a l o r=f a l s e ;289 i n t c=0;290 i n t max va l o r e s=t h i s . v a r s . g e t ( i ) . v a l o r e s . s i z e ( ) ;291 whi le ( c<max va l o r e s && ! e n c v a l o r ) {292 i f ( t h i s . v a r s . g e t ( i ) . v a l o r e s . g e t ( c ) . e qua l s (

v a l o r ) ) {293 e n c v a l o r=t r u e ;294 aux = new i n t [ 3 ] ;295 / / Devue l vo e l i d e n t i f i c a d o r r e a l de l a

v a r i a b l e , es d e c i r , v a r i a b l e296 / / t e n i e ndo en cuen ta e l v a l o r de l a r r ay

s i e s t a d e c l a r ado y l a p o s i c i o n de l av a r i a b l e

297298 / / Ademas para l a o p t i m i z a c i o n tengo que

d e v o l v e r e l i d − e l numero dev a r i a b l e s que tenga

299 / / po r d e t r a s que tengan mas de 1 v a l o rpo r v a r i a b l e . Ademas hay quec o n t a b i l i z a r d e n t r o de l a

221

Page 232: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

300 / / v a r i a b l e que se e s t e a c ced i endo e li n d i c e que t i e n e y sumar uno por cadai n d i c e que d e j e po r d e t r a s

301 / / de su misma v a r i a b l e302 i n t numva r s conmasdedos va l o r e s =0;303 f o r ( i n t x=0; x<i ; x++){304 i f ( ( t h i s . v a r s . g e t ( x ) . v a l o r e s . s i z e ( ) )

>1)305 numva r s conmasdedos va l o r e s+=( t h i s .

v a r s . g e t ( x ) . n umva r i a b l e s ) ;306 }

307 / / Ahora sumar l o s i n d i c e s que se de j anpor d e t r a s en l a misma v a r i a b l e

308 numva r s conmasdedos va l o r e s+=i n d i c e a r r a y −1;309 aux [ 0 ] = ( t h i s . v a r s . g e t ( i ) . i n d i c e 2 +( (

i n d i c e a r r a y −1)∗ t h i s . v a r s . g e t ( i ) .v a l o r e s . s i z e ( ) )+c )−numva r s conmasdedos va l o r e s ;

310 aux [ 1 ]= c ;311 / / Si es e l u l t i m o v a l o r marco un f l a g

para l a o p t i m i z a c i o n312 i f ( c==maxva lo re s −1) aux [ 2 ] = 1 ;313 e l se aux [ 2 ] = 0 ;314 }

315 c++;222

Page 233: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

316 }

317 i f ( ! e n c v a l o r ) {318 throw new TSEx c e p t i o n v a l o r n o e n c o n t r a d o ( v a l o r

) ;319 }

320 }

321 i ++;322 }

323 i f ( ! enc ) {324 throw new TSEx c e p t i o n v a r i a b l e n o e n c o n t r a d a ( v a r i a b l e ) ;325 }

326 r e t u r n aux ;327 }

328329330 pub l i c Vec to r<St r i ng> g e t I d e n t i f i c a d o r e s R e a lO p t i m i z a d o ( S t r i n g

v a r i a b l e , i n t i n d i c e ) th rows

TSEx c ep t i o n v a r i a b l e n o en c o n t r a d a ,TSEx c ep t i o n v a l o r n o en c on t r a d o ,TSE x c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o {

331 Vec to r<St r i ng> i d e n t i f i c a d o r e s=n u l l ;332 boolean enc=f a l s e ;333 i n t i =0;334 i n t maxva rs=t h i s . v a r s . s i z e ( ) ;335 whi le ( i<maxvars && ! enc ) {

223

Page 234: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

336 i f ( t h i s . v a r s . g e t ( i ) . ge tNombreVar iab le ( ) . e qua l s (v a r i a b l e ) ) {

337 enc=t r u e ;338 i d e n t i f i c a d o r e s=new Vec to r<St r i ng > ( ) ;339 f o r ( i n t c=0; c<t h i s . v a r s . g e t ( i ) . v a l o r e s . s i z e ( ) −1;

c++){340 i n t [ ] i d e n t i f i c a d o r R e a l=t h i s .

g e t I d e n t i f i c a d o r R e a l ( t h i s . v a r s . g e t ( i ) .ge tNombreVar iab le ( ) , i n d i c e , t h i s . v a r s . g e t (i ) . v a l o r e s . g e t ( c ) ) ;

341 i d e n t i f i c a d o r e s . add ( i d e n t i f i c a d o r R e a l [ 0 ]+ ” ” ) ;342 }

343 }

344 i ++;345 }

346 i f ( ! enc ) {347 / / Si no ha en con t r ado l a v a r i a b l e , lanzamos e x c e p c i o n348 throw new TSEx c e p t i o n v a r i a b l e n o e n c o n t r a d a ( v a r i a b l e ) ;349 }

350 r e t u r n i d e n t i f i c a d o r e s ;351 }

352353 pub l i c boolean e x i s t e V a r i a b l e ( S t r i n g v a r i a b l e ) {354 boolean enc=f a l s e ;355 i n t i =0;

224

Page 235: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

356 i n t maxva rs=t h i s . v a r s . s i z e ( ) ;357 whi le ( i<maxvars && ! enc ) {358 i f ( t h i s . v a r s . g e t ( i ) . ge tNombreVar iab le ( ) . e qua l s (

v a r i a b l e ) )359 enc=t r u e ;360 i ++;361 }

362 r e t u r n enc ;363 }

364365366 pub l i c vo i d i n s e r t a V a r i a b l e ( S t r i n g v a r i a b l e , i n t numva r i a b l e s )

th rows TSEx c ep t i o n nomb r e v a r i a b l e dup l i c a da ,TSEx c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o ,TSE x c e p t i o n v a l o r y v a r i b a l e c o i n c i d e n ,TSE x c e p t i o n i n d i c e v a r i a b l e f u e r a d e r a n g o {

367 / / Compruebo que no sea una pa l ab r a r e s e r v a d a de l l e n g u a j e368 f o r ( i n t i =0; i<t h i s . p a l ab r a sRe se r v ada s . s i z e ( ) ; i++){369 i f ( v a r i a b l e . equa l s ( t h i s . p a l ab r a sRe se r v ada s . ge t ( i ) ) ) {370 throw new TSEx c e p t i o n n omb r e v a r i a b l e d up l i c a d a (

v a r i a b l e ) ;371 }

372 }

373 / / Compruebo que no se r e p i t a l a v a r i a b l e374 f o r ( i n t i =0; i<t h i s . v a r s . s i z e ( ) ; i++){

225

Page 236: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

375 i f ( t h i s . v a r s . g e t ( i ) . ge tNombreVar iab le ( ) . e qua l s (v a r i a b l e ) ) {

376 throw new TSEx c e p t i o n n omb r e v a r i a b l e d up l i c a d a (v a r i a b l e ) ;

377 }

378 }

379 / / Compruebo que e l i n d i c e de l a r r ay sea c o r r e c t o380 i f ( numva r i ab l e s <1 | | numva r i ab l e s >10000) {381 throw new TSE x c e p t i o n i n d i c e v a r i a b l e f u e r a d e r a n g o (

n umva r i a b l e s ) ;382 }

383 / / Compruebo que l a v a r i a b l e no e x i s t a en n inguno de l o sv a l o r e s

384 boolean enc=f a l s e ;385 i n t i =0;386 i n t maxva rs=t h i s . v a r s . s i z e ( ) ;387 whi le ( i<maxvars && ! enc ) {388 i n t c=0;389 i n t max va l o r e s=t h i s . v a r s . g e t ( i ) . v a l o r e s . s i z e ( ) ;390 whi le ( c<max va l o r e s && ! enc ) {391 i f ( t h i s . v a r s . g e t ( i ) . v a l o r e s . g e t ( c ) . e qua l s (

v a r i a b l e ) ) {392 enc=t r u e ;393 }

394 c++;226

Page 237: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

395 }

396 i ++;397 }

398 i f ( enc ) {399 throw new TSE x c e p t i o n v a l o r y v a r i b a l e c o i n c i d e n (

v a r i a b l e ) ;400 }

401 t h i s . v a r s . add ( new Va r i a b l e ( v a r i a b l e , n umva r i a b l e s ) ) ;402 }

403404405406407 pub l i c vo i d i n s e r t a V a l o r E nVa r i a b l e ( S t r i n g v a r i a b l e , S t r i n g

v a l o r ) th rows TSEx c ep t i o n v a r i a b l e n o en c o n t r a d a ,TSE x c e p t i o n v a l o r d e v a r i a b l e y a e x i s t e ,T SE x c e p t i o n v a l o r y v a r i b a l e c o i n c i d e n {

408 / / Busco l a v a r i a b l e409 i n t max=t h i s . v a r s . s i z e ( ) ;410 boolean enc=f a l s e ;411 i n t i =−1;412 i n t x=0;413 / / Pr imero compruebo que no c o i n c i d a e l n inguna v a r i a b l e

con e l v a l o r

227

Page 238: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

414 / / depaso mi ro s i l a v a r i a b l e e x i s t e , s i es a s i guardo l ap o s i c i o n en l a

415 / / que se e n c u r n t r a416 whi le ( x<max ) {417 i f ( t h i s . v a r s . g e t ( x ) . ge tNombreVar iab le ( ) . e qua l s (

v a l o r ) ) {418 throw new TSE x c e p t i o n v a l o r y v a r i b a l e c o i n c i d e n

( v a l o r ) ;419 }

420 i f ( t h i s . v a r s . g e t ( x ) . ge tNombreVar iab le ( ) . e qua l s (v a r i a b l e ) ) {

421 enc=t r u e ;422 i=x ;423 }

424 x++;425 }

426 / / Si l a v a r i a b l e e x i s t e ya p rosegu imos427 i f ( enc ) {428 boolean v a l o r e n c=f a l s e ;429 i n t max va l o r=t h i s . v a r s . g e t ( i ) . v a l o r e s . s i z e ( ) ;430 i n t c=0;431 whi le ( c<max va l o r && ! v a l o r e n c ) {432 i f ( t h i s . v a r s . g e t ( i ) . v a l o r e s . g e t ( c ) . e qua l s (

v a l o r ) ) {433 v a l o r e n c=t r u e ;

228

Page 239: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

434 }

435 c++;436 }

437 / / Si se ha en con t r ado e l v a l o r , l a n z o e x c e p c i o nya que no pueden e s t a r r e p e t i d o s

438 i f ( v a l o r e n c ) {439 throw new TSE x c e p t i o n v a l o r d e v a r i a b l e y a e x i s t e

( v a r i a b l e , v a l o r ) ;440 / / Si no l o ha encon t rado , i n s e r t o e l nuevo v a l o r441 } e l se {

442 t h i s . v a r s . g e t ( i ) . v a l o r e s . add ( v a l o r ) ;443 }

444 } e l se {

445 / / Si no ha en con t r ado l a v a r i a b l e , lanzamos e x c e p c i o n446 throw new TSEx c e p t i o n v a r i a b l e n o e n c o n t r a d a ( v a r i a b l e ) ;447 }

448 }

449450451 pub l i c Vec to r<St r i ng> g e t R e s t r i c c i o n e s V a r i a b l e s ( S t r i n g s i ,

S t r i n g sd ) th rows TSEx c ep t i o n v a r i a b l e n o en c o n t r a d a ,TSEx c ep t i o n v a l o r n o en c on t r a d o ,TSE x c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o {

452 Vec to r<St r i ng> r e s=new Vec to r<St r i ng > ( ) ;453 f o r ( i n t a=0;a<t h i s . v a r s . s i z e ( ) ; a++) {

229

Page 240: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

454 f o r ( i n t b=0;b<t h i s . v a r s . g e t ( a ) . n umva r i a b l e s ; b++) {455 f o r ( i n t i =0; i<t h i s . v a r s . g e t ( a ) . v a l o r e s . s i z e ( ) −1;

i++) {456 S t r i n g aux1=” ” , aux2=” ” ;457 i n t [ ] v a r 1=t h i s . g e t I d e n t i f i c a d o r R e a l ( t h i s

. v a r s . g e t ( a ) . ge tNombreVar iab le ( ) , b+1 ,t h i s . v a r s . g e t ( a ) . v a l o r e s . g e t ( i ) ) ;

458 aux1=” x ”+s i+va r1 [ 0 ]+ sd ;459 r e s . add ( aux1 ) ;460 }

461 }

462 }

463 r e t u r n r e s ;464 }

465466 pub l i c Vec to r<St r i ng> g e t R e s t r i c c i o n e s P r o p o s i c i o n a l ( S t r i n g s i

, S t r i n g sd ) th rows TSEx c ep t i o n v a r i a b l e n o en c o n t r a d a ,TSEx c ep t i o n v a l o r n o en c on t r a d o ,TSE x c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o {

467 Vec to r<St r i ng> pa re s=new Vec to r<St r i ng > ( ) ;468 f o r ( i n t a=0;a<t h i s . v a r s . s i z e ( ) ; a++) {469 f o r ( i n t b=0;b<t h i s . v a r s . g e t ( a ) . n umva r i a b l e s ; b++) {470 f o r ( i n t i =0; i<t h i s . v a r s . g e t ( a ) . v a l o r e s . s i z e ( ) −1;

i++) {

230

Page 241: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

471 f o r ( i n t c=i +1; c<t h i s . v a r s . g e t ( a ) . v a l o r e s .s i z e ( ) −1; c++) {

472 S t r i n g aux1=” ” , aux2=” ” ;473 / / NoCompruebo s i es e l u l t i m o v a l o r para

d e v o l v e r op t im i zado , ya que l a p r ime rav a r i a b l e

474 / / d e l b inomio nunca puede s e r e l u l t i m ov a l o r de l a v a r i a b l e

475 i n t [ ] v a r 1=t h i s . g e t I d e n t i f i c a d o r R e a l ( t h i s

. v a r s . g e t ( a ) . ge tNombreVar iab le ( ) , b+1 ,t h i s . v a r s . g e t ( a ) . v a l o r e s . g e t ( i ) ) ;

476 aux1=” x ”+s i+va r1 [ 0 ]+ sd ;477478 i n t [ ] v a r 2=t h i s . g e t I d e n t i f i c a d o r R e a l ( t h i s

. v a r s . g e t ( a ) . ge tNombreVar iab le ( ) , b+1 ,t h i s . v a r s . g e t ( a ) . v a l o r e s . g e t ( c ) ) ;

479 aux2=” x ”+s i+va r2 [ 0 ]+ sd ;480 pa re s . add ( aux1 ) ;481 pa re s . add ( aux2 ) ;482 }

483 }

484 }

485 }

486 r e t u r n pa re s ;487 }

231

Page 242: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

488489490491 pub l i c i n t ge tNumValoresDeVar iab le ( i n t i n d i c e 3 ) th rows

TSEx c e p t i o n v a r i a b l e n o e n c o n t r a d a {492 boolean enc=f a l s e ;493 i n t i =0 , c e n t v a l o r =0 , aux =0;494 whi le ( ! enc && i < t h i s . v a r s . s i z e ( ) ) {495 i n t c=0;496 whi le ( ! enc && c<t h i s . v a r s . g e t ( i ) . n umva r i a b l e s ) {497 i f ( i n d i c e 3==aux ) {498 enc=t r u e ;499 c e n t v a l o r=t h i s . v a r s . g e t ( i ) . v a l o r e s . s i z e ( ) ;500 }

501 aux++;502 c++;503 }

504 i ++;505 }

506 i f ( ! enc )507 throw new TSEx c e p t i o n v a r i a b l e n o e n c o n t r a d a ( ” ” ) ;508 r e t u r n c e n t v a l o r ;509 }

510511

232

Page 243: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

512 pub l i c Vec to r<St r i ng> g e t R e s t r i c c i o n e s A l g e b r a i c a s ( S t r i n g s i ,S t r i n g sd ) th rows TSEx c ep t i o n v a r i a b l e n o en c o n t r a d a ,TSEx c ep t i o n v a l o r n o en c on t r a d o ,TSE x c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o {

513 Vec to r<St r i ng> pa re s=new Vec to r<St r i ng > ( ) ;514 f o r ( i n t a=0;a<t h i s . v a r s . s i z e ( ) ; a++) {515 f o r ( i n t b=0;b<t h i s . v a r s . g e t ( a ) . n umva r i a b l e s ; b++) {516517 }

518 }

519 r e t u r n pa re s ;520 }

521522523 pub l i c S t r i n g imp r im i rTS ( ) th rows

TSEx c ep t i o n v a r i a b l e n o en c o n t r a d a ,TSEx c ep t i o n v a l o r n o en c on t r a d o ,TSE x c e p t i o n i n d i c e d e v a r i a b l e n o r a n g o {

524 S t r i n g r e s=” \n Tabla de s imbo l o s \n=============================================\n ” ;

525 i n t t o t =0;526 i n t aux =0;527 f o r ( i n t i =0; i<t h i s . v a r s . s i z e ( ) ; i++){528 f o r ( i n t nv =0; nv<t h i s . v a r s . g e t ( i ) . n umva r i a b l e s ; nv++)

{

233

Page 244: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

529 f o r ( i n t c=0; c<t h i s . v a r s . g e t ( i ) . v a l o r e s . s i z e ( ) ; c++){

530 / / Ca l c u l o de l i n d i c e r e a l p r o p o s i c i o n a l531 S t r i n g s t r r e a l=” ” ;532 i f ( c<t h i s . v a r s . g e t ( i ) . v a l o r e s . s i z e ( ) −1){533 s t r r e a l=” { ” +( t o t−aux )+” } ” ;534 } e l se aux++;535 / / Ca l c u l o e l i n d i c e r e a l a l g e b r a i c o536 S t r i n g i n d i c e 3=” ” ;537 i n t au xan t s =0;538 f o r ( i n t x=0; x<i ; x++){539 i f ( t h i s . v a r s . g e t ( x ) . numva r i ab l e s >1)540 auxan t s+= t h i s . v a r s . g e t ( x ) .

n umva r i ab l e s −1;541 }

542 i f ( c==0){543 i n d i c e 3=” [ ” +( i+nv+auxan t s )+” ] ” ;544 }

545 r e s+=” { ”+t o t+” }\ t ”+s t r r e a l+” \ t ”+i n d i c e 3+” \ t ”+t h i s . v a r s . g e t ( i ) . ge tNombreVar iab le ( )+” [ ” +(nv+1)+” ]= ”+t h i s . v a r s . g e t ( i ) . v a l o r e s . g e t ( c )+” \n ” ;

546 t o t ++;547 }

548 }

234

Page 245: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

549 r e s+=”−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n ” ;

550 }

551 r e t u r n r e s ;552 }

553554 }

235

Page 246: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

236

Page 247: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

G. APENDICE: Modificacion, compilacion y empaquetado del

traductor

G.1. Compilacion

REQUISITO: Tener en el classpath las rutas donde se encuentra “java.exe” y “javac.exe”

Dependiendo de la version de Java, normalmente se encuentran en:java.exe:

C:\Program Files\Java\jre1.5.0_12\bin

javac.exe:C:\Program Files\Java\jdk1.6.0_16\bin

Los pasos para realizar las modificaciones y compilar de nuevo el traductor son:1. Realizar las modificaciones necesarias en el fichero ‘traductor.jj’, este es el fichero que

tiene la especificacion del lenguaje en JavaCC2. Una vez realizados los cambios, compilar el fichero‘traductor.jj’:

R:\TFC-DATOS\javacc-4.0\bin > javacc traductor.jj

Compilar siempre en la carpeta donde se encuentre ‘javacc.bat’Esta compilacion generara las clases en Java necesarias en el directorio destinoindicado en el archivo ‘traductor.jj’ en la primera lıneaOutput_directory="R:\\TFC-DATOS\\javacc-4.0\\traductor";

3. Ir a la carpeta de destino Output directory :237

Page 248: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

cd R:\\TFC-DATOS\\javacc-4.0\\traductor"

4. Ahora solo tenemos que compilar la clase java principal del traductor: ‘Traductor.java’R:\TFC-DATOS\javacc-4.0\traductor > javac Traductor.java

G.2. Empaquetado

REQUISITO: Tener en el classpath las rutas donde se encuentra “jar.exe”

Dependiendo de la version de Java, normalmente se encuentran en:java.exe:

C:\Program Files\Java\jdk1.6.0_16\bin

1. Crear el archivo jar con las clases ya compiladas del traductor:R:\TFC-DATOS\javacc-4.0\traductor >

jar -cf traductor.jar *.class

2. Abrir “traductor.jar” con un programa de compresion, por ejemplo WinRAR y poner eltexto del siguiente cuadro en “MANIFEST.M”:

Manifest-Version: 1.0

Main-Class: Traductor

3. Por ultimo, poner las carpetas: ‘excepciones’ y ‘tablasimboloscompilador’ en el raız delfichero traductor.jar

Ya esta empaquetado el programa y listo para ejecutar:R:\TFC-DATOS\javacc-4.0\traductor >

java -jar Traductor.jar 1 1 < fuente.txt

238

Page 249: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

Referencias

[1] E. Roanes-Lozano, A. Hernando, L. Laita, and E. Roanes-Macıas, “A groebner bases-basedapproach to backward reasoning in rule based expert systems,” Annals of Mathematics

and Artificial Intelligence, vol. 56, pp. 297–311, 2009. 10.1007/s10472-009-9147-4.[2] J. Alonso and E. Briales, “Logicas polivalentes y bases de grobner.,” In: C. Martin, ed.:

Actas del V Congreso de Lenguajes Naturales y Lenguajes Formales. University of Seville,

Seville, pp. 307–315, 1995.[3] J. Chazarain, A. Riscos, J. A. Alonso, and E. Briales, “Multi-valued logic and grobner bases

with applications to modal logic,” J. Symb. Comput., vol. 11, pp. 181–194, April 1991.[4] J. Hsiang, “Refutational theorem proving using term-rewriting systems,” Artificial

Intelligence, vol. 25, no. 3, pp. 255 – 300, 1985.[5] D. Kapur and P. Narendran, “An equational approach to theorem proving in first-order

predicate calculus,” in Proceedings of the 9th international joint conference on Artificial

intelligence - Volume 2, (San Francisco, CA, USA), pp. 1146–1153, Morgan KaufmannPublishers Inc., 1985.

[6] E. Roanes-Lozano, L. M. Laita, and E. Roanes-Macıas, “A polynomial model for multivaluedlogics with a touch of algebraic geometry and computer algebra,” Mathematics and

Computers in Simulation, vol. 45, pp. 83–99, 1998.[7] A. Capaini and G. Niesi, “Cocoa user’s manual. dept. mathematics, univ. of genova, 1996.”

Available at http://cocoa.dima.unige.it.[8] D. Perkinson, “Cocoa 4.0 online help (electronic file acompanying cocoa v.4.0).”[9] W. Decker, G.-M. Greuel, G. Pfister, and H. Schonemann, “Singular 3-1-1 —

A computer algebra system for polynomial computations. 2010.” Available athttp://www.singular.uni-kl.de.

239

Page 250: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

[10] M. Brickenstein and A. Dreyer, “Polybori: A framework for gr’obner-basis computationswith boolean polynomials,” Journal of Symbolic Computatione, vol. 44, no. 9, pp. 1326–1345, 2009.

[11] L. Laita, E. Roanes-Lozano, V. Maojo, L. de Ledesma, and L. Laita, “An expert systemfor managing medical appropriateness criteria based on computer algebra techniques,”Computers and Mathematics with Applications, vol. 51, no. 5, pp. 473–481, 2000.

[12] M. Lourdes Jimenez, J. M. Santamarıa, R. Barchino, L. Laita, L. M. Laita, L. A. Gonzalez, andA. Asenjo, “Knowledge representation for diagnosis of care problems through an expertsystem: Model of the auto-care deficit situations,” Expert System with Applications, vol. 34,pp. 2847–2857, 2008.

[13] C. Perez-Carretero, L. M. Laita, E. Roanes-Lozano, L. Lazaro, J. Gonzalez-Cajal, andL. Laita, “A logic and computer algebra-based expert system for diagnosis of anorexia,”Mathematics and Computers in Simulation, vol. 58, pp. 183–202, February 2002.

[14] C. Rodrıguez-Solano, L. M. Laita, E. Roanes-Lozano, L. Lopez-Corral, and L. Laita,“A computational system for diagnosis of depressive situations,” Expert Systems with

Applications, vol. 31, no. 1, pp. 47 – 55, 2006.[15] M. Minsk, “A framework for representing knowledge,” MIT-AI Laboratory Memo, vol. 306,

1974.[16] A. Hernando, “A new algebraic model for implementing expert systems represented under

the ‘concept-attribute-value’ paradigm,” Mathematics and Computers in Simulation, vol. InPress, Corrected Proof, pp.–, 2010.

[17] B. Buchberger, “Bruno buchberger’s phd thesis 1965: An algorithm for finding the basiselements of the residue class ring of a zero dimensional polynomial ideal,” J. Symb.

Comput., vol. 41, pp. 475–511, March 2006.240

Page 251: Lenguaje para implementar sistemas expertos con … · A mis companeros˜ del proyecto Dynamic Complexity of Cooperation-Based Self-Organizing Networks de la European Science Foundation

[18] D. A. Cox, J. Little, and D. O’Shea, Ideals, Varieties, and Algorithms. An Introduction to

Computational Algebraic Geometry and Commutative Algebra. Undergraduate Texts inMathematics, Springer, 2007.

[19] “National comprenhensive cancer network. nccn clinical practice guidelines in oncologyv.2.2010..” Available at http://www.nccn.org.

[20] ISO/IEC, “14977:1996 information technology – syntactic metalanguage – extended bnf.”Available athttp://www.iso.org/iso/catalogue detail.htm?csnumber=26153.

[21] S. Microsystems, “Javacc: a parser/scanner generator for java.” Available athttps://javacc.dev.java.net/ .

[22] B. Buchberger, “Grobner bases: A short introduction for systems theorists,” in Computer

Aided Systems Theory — EUROCAST 2001 (R. Moreno-Dıaz, B. Buchberger, andJ. Luis Freire, eds.), vol. 2178 of Lecture Notes in Computer Science, pp. 1–19, SpringerBerlin / Heidelberg, 2001. 10.1007/3-540-45654-6 1.

241