ikov34

v1ER

Feb 23rd, 2021
720
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.um.feri.ears.algorithms.Algorithm;
  2. import org.um.feri.ears.algorithms.so.abc.ABC;
  3. import org.um.feri.ears.algorithms.so.gwo.GWO;
  4. import org.um.feri.ears.algorithms.so.jade.JADE;
  5. import org.um.feri.ears.algorithms.so.pso.PSO;
  6. import org.um.feri.ears.algorithms.so.soma.SOMA;
  7. import org.um.feri.ears.algorithms.AlgorithmInfo;
  8. import org.um.feri.ears.algorithms.so.random.RandomWalkAlgorithm;
  9. import org.um.feri.ears.algorithms.so.tlbo.TLBOAlgorithm;
  10. import org.um.feri.ears.benchmark.RatingBenchmark;
  11. import org.um.feri.ears.benchmark.RatingCEC2015;
  12. import org.um.feri.ears.benchmark.RatingRPUOed30;
  13. import org.um.feri.ears.problems.*;
  14. import org.um.feri.ears.problems.results.BankOfResults;
  15. import org.um.feri.ears.rating.Player;
  16. import org.um.feri.ears.rating.ResultArena;
  17. import org.um.feri.ears.util.Util;
  18.  
  19. import java.io.File;
  20. import java.sql.Timestamp;
  21. import java.io.FileWriter;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.Date;
  26.  
  27. public class Vaja1 {
  28.  
  29.  
  30.     public static FileWriter myWriter;
  31.  
  32.     static class SomaParams extends Problem
  33.     {
  34.         private ArrayList<Algorithm> algorithms;
  35.  
  36.         public SomaParams()
  37.         {
  38.             super(5,0);
  39.             // strategy,popsize, step,pathLength,prt
  40.             lowerLimit = new ArrayList<Double>(Arrays.asList(0.0,50d, 0.3d, 3.1d, 0.3d));
  41.             upperLimit = new ArrayList<Double>(Arrays.asList(3.0,100d, 1d, 10.0, 0.3d));
  42.             RatingBenchmark.debugPrint = true; //prints one on one results
  43.             this.minimize = false;
  44.         }
  45.  
  46.  
  47.         @Override
  48.         public double eval(double[] x) {
  49.             Util.rnd.setSeed(5036538); //set the seed of the random generator
  50.             algorithms = new ArrayList<>();
  51.             algorithms.add(new RandomWalkAlgorithm());
  52.             algorithms.add(new ABC());
  53.             algorithms.add(new GWO());
  54.             algorithms.add(new TLBOAlgorithm());
  55.             algorithms.add(new PSO());
  56.             // strategy,step,pathLength,prt
  57.             algorithms.add(new SOMA(SOMA.Strategy.values()[(int)x[0] % 4], (int)x[1],x[2],x[3],x[4]));
  58.             ResultArena ra = new ResultArena(100); // the result arena holds information about the players (wins, loses, draws, ratings...)
  59.             RatingCEC2015 cec2015 = new RatingCEC2015(); // benchmark with prepared tasks and settings
  60.             cec2015.setDisplayRatingIntervalChart(false);
  61.             cec2015.registerAlgorithms(algorithms);  // register the algorithms in the benchmark
  62.  
  63.             for (Algorithm al:algorithms) {
  64.                 ra.addPlayer(al, al.getID()); // add players with initial ratings
  65.             }
  66.             BankOfResults ba = new BankOfResults();
  67.  
  68.             cec2015.run(ra, ba, 1); //start the tournament with 1 runs/repetitions
  69.  
  70.             ArrayList<Player> list = ra.getPlayers();
  71.             for (Player p: list)
  72.             {
  73.                 if(p.getPlayerId() == algorithms.get(2).getID()) // id od SOME
  74.                 {
  75.                     double fitness = p.getRatingData().getRating();
  76.                     try
  77.                     {
  78.                         myWriter.write(String.format("%s;%f;%f;%f;%f;%f;%f\r\n", new Timestamp(System.currentTimeMillis()).toString(), fitness, (int)x[0], (int)x[1],x[2],x[3],x[4]).replace('.',','));
  79.                         myWriter.flush();
  80.                     }
  81.                     catch(IOException ex)
  82.                     {
  83.                         System.out.println("Napaka: " + ex.getMessage());
  84.                     }
  85.  
  86.                     return fitness;
  87.                 }
  88.             }
  89.  
  90.             return 0;
  91.         }
  92.  
  93.     }
  94.  
  95.     public static String variablesStr(double[] vars)
  96.     {
  97.         String result = "";
  98.  
  99.         for(int i = 0; i < vars.length; ++i)
  100.         {
  101.             result += (vars[i] + " ");
  102.         }
  103.  
  104.         return result;
  105.     }
  106.  
  107.  
  108.     public static void main(String[] args) throws IOException {
  109.  
  110.         String filename = String.format("run_%d.csv", new Date().getTime());
  111.  
  112.         File myObj = new File(filename);
  113.         if (myObj.createNewFile()) {
  114.             System.out.println("File created: " + myObj.getName());
  115.         }
  116.  
  117.         myWriter = new FileWriter(filename);
  118.         myWriter.write("cas;fitness;strategy;popSize;step;pathLength;prt;\r\n");
  119.  
  120.         Problem p = new Vaja1.SomaParams();
  121.         Task metaevolucija = new Task(EnumStopCriteria.STAGNATION,0, 0, 0, 0.001, p);
  122.         Algorithm jde = new JADE();
  123.         DoubleSolution best;
  124.         try {
  125.             System.out.println("Zaganjam metaevolucijo SOME z JADE...");
  126.             best = jde.execute(metaevolucija);
  127.             myWriter.close();
  128.         } catch (StopCriteriaException e) {
  129.             e.printStackTrace();
  130.         }
  131.     }
  132. }
  133.  
RAW Paste Data