|
| 1 | +package IA.Centrals; |
| 2 | + |
| 3 | + |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Vector; |
| 7 | + |
| 8 | +import aima.search.framework.Successor; |
| 9 | +import aima.search.framework.SuccessorFunction; |
| 10 | + |
| 11 | +public class CentralsGeneradorEstats implements SuccessorFunction { |
| 12 | + |
| 13 | + private int debug; |
| 14 | + private int iter; |
| 15 | + private boolean[] vops; |
| 16 | + |
| 17 | + public CentralsGeneradorEstats(int deb, boolean[] ops){ |
| 18 | + this.debug = deb; |
| 19 | + iter=0; |
| 20 | + vops = new boolean[6]; |
| 21 | + for(int i=0;i<6;i++)vops[i] = ops[i]; |
| 22 | + } |
| 23 | + |
| 24 | + public List getSuccessors(Object state) { |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + int count = 0; |
| 29 | + ArrayList retVal= new ArrayList(); |
| 30 | + Representacio estat = (Representacio) state; |
| 31 | + |
| 32 | + |
| 33 | + // pintem la solucio escollida anteriorment |
| 34 | + /*cjf.setPlano(estat.getAmplada(),estat.getAltura(),estat.getPlano(),estat.getSol(),estat.getVNodo() );//3=tp |
| 35 | + cjf.repaint(); |
| 36 | + BufferedReader in = new BufferedReader( new InputStreamReader(System.in) ); |
| 37 | + |
| 38 | + |
| 39 | + System.out.print("Ingrese string "); |
| 40 | + String r=""; |
| 41 | + try { |
| 42 | + r = in.readLine(); |
| 43 | + } catch (IOException e) { |
| 44 | + // TODO Auto-generated catch block |
| 45 | + e.printStackTrace(); |
| 46 | + } |
| 47 | + System.out.println("string = "+r); |
| 48 | + */ |
| 49 | + if(debug>iter){ |
| 50 | + iter++; |
| 51 | + |
| 52 | + CentralsFuncioHeuristica1 CHF =new CentralsFuncioHeuristica1(); |
| 53 | + Vector solActual = (Vector)estat.getSol(); |
| 54 | + |
| 55 | + |
| 56 | + if(vops[0]){ //OPERADOR INTERCANVIAR CONNEXIO 1 |
| 57 | + for(int i=0;i<solActual.size();i++){ |
| 58 | + if(((Vector)solActual.get(i)).size()!=0) { |
| 59 | + for(int j=1; j<((Vector)solActual.get(i)).size();j++) { |
| 60 | + for (int k=0;k<solActual.size();k++) { |
| 61 | + if(((Vector)solActual.get(k)).size()!=0) { |
| 62 | + Representacio newEstat = new Representacio(estat); |
| 63 | + |
| 64 | + if (newEstat.interConexio(i,Integer.parseInt(((Vector)solActual.get(i)).get(j).toString()),k)) { |
| 65 | + |
| 66 | + //int v2=CHF.getHeuristicValue(newEstat); |
| 67 | + String S=new String("Interconexion= "+" "+i+" Up"); |
| 68 | + retVal.add(new Successor(S,newEstat)); |
| 69 | + count++; |
| 70 | + |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + if(vops[1]){ //OPEARDOR INTERCANVIAR CONNEXIO 2 |
| 80 | + for(int i=0;i<solActual.size();i++){ |
| 81 | + if(((Vector)solActual.get(i)).size()!=0) { |
| 82 | + for(int j=1; j<((Vector)solActual.get(i)).size();j++) { |
| 83 | + for (int k=0;k<solActual.size();k++) { |
| 84 | + if(((Vector)solActual.get(k)).size()!=0) { |
| 85 | + Representacio newEstat = new Representacio(estat); |
| 86 | + |
| 87 | + if (newEstat.interConexio2(i,Integer.parseInt(((Vector)solActual.get(i)).get(j).toString()),k)) { |
| 88 | + |
| 89 | + //int v2=CHF.getHeuristicValue(newEstat); |
| 90 | + String S=new String("Interconexion= "+" "+i+" Up"); |
| 91 | + retVal.add(new Successor(S,newEstat)); |
| 92 | + count++; |
| 93 | + |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + //VARIABLES PER GESTIONAR OPERADORS DE REPETIDORS |
| 104 | + int numrep = estat.getNumRepetidors(); |
| 105 | + boolean [] reputil= new boolean[numrep]; |
| 106 | + |
| 107 | + for(int i = 0;i< reputil.length;i++)reputil[i]=false; |
| 108 | + |
| 109 | + |
| 110 | + int rutil=0; |
| 111 | + for (int i=estat.getNumCentrals();i<estat.getEstat().size();i++) { |
| 112 | + if(((Vector)estat.getEstat().get(i)).size()!=0) { |
| 113 | + reputil[i-estat.getNumCentrals()]=true; |
| 114 | + rutil++; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if(vops[2]){ //OPEARDOR AFEGIR REPETIDOR |
| 119 | + if(rutil<estat.getMaxRep()) |
| 120 | + { |
| 121 | + for(int i = estat.getNumCentrals();i<estat.getNumCentrals()+estat.getNumRepetidors();i++){ |
| 122 | + if(!reputil[i-estat.getNumCentrals()]){ |
| 123 | + for (int j=0;j<estat.getEstat().size();j++){ |
| 124 | + if(((Vector)estat.getEstat().get(j)).size()!=0) { |
| 125 | + for (int k=1;k<((Vector)estat.getEstat().get(j)).size();k++) { |
| 126 | + int actual = Integer.parseInt(((Vector)estat.getEstat().get(j)).get(k).toString()); |
| 127 | + if(actual>j){ |
| 128 | + Representacio newEstat = new Representacio(estat); |
| 129 | + if(newEstat.afegirRepetidor(j,actual,i)){ |
| 130 | + //int v2=CHF.getHeuristicValue(newEstat); |
| 131 | + String S=new String("AfegirRepetidor"); |
| 132 | + retVal.add(new Successor(S,newEstat)); |
| 133 | + count++; |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if(vops[3]){ //OPEARDOR TREURE REP |
| 146 | + for(int i = estat.getNumCentrals();i<estat.getNumCentrals()+estat.getNumRepetidors();i++){ |
| 147 | + if(reputil[i-estat.getNumCentrals()]){ |
| 148 | + Representacio newEstat = new Representacio(estat); |
| 149 | + newEstat.treureRepetidor(i); |
| 150 | + //int v2=CHF.getHeuristicValue(newEstat); |
| 151 | + String S=new String("AfegirRepetidor"); |
| 152 | + retVal.add(new Successor(S,newEstat)); |
| 153 | + count++; |
| 154 | + |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + if(vops[4]){ //OPEARDOR INTERCANVIAR REPETIDOR |
| 164 | + for(int i = estat.getNumCentrals();i<estat.getNumCentrals()+estat.getNumRepetidors();i++){ |
| 165 | + if(reputil[i-estat.getNumCentrals()]){ |
| 166 | + for(int j = estat.getNumCentrals();j<estat.getNumCentrals()+estat.getNumRepetidors();j++){ |
| 167 | + if(!reputil[j-estat.getNumCentrals()]){ |
| 168 | + Representacio newEstat = new Representacio(estat); |
| 169 | + newEstat.interRepetidor(i,j); |
| 170 | + //int v2=CHF.getHeuristicValue(newEstat); |
| 171 | + String S=new String("InterRepetidor"); |
| 172 | + retVal.add(new Successor(S,newEstat)); |
| 173 | + count++; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + } |
| 178 | + |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + if(vops[5]){ //OPEARDOR INTERCANVIAR REPETIDOR 2 |
| 183 | + int cvell = count; |
| 184 | + for(int i = estat.getNumCentrals();i<estat.getNumCentrals()+estat.getNumRepetidors();i++){ |
| 185 | + if(reputil[i-estat.getNumCentrals()]){ |
| 186 | + for(int j = i+1;j<estat.getNumCentrals()+estat.getNumRepetidors();j++){ |
| 187 | + if(reputil[j-estat.getNumCentrals()]){ |
| 188 | + Representacio newEstat = new Representacio(estat); |
| 189 | + if(newEstat.interRepetidor2(i,j)){ |
| 190 | + //int v2=CHF.getHeuristicValue(newEstat); |
| 191 | + String S=new String("InterRepetidor"); |
| 192 | + retVal.add(new Successor(S,newEstat)); |
| 193 | + count++; |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + } |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + System.out.println("cunt-cvell="+(count-cvell)); |
| 203 | + } |
| 204 | + |
| 205 | + |
| 206 | + }//FI DEBUG ITER |
| 207 | + |
| 208 | + System.out.println("num estats generats= " + count); |
| 209 | + return retVal; |
| 210 | + |
| 211 | + } |
| 212 | +} |
0 commit comments