Advertisement
Guest User

Program.cs

a guest
Nov 7th, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using System.Data;
  6. using System.Text;
  7. using System.IO;
  8.  
  9. namespace AX_Heuristic
  10. {
  11.     class Program
  12.     {
  13.         /// <summary>
  14.         /// The main entry point for the application.
  15.         /// </summary>
  16.         [STAThread]
  17.         static void Main()
  18.         {
  19.             Application.EnableVisualStyles();
  20.             Application.SetCompatibleTextRenderingDefault(false);
  21.             Application.Run(new Form1());
  22.         }
  23.    
  24.  
  25.         public job[] jobPool;
  26.         public long[] schedule;
  27.  
  28.         public void AX1Program(string path)
  29.         {
  30.             List<string[]> jobData = new List<string[]>();
  31.  
  32.             Program agent = new Program();
  33.  
  34.             jobData = Program.parseCSV(path);
  35.  
  36.             agent.jobPool = new job[jobData.Count];
  37.  
  38.             int count = 0;
  39.             long parent = 0;
  40.             long jobId = 0;
  41.             double dueDate = 0.00;
  42.  
  43.             foreach (string[] s in jobData)
  44.             {
  45.                 //for (int i = 0; i < s.Length; i++)
  46.                 //{
  47.                 //    //writes down the file values
  48.                 //    Console.WriteLine(s[i]);
  49.                 //}
  50.                 //column 1 jobid
  51.                 //Column 2 bomid
  52.                 //column 3 parent
  53.                 //column 4 processign time
  54.                 //column 5 due date
  55.                 agent.jobPool[count] = new job(Convert.ToInt16(s[0]), Convert.ToInt64(s[1]), Convert.ToInt64(s[2]), Convert.ToDouble(s[3]), Convert.ToDouble(s[4]));
  56.                 parent = agent.jobPool[count].getParent();
  57.                 while (parent != 0)
  58.                 {
  59.                     dueDate = dueDate + methodpool.getJobbyJobId(agent.jobPool, parent).getProcessingTime();
  60.                     jobId = parent;
  61.                     parent = methodpool.getJobbyJobId(agent.jobPool, parent).getParent();
  62.                 }
  63.                 if (dueDate != 0)
  64.                 {
  65.                     agent.jobPool[count].setDuedate(methodpool.getJobbyJobId(agent.jobPool, jobId).getDueDate() - dueDate);
  66.                 }
  67.                 dueDate = 0;
  68.                 count++;
  69.             }
  70.  
  71.             job tempJob = new job();
  72.             bool swaped = true;
  73.             int j = 0;
  74.             // sort jobs by EDD
  75.             while (swaped)
  76.             {
  77.                 swaped = false;
  78.                 j++;
  79.                 for (int i = 0; i < count - j; i++)
  80.                 {
  81.                     if (agent.jobPool[i].getDueDate() > agent.jobPool[i + 1].getDueDate())
  82.                     {
  83.                         tempJob = agent.jobPool[i];
  84.                         agent.jobPool[i] = agent.jobPool[i + 1];
  85.                         agent.jobPool[i + 1] = tempJob;
  86.                         swaped = true;
  87.                     }
  88.                 }
  89.             }
  90.  
  91.             // print sorted job
  92.             Form1 Frm1 = new Form1();
  93.             Frm1.UpdateStatusBar("Sorted jobs by EDD....");
  94.             Frm1.Refresh();
  95.             //Console.WriteLine("/n");
  96.             foreach (job job in agent.jobPool)
  97.             {
  98.                 Console.WriteLine("{0}   {1}   {2}   {3}   {4}", job.jobId.ToString(), job.bomId.ToString(), job.getParent(), job.getProcessingTime(), job.getDueDate());
  99.                 //Console.WriteLine("/n");
  100.             }
  101.  
  102.             // make sched
  103.             double totalCompletion = 0.00;
  104.             bool end = false;
  105.             int nextPos = 0;
  106.             int nextpos1 = 0;
  107.             long bomid;
  108.             job highPi = new job();
  109.             job job1 = new job();
  110.             job dummy = new job();
  111.             long[] schedule1 = new long[agent.jobPool.Length];
  112.             agent.schedule = new long[agent.jobPool.Length];
  113.             long[] earlyBom = new long[agent.jobPool.Length];
  114.             long[] lateBom = new long[agent.jobPool.Length];
  115.             Console.WriteLine("Start assigning jobs....");
  116.             while (end == false)
  117.             {
  118.                 foreach (job jb in agent.jobPool)
  119.                 {
  120.                     if (jb.flag == 0)
  121.                     {
  122.                         if (methodpool.isChildAssigned(agent.jobPool, jb.jobId) == false)
  123.                         {
  124.                             //assign job
  125.                             agent.schedule[nextPos] = jb.jobId;
  126.                             Console.WriteLine(jb.jobId);
  127.                             nextPos++;
  128.                             jb.flag = 1;
  129.                             // store total completion
  130.                             totalCompletion = totalCompletion + jb.getProcessingTime();
  131.  
  132.                             //store highest pi job
  133.                             if (highPi.getProcessingTime() < jb.getProcessingTime())
  134.                                 highPi = jb;
  135.  
  136.                             // check Moore's modified criteria
  137.                             if (totalCompletion > jb.getDueDate())
  138.                             {
  139.                                 Console.WriteLine("Late job...remove jobs...");
  140.                                 //remove job from sched
  141.                                 for (int m = 0; m < agent.schedule.Length; m++)
  142.                                 {
  143.                                     //no more iteration if last job of schedule is traversed
  144.                                     if (agent.schedule[m] == 0)
  145.                                         break;
  146.  
  147.                                     bomid = methodpool.getJobbyJobId(agent.jobPool, agent.schedule[m]).bomId;
  148.                                     // get all jobs of same bom of highPi
  149.                                     if (bomid == highPi.bomId)
  150.                                     {
  151.                                         // completion time reduction
  152.                                         job1 = methodpool.getJobbyJobId(agent.jobPool, agent.schedule[m]);
  153.                                         totalCompletion = totalCompletion - job1.getProcessingTime();
  154.  
  155.                                         // remove job at position m (shift later jobs from m left by one position)
  156.                                         //for (int n = m; n < agent.schedule.Length - 1; n++)
  157.                                         //{
  158.                                         //    agent.schedule[n] = agent.schedule[n + 1];
  159.                                         //    if (agent.schedule[n+1] == 0)
  160.                                         //        break;
  161.                                         //}
  162.                                         //nextPos--;                                
  163.                                         Array.Clear(agent.schedule, m, 1);
  164.  
  165.                                     }
  166.                                 }
  167.  
  168.                                 // flag all jobs in the jobpool
  169.                                 foreach (job jb1 in agent.jobPool)
  170.                                 {
  171.                                     if (jb1.bomId == highPi.bomId)
  172.                                         jb1.flag = 2;
  173.                                 }
  174.  
  175.                                 nextPos = 0;
  176.                                 highPi = dummy;
  177.                                 Array.Clear(schedule1, 0, agent.schedule.Length);
  178.                                 // get the new schedule after removing the cleared positions
  179.                                 for (int m = 0; m < agent.schedule.Length; m++)
  180.                                 {
  181.                                     if (agent.schedule[m] != 0)
  182.                                     {
  183.                                         schedule1[nextPos] = agent.schedule[m];
  184.                                         nextPos++;
  185.                                     }
  186.                                 }
  187.                                 Array.Clear(agent.schedule, 0, agent.schedule.Length);
  188.                                 agent.schedule = schedule1;
  189.  
  190.  
  191.                                 // get new highPI
  192.                                 highPi = dummy;
  193.                                 for (int n = 0; n < agent.schedule.Length; n++)
  194.                                 {
  195.                                     job1 = methodpool.getJobbyJobId(agent.jobPool, agent.schedule[n]);
  196.                                     if (agent.schedule[n] == 0)
  197.                                         break;
  198.                                     if (highPi.getProcessingTime() < job1.getProcessingTime())
  199.                                         highPi = job1;
  200.                                 }
  201.  
  202.  
  203.  
  204.                                 // write schedule after removal of job
  205.                                 foreach (long jb2 in agent.schedule)
  206.                                 {
  207.                                     if (jb2 == 0)
  208.                                         break;
  209.                                     else
  210.                                         Console.WriteLine(jb2);
  211.                                 }
  212.                                 Console.WriteLine("Start scheduling again");
  213.  
  214.                                 // remove all child
  215.                                 //done above
  216.                                 //flag off all jobs in the BOM
  217.                                 //done above
  218.                             }
  219.                         }
  220.                     }
  221.                 }
  222.                 end = methodpool.isPoolEmpty(agent.jobPool);
  223.             }
  224.  
  225.             // just assign the unassigned jobs according to precedence
  226.             Console.WriteLine("Assign late jobs to the schedule");
  227.             foreach (job jb2 in agent.jobPool)
  228.             {
  229.                 if (jb2.flag == 2)
  230.                 {
  231.                     if (methodpool.isChildAssigned(agent.jobPool, jb2.jobId) == false)
  232.                     {
  233.                         //assign job
  234.                         agent.schedule[nextPos] = jb2.jobId;
  235.                         Console.WriteLine(jb2.jobId);
  236.                         nextPos++;
  237.                         //methodpool.getJobbyJobId(agent.jobPool, jb2.jobId).flag = 1;
  238.                     }
  239.                 }
  240.             }
  241.             Console.WriteLine("Final Schedule...");
  242.             nextPos = 0;
  243.             nextpos1 = 0;
  244.             long bom = 0;
  245.             foreach (long jb2 in agent.schedule)
  246.             {
  247.                 Console.WriteLine(jb2);
  248.                 if (methodpool.getJobbyJobId(agent.jobPool, jb2).flag == 1)
  249.                 {
  250.                     bom = methodpool.getJobbyJobId(agent.jobPool, jb2).bomId;
  251.                     if (Array.Exists(earlyBom, element => element == bom) == false)
  252.                     {
  253.                         earlyBom[nextPos] = bom;
  254.                         nextPos++;
  255.                     }
  256.                 }
  257.                 else
  258.                 {
  259.                     bom = methodpool.getJobbyJobId(agent.jobPool, jb2).bomId;
  260.                     if (Array.Exists(lateBom, element => element == bom) == false)
  261.                     {
  262.                         lateBom[nextpos1] = bom;
  263.                         nextpos1++;
  264.                     }
  265.                 }
  266.             }
  267.             Console.WriteLine("Early BOMs...");
  268.             foreach (long jb2 in earlyBom)
  269.             {
  270.                 if (jb2 == 0)
  271.                     break;
  272.                 Console.WriteLine(jb2);
  273.             }
  274.             Console.WriteLine("Late BOMs...");
  275.             foreach (long jb2 in lateBom)
  276.             {
  277.                 if (jb2 == 0)
  278.                     break;
  279.                 Console.WriteLine(jb2);
  280.             }
  281.             Console.WriteLine("Press return to exit...");
  282.             Console.ReadLine();
  283.         }
  284.  
  285.  
  286.  
  287.         // function to read CSV file
  288.         public static List<string[]> parseCSV(string path)
  289.         {
  290.             List<string[]> parsedData = new List<string[]>();
  291.  
  292.             try
  293.             {
  294.                 using (StreamReader readFile = new StreamReader(path))
  295.                 {
  296.                     string line;
  297.                     string[] row;
  298.  
  299.                     while ((line = readFile.ReadLine()) != null)
  300.                     {
  301.                         row = line.Split(',');
  302.                         parsedData.Add(row);
  303.                     }
  304.                 }
  305.             }
  306.             catch (Exception e)
  307.             {
  308.                 MessageBox.Show(e.Message);
  309.             }
  310.  
  311.             return parsedData;
  312.         }
  313.     }
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement