- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import javax.swing.*;
- import java.util.*;
- {
- JButton btn1;
- JButton btn2;
- JButton btn3;
- JButton btn4;
- JButton btnOK;
- JLabel lblIntro;
- static AssignmentGrid grid;
- Vector data;
- LoadFromFile lff;
- FileDialog browse;
- JPanel buttons;
- public static int zCount = 0;
- public static int stage = 1;
- public AssignmentGUI()
- {
- btnOK.addActionListener(this);
- this.setTitle("Visual Assignment Problem Solver");
- +"<br>Welcome to the <br><i><font color = maroon>"
- +"Visual Assignment Problem Solver</color></i>.</b>"
- +"<br><font size = 4><center> <i>n</i> companies submit bids to perform <i>n</i> jobs."
- +"<br><br>The goal of the Assignment Problem"
- +"<br>is to assign one job to each company so as to"
- +"<br>minimize the total cost of completing all of the jobs."
- +"<br>The problem is represented here as a grid"
- +"<br>with columns representing jobs to be assigned,"
- +"<br>rows representing the different companies,"
- +"<br>and the values in each cell represent the bids"
- +"<br>by each company for each job.<br>"
- +"<br>This application is a visual, interactive"
- +"<br>implementation of the <i>Hungarian Method.</i><br>"
- +"<br>Designed and implemented by <b>Brian Flink</b>"
- +"<br> as a class project for <b>Moshe Rosenfeld's</b>"
- +"<br>TCSS 343 Spring 2001 class at the"
- +"<br><i><b>University of Washington, Tacoma.</b>"
- grid = new AssignmentGrid();
- //this.pack();
- this.setVisible(true);
- this.addWindowListener(this);
- btn1.addActionListener(this);
- btn2.addActionListener(this);
- btn2.setEnabled(false);
- btn3.addActionListener(this);
- btn3.setEnabled(false);
- btn4.addActionListener(this);
- //button panel
- buttons.add(btn1);
- buttons.add(btn2);
- buttons.add(btn3);
- buttons.add(zeros);
- }
- {
- if(source == btnOK)
- {
- //Remove intro contents
- this.getContentPane().remove(lblIntro);
- this.getContentPane().remove(btnOK);
- this.pack();
- }
- if(source == btn1)
- {
- if(btn1.getText().equals("Browse"))
- {
- browse.show();
- status.setText(" Load the data to the table.");
- }
- else
- {
- if(stage == 1)
- grid.next();
- else if(stage == 2)
- {
- grid.next();
- AssignmentGrid.done = false;
- }
- else if(stage == 3)
- {
- if(AssignmentGrid.done)
- grid.next();
- else
- grid.greedy();
- }
- else if(stage == 4)
- grid.next();
- else if(stage == 5)
- {
- this.setStatus();
- grid.pathStep();
- AssignmentGrid.done = false;
- }
- else if(stage == 6)
- {
- this.setStatus();
- if(AssignmentGrid.done == false)
- grid.markRowWithSelectedZero();
- }
- else if(stage == 7)
- grid.next();
- else if(stage == 8)
- grid.next();
- else if(stage == 9)
- grid.next();
- else if(stage == 10)
- this.setStatus();
- this.repaint();
- }
- }
- if(source == btn2)
- {
- if(btn2.getText().equals("Load"))
- {
- lff = new LoadFromFile(browse.getDirectory(), browse.getFile());
- grid.initVector(lff.GetData());
- this.repaint();
- status.setText(" Press start to begin solving the assignment problem.");
- }
- else
- {
- grid.next();
- if(stage == 10)
- this.setStatus();
- }
- }
- if(source == btn3)
- {
- if(btn3.getText().equals("Start"))
- {
- status.setText(" Stage 1: Find the minimum value in each row and subtract from all in row.");
- btn1.setText("Next Step");
- btn2.setText("Next Stage");
- buttons.add(btn4, 2);
- btn4.setVisible(true);
- }
- else
- {
- grid.solve();
- this.setStatus();
- this.repaint();
- }
- }
- if(source == btn4)
- {
- if(stage < 10)
- {
- while(stage < 10 && stage != 5)
- grid.next();
- do
- {
- findAug();
- }while(stage < 10 && !grid.isAug);
- }
- }
- this.setButtons(source);
- }
- //Handle button enabling. If done in the blocks above, it causes a delay
- //if it attempts to disable a button immediately after it is activated.
- {
- if(source == btnOK)
- {
- btn1.setEnabled(true);
- btn2.setEnabled(false);
- btn3.setEnabled(false);
- btn4.setEnabled(false);
- }
- if(source == btn1)
- {
- if(btn1.getText().equals("Browse"))
- {
- btn1.setEnabled(true);
- btn2.setEnabled(true);
- btn3.setEnabled(false);
- btn4.setEnabled(false);
- }
- else
- {
- btn1.setEnabled(true);
- btn2.setEnabled(true);
- btn3.setEnabled(true);
- btn4.setEnabled(true);
- if(stage == 10)
- {
- buttons.remove(btn1);
- btn1.addActionListener(this);
- buttons.add(btn1, 0);
- btn1.setEnabled(false);
- btn2.setEnabled(false);
- btn3.setEnabled(false);
- btn4.setEnabled(false);
- }
- }
- }
- if(source == btn2)
- {
- if(btn2.getText().equals("Load"))
- {
- btn1.setEnabled(false);
- buttons.remove(btn2);
- btn2.addActionListener(this);
- buttons.add(btn2, 1);
- btn2.setEnabled(false);
- btn3.setEnabled(true);
- btn4.setEnabled(false);
- }
- else
- {
- if(stage == 10)
- {
- btn1.setEnabled(false);
- buttons.remove(btn2);
- btn2.addActionListener(this);
- buttons.add(btn2, 1);
- btn2.setEnabled(false);
- btn3.setEnabled(false);
- btn4.setEnabled(false); }
- else
- {
- btn1.setEnabled(true);
- btn2.setEnabled(true);
- btn3.setEnabled(true);
- btn4.setEnabled(true);
- }
- }
- }
- if(source == btn3)
- {
- if(btn3.getText().equals("Start"))
- {
- btn3.setText("Solve");
- btn1.setEnabled(true);
- btn2.setEnabled(true);
- btn3.setEnabled(true);
- btn4.setEnabled(true);
- }
- else
- {
- btn1.setEnabled(false);
- btn2.setEnabled(false);
- buttons.remove(btn3);
- btn3.addActionListener(this);
- buttons.add(btn3, 3);
- btn3.setEnabled(false);
- btn4.setEnabled(false);
- }
- }
- if(source == btn4)
- {
- if(stage < 10)
- {
- btn1.setEnabled(true);
- btn2.setEnabled(true);
- btn3.setEnabled(true);
- btn4.setEnabled(true);
- }
- else
- {
- btn1.setEnabled(false);
- btn2.setEnabled(false);
- btn3.setEnabled(false);
- buttons.remove(btn4);
- btn4.addActionListener(this);
- buttons.add(btn4, 2);
- btn4.setEnabled(false);
- }
- }
- }
- //Finds the next augmenting path
- private void findAug()
- {
- if(stage == 5)
- {
- do
- {
- grid.pathStep();
- }while(!grid.isAug && stage == 5);
- }
- else
- while(stage < 10 && stage != 5)
- grid.next();
- }
- public static void setStatus()
- {
- if(stage == 1)
- status.setText(" Stage 1: Find the minimum value in each row and subtract from all in row.");
- else if(stage == 2)
- status.setText(" Stage 2: Find the minimum value in each column and subtract from all in column.");
- else if(stage == 3)
- status.setText(" Stage 3: Mark zeros found on veritces.");
- else if(stage == 4)
- status.setText(" Stage 4: Mark unselected zeros.");
- else if(stage == 5)
- status.setText(" Stage 5: Find augmenting paths. Path must terminate on an unselected zero. ");
- else if(stage == 6)
- status.setText(" Stage 6: Lock out rows containing selected zeros.");
- else if(stage == 7)
- status.setText(" Stage 7: Find minimum value that is not locked out.");
- else if(stage == 8)
- status.setText(" Stage 8: Add minimum value to dark gray cells. Subtract from white cells.");
- else if(stage == 9)
- status.setText(" Stage 9: Go back to stage 3. Repeat process until all zeros are found.");
- else if(stage == 10)
- {
- status.setText(" Assignment problem has been solved! Minumum cost = " + grid.getSum()); }
- else
- stage = 10;
- }
- public static void setCount()
- {
- }
- //Exits when close icon is clicked
- {
- }
- //Overwrite WindowListener methods
- //Run main.
- {
- new AssignmentGUI();
- }
- }
Raw Paste