Esempio n. 1
0
File: pgb.c Progetto: soviet/VCP-PGB
/**
 *
 * Funcion que libera la memoria del arbol, se liberar en postorden.
 *
 **/
void freeArbol(Arbol *a)
{
  if(a != NULL)
    {
      freeArbol(a->izq);
      freeArbol(a->der);
      free(a);
    }

}
Esempio n. 2
0
File: pgb.c Progetto: soviet/VCP-PGB
/**
 *
 * Funcion que que convierte el cromosma binario en un Arbol, esta funcion es
 * llamada por la funcionObjetivo del AG
 *
 **/
void convertion(bool *chrm, bool grafica)
{

  Arbol *A, *izq, *aux, *aux2;
  int i, nChrm, h, hmax, setear, nuevo_valor, *array;/* [8] = {6, 0, 16, 7, 1, 18, 4, 21}; */
  bool lado = 0;
  FILE *filetree;
  char *str;
  char nombres[][35] = {
    /* Funciones de Coloracion */
    "Greedy Coloring", "ColoringMoreFrequentColor", "ColoringLessFrequentColor", "UnColoring",
    "ColoringNeighbors", "InterchangeColor", "UncoloringNeighbors",
    /* Funciones que se repiten - solo de coloracion */
    "EqualX", "IfX",  "AndX",
    /* Funciones Genericas */
    "While", "If", "And", "Equal", "Or", "Not",
    /* Terminales */
    "vertexLargestDegree","vertexMinimumDegree", "firstVertex", "vertexLowestNumberedColor",
    "vertexHighestNumberedColor", "vertexMoreFrequentColor", "vertexLessFrequentColor",
    "vertexSaturationDegree", "vertexMoreUncoloredNeighbors", "vertexIncidenceDegree",
    "not_Increase?", "ExistUncoloredVertex?",
    /* Terminales que se repiten */
    "vertexLowestNumberedColor", "vertexHighestNumberedColor", "vertexMoreFrequentColor",
    "vertexLessFrequentColor"};

  A = NULL;
  nChrm = sizeChrm / nByte;
  contador  = nChrm;
  bandera = 0;
  h = 0;
  hmax = 12;

  array = calloc(nChrm, sizeof(int));
  getChrm(chrm, array);

  for(i=0; i<5; i++)
    funciones[i] = 1;
  for(i=5; i<16; i++)
    funciones[i] = 2;

  funciones[15] = 1;
  funciones[6] = 1;

  for(bandera=0, i=0; i<nChrm; i++)
    {
      /* /\* Si el primer elemento es un Terminal se cambia por una Funcion *\/ */
      /* if(i == 0 && array[i] > 15) */
      /* 	{ */
      /* 	  setear = abs((0 % 16) - (array[i] % 16)); */
      /* 	  array[0] = (setear < 16) ? setear: 0; */
      /* 	} */

      /* /\* Si el ultimo elemento es una Funcion se cambia por un Terminal *\/ */
      /* if(i == (nChrm - 1) && array[(nChrm - 1)] < 16) */
      /* 	array[nChrm - 1] = array[nChrm - 1] + 16; */

      construir(&A, array[i], i, -1, -1);

      if(bandera == 0)
	{
	  /* Guardamos el arbol en otro arbol auxiliar */
	  aux = A;

	  /* Creamos un segundo Arbol auxiliar con los datos del nodo que
	     correspondia */
	  aux2 = crearNodo(array[i], i);

	  /* El arbol original lo setiamos y creamos un nodo con una
	     Funcion, que sea siempre con dos argumentos, una funcion generica*/
	  setear = abs((A->id % 16) - (A->valor % 16));
	  nuevo_valor = ((setear < 15) ? setear: 14);

	  if(funciones[nuevo_valor] == 1)
	    nuevo_valor = nuevo_valor + 10;

	  if(nuevo_valor ==  5 || nuevo_valor ==  6)
	    {
	      nuevo_valor = nuevo_valor + 8;
	    }

	  A = crearNodo(nuevo_valor, contador++);
	  /* El nuevo nodo tiene de hijo a la izq al primer auxiliar y el
	     der. al segundo auxiliar si LADO es 0 si es 1 viceversa.
	     Se van rotando para que los arboles no sean tan desbalanciados*/

	  if(lado == 0)
	    {
	      A->izq = aux;
	      A->der = aux2;
	      lado = 1;
	    }
	  else
	    {
	      A->izq = aux2;
	      A->der = aux;
	      lado = 0;
	    }

	}
      bandera = 0;
      /* printf("Fin Costruir\n\n"); */

    }
  /* Liberar el arreglo con las F y T */
  /* free(array); */
  rebuilt(A);
  /* salidaNameArbol(A); */

  if(grafica) {

    i = snprintf(NULL, NULL, "%s/Arbol.dot", ruta_resultados);
    str = malloc((i+1) *sizeof(char));
    snprintf(str, i+1, "%s/Arbol.dot", ruta_resultados);

    filetree = fopen(str, "w");
    free(str);
    fprintf(filetree, "digraph G{\n");
    fprintf(filetree, "%d [ label = \"%s\" ];\n", A->id, nombres[A->valor]);

    izq = A->izq;

    fprintf(filetree, "%d [ label = \"%s\" ];\n", izq->id, nombres[izq->valor]);
    imprimir(A, filetree, nombres);

    fprintf(filetree, "}");

    fclose(filetree);

    i = snprintf(NULL, NULL, "dot %s/Arbol.dot -o %s/arbol.png -Tpng", ruta_resultados, ruta_resultados);
    str = malloc((i+1) *sizeof(char));
    snprintf(str, i+1, "dot %s/Arbol.dot -o %s/arbol.png -Tpng", ruta_resultados, ruta_resultados);

    printf("%s\n",str);

    if(system(str))
      printf("No se pudo dibujar el Arbol\n");
    free(str);
  }

  conexion(A, grafica);

  freeArbol(A);
  free(array);
}
int main() {
    //se inicia el arbol
    struct t_node *arbol = NULL;
    iniciarArbol(&arbol);

    //se inicia el arreglo de funciones
    void (*arregloCallbacks[9])(char *arg);
    arregloCallbacks[0] = terminar_programa;
    arregloCallbacks[1] = insertar;
    arregloCallbacks[2] = eliminar_linea;
    arregloCallbacks[3] = eliminar_coincidencia;
    arregloCallbacks[4] = mostrar_por_linea;
    arregloCallbacks[5] = crear_archivo;
    arregloCallbacks[6] = eliminar_archivo;
    arregloCallbacks[7] = truncar_archivo;
    arregloCallbacks[8] = mostrarDatosOrdenados;

    //se inicializan variables
    int cantidadInstrucciones;
    scanf("%d", &cantidadInstrucciones);
    getchar();
    char *comando, **comandoProcesado;
    int indiceFuncion;
    size_t tamanoComandoProcesado;
    int iteracion;
    for(iteracion = 0; iteracion < cantidadInstrucciones; iteracion++) { //leyendo los comandos
        printf(">> ");
        indiceFuncion = primerNumero(); //se lee el numero de funcion
        getchar();
        comando = leerComando(); //se lee el resto del comando
        comandoProcesado = split(comando, strlen(comando), ' ', &tamanoComandoProcesado); // se splitea

        //se agrega el comando y la funcion al arbol
        if(indiceFuncion < 8) {
            agregarDatosArbol(&arbol, comandoProcesado, tamanoComandoProcesado, arregloCallbacks[indiceFuncion]);
        }
        if(indiceFuncion == 9) {
            agregarDatosArbol(&arbol, comandoProcesado, tamanoComandoProcesado, arregloCallbacks[9]);
        }
    }
    free(comando);
    free(comandoProcesado);

    printf("\n");

    char *comandoARealiar = NULL;
    char **comandoARealiarSpliteado;
    size_t tamanoComandoARealizar;
    int comandoExiste, iterandoArreglos;
    while(ejecucion) {
        comandoExiste = 0;
        printf("> ");
        comandoARealiar = leerComando();

        if(strlen(comandoARealiar)>0) {
            comandoARealiarSpliteado = split(comandoARealiar, strlen(comandoARealiar), ' ', &tamanoComandoARealizar);

            comandoExiste = ejecutarFuncionArbol(arbol, comandoARealiarSpliteado, tamanoComandoARealizar);

            if(!comandoExiste) {
                printf(">>>No existe el comando [");
                for(iterandoArreglos = 0; iterandoArreglos<(tamanoComandoARealizar-1); iterandoArreglos++) {
                    printf("%s", comandoARealiarSpliteado[iterandoArreglos]);
                    if(iterandoArreglos<tamanoComandoARealizar-2) {
                        printf(" ");
                    }
                }
                printf("]\n");
            }
        }

    }
    free(comandoARealiar);
    free(comandoARealiarSpliteado);
    freeArbol(&arbol);
    return 0;
}