⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nrpop.java

📁 robocode机器人
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
 */
 public void setSize(int s){Pnet.setSize(s);}
 /**
 * Removes the net at <TT>"index"</TT> position, and vector size is reduced (all next nets
 * are shifted back).
 * In a population NrPop the nets are numbered starting from 1
 */
 public void removeNNet(int index){Pnet.removeElementAt(index-1);}

 /**
 * Initializes fitness array (it must be called before using other fitness methods).
 * <BR> Or if PopSize is changed.
 */
 public void fitInit()
 {
  int i;
  fitness= new Fitness(Pnet.size());
  for (i=1;i<=Pnet.size();i++){fitness.setFit(i,0);}
 }

 /**
 * Set fitness value for NNet number <TT>"netnumber"</TT> .
 */
 public void fitSet(int netnumber,float val){fitness.setFit(netnumber,val);}

 /**
 * Return the NNet number corresponding to <TT>"position"</TT> number in graded list.
 * <BR>Must be utilized after RankingMax or Min call.
 * <BR>First position is 1. Last position is PopSize.
 */
 public int fitGetNumAtPos(int position){return fitness.getNum(position);}

 /**
 * Return the NNet pointer corresponding to <TT>"position"</TT> number in graded list.
 * <BR>Must be utilized after RankingMax or Min call.
 * <BR>First position is 1. Last position is PopSize.
 */
 public NNet fitGetNetAtPos(int position){return getNNet(fitness.getNum(position));}

 /**
 * Return the fitness value corresponding to <TT>"position"</TT> number in graded list.
 * <BR>Must be utilized after RankingMax or Min call.
 * <BR>First position is 1. Last position is PopSize.
 */
 public float fitGetValAtPos(int position){return fitness.getVal(position);}

 /**
 * Order the fitness array in a graded list where the first position corresponds
 * to NNet with minimum fitness value.
 * <BR>Must be utilized after RankingMax or Min call.
 */
 public void fitRankingMin(){fitness.rankMin();}

 /**
 * Order the fitness array in a graded list where the first position corresponds
 * to NNet with maximum fitness value.
 * <BR>Must be utilized after RankingMax or Min call.
 */
 public void fitRankingMax(){fitness.rankMax();}

 /**
 * Return the array corresponding to the graded list of NNet number.
 * <BR>Must be utilized after RankingMax or Min call.
 * <BR>In this case first position is 0 and last position is PopSize-1.
 */
 public int[] fitGetGradingList(){return fitness.getArrayNum();}

 /**
 * Return the fitness stored value of NNet number <TT>"netnumber"</TT>.
 * <BR>It scan the fitness array (expensive)
 */
 public float fitGetFitness(int netnumber)
 {
  int i;
  for (i=0;i<Pnet.size();i++) {if (fitness.net[i]==netnumber) return fitness.fit[i];}
  return 0;
 }

 /**
 * Saves the whole population networks on the file named <TT>"namefile"</TT>.
 * This population can be read with nrPopLoad method
 */
 public void nrPopSave(String nomefile)
 {
   int i;
   FileOutputStream fileout;
   PrintStream pf;
   try
   {
    fileout= new FileOutputStream(nomefile);
    pf=new PrintStream(fileout);
    pf.println("NPOP-DATA "+this.PopSize());
    for (i=1;i<=this.PopSize();i++) this.getNNet(i).saveNNet(pf,i);
    pf.close();
   }
   catch (FileNotFoundException e){NNError.err("Impossible to create file "+nomefile);}
 }


 /**
 * Saves the networks enumerated on <TT>"numnet[]"</TT> array for <TT>"len"</TT> elements.
 * This population can be read with nrPopLoad constructor method
 */
 public void nrPopSave(String nomefile,int numnet[],int len)
 {
   int i,l;
   FileOutputStream fileout;
   PrintStream pf;
   if (numnet.length<len) l=numnet.length;else l=len;
   try
   {
    fileout= new FileOutputStream(nomefile);
    pf=new PrintStream(fileout);
    pf.println("NPOP-DATA "+this.PopSize());
    for (i=0;i<l;i++)
    { if (numnet[i]<= this.PopSize()) this.getNNet(numnet[i]).saveNNet(pf,numnet[i]); }
    pf.close();
   }
   catch (FileNotFoundException e){NNError.err("Impossible to create file "+nomefile);}
 }

 /**
 * This constructor loads a population network saved with nrPopSave method.
 * To distinguish this constructor from a new population constructor (that use
 * a description file), this constructor needs a flag <TT>"load"</TT>= true
 */
 public NrPop(String nomefile,boolean load)
 {
   int i;
   int npop=0,net;
   NNet nnet;
   FileInputStream fileinp;
   InputStreamReader fileread;
   StreamTokenizer tok;
   try
   {
    fileinp= new FileInputStream(nomefile);
    fileread= new InputStreamReader(fileinp);
    tok=new StreamTokenizer(fileread);
    tok.whitespaceChars(0x2C,0x2C); /* "," */
    tok.whitespaceChars(0x3A,0x3A); /* ":" */
    try{tok.nextToken();}catch (IOException e){return;}
    if (tok.sval.equals("NPOP-DATA"))
    try {tok.nextToken(); npop=(int)tok.nval;}catch (IOException e){NNError.err("Error on net file");return;}
    this.Pnet=new Vector(npop);
    for (i=1;i<=npop;i++)
     {
      try{tok.nextToken();}catch (IOException e){break;}
      if (tok.sval.equals("NET"))
      try{tok.nextToken(); net=(int)tok.nval;}catch (IOException e){NNError.err("Error on net file");break;}
      nnet= new NNet(); nnet.loadNNet(tok);
      this.addNNet(nnet);
     }
//    fileread.close();
   }
   catch (FileNotFoundException e){NNError.err("File not found "+nomefile);}
 }

 static String values(String val,int n)
 {
  int i,p=1,cp=0;
  if (val.length()==0) return "";
  for (i=1;i<val.length();i++)
  {if ( val.charAt(i)==','|val.charAt(i)=='('|val.charAt(i)==')' )
   {cp++; if (cp==n) return val.substring(p,i);p=i+1;}
  }
  {cp++; if (cp==n) return val.substring(p,i);p=i+1;}
  return "";
 }

 /**
 * This utility reads a file and put each record in a string array and return
 * the pointer. It is useful to optimize the learning or test procedure.
 */
 public static String[] fileToStrArray(String filename)
 {
   Vector rec=new Vector(100);
   String strarray[]=null;
   Object arr[];
   String line;
   FileReader fread;
   LineNumberReader fline;
   try
   {
    fread= new FileReader(filename);
    fline= new LineNumberReader(fread);
    while(true)
    { try {line=fline.readLine();
           if (line==null) break;
           rec.addElement(line) ;}
     catch (IOException e) {break;} }
    strarray=new String[rec.size()];
    arr=rec.toArray(strarray);
    try{fline.close();fread.close();} catch (IOException e){};
    return strarray;
   }catch (FileNotFoundException e) {NNError.err("File not found "+filename);}
   return null;
 }


}

/************************************************************************/

/**
* Class Rand initialize a random generator.
* If called without parameter it use time as seed.
*/

class Rand extends Random
{
 public Rand(int s){super ((long) s);}
 public Rand(){super();}
 public int iab(int a,int b) {return a+nextInt(b-a+1);}
 public float fab(float a, float b) {return a+nextFloat()*(b-a);}
 public float gauss(float dev) {return (float) (nextGaussian()*dev);}
}

/************************************************************************/

class Fitness
{
 int dim;
 float fit[];
 int   net[];
 Fitness(int n){fit=new float[n];net=new int[n];dim=n;}
 void setFit(int nnet,float nfit){net[nnet-1]=nnet;fit[nnet-1]=nfit;}
 void rankMax()
 {int c,i,appo2;
    float appo1;
    for (c=0; c<dim; c++)
    {for (i=dim-1;i>c;i--)
      {if (fit[i] > fit[i-1])
       { appo1=fit[i]; fit[i]=fit[i-1]; fit[i-1]=appo1;
         appo2=net[i]; net[i]=net[i-1]; net[i-1]=appo2;}
      }
    }
 }
 void rankMin()
 {int c,i,appo2;
    float appo1;
    for (c=0; c<dim; c++)
    {for (i=dim-1;i>c;i--)
      {if (fit[i] < fit[i-1])
       { appo1=fit[i]; fit[i]=fit[i-1]; fit[i-1]=appo1;
         appo2=net[i]; net[i]=net[i-1]; net[i-1]=appo2;}
      }
    }
 }
 int getNum(int i){return net[i-1];}
 float getVal(int i){return fit[i-1];}
 int[] getArrayNum(){return net;}
 float[] getArrayVal(){return fit;}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -