分享

MapReduce与遗传算法、MapReduce与粒子群算法结合与实现

howtodown 发表于 2015-1-9 17:25:11 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 5 37026
本帖最后由 howtodown 于 2015-1-9 17:37 编辑

问题导读

1.粒子群算法的MapReduce如何通过代码实现?
2.MapReduce如何与遗传算法结合?








遗传算法(大白话解析遗传算法):遗传算法入门介绍

ava代码用遗传算法解决0-1背包:http://wenku.baidu.com/view/20beb6da6f1aff00bed51ea8.html

一、MapReduce和遗传算法结合:(参考文献:MapReduce-GA-eScience2008)

Map——评估每个个体
输入: (key, value)  (个体索引, 个体)
输出: (key,value)  (相同默认的键值, 个体)

Reduce——选择局部最优个体
输出: (key,value)  (个体, 1)

Reduce——全局最优
输出: (key, value)  (最优个体,1)


Coordinator——交叉、变异


二、MapReduce和粒子群算法结合:(参考文献:Parallel PSOUsing MapReduce)
Map——得到最优个体解
输入:(key, value)(粒子索引,粒子状态:包括相邻节点、位置坐标、速度、位置值、个人最优位置、个人最    优值、全局最优位置、全局最优值)
输出:(key, value) (最优粒子索引,最优粒子状态:同上)

Reduce——得到最优全局解
输出:(key, value) (全局最优粒子索引,全局最优粒子状态:同上)

三、产生初始粒子


  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.Random;
  6. public class psotest {
  7.    
  8.     private final int iRang= 30;
  9.     static int dim =2;
  10.     static int sizepop =20;
  11.     double sum1 = 0;
  12.     double sum2 = 0;
  13.     double g = 10000;
  14.     private Random random =new Random();
  15.     public double[][] pop =new double[sizepop][dim];
  16.     public double[][] dpbest= new double[sizepop][dim];
  17.     public double[][] V =new double[sizepop][dim];
  18.     public double[] fitness= new double[sizepop];
  19.     public double[] gbest =new double[dim];
  20.     public double[]fitnessgbest = new double[sizepop];
  21.     public double[]bestfitness = new double[sizepop];
  22.     int m_iTempPos =999;
  23.     public void c() {
  24.        for(int i = 0; i < sizepop;i++)
  25.        {
  26.            for(int j= 0; j < dim; j++) {
  27.               pop[i][j] = (random.nextDouble() - 0.5) * 2 *iRang;
  28.               V[i][j] = dpbest[i][j] =pop[i][j];
  29.            }
  30.            for(int j= 0; j < dim; j++) {
  31.               sum1 += Math.pow(pop[i][j],2);
  32.               sum2 +=Math.cos(2*Math.PI*pop[i][j]);
  33.            }
  34.            fitness[i]= -20 * Math.exp(-0.2 * Math.sqrt((1.0 / dim) * sum1))
  35.                  - Math.exp((1.0 / dim) * sum2) + 20 +2.72;
  36.           bestfitness[i] = 10000;
  37.           if(bestfitness[i] > fitness[i]) {
  38.               bestfitness[i] =fitness[i];
  39.               for(int j = 0; j< dim; j++) {
  40.                  dpbest[i][j] = pop[i][j];
  41.               }
  42.            }
  43.        }
  44.       
  45.        for(int i = 0; i < sizepop;i++)
  46.        {
  47.            if(bestfitness[i] < g) {
  48.                  g = bestfitness[i];
  49.                  m_iTempPos = i;
  50.            }
  51.        }
  52.        if (m_iTempPos != 999) {
  53.            for (int j= 0; j < dim; j++) {
  54.               gbest[j] =dpbest[m_iTempPos][j];
  55.            }
  56.        }
  57.     }
  58.     public static voidmain(String[] args) throws IOException {
  59.        psotest pso = new psotest();
  60.        pso.c();
  61.        File file = newFile("E:\\JavaProgram\\psotest\\input\\file.txt");
  62.        BufferedWriter out=new BufferedWriter(newFileWriter(file,true));
  63.        for(int i = 0; i < sizepop; i++){
  64.           out.write(i + " ");
  65.            for(int j= 0; j < dim; j++) {
  66.               out.write(pso.pop[i][j] + " "+ pso.V[i][j] + " " + pso.dpbest[i][j] + " " + pso.gbest[j] + "");
  67.            }
  68.           out.write(pso.bestfitness[i]+" "+pso.g);
  69.           out.newLine();
  70.        }
  71.        out.close();
  72.     }
  73. }
复制代码

四、粒子群算法的MapReduce版:

  1. package test;
  2. import java.io.IOException;
  3. import java.util.Random;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.FileSystem;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.DoubleWritable;
  8. import org.apache.hadoop.io.Text;
  9. import org.apache.hadoop.mapreduce.Job;
  10. import org.apache.hadoop.mapreduce.Mapper;
  11. import org.apache.hadoop.mapreduce.Reducer;
  12. importorg.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  13. importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  14. import org.apache.hadoop.util.GenericOptionsParser;
  15. public class posmr {
  16.     public static classIntSumReducer extends
  17.           Mapper<Object, Text, DoubleWritable,Text> {
  18.        private DoubleWritable job1map_key = newDoubleWritable();
  19.        private Text job1map_value = new Text();
  20.        static int dim = 2;
  21.        static int sizepop = 20;
  22.        private final double w = 1;
  23.        static double c1 = 1;
  24.        static double c2 = 1;
  25.        public double[] pop = new double[dim];
  26.        public double[] dpbest = new double[dim];
  27.        public double[] V = new double[dim];
  28.        public double[] fitness = newdouble[sizepop];
  29.        public double[] gbest = new double[dim];
  30.        public String[] S_value = new String[dim];
  31.        public String F_value;
  32.        public double bestfitness;
  33.        public double m_dFitness;
  34.        private Random random = new Random();
  35.        double g;
  36.        double sum1;
  37.        double sum2;
  38.        public void map(Object key, Text values, Contextcontext)
  39.               throws IOException,InterruptedException {
  40.            Stringitr[] = values.toString().split("\\s");
  41.            int k =1;
  42.            F_value ="";
  43.            for (int j= 0; j < dim; j++) {
  44.               pop[j] =Double.valueOf((itr[k++]));
  45.               V[j] =Double.valueOf((itr[k++]));
  46.               dpbest[j] =Double.valueOf((itr[k++]));
  47.               gbest[j] =Double.valueOf((itr[k++]));
  48.            }
  49.           bestfitness = Double.valueOf((itr[k++]));
  50.            g =Double.valueOf((itr[k++]));
  51.            for (int i= 0; i < dim; i++) {
  52.               V[i] = w * V[i] + c1 *random.nextDouble()
  53.                      *(dpbest[i] - pop[i]) + c2 * random.nextDouble()
  54.                      *(gbest[i] - pop[i]);
  55.               pop[i] = pop[i] + V[i];
  56.            }
  57.            sum1 =0;
  58.            sum2 =0;
  59.            //计算Ackley 函数的值
  60.            for (int i= 0; i < dim; i++) {
  61.               sum1 += pop[i] *pop[i];
  62.               sum2 += Math.cos(2 * Math.PI* pop[i]);
  63.            }
  64.            //m_dFitness 计算出的当前值
  65.            m_dFitness= -20 * Math.exp(-0.2 * Math.sqrt((1.0 / dim) * sum1))
  66.                  - Math.exp((1.0 / dim) * sum2) + 20 +2.72;
  67.           if(m_dFitness < 0) {
  68.               System.out.println(sum1 + " "+ m_dFitness + " " + sum2);
  69.            }
  70.            if(m_dFitness < bestfitness) {
  71.               bestfitness =m_dFitness;
  72.               for (int i = 0; i< dim; i++) {
  73.                  dpbest[i] = pop[i];
  74.               }
  75.            }
  76.            for (int j= 0; j < dim; j++) {
  77.               S_value[j] =Double.toString(pop[j]) + " "
  78.                      +Double.toString(V[j]) + " "
  79.                      +Double.toString(dpbest[j]) + " ";
  80.            }
  81.            for (int j= 0; j < dim; j++) {
  82.               F_value += S_value[j];
  83.            }
  84.           job1map_key.set(bestfitness);
  85.           job1map_value.set(F_value);
  86.           context.write(job1map_key, job1map_value);
  87.        }
  88.     }
  89.     public static classjob1Reducer extends
  90.           Reducer<DoubleWritable, Text, DoubleWritable,Text> {
  91.        private DoubleWritable job1reduce_key = newDoubleWritable();
  92.        private Text job1reduce_value = newText();
  93.        static int dim = 2;
  94.        static int sizepop = 20;
  95.        public double[] pop = new double[dim];
  96.        public double[] dpbest = new double[dim];
  97.        public double[] V = new double[dim];
  98.        public double[] gbest = new double[dim];
  99.        public double[] gbest_temp = newdouble[dim];
  100.        public String[] S_value = new String[dim];
  101.        public String F_value;
  102.        public double m_dFitness =Double.MAX_VALUE;
  103.        public void reduce(DoubleWritable key,Iterable<Text> values,
  104.               Context context) throwsIOException, InterruptedException {
  105.            Doublebestfitness = Double.valueOf(key.toString());
  106.            intk;
  107.            if(bestfitness < m_dFitness) {
  108.               m_dFitness =bestfitness;
  109.               for (Text val : values){
  110.                  String itr[] = val.toString().split(" ");
  111.                  k = 0;
  112.                  for (int j = 0; j < dim; j++){
  113.                      pop[j] =Double.valueOf((itr[k++]));
  114.                      V[j] =Double.valueOf((itr[k++]));
  115.                      dpbest[j]= Double.valueOf((itr[k++]));
  116.                  }
  117.                  for (int j = 0; j < dim; j++){
  118.                      gbest[j] =dpbest[j];
  119.                     gbest_temp[j] = dpbest[j];
  120.                  }
  121.                  F_value = "";
  122.                  for (int j = 0; j < dim; j++){
  123.                      S_value[j]= Double.toString(pop[j]) + " "
  124.                            + Double.toString(V[j]) + " "
  125.                            + Double.toString(dpbest[j]) + " "
  126.                            + Double.toString(gbest[j]) + " ";
  127.                  }
  128.                  for (int j = 0; j < dim; j++){
  129.                      F_value +=S_value[j];
  130.                  }
  131.                  F_value += (Double.toString(bestfitness)) + ""
  132.                         +(Double.toString(m_dFitness));
  133.                  job1reduce_key.set(1);
  134.                  job1reduce_value.set(F_value);
  135.                  context.write(job1reduce_key,job1reduce_value);
  136.               }
  137.            } else{
  138.               for (Text val : values){
  139.                  String itr[] = val.toString().split(" ");
  140.                  k = 0;
  141.                  for (int j = 0; j < dim; j++){
  142.                      pop[j] =Double.valueOf((itr[k++]));
  143.                      V[j] =Double.valueOf((itr[k++]));
  144.                      dpbest[j]= Double.valueOf((itr[k++]));
  145.                  }
  146.                  for (int j = 0; j < dim; j++){
  147.                      gbest[j] =gbest_temp[j];
  148.                  }
  149.                  F_value = "";
  150.                  for (int j = 0; j < dim; j++){
  151.                      S_value[j]= Double.toString(pop[j]) + " "
  152.                            + Double.toString(V[j]) + " "
  153.                            + Double.toString(dpbest[j]) + " "
  154.                            + Double.toString(gbest[j]) + " ";
  155.                  }
  156.                  for (int j = 0; j < dim; j++){
  157.                      F_value +=S_value[j];
  158.                  }
  159.                  F_value += (Double.toString(bestfitness)) + ""
  160.                         +(Double.toString(m_dFitness));
  161.                  job1reduce_key.set(1);
  162.                  job1reduce_value.set(F_value);
  163.                  context.write(job1reduce_key,job1reduce_value);
  164.               }
  165.            }
  166.        }
  167.     }
  168.     public static voidmain(String[] args) throws Exception {
  169.           Configuration conf = new Configuration();
  170.            String[]otherArgs = new GenericOptionsParser(conf, args)
  171.                  .getRemainingArgs();
  172.            if(otherArgs.length != 2) {
  173.               System.err.println("Usage:wordcount <in><out>");
  174.               System.exit(2);
  175.            }
  176.            Stringinput = otherArgs[0];
  177.            Stringoutput = otherArgs[1];
  178.            FileSystemfs;
  179.            try{
  180.               fs =FileSystem.get(conf);
  181.               int step = 20;
  182.               for(int i = 0; i< step; i++) {
  183.                  System.out.println("第" + i + "次:" + i);
  184.                  Job job = new Job(conf, "word count");
  185.                  job.setJarByClass(posmr.class);
  186.                  
  187.                  job.setMapperClass(IntSumReducer.class);
  188.                  job.setReducerClass(job1Reducer.class);
  189.                  
  190.                 job.setMapOutputKeyClass(DoubleWritable.class);
  191.                  job.setMapOutputValueClass(Text.class);
  192.                  
  193.                  job.setOutputKeyClass(Text.class);
  194.                  job.setOutputValueClass(Text.class);
  195.                  
  196.                  FileInputFormat.addInputPath(job, newPath(input));
  197.                  FileOutputFormat.setOutputPath(job, newPath(output));
  198.                  
  199.                  job.waitForCompletion(true);
  200.                  input = output;  
  201.                  output += i;
  202.               }
  203.            } catch(IOException e) {
  204.               e.printStackTrace();
  205.            } catch(InterruptedException e) {
  206.               e.printStackTrace();
  207.            } catch(ClassNotFoundException e) {
  208.               e.printStackTrace();
  209.            }
  210.        }
  211. }
复制代码


























已有(5)人评论

跳转到指定楼层
maizhu 发表于 2015-1-9 18:02:12
好,学习了
回复

使用道具 举报

stark_summer 发表于 2015-1-9 18:22:39
回复

使用道具 举报

zqh_2014 发表于 2015-1-13 14:03:25
回复

使用道具 举报

xegz 发表于 2016-1-29 21:14:53
求分享下粒子群mapreduce的测试数据集
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条