Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.awt.*;
- import java.awt.geom.Point2D;
- import java.sql.Timestamp;
- import java.util.ArrayList;
- import java.util.Collections;
- /**
- * Created by DELL on 10/9/2015.
- */
- public class TaskMap
- {
- final private int div_param;
- final private int square_search_factor;
- final private double op_val_thres;
- private ArrayList<Task>[][] data;
- private int[][] left_by_squares;
- private int unsatisfied_customers;
- final private double delta_x;
- final private double delta_y;
- final private Point2D left_down;
- final private Point2D right_up;
- public TaskMap(ArrayList<Task> tasks, int div_param, int square_search_factor, double op_val_thres)
- {
- this.div_param = div_param;
- this.square_search_factor = square_search_factor;
- this.op_val_thres = op_val_thres;
- this.unsatisfied_customers = tasks.size();
- data = new ArrayList[div_param][div_param];
- for(int i = 0; i < div_param; ++i)
- for(int j = 0; j < div_param; ++j)
- data[i][j] = new ArrayList<Task>();
- left_by_squares = new int[div_param][div_param];
- double x_min = Collections.max(tasks, Task::compare_X).pos.getX();
- double x_max = Collections.min(tasks, Task::compare_X).pos.getX();
- double y_max = Collections.max(tasks, Task::compare_Y).pos.getY();
- double y_min = Collections.min(tasks, Task::compare_Y).pos.getY();
- left_down = new Point2D.Double(x_min,y_min);
- right_up = new Point2D.Double(x_max,y_max);
- delta_x = (right_up.getX() - left_down.getX()) / div_param + 1.0;
- delta_y = (right_up.getY() - left_down.getY()) / div_param + 1.0;
- for(Task e : tasks)
- {
- data[get_square(e).x][get_square(e).y].add(e);
- }
- for(int i = 0; i < div_param; ++i)
- for(int j = 0; j < div_param; ++j)
- left_by_squares[i][j] = data[i][j].size();
- }
- Point get_square(Task e)
- {
- return new Point(((int) (e.pos.getX() / delta_x)),((int) (e.pos.getY() / delta_y)));
- }
- private Boolean is_legit(int i, int j)
- {
- return i >= 0 && i < div_param && j >=0 && j < div_param;
- }
- private double calc_op_val(Task to, Timestamp dest_time)
- {
- if (dest_time.after(to.to))
- return -1;
- return (to.from.getTime() - dest_time.getTime()) / 3.6e+6;
- }
- public Task find_next_stop(Task e, Timestamp curr_time)
- {
- double max_op_val = 0;
- Task to = null;
- Point init = get_square(e);
- for(int i = init.x - square_search_factor; i <= init.x + square_search_factor; ++i)
- for(int j = init.y - square_search_factor; j <= init.y + square_search_factor; ++j)
- {
- if (!is_legit(i,j))
- continue;
- for(Task t : data[i][j])
- {
- if (t.planned)
- continue;
- double op_val = calc_op_val(t,curr_time); //fix curr_time
- if (op_val > max_op_val)
- {
- to = t;
- max_op_val = op_val;
- }
- }
- }
- if (max_op_val >= op_val_thres)
- {
- return to;
- }
- else
- return null;
- }
- private Task find_first_point()
- {
- int max_i = 0,max_j = 0;
- for(int i = 0; i < div_param; ++i)
- for(int j = 0; j < div_param; ++j)
- if (left_by_squares[i][j] > left_by_squares[max_i][max_j])
- {
- max_i = i;
- max_j = j;
- }
- for(Task e : data[max_i][max_j])
- if (!e.planned)
- return e;
- return null;
- }
- private void update_square(Task s)
- {
- Point f = get_square(s);
- --left_by_squares[f.x][f.y];
- }
- public ArrayList<DeliverCar> solve()
- {
- ArrayList<DeliverCar> couriers = new ArrayList<DeliverCar>();
- while(unsatisfied_customers > 0)
- {
- DeliverCar c = new DeliverCar();
- Task stop = find_first_point();
- while(stop != null)
- {
- Timestamp new_time = c.add_stop(stop);
- stop.planned = true;
- --unsatisfied_customers;
- update_square(stop);
- stop = find_next_stop(stop,new_time);
- }
- couriers.add(c);
- }
- return couriers;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement