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

📄 bpso.java

📁 一个BPSO实现,基本上实现了BPSO的代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		{
			return 0;
		}
		else 
		{
			return 1;
		}
	}
		
	// return globalBest value
	public double getMaxValue()
	{
		return this.globalBest;
	}
	
	// parameter as particle Id
	public double evaluate(int PId)
	{
		
		System.out.println("evaluate particle: (index)" + PId);
		
		try 
		{
			// modify training data using particle information
			modifyData(PId);
		}
		catch (IOException ioe) 
		{
			ioe.printStackTrace();
		}
		
		return classify();
	}
	
	// modify the internal training data with the particle information
	public void modifyData(int PId) throws IOException 
	{
		// copying the data definition
		BufferedWriter fw = new BufferedWriter(new FileWriter("reduced.arff"));
		fw.write(Instances.ARFF_RELATION + " reducedSet");
		fw.newLine();
		fw.newLine();
		
		for (int i = 0; i < internalTrain.numAttributes() - 1; i++) 
		{
			fw.write(internalTrain.attribute(i).toString());
			fw.newLine();
		}
		
		fw.write(internalTrain.classAttribute().toString());
		fw.newLine();
		fw.newLine();
		fw.write(Instances.ARFF_DATA);
		fw.newLine();
		
		
		// copying case samples (sample with class label of "1") into the file
		// also, loading the control samples (sample with class label of "0") into " control[] "
		control = new String[CONTROLSIZE];
		
		int controlIndex = 0;
		
		for (int i = 0; i < internalTrain.numInstances(); i++) 
		{
			if (internalTrain.instance(i).classValue() == 1) 
			{
				fw.write(internalTrain.instance(i).toString());
				fw.newLine();
			}
			else if (internalTrain.instance(i).classValue() == 0) 
			{
				control[controlIndex] = internalTrain.instance(i).toString();
				controlIndex++;
			}
		}
		
		// adding control samples into the file based on the particle information
		for (int j = 0; j < CONTROLSIZE; j++)
		{
			if(particles[PId][j] == 1) 
			{
				fw.write(control[j]);
				fw.newLine();
			}
		}
		
		fw.close();
		
	}
	
	// the target function in fitness form
	public double classify() 
	{
		double fitnessValue = 0.0;
		double classifiersScore = 0.0;
		
		/* load in the modified data set */
		try 
		{
			Instances reducedSet = new Instances(new BufferedReader(new FileReader("reduced.arff")));
			reducedSet.setClassIndex(reducedSet.numAttributes() - 1);
			
			// calculating the evaluation values using each classifier respectively
			J48 tree = new J48();
			classifiersScore = classifier(tree, reducedSet, internalTest);		
			if (detail == true)
			{
				System.out.println("J48 overall: " + classifiersScore);
			}
			fitnessValue += classifiersScore;
				
			IBk nn3 = new IBk(3);
			classifiersScore = classifier(nn3, reducedSet, internalTest);			
			if (detail == true)
			{
				System.out.println("3NN overall: " + classifiersScore);
			}
			fitnessValue += classifiersScore;

			NaiveBayes nb = new NaiveBayes();
			classifiersScore = classifier(nb, reducedSet, internalTest);		
			if (detail == true)
			{
				System.out.println("NB overall: " + classifiersScore);
			}
			fitnessValue += classifiersScore;
			
			RandomForest rf5 = new RandomForest();
			rf5.setNumTrees(5);
			classifiersScore = classifier(rf5, reducedSet, internalTest);		
			if (detail == true)
			{
				System.out.println("RF(5) overall: " + classifiersScore);
			}
			fitnessValue += classifiersScore;

			Logistic log = new Logistic();
			classifiersScore = classifier(log, reducedSet, internalTest);		
			if (detail == true)
			{
				System.out.println("LOG overall: " + classifiersScore);
			}
			fitnessValue += classifiersScore;
			
			/*
			RBFNetwork rbf = new RBFNetwork();
			classifiersScore = classifier(rbf, reducedSet, internalTest);		
			if (detail == true)
			{
				System.out.println("RBF overall: " + classifiersScore);
			}
			fitnessValue += classifiersScore;
			*/
		}
		catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
		
		fitnessValue /= 5;
		
		System.out.println("overall fitness: " + fitnessValue);
		System.out.println("---------------------------------------------------");
		
		return fitnessValue;
	}
	
	public double classifier(Classifier c, Instances train, Instances test)
	{
		double AUC = 0;
		double fM = 0;
		double gMean = 0;
		
		try 
		{
			c.buildClassifier(train);
		
			// evaluate classifier
			Evaluation eval = new Evaluation(train);
			eval.evaluateModel(c, test);	

		
			AUC = eval.areaUnderROC(1);
			fM = eval.fMeasure(1);
			gMean = eval.truePositiveRate(0);
			gMean *= eval.truePositiveRate(1);
			gMean = Math.sqrt(gMean);

		}
		catch(IOException ioe) 
		{
			ioe.printStackTrace();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		if (detail == true)
		{
			System.out.print("AUC: " + dec.format(AUC) + " | ");
			System.out.print("F-Measure: " + dec.format(fM) + " | ");
			System.out.println("g-mean : " + dec.format(gMean));
		}

		return (AUC + fM + gMean) / 3;
	}
	
	public void printMatrix(int[][] m) 
	{
		sum  = 0.0;
		for (int i = 0; i < m.length; i++)
		{
			for (int j = 0; j < m[i].length; j++)
			{
				System.out.print(m[i][j]+" ");
			}
			
			System.out.println(localBest[i]);
			sum += localBest[i]; 
		}
		System.out.println("average is " + sum / m.length);
	}
	
	public void saveResults(int fold) 
	{
		
		try 
		{
			if (fold == 0) {
				BufferedWriter bw = new BufferedWriter(new FileWriter("sampling.out"));
			
				for (int i = 0; i < POPULATIONSIZE; i++) 
				{
					for (int j = 0; j < CONTROLSIZE; j++)
					{
						if (localBestOfParticles[i][j] == 1)
						{	
							bw.write(control[j]);
							bw.newLine();
						}
					}
				}
			
				bw.close();
			}
			else {
				BufferedWriter bw = new BufferedWriter(new FileWriter("sampling.out", true));
				
				for (int i = 0; i < POPULATIONSIZE; i++) 
				{
					for (int j = 0; j < CONTROLSIZE; j++)
					{
						if (localBestOfParticles[i][j] == 1)
						{
							bw.append(control[j]);
							bw.newLine();
						}
					}
				}
			
				bw.close();
			}
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	// main program for test
	public static void main(String[] args)
	{
		String fileName = null;
		boolean detail = true;
		try
		{

			
			
			for (int i = 0; i < args.length; i++)
			{
				if (args[i].equals("-f")) 
				{
					fileName = args[i+1];
				}
				if (args[i].equals("-p"))
				{
					detail = false;
				}
			}
			BPSO bpso = new BPSO(fileName, detail);
			
			// output to txt file
			/*
			for (int count = 1; count <= 10 ; count++)
			{
				File   test   =   new   File("test" + count + ".txt"); 
				PrintStream   out   =   new   PrintStream(new FileOutputStream(test));   
				System.setOut(out);  
				
			}
			*/
		}
		catch(Exception   e)
		{
			
		}


		
		// this is the max value of the population
		
		/*
		for(int i = 0; i<100;i++)
		{
			test.findMaxValue();
			sum += test.getMaxValue();
			System.out.println(test.getMaxValue());
			test = new BPSO();
		}	
		System.out.println(sum/100);
		*/
	}
}

⌨️ 快捷键说明

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