Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. package com.mrozwadowski.tsp;
  2.  
  3. import com.mrozwadowski.tsp.aco.ACParams;
  4.  
  5. import java.io.*;
  6. import java.util.Scanner;
  7.  
  8. /**
  9.  *
  10.  * @author Jakub
  11.  */
  12. public class Tester {
  13.     Graph graph;
  14.     PrintStream out;
  15.     Scanner scanner;
  16.     double values[] = new double[6];
  17.     int iters = 50;
  18.    
  19.     static int loop = 5;
  20.    
  21.     static double alpha[] = {1.0, 2.0};
  22.     static double beta[] = {2.0, 6.0};
  23.     static double tau0[] = {0.1, 1.0};
  24.     static double rho[] = {0.5, 0.8};
  25.     static double Q[] = {100, 200};
  26.     static double ants[] = {0.2, 0.5};
  27.    
  28.     double a1, b1, t1, r1, q1, an1;
  29.    
  30.     double[] makeValues(double a, double b, double t, double r, double q, double an){
  31.         double vals[] = new double[6];
  32.         vals[0] = a;
  33.         vals[1] = b;
  34.         vals[2] = t;
  35.         vals[3] = r;
  36.         vals[4] = q;
  37.         vals[5] = an;
  38.         return vals;
  39.     }
  40.    
  41.     Tester(InputStream in, PrintStream out) {
  42.         scanner = new Scanner(in);
  43.         this.out = out;
  44.     }
  45.    
  46.     void readNumCities() throws Exception {
  47.         int numCities = scanner.nextInt();
  48.         if (numCities < 2) {
  49.             throw new Exception("Należy podać co najmniej dwa punkty!");
  50.         }
  51.         graph = new Graph();
  52.     }
  53.  
  54.     void readCities() {
  55.         while (scanner.hasNextInt()) {
  56.             graph.addCity(new City(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()));
  57.         }
  58.     }
  59.    
  60.     void findAndPrintResults() {
  61.         ACParams colony = new ACParams(graph, values, iters);
  62.         Solution solution = colony.findPath();
  63.  
  64.         System.out.println("Result: "+solution.getLength());
  65.  
  66.         out.println(colony.getNumCities());
  67.         for (City city: solution.getCities()) {
  68.             out.println(city.getNum()+" "+city.getX()+" "+city.getY());
  69.         }
  70.     }
  71.    
  72.     public static void main(String[] args) throws Exception {
  73.         /*
  74.         If you run the program with an argument, the instance will be loaded
  75.         from a file in `instances` directory and a solution will be written
  76.         to `solutions`. Otherwise, standard I/O will be used.
  77.         */
  78.         InputStream in = System.in;
  79.         PrintStream out = System.out;
  80.  
  81.         if (args.length == 1) {
  82.             String file = args[0];
  83.             in = new FileInputStream("instances/"+file);
  84.             out = new PrintStream("solutions/"+file);
  85.         }
  86.  
  87.         Tester app = new Tester(in, out);
  88.        
  89.         app.readNumCities();
  90.         app.readCities();
  91.        
  92.         for (double a = alpha[0]; a <= alpha[1]; a += (alpha[1]-alpha[0])/loop){
  93.             for (double b = beta[0]; b <= beta[1]; b += (beta[1]-beta[0]/loop)){
  94.                 for (double t = tau0[0]; t <= tau0[1]; t += (tau0[1]-tau0[0]/loop)){
  95.                 for (double r = rho[0]; r <= rho[1]; r += (rho[1]-rho[0]/loop)){
  96.                 for (double q = Q[0]; q <= Q[1]; q += (Q[1]-Q[0]/loop)){
  97.                 for (double an = ants[0]; an <= ants[1]; an += (ants[1]-ants[0]/loop)){
  98.                     //values[] = {a, b, t, r, q, an};
  99.                     //values[] = makeValues(a,b,t,r,q,an);
  100.                     app.findAndPrintResults();
  101.             }
  102.             }
  103.             }
  104.             }
  105.             }
  106.         }
  107.        
  108.         //app.readNumCities();
  109.         //app.readCities();
  110.         //app.findAndPrintResults();
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement