/src/keel/Algorithms/Subgroup_Discovery/MESDIF/MESDIF/CromCAN.java

https://github.com/SCI2SUGR/KEEL · Java · 173 lines · 55 code · 26 blank · 92 comment · 9 complexity · 78047af0fc873eaebb641e2642eeabae MD5 · raw file

  1. /***********************************************************************
  2. This file is part of KEEL-software, the Data Mining tool for regression,
  3. classification, clustering, pattern mining and so on.
  4. Copyright (C) 2004-2010
  5. F. Herrera (herrera@decsai.ugr.es)
  6. L. Sánchez (luciano@uniovi.es)
  7. J. Alcalá-Fdez (jalcala@decsai.ugr.es)
  8. S. García (sglopez@ujaen.es)
  9. A. Fernández (alberto.fernandez@ujaen.es)
  10. J. Luengo (julianlm@decsai.ugr.es)
  11. This program is free software: you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation, either version 3 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program. If not, see http://www.gnu.org/licenses/
  21. **********************************************************************/
  22. package keel.Algorithms.Subgroup_Discovery.MESDIF.MESDIF;
  23. import org.core.*;
  24. /**
  25. * <p> Defines the structure and manage the contents of a canonical rule.
  26. * This implementation uses only integer values to store the gens.
  27. *
  28. * @author Created by Pedro González (University of Jaen) 18/02/2004
  29. * @author Modified by Pedro González (University of Jaen) 4/08/2007
  30. * @author Modified by Cristóbal J. Carmona (University of Jaen) 30/06/2010
  31. * @version 2.0
  32. * @since JDK1.5
  33. * </p>
  34. */
  35. public class CromCAN {
  36. private int num_genes; // Number of genes
  37. private int chromosome []; // Individual content - integer representation
  38. /**
  39. * <p>
  40. * Creates new instance of chromosome, no initialization
  41. * </p>
  42. * @param length Length of the chromosome
  43. */
  44. public CromCAN(int length) {
  45. num_genes = length;
  46. chromosome = new int [length];
  47. }
  48. /**
  49. * <p>
  50. * Random initialization of an existing chromosome
  51. * </p>
  52. * @param Variables contents the characteristics of the variables
  53. */
  54. public void initCromRnd (TableVar Variables) {
  55. for (int i=0; i<num_genes; i++)
  56. chromosome[i] = Randomize.Randint (0, Variables.getNLabelVar(i));
  57. }
  58. /**
  59. * <p>
  60. * Biased Random initialization of an existing chromosome
  61. * The random inicializacion is biased by generating
  62. * chromosomes with a maximum number or participating variables
  63. * and for an indicated % of the population (the rest is random)
  64. * </p>
  65. * @param Variables contents the type of the variable, and the number of labels.
  66. * @param porcVar percentage of variables to appear in the biased initialization
  67. */
  68. public void initCromBsd (TableVar Variables , float porcVar) {
  69. int num_var;
  70. /* Array of integers to show if each chromosome is initialized */
  71. int crom_inic[]= new int[num_genes];
  72. for (int i=0; i<num_genes; i++)
  73. crom_inic[i] = 0;
  74. /* First, obtain the number of variables to appear in the chromosome */
  75. int numInterv = Randomize.Randint (1, Math.round(porcVar*num_genes));
  76. /* Initialize numInterv variables to take part in the individual */
  77. int var=0;
  78. while (var<numInterv) {
  79. num_var = Randomize.Randint (0, num_genes-1);
  80. /* If not initialized, initialize and increase the count */
  81. if (crom_inic[num_var]==0) {
  82. chromosome[num_var] = Randomize.Randint (0, Variables.getNLabelVar(num_var));
  83. crom_inic[num_var]=1;
  84. var++;
  85. }
  86. }
  87. /* Initialize the rest of variables to "not-intervene" */
  88. for (int i=0; i<num_genes; i++) {
  89. if (crom_inic[i]!=0) {
  90. chromosome[i] = Variables.getNLabelVar(i);
  91. }
  92. }
  93. }
  94. /**
  95. * <p>
  96. * Retuns the value of the gene indicated
  97. * </p>
  98. * @param pos Position of the variable in the chromosome
  99. * @return Value of the variable
  100. */
  101. public int getCromElem (int pos) {
  102. return chromosome[pos];
  103. }
  104. /**
  105. * <p>
  106. * Sets the value of the indicated gene of the chromosome
  107. * </p>
  108. * @param pos Position of the variable in the chromosome
  109. * @param value Value of the variable
  110. */
  111. public void setCromElem (int pos, int value ) {
  112. chromosome[pos] = value;
  113. }
  114. /**
  115. * <p>
  116. * Retuns the length of the chromosome
  117. * </p>
  118. * @return Length of the chromosome
  119. */
  120. public int getCromLength () {
  121. return num_genes;
  122. }
  123. /**
  124. * <p>
  125. * Prints the chromosome genes
  126. * </p>
  127. * @param nFile File to write the chromosome
  128. */
  129. public void print(String nFile) {
  130. String contents;
  131. contents = "Chromosome: ";
  132. for(int i=0; i<num_genes; i++)
  133. contents+= chromosome[i] + " ";
  134. contents+= "\n";
  135. if (nFile=="")
  136. System.out.print (contents);
  137. else
  138. Files.addToFile(nFile, contents);
  139. }
  140. }