Advertisement
firedigger

Untitled

Oct 11th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.awt.*;
  4. import java.awt.geom.Point2D;
  5. import java.sql.Timestamp;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8.  
  9. /**
  10.  * Created by DELL on 10/9/2015.
  11.  */
  12. public class TaskMap
  13. {
  14.     final private int div_param;
  15.     final private int square_search_factor;
  16.     final private double op_val_thres;
  17.  
  18.     private ArrayList<Task>[][] data;
  19.     private int[][] left_by_squares;
  20.     private int unsatisfied_customers;
  21.  
  22.     final private double delta_x;
  23.     final private double delta_y;
  24.  
  25.     final private Point2D left_down;
  26.     final private Point2D right_up;
  27.  
  28.  
  29.     public TaskMap(ArrayList<Task> tasks, int div_param, int square_search_factor, double op_val_thres)
  30.     {
  31.         this.div_param = div_param;
  32.         this.square_search_factor = square_search_factor;
  33.         this.op_val_thres = op_val_thres;
  34.  
  35.         this.unsatisfied_customers = tasks.size();
  36.         data = new ArrayList[div_param][div_param];
  37.         for(int i = 0; i < div_param; ++i)
  38.             for(int j = 0; j < div_param; ++j)
  39.                 data[i][j] = new ArrayList<Task>();
  40.  
  41.         left_by_squares = new int[div_param][div_param];
  42.  
  43.         double x_min = Collections.max(tasks, Task::compare_X).pos.getX();
  44.         double x_max = Collections.min(tasks, Task::compare_X).pos.getX();
  45.         double y_max = Collections.max(tasks, Task::compare_Y).pos.getY();
  46.         double y_min = Collections.min(tasks, Task::compare_Y).pos.getY();
  47.  
  48.         left_down = new Point2D.Double(x_min,y_min);
  49.         right_up = new Point2D.Double(x_max,y_max);
  50.  
  51.         delta_x = (right_up.getX() - left_down.getX()) / div_param + 1.0;
  52.         delta_y = (right_up.getY() - left_down.getY()) / div_param + 1.0;
  53.  
  54.         for(Task e : tasks)
  55.         {
  56.             data[get_square(e).x][get_square(e).y].add(e);
  57.         }
  58.         for(int i = 0; i < div_param; ++i)
  59.             for(int j = 0; j < div_param; ++j)
  60.                 left_by_squares[i][j] = data[i][j].size();
  61.     }
  62.  
  63.     Point get_square(Task e)
  64.     {
  65.         return new Point(((int) (e.pos.getX() / delta_x)),((int) (e.pos.getY() / delta_y)));
  66.     }
  67.  
  68.  
  69.     private Boolean is_legit(int i, int j)
  70.     {
  71.         return i >= 0 && i < div_param && j >=0 && j < div_param;
  72.     }
  73.  
  74.  
  75.     private double calc_op_val(Task to, Timestamp dest_time)
  76.     {
  77.         if (dest_time.after(to.to))
  78.             return -1;
  79.  
  80.         return (to.from.getTime() - dest_time.getTime()) / 3.6e+6;
  81.     }
  82.  
  83.     public Task find_next_stop(Task e, Timestamp curr_time)
  84.     {
  85.         double max_op_val = 0;
  86.         Task to = null;
  87.  
  88.         Point init = get_square(e);
  89.         for(int i = init.x - square_search_factor; i <= init.x + square_search_factor; ++i)
  90.             for(int j = init.y - square_search_factor; j <= init.y + square_search_factor; ++j)
  91.             {
  92.                 if (!is_legit(i,j))
  93.                     continue;
  94.  
  95.                 for(Task t : data[i][j])
  96.                 {
  97.                     if (t.planned)
  98.                         continue;
  99.  
  100.                     double op_val = calc_op_val(t,curr_time); //fix curr_time
  101.  
  102.                     if (op_val > max_op_val)
  103.                     {
  104.                        to = t;
  105.                        max_op_val = op_val;
  106.                     }
  107.                 }
  108.  
  109.             }
  110.         if (max_op_val >= op_val_thres)
  111.         {
  112.             return to;
  113.         }
  114.         else
  115.             return null;
  116.     }
  117.  
  118.  
  119.     private Task find_first_point()
  120.     {
  121.         int max_i = 0,max_j = 0;
  122.         for(int i = 0; i < div_param; ++i)
  123.             for(int j = 0; j < div_param; ++j)
  124.                 if (left_by_squares[i][j] > left_by_squares[max_i][max_j])
  125.                 {
  126.                     max_i = i;
  127.                     max_j = j;
  128.                 }
  129.         for(Task e : data[max_i][max_j])
  130.             if (!e.planned)
  131.                 return e;
  132.         return null;
  133.     }
  134.  
  135.  
  136.     private void update_square(Task s)
  137.     {
  138.         Point f = get_square(s);
  139.         --left_by_squares[f.x][f.y];
  140.     }
  141.  
  142.     public ArrayList<DeliverCar> solve()
  143.     {
  144.         ArrayList<DeliverCar> couriers = new ArrayList<DeliverCar>();
  145.         while(unsatisfied_customers > 0)
  146.         {
  147.             DeliverCar c = new DeliverCar();
  148.             Task stop = find_first_point();
  149.             while(stop != null)
  150.             {
  151.                 Timestamp new_time = c.add_stop(stop);
  152.                 stop.planned = true;
  153.                 --unsatisfied_customers;
  154.                 update_square(stop);
  155.  
  156.                 stop = find_next_stop(stop,new_time);
  157.             }
  158.             couriers.add(c);
  159.         }
  160.         return couriers;
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement