📄 bfagent.java
字号:
int specificity = 0; //note here a "raw" for loop around the fcastList. I could create an index //and do the swarm thing, but I leave this here to keep myself humble. for (f = 0; f < privateParams.numfcasts; f++) { parent1 = (BFCast)(fcastList.get(0)); specificity += parent1.getSpecificity(); } avspecificity = ((double) specificity)/(double)privateParams.numfcasts; } newList.clear(); return this;}/*"This is a method that copies the instance variables out of one forecast object into another. It copies not only the bitvector of monitored conditions, but also the forecast value, strength, variance, specFactor, specificity, and so forth. The only deviation is that if the return from the original forecast's getCnt method (its count value) is equal to 0, then the strength of the copy is equal to the value of a static variable named minstrength."*/public BFCast CopyRule$From( BFCast to , BFCast from){ to.setForecast( from.getForecast()); to.setLforecast( from.getLforecast()); to.setVariance( from.getVariance()); to.setStrength( from.getStrength()); to.setAval( from.getAval()); to.setBval( from.getBval()); to.setCval( from.getCval()); to.setSpecfactor( from.getSpecfactor()); to.setLastactive( from.getLastactive()); to.setSpecificity( from.getSpecificity()); to.setConditions( from.getConditions()); to.setCnt( from.getCnt()); if ( from.getCnt() ==0) to.setStrength( minstrength); return to;}/*"Given a list of forecasts, find the worst ones and put them into apool of rejects. This method requires 2 inputs, the name of the rejectlist (actually, a Swarm Array) and the Array of forecasts. "*/public void MakePool$From (LinkedList rejects , LinkedList list){ int top; int i,j = 0 ; BFCast aForecast; BFCast aReject; top = -1; //pj: why not just start at 1 so we never worry about putting forecast 0 into the mix? for ( i=1; i < privateParams.npool; i++ ) { aForecast=(BFCast)(list.get(i)); for ( j=top; j >= 0 && ((aReject=(BFCast)(rejects.get(j)))!=null)&& (aForecast.getStrength() < aReject.getStrength()); j--) { rejects.add(j+1 ,aReject ); } //note j decrements at the end of this loop rejects.add(j+1 ,aForecast ); top++; } for ( ; i < privateParams.numfcasts; i++) { aForecast=(BFCast)(list.get(i)); if ( aForecast.getStrength() < ((BFCast)(rejects.get(top))).getStrength() ) { for ( j = top-1; j >= 0 && ((aReject=(BFCast)(rejects.get(j)))!=null) && (aForecast.getStrength() < aReject.getStrength()); j--) { rejects.add( j+1, aReject); } } rejects.add( j+1, aForecast); } //pj:note: we are not checking to see if forecast 0 is in here}/*------------------------------------------------------*//* Tournament *//*------------------------------------------------------*/public BFCast Tournament (LinkedList list){ int numfcasts=list.size(); BFCast candidate1 = (BFCast)(list.get(irand(numfcasts))); BFCast candidate2; do candidate2 = (BFCast)(list.get(irand(numfcasts))); while (candidate2 == candidate1); if (candidate1.getStrength() > candidate2.getStrength()) return candidate1; else return candidate2;}/*------------------------------------------------------*//* Mutate *//*------------------------------------------------------*/public boolean Mutate$Status (BFCast new2 , boolean changed) /* * For the condition bits, Mutate() looks at each bit with * probability pmutation. If chosen, a bit is changed as follows: * 0 -> * with probability 2/3, 1 with probability 1/3 * 1 -> * with probability 2/3, 0 with probability 1/3 * * -> 0 with probability 1/3, 1 with probability 1/3, * unchanged with probability 1/3 * This maintains specificity on average. *VERY CONFUSING, because the values are actually 0,1, and 2 CONVERTED LIKE SO 0 1 1/3 2 1/3 1 0 2/3 2 1/3 2 0 2/3 1 1/3 * * For the forecasting parameters, Mutate() may do one of two things, * independently for each parameter. * 1. "Long jump": the parameter is chosen randomly from its min-max * range. * 2. "Short jump": the parameter is chosen randomly from a uniform * distribution from oldvalue-nhood*range to oldvalue+nhood*range, * where range = max-min. Values outside the min-max range are * mapped to the endpoint. * Method 1 is used with probability plong, method 2 is used with * probability pshort, and the parameter is left unchanged with * probability 1-plong-pshort. * * Returns YES if it actually changed anything, otherwise NO. */{ int bit; double choice, temp; boolean bitchanged = false; int bitlist[] = new int[privateParams.condbits]; bitlist= privateParams.getBitListPtr(); //pj: dont know why BFagents introduced bitchanged.?? bitchanged = changed; if (privateParams.pmutation > 0) { for (bit = 0; bit < privateParams.condbits; bit++) { if (bitlist[bit] < 0) continue; if (drand() < privateParams.pmutation) { //cond = cond0 + WORD(bit); //if (*cond & MASK[bit]) if (new2.getConditionsbit( bit) > 0 ) { if (irand(3) > 0) { // *cond &= NMASK[bit]; //nr->specificity--; new2.maskConditionsbit( bit); new2.decrSpecificity(); } else // *cond ^= MASK[bit]; new2.switchConditionsbit( bit); bitchanged = changed = true; } else if (irand(3) > 0) { // *cond |= (irand(2)+1) << SHIFT[bit]; // nr->specificity++; new2.setConditionsbit$FromZeroTo( bit , (irand(2)+1)); new2.incrSpecificity(); bitchanged = changed = true; } } } } /* mutate p+d coefficient */ choice = drand(); if (choice < privateParams.plong) { /* long jump = uniform distribution between min and max */ new2.setAval( privateParams.a_min + privateParams.a_range*drand()) ; changed = true; } else if (choice < privateParams.plong + privateParams.pshort) { /* short jump = uniform within fraction nhood of range */ temp = new2.getAval() + privateParams.a_range*privateParams.nhood*urand(); new2.setAval( (temp > privateParams.a_max? privateParams.a_max: (temp < privateParams.a_min? privateParams.a_min: temp))); changed = true; } /* else leave alone */ /* mutate dividend coefficient */ choice = drand(); if (choice < privateParams.plong) { /* long jump = uniform distribution between min and max */ new2.setBval( privateParams.b_min + privateParams.b_range*drand() ); changed = true; } else if (choice < privateParams.plong + privateParams.pshort) { /* short jump = uniform within fraction nhood of range */ temp = new2.getBval() + privateParams.b_range*privateParams.nhood*urand(); new2.setBval( (temp > privateParams.b_max? privateParams.b_max: (temp < privateParams.b_min? privateParams.b_min: temp))); changed = true; } /* else leave alone */ /* mutate constant term */ choice = drand(); if (choice < privateParams.plong) { /* long jump = uniform distribution between min and max */ new2.setCval( privateParams.c_min + privateParams.c_range*drand()); changed = true; } else if (choice < privateParams.plong + privateParams.pshort) { /* short jump = uniform within fraction nhood of range */ temp = new2.getCval() + privateParams.c_range*privateParams.nhood*urand(); new2.setCval( (temp > privateParams.c_max? privateParams.c_max: (temp < privateParams.c_min? privateParams.c_min: temp))); changed = true; } /* else leave alone */ new2.setCnt(0); if (changed) { new2.updateSpecfactor(); } return(changed);}/*------------------------------------------------------*//* Crossover *//*------------------------------------------------------*/public BFCast Crossover$Parent1$Parent2( BFCast newForecast , BFCast parent1 , BFCast parent2) /* * On the condition bits, Crossover() uses uniform crossover -- each * bit is chosen randomly from one parent or the other. * For the real-valued forecasting parameters, Crossover() does * one of three things: * 1. Choose a linear combination of the parents' parameters, * weighted by strength. * 2. Choose each parameter randomly from each parent. * 3. Choose one of the parents' parameters (all from one or all * from the other). * Method 1 is chosen with probability plinear, method 2 with * probability prandom, method 3 with probability 1-plinear-prandom. */{ /* Uniform crossover of condition bits */ int bit; // unsigned int *cond1, *cond2, *newcond; int word; double weight1, weight2, choice; newForecast.setSpecificity(0); for (word = 0; word <privateParams.condwords; word++) newForecast.setConditionsWord$To( word , 0); for (bit = 0; bit < privateParams.condbits; bit++) { if ( irand(2) == 0) { int value=parent1.getConditionsbit(bit); newForecast.setConditionsbit$FromZeroTo( bit , value); if (value > 0) newForecast.incrSpecificity(); } else { int value= parent2.getConditionsbit( bit); newForecast.setConditionsbit$FromZeroTo( bit , value ); if (value > 0) newForecast.incrSpecificity(); } } /* Select one crossover method for the forecasting parameters */ choice = drand(); if (choice < privateParams.plinear) { /* Crossover method 1 -- linear combination */ weight1 = parent1.getStrength() / (parent1.getStrength() + parent2.getStrength()); weight2 = 1.0-weight1; newForecast.setAval(weight1*parent1.getAval() + weight2*parent2.getAval() ); newForecast.setBval(weight1*parent1.getBval() + weight2*parent2.getBval() ); newForecast.setCval(weight1*parent1.getCval() + weight2*parent2.getCval() ) ; } else if (choice < privateParams.plinear + privateParams.prandom) { /* Crossover method 2 -- randomly from each parent */ if(irand(2)!=0) newForecast.setAval( parent1.getAval()) ; else newForecast.setAval( parent2.getAval()) ; if(irand(2)!=0) newForecast.setBval( parent1.getBval()) ; else newForecast.setBval( parent2.getBval()); if(irand(2)!=0) newForecast.setCval( parent1.getCval()) ; else newForecast.setCval( parent2.getCval()); } else { /* Crossover method 3 -- all from one parent */ if (irand(2)!=0) { newForecast.setAval( parent1.getAval()) ; newForecast.setBval( parent1.getBval()) ; newForecast.setCval( parent1.getCval()) ; } else { newForecast.setAval( parent2.getAval()) ; newForecast.setBval( parent2.getBval()) ; newForecast.setCval( parent2.getCval()) ; } } { //This is just error checking! BitVector newcond; int specificity=0; newForecast.setCnt( 0 ); // call it new in any case newForecast.updateSpecfactor(); newForecast.setStrength( 0.5*(parent1.getStrength() + parent2.getStrength())); //pj: next steps are purely diagnostic! newcond = newForecast.getConditionsObject(); for (bit = 0; bit < privateParams.condbits; bit++) //if ((newcond[WORD(bit)]& ( 3 << ((bit%16)*2))) != 0) if ( newcond.getConditionsbit( bit) != 0 ) { specificity++; } //printf("CrossoverDiagnostic: newforecast Specificity %d should equal %d \n", [newForecast getSpecificity],specificity); } return newForecast;}/*------------------------------------------------------*//* TransferFcasts *//*------------------------------------------------------*/public void TransferFcastsFrom$To$Replace( LinkedList newList , LinkedList forecastList , LinkedList rejects){ BFCast aForecast; BFCast toDieForecast; //nnew = pp->nnew; for( int i=0; i<newList.size(); i++ ) { aForecast = (BFCast)newList.get(i); //toDieForecast = GetMort(aForecast, rejects); toDieForecast = this.GetMort$Rejects( aForecast , rejects); toDieForecast = this.CopyRule$From( toDieForecast , aForecast); }}/*------------------------------------------------------*//* GetMo
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -