| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
package weka.classifiers.bayes.net.search.local; |
| 23 | |
|
| 24 | |
import java.util.Enumeration; |
| 25 | |
import java.util.Vector; |
| 26 | |
|
| 27 | |
import weka.classifiers.bayes.BayesNet; |
| 28 | |
import weka.core.Instances; |
| 29 | |
import weka.core.Option; |
| 30 | |
import weka.core.RevisionUtils; |
| 31 | |
import weka.core.Utils; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | 0 | public class LAGDHillClimber |
| 73 | |
extends HillClimber { |
| 74 | |
|
| 75 | |
|
| 76 | |
static final long serialVersionUID = 7217437499439184344L; |
| 77 | |
|
| 78 | |
|
| 79 | 0 | int m_nNrOfLookAheadSteps = 2; |
| 80 | |
|
| 81 | |
|
| 82 | 0 | int m_nNrOfGoodOperations = 5; |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
protected void search(BayesNet bayesNet, Instances instances) throws Exception { |
| 92 | 0 | int k=m_nNrOfLookAheadSteps; |
| 93 | 0 | int l=m_nNrOfGoodOperations; |
| 94 | 0 | lookAheadInGoodDirectionsSearch(bayesNet, instances, k, l); |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
protected void lookAheadInGoodDirectionsSearch(BayesNet bayesNet, Instances instances, int nrOfLookAheadSteps, int nrOfGoodOperations) throws Exception { |
| 109 | 0 | System.out.println("Initializing Cache"); |
| 110 | 0 | initCache(bayesNet, instances); |
| 111 | |
|
| 112 | 0 | while (nrOfLookAheadSteps>1) { |
| 113 | 0 | System.out.println("Look Ahead Depth: "+nrOfLookAheadSteps); |
| 114 | 0 | boolean legalSequence = true; |
| 115 | 0 | double sequenceDeltaScore = 0; |
| 116 | 0 | Operation [] bestOperation=new Operation [nrOfLookAheadSteps]; |
| 117 | |
|
| 118 | 0 | bestOperation = getOptimalOperations(bayesNet, instances, nrOfLookAheadSteps, nrOfGoodOperations); |
| 119 | 0 | for (int i = 0; i < nrOfLookAheadSteps; i++) { |
| 120 | 0 | if (bestOperation [i] == null) { |
| 121 | 0 | legalSequence=false; |
| 122 | |
} else { |
| 123 | 0 | sequenceDeltaScore += bestOperation [i].m_fDeltaScore; |
| 124 | |
} |
| 125 | |
} |
| 126 | 0 | while (legalSequence && sequenceDeltaScore > 0) { |
| 127 | 0 | System.out.println("Next Iteration.........................."); |
| 128 | 0 | for (int i = 0; i < nrOfLookAheadSteps; i++) { |
| 129 | 0 | performOperation(bayesNet, instances,bestOperation [i]); |
| 130 | |
} |
| 131 | 0 | bestOperation = getOptimalOperations(bayesNet, instances, nrOfLookAheadSteps, nrOfGoodOperations); |
| 132 | 0 | sequenceDeltaScore = 0; |
| 133 | 0 | for (int i = 0; i < nrOfLookAheadSteps; i++) { |
| 134 | 0 | if (bestOperation [i] != null) { |
| 135 | 0 | System.out.println(bestOperation [i].m_nOperation + " " + bestOperation [i].m_nHead + " " + bestOperation [i].m_nTail); |
| 136 | 0 | sequenceDeltaScore += bestOperation [i].m_fDeltaScore; |
| 137 | |
} else { |
| 138 | 0 | legalSequence = false; |
| 139 | |
|
| 140 | |
} |
| 141 | 0 | System.out.println("DeltaScore: "+sequenceDeltaScore); |
| 142 | |
} |
| 143 | |
} |
| 144 | 0 | --nrOfLookAheadSteps; |
| 145 | 0 | } |
| 146 | |
|
| 147 | |
|
| 148 | 0 | Operation oOperation = getOptimalOperation(bayesNet, instances); |
| 149 | 0 | while ((oOperation != null) && (oOperation.m_fDeltaScore > 0)) { |
| 150 | 0 | performOperation(bayesNet, instances, oOperation); |
| 151 | 0 | System.out.println("Performing last greedy steps"); |
| 152 | 0 | oOperation = getOptimalOperation(bayesNet, instances); |
| 153 | |
} |
| 154 | |
|
| 155 | 0 | m_Cache = null; |
| 156 | 0 | } |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
protected Operation getAntiOperation(Operation oOperation) throws Exception { |
| 166 | 0 | if (oOperation.m_nOperation == Operation.OPERATION_ADD) |
| 167 | 0 | return (new Operation (oOperation.m_nTail, oOperation.m_nHead, Operation.OPERATION_DEL)); |
| 168 | |
else { |
| 169 | 0 | if (oOperation.m_nOperation == Operation.OPERATION_DEL) |
| 170 | 0 | return (new Operation (oOperation.m_nTail, oOperation.m_nHead, Operation.OPERATION_ADD)); |
| 171 | |
else { |
| 172 | 0 | return (new Operation (oOperation.m_nHead, oOperation.m_nTail, Operation.OPERATION_REVERSE)); |
| 173 | |
} |
| 174 | |
} |
| 175 | |
} |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
protected Operation [] getGoodOperations(BayesNet bayesNet, Instances instances, int nrOfGoodOperations) throws Exception { |
| 188 | 0 | Operation [] goodOperations=new Operation [nrOfGoodOperations]; |
| 189 | 0 | for (int i = 0; i < nrOfGoodOperations; i++) { |
| 190 | 0 | goodOperations [i] = getOptimalOperation(bayesNet, instances); |
| 191 | 0 | if (goodOperations[i] != null) { |
| 192 | 0 | m_Cache.put(goodOperations [i], -1E100); |
| 193 | 0 | } else i=nrOfGoodOperations; |
| 194 | |
} |
| 195 | 0 | for (int i = 0; i < nrOfGoodOperations; i++) { |
| 196 | 0 | if (goodOperations[i] != null) { |
| 197 | 0 | if (goodOperations [i].m_nOperation!=Operation.OPERATION_REVERSE) { |
| 198 | 0 | m_Cache.put(goodOperations [i], goodOperations [i].m_fDeltaScore); |
| 199 | |
} else { |
| 200 | 0 | m_Cache.put(goodOperations [i], goodOperations [i].m_fDeltaScore - m_Cache.m_fDeltaScoreAdd[goodOperations[i].m_nHead] [goodOperations [i].m_nTail]); |
| 201 | |
} |
| 202 | 0 | } else i=nrOfGoodOperations; |
| 203 | |
} |
| 204 | 0 | return goodOperations; |
| 205 | |
} |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
protected Operation [] getOptimalOperations(BayesNet bayesNet, Instances instances, int nrOfLookAheadSteps, int nrOfGoodOperations) throws Exception { |
| 218 | 0 | if (nrOfLookAheadSteps == 1) { |
| 219 | 0 | Operation [] bestOperation = new Operation [1]; |
| 220 | 0 | bestOperation [0] = getOptimalOperation(bayesNet, instances); |
| 221 | 0 | return(bestOperation); |
| 222 | |
} else { |
| 223 | 0 | double bestDeltaScore = 0; |
| 224 | 0 | double currentDeltaScore = 0; |
| 225 | 0 | Operation [] bestOperation = new Operation [nrOfLookAheadSteps]; |
| 226 | 0 | Operation [] goodOperations = new Operation [nrOfGoodOperations]; |
| 227 | 0 | Operation [] tempOperation = new Operation [nrOfLookAheadSteps-1]; |
| 228 | 0 | goodOperations = getGoodOperations(bayesNet, instances, nrOfGoodOperations); |
| 229 | 0 | for (int i = 0; i < nrOfGoodOperations; i++) { |
| 230 | 0 | if (goodOperations[i] != null) { |
| 231 | 0 | performOperation(bayesNet, instances, goodOperations [i]); |
| 232 | 0 | tempOperation = getOptimalOperations(bayesNet, instances, nrOfLookAheadSteps-1, nrOfGoodOperations); |
| 233 | 0 | currentDeltaScore = goodOperations [i].m_fDeltaScore; |
| 234 | 0 | for (int j = 0; j < nrOfLookAheadSteps-1; j++) { |
| 235 | 0 | if (tempOperation [j] != null) { |
| 236 | 0 | currentDeltaScore += tempOperation [j].m_fDeltaScore; |
| 237 | |
} |
| 238 | |
} |
| 239 | 0 | performOperation(bayesNet, instances, getAntiOperation(goodOperations [i])); |
| 240 | 0 | if (currentDeltaScore > bestDeltaScore) { |
| 241 | 0 | bestDeltaScore = currentDeltaScore; |
| 242 | 0 | bestOperation [0] = goodOperations [i]; |
| 243 | 0 | for (int j = 1; j < nrOfLookAheadSteps; j++) { |
| 244 | 0 | bestOperation [j] = tempOperation [j-1]; |
| 245 | |
} |
| 246 | |
} |
| 247 | 0 | } else i=nrOfGoodOperations; |
| 248 | |
} |
| 249 | 0 | return(bestOperation); |
| 250 | |
} |
| 251 | |
} |
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
public void setMaxNrOfParents(int nMaxNrOfParents) { |
| 260 | 0 | m_nMaxNrOfParents = nMaxNrOfParents; |
| 261 | 0 | } |
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
public int getMaxNrOfParents() { |
| 269 | 0 | return m_nMaxNrOfParents; |
| 270 | |
} |
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
public void setNrOfLookAheadSteps(int nNrOfLookAheadSteps) { |
| 278 | 0 | m_nNrOfLookAheadSteps = nNrOfLookAheadSteps; |
| 279 | 0 | } |
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
public int getNrOfLookAheadSteps() { |
| 287 | 0 | return m_nNrOfLookAheadSteps; |
| 288 | |
} |
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
public void setNrOfGoodOperations(int nNrOfGoodOperations) { |
| 296 | 0 | m_nNrOfGoodOperations = nNrOfGoodOperations; |
| 297 | 0 | } |
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
public int getNrOfGoodOperations() { |
| 305 | 0 | return m_nNrOfGoodOperations; |
| 306 | |
} |
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
public Enumeration listOptions() { |
| 315 | 0 | Vector newVector = new Vector(); |
| 316 | |
|
| 317 | 0 | newVector.addElement(new Option("\tLook Ahead Depth", "L", 2, "-L <nr of look ahead steps>")); |
| 318 | 0 | newVector.addElement(new Option("\tNr of Good Operations", "G", 5, "-G <nr of good operations>")); |
| 319 | |
|
| 320 | 0 | Enumeration enm = super.listOptions(); |
| 321 | 0 | while (enm.hasMoreElements()) { |
| 322 | 0 | newVector.addElement(enm.nextElement()); |
| 323 | |
} |
| 324 | 0 | return newVector.elements(); |
| 325 | |
} |
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
public void setOptions(String[] options) throws Exception { |
| 364 | 0 | String sNrOfLookAheadSteps = Utils.getOption('L', options); |
| 365 | 0 | if (sNrOfLookAheadSteps.length() != 0) { |
| 366 | 0 | setNrOfLookAheadSteps(Integer.parseInt(sNrOfLookAheadSteps)); |
| 367 | |
} else { |
| 368 | 0 | setNrOfLookAheadSteps(2); |
| 369 | |
} |
| 370 | |
|
| 371 | 0 | String sNrOfGoodOperations = Utils.getOption('G', options); |
| 372 | 0 | if (sNrOfGoodOperations.length() != 0) { |
| 373 | 0 | setNrOfGoodOperations(Integer.parseInt(sNrOfGoodOperations)); |
| 374 | |
} else { |
| 375 | 0 | setNrOfGoodOperations(5); |
| 376 | |
} |
| 377 | |
|
| 378 | 0 | super.setOptions(options); |
| 379 | 0 | } |
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
public String[] getOptions() { |
| 387 | 0 | String[] superOptions = super.getOptions(); |
| 388 | 0 | String[] options = new String[9 + superOptions.length]; |
| 389 | 0 | int current = 0; |
| 390 | |
|
| 391 | 0 | options[current++] = "-L"; |
| 392 | 0 | options[current++] = "" + m_nNrOfLookAheadSteps; |
| 393 | |
|
| 394 | 0 | options[current++] = "-G"; |
| 395 | 0 | options[current++] = "" + m_nNrOfGoodOperations; |
| 396 | |
|
| 397 | |
|
| 398 | 0 | for (int iOption = 0; iOption < superOptions.length; iOption++) { |
| 399 | 0 | options[current++] = superOptions[iOption]; |
| 400 | |
} |
| 401 | |
|
| 402 | |
|
| 403 | 0 | while (current < options.length) { |
| 404 | 0 | options[current++] = ""; |
| 405 | |
} |
| 406 | 0 | return options; |
| 407 | |
} |
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
public String globalInfo() { |
| 415 | 0 | return "This Bayes Network learning algorithm uses a Look Ahead Hill Climbing algorithm called LAGD Hill Climbing." + |
| 416 | |
" Unlike Greedy Hill Climbing it doesn't calculate a best greedy operation (adding, deleting or reversing an arc) " + |
| 417 | |
"but a sequence of nrOfLookAheadSteps operations, which leads to a network structure whose score is most likely " + |
| 418 | |
"higher in comparison to the network obtained by performing a sequence of nrOfLookAheadSteps greedy operations. " + |
| 419 | |
"The search is not restricted by an order " + |
| 420 | |
"on the variables (unlike K2). The difference with B and B2 is that this hill " + |
| 421 | |
"climber also considers arrows part of the naive Bayes structure for deletion."; |
| 422 | |
} |
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
public String nrOfLookAheadStepsTipText() { |
| 428 | 0 | return "Sets the Number of Look Ahead Steps. 'nrOfLookAheadSteps = 2' means that all network structures in a " + |
| 429 | |
"distance of 2 (from the current network structure) are taken into account for the decision which arcs to add, " + |
| 430 | |
"remove or reverse. 'nrOfLookAheadSteps = 1' results in Greedy Hill Climbing." ; |
| 431 | |
} |
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
public String nrOfGoodOperationsTipText() { |
| 437 | 0 | return "Sets the Number of Good Operations per Look Ahead Step. 'nrOfGoodOperations = 5' means that for the next " + |
| 438 | |
"Look Ahead Step only the 5 best Operations (adding, deleting or reversing an arc) are taken into account for the " + |
| 439 | |
"calculation of the best sequence consisting of nrOfLookAheadSteps operations." ; |
| 440 | |
} |
| 441 | |
|
| 442 | |
|
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
public String getRevision() { |
| 448 | 0 | return RevisionUtils.extract("$Revision: 8034 $"); |
| 449 | |
} |
| 450 | |
|
| 451 | |
} |