Advertisement
LeeSeng

Sequencer

Aug 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. package system.domain;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8.  
  9. public class Sequencer {
  10.     private Comparator<Job> byFinishTime;
  11.    
  12.     // Raw Input
  13.     private List<Job> jobList;
  14.    
  15.     // To count the number of worker we need
  16.     private int counter;
  17.    
  18.     // Results of profit and sequenced job is store in this ArrayList
  19.     private List<Integer> maxProfits;
  20.     private List<LinkedList<Job>> jobSequencedList;
  21.    
  22.     public Sequencer() {
  23.         this.byFinishTime = new JobComparatorByFinishTime();
  24.         this.counter = 0;
  25.        
  26.         // Results of profit and sequenced job is store in this ArrayList
  27.         this.jobSequencedList = new ArrayList<>();
  28.         this.maxProfits = new ArrayList<>();
  29.     }
  30.    
  31.     public Sequencer(List<Job> jobList) {
  32.         this.byFinishTime = new JobComparatorByFinishTime();
  33.         this.jobList = jobList;
  34.         this.counter = 0;
  35.        
  36.         // Results of profit and sequenced job is store in this ArrayList
  37.         this.jobSequencedList = new ArrayList<>();
  38.         this.maxProfits = new ArrayList<>();
  39.     }
  40.    
  41.     public void calculateMaxProfitAndSequencingJob() {
  42.         int counter = 0;
  43.         while (jobList.size() != 0) {
  44.             // A list of profit, that can be produced by several of job sequences.
  45.             List<Integer> profitList = new ArrayList<>();
  46.             // A list of sequences of job, the sequence can produce maximum profit will be selected.
  47.             List<LinkedList<Job>> jobSequencedToBeSelected = new ArrayList<>();
  48.            
  49.             int jobSequencedIndex = 0;
  50.            
  51.             int maxProfit = Integer.MIN_VALUE;
  52.             LinkedList<Job> jobSequenced = new LinkedList<>();
  53.            
  54.             Collections.sort(jobList, byFinishTime);
  55.             profitList.add(jobList.get(0).getProfit());
  56.             jobSequencedToBeSelected.add(new LinkedList<>());
  57.             jobSequencedToBeSelected.get(0).add(jobList.get(0));
  58.            
  59.             for (int i = 1; i < jobList.size(); i++) {
  60.                 profitList.add(jobList.get(i).getProfit());
  61.                 jobSequencedToBeSelected.add(new LinkedList<>());
  62.                 jobSequencedToBeSelected.get(i).add(jobList.get(i));
  63.                
  64.                 for (int j = i - 1; j >= 0; j--) {
  65.                     if (jobList.get(j).getFinishTime() <= jobList.get(i).getStartTime()) {
  66.                         int previousAndNowProfit = jobList.get(i).getProfit() + profitList.get(j);
  67.                         int element = 0;
  68.                         boolean changed = false;
  69.                        
  70.                         if (previousAndNowProfit > profitList.get(i)) {
  71.                             element = previousAndNowProfit;
  72.                             profitList.set(i, element);
  73.                             changed = true;
  74.                         }
  75.                        
  76.                         if (changed) {
  77.                             LinkedList<Job> previousList = jobSequencedToBeSelected.get(j);
  78.                             jobSequencedToBeSelected.get(i).clear();
  79.                             jobSequencedToBeSelected.get(i).add(jobList.get(i));
  80.                             jobSequencedToBeSelected.get(i).addAll(0, previousList);
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.            
  86.             for (int i = 0; i < profitList.size(); i++) {
  87.                 if (maxProfit <= profitList.get(i)) {
  88.                     maxProfit = profitList.get(i);
  89.                     jobSequencedIndex = i;
  90.                 }
  91.             }
  92.            
  93.             jobSequenced = jobSequencedToBeSelected.get(jobSequencedIndex);
  94.             this.jobList.removeAll(jobSequenced);
  95.            
  96.             this.maxProfits.add(counter, maxProfit);
  97.             this.jobSequencedList.add(counter, jobSequenced);
  98.             counter++;
  99.         }
  100.         this.counter = counter;
  101.     }
  102.    
  103.     public void setJobList(List<Job> jobList) {
  104.         this.jobList = jobList;
  105.     }
  106.    
  107.     public List<LinkedList<Job>> getJobSequenced() {
  108.         return this.jobSequencedList;
  109.     }
  110.    
  111.     public LinkedList<Job> getSpecifiedJobSequenced(int index) {
  112.         return this.jobSequencedList.get(index);
  113.     }
  114.    
  115.     public List<Integer> getMaxProfit() {
  116.         return this.maxProfits;
  117.     }
  118.    
  119.     public int getSpecifiedMaxProfit(int index) {
  120.         return this.maxProfits.get(index);
  121.     }
  122.    
  123.     public int getCounter() {
  124.         return this.counter;
  125.     }
  126.    
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement