Simulate the simplex method with GUI

1. Program overview

This program is a program for finding the optimum solution of the maximization function in linear programming. In linear programming (in the case of maximization), there are maximization functions and constraints. The maximization function is a linear function and is a function for the purpose of maximization. Constraints are linear inequalities and linear equations, which define the range of solutions for the maximization function. However, in this program, both the maximization function and the constraint condition are set to 2 variables (X, Y) respectively (for details of the maximization function and the constraint condition in this program, refer to the operation method in Chapter 3). There is a simplex method as an algorithm for finding the optimum solution. The simplex method repeats moving a point in the feasible region as a starting point to the direction in which the maximization function becomes larger. When the point stops moving, the maximization function becomes the optimal solution. In this program, the user inputs and defines the maximization function and constraints. Then, based on the input maximization function and constraints, the movement of points by the simplex method is simulated, and the optimum solution is obtained and displayed. However, this program only simulates the movement of points in the simplex method with a GUI, and does not actually find the optimum solution using the simplex method. For the optimal solution, the coordinates of each vertex in the feasible region are assigned to the maximization function, and the solution with the largest value is displayed as the optimal solution.

2. Program structure

The structure of the program is as follows.

  1. Simplex_simu class
  2. main method 2.MyPanel7 class
  3. Constructor
  4. paintComponent method
  5. run method
  6. actionPerformed method

3. Class processing content

The contents of each process will be explained. Please read it while comparing it with the code (Chapter 5).

3.1. Simplex_simu class

In the main method of this class, the panel displayed by the function of MyPanel7 class is pasted into the window. It also describes the basic window processing, such as window size, processing settings when the close button is pressed, and making the window visible so that the user can operate it.

3.2.MyPanel7 class

In the constructor, the text field for inputting the maximization function and the constraint condition is declared and arranged. In addition, in order to make it easier for the user to understand where and what to enter, labels are also declared and placed to encourage input. Then, the input value is reflected in the program, and two buttons, a button for drawing a straight line on the graph (button1) and a button for starting the simulation (button2), are also declared and arranged. In this way, the constructor mainly describes the input part in the screen. The actionPerformed method describes the process when the button is clicked. When button1 is clicked, the value is taken from the text field and stored in the array info. When button2 is clicked, the simplex method simulation is started. In the run method, the thread is stopped for 100ms. By stopping for 100ms, the user can check the movement of the points during the simulation. Then, in the paintComponent method, simulation (movement of points) and calculation of the optimum solution are performed. Please read the comment text of the source code to find out what kind of description is being performed. Here, a rough process will be described. First, draw three straight lines on the graph from the three input constraints. Then, the x-intercept, y-intercept, and intersection are obtained from the three straight lines. From those points, points that are not included in the feasible region are set to coordinates (-1, -1) and excluded. The points not excluded here are the vertices of the feasible region and are candidates for the optimum solution. In this way, the one that gives the maximum solution is searched from the candidates for the optimum solution obtained. The one that gives the maximum solution is the optimum solution. Now that the optimal solution has been found, we will perform a simulation. First, pay attention to the coefficient of the maximization function, and increase the variable with the larger coefficient. That is, starting from the origin, the point is moved to the intercept of the variable with the larger coefficient. At this time, if the intercept gives the optimum solution, stop there. Then, after moving to the intercept, move to the intersection of two straight lines close to the intercept. If this intersection gives the optimal solution, stop there. Then, it is further moved to the next intersection. If this intersection gives the optimal solution, stop there. In this way, conditional branching is performed and the movement of the points is determined depending on which point in the feasible region the optimum solution point is located. Then, when it stops at the point where the optimum solution is obtained, the optimum solution is displayed in the upper right corner of the graph, and the process is completed.

4. Operation method

When the objective function is S₁X + S₂Y, the user can input and determine S₁ and S₂, which are the coefficients of X and Y, from the input field. However, the values entered in S₁ and S₂ are limited to positive integers. In addition, the constraints for determining the feasible region are limited to three equations for convenience. When the three equations are αiX + βiY ≤ γi (1 ≤ i ≤ 3), the user can input and determine αi, βi, and γi from the input field (for convenience, all three equations have inequality signs). ≤). However, these values are also limited to positive integers. After inputting the maximization function and constraints, click the button "(1) Set the input value and output to the graph". By clicking this button, the entered value will be assigned to each variable in the program. Then, the constraints are written out on the graph. Next, click the button "② Simulation start". By clicking this button, the movement of points by the simplex method starts with the origin as the starting point. Then, (X, Y) where the point stops gives the optimum solution of the maximization function. The optimum solution at this time is displayed in the upper right of the graph, and the program ends. This is how to operate this program. You can do it as many times as you like by entering the numbers again and clicking the two buttons in sequence.

5. Code

Simplex_simu.java


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Font;

public class Simplex_simu {
    public static void main(String[] args) {
        JFrame frame = new JFrame();     //Frame generation
        
        frame.setTitle("Simplex method simulation");       //Window title
        frame.setSize(675, 680);                                      //Window size
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //Processing when the × button is pressed
        
        MyPanel panel = new MyPanel(); //Generate MyPanel
        frame.getContentPane().add(panel); //Paste My Panel on the frame
        
        frame.setVisible(true);             //Make the window visible
    }
}

class MyPanel extends JPanel implements Runnable,ActionListener {
    int count = 0;
    int info[] = new int[11];
    
    int simu_judge = 0;       //Variables for deciding whether to start the simulation
                              //simu_judge=1 to start the simulation
    
    int scale_judge = 0;      //Variable to determine whether to write the scale to the axis of the graph
                              //scale_judge=1 to start the simulation
    
    //Circle(Show the solution at that time)Center coordinates of
    int x = 100;
    int y = 600;
    
    //Stores the number of steps to find the optimal solution(step=At 4, the optimum solution is found and finished)
    int step = 0;
    
    //X at the intersection of three straight lines,Variable to store the y coordinate
    int x_intersection12 = -1;   //The x coordinate of the intersection of the first straight line and the second straight line
    int x_intersection13 = -1;   //The x coordinate of the intersection of the first straight line and the third straight line
    int x_intersection23 = -1;   //The x-coordinate of the intersection of the second and third straight lines
    int y_intersection12 = -1;   //Y coordinate of the intersection of the first entered straight line and the second entered straight line
    int y_intersection13 = -1;   //The y coordinate of the intersection of the first straight line and the third straight line
    int y_intersection23 = -1;   //The y coordinate of the intersection of the second straight line and the third straight line
    
    //Text field(Maximization function)
    JTextField o_x = new JTextField(10);
    JTextField o_y = new JTextField(10);
    JTextField o_n = new JTextField(10);
    
    //Text field(Constraint 1 and 1st straight line to be input)
    JTextField s_x1 = new JTextField(10);
    JTextField s_y1 = new JTextField(10);
    JTextField s_n1= new JTextField(10);
    
    //Text field(Constraint 2 and 2nd straight line to be input)
    JTextField s_x2 = new JTextField(10);
    JTextField s_y2 = new JTextField(10);
    JTextField s_n2 = new JTextField(10);
    
    //Text field(Constraint 3 and 3rd straight line to be input)
    JTextField s_x3 = new JTextField(10);
    JTextField s_y3 = new JTextField(10);
    JTextField s_n3 = new JTextField(10);
    
    //label(Guidance to prompt input on the screen)
    JLabel label1 = new JLabel("x            +");
    JLabel label2 = new JLabel("y");
    JLabel label3 = new JLabel("");
    JLabel label4 = new JLabel("x            +");
    JLabel label5 = new JLabel("y           ≦");
    JLabel label6 = new JLabel("x            +");
    JLabel label7 = new JLabel("y           ≦");
    JLabel label8 = new JLabel("x            +");
    JLabel label9 = new JLabel("y           ≦");
    JLabel label10 = new JLabel("x≧0、y≧0");
    JLabel label11 = new JLabel("Objective function(Maximization function)");
    JLabel label12 = new JLabel("Constraints");
    
    //Button 1(Reflect the entered value in the program and draw a straight line on the graph)
    JButton button1 = new JButton("(1) Set the input value and output it to the graph");
    //Button 2(Start simulating the simplex method)
    JButton button2 = new JButton("② Simulation start");
    
    public MyPanel() {
        //Register event listener
        button1.addActionListener(this);
        button2.addActionListener(this);
        
        //Panel layout settings
        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(1,1));
        JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1,5));
        JPanel panel3 = new JPanel();
        panel3.setLayout(new GridLayout(1,1));
        JPanel panel4 = new JPanel();
        panel4.setLayout(new GridLayout(1,5));
        JPanel panel5 = new JPanel();
        panel5.setLayout(new GridLayout(1,5));
        JPanel panel6 = new JPanel();
        panel6.setLayout(new GridLayout(1,5));
        JPanel panel7 = new JPanel();
        panel7.setLayout(new GridLayout(1,1));
        
        //Unify the color of each panel to white
        panel1.setBackground(Color.WHITE);
        panel2.setBackground(Color.WHITE);
        panel3.setBackground(Color.WHITE);
        panel4.setBackground(Color.WHITE);
        panel5.setBackground(Color.WHITE);
        panel6.setBackground(Color.WHITE);
        panel7.setBackground(Color.WHITE);
        
        //Add each component to each panel
        panel1.add(label11);
        
        panel2.add(o_x);
        panel2.add(label1);
        panel2.add(o_y);
        panel2.add(label2);
        panel2.add(label3);
        
        panel3.add(label12);
        
        panel4.add(s_x1);
        panel4.add(label4);
        panel4.add(s_y1);
        panel4.add(label5);
        panel4.add(s_n1);
        
        panel5.add(s_x2);
        panel5.add(label6);
        panel5.add(s_y2);
        panel5.add(label7);
        panel5.add(s_n2);
        
        panel6.add(s_x3);
        panel6.add(label8);
        panel6.add(s_y3);
        panel6.add(label9);
        panel6.add(s_n3);
        
        panel7.add(label10);
        
        
        //Each panel, one panel f_Paste on panel1
        JPanel f_panel1 = new JPanel();
        f_panel1.setLayout(new GridLayout(9,1));
        f_panel1.add(panel1);
        f_panel1.add(panel2);
        f_panel1.add(panel3);
        f_panel1.add(panel4);
        f_panel1.add(panel5);
        f_panel1.add(panel6);
        f_panel1.add(panel7);
        f_panel1.add(button1);
        f_panel1.add(button2);
        
        f_panel1.setBackground(Color.WHITE);    //f_Install panel1 in white
        add(f_panel1);                          //f_Add panel1 to MyPanel
        
        setBackground(Color.WHITE);
        Thread refresh = new Thread(this);
        refresh.start();
        
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);             //Fill the component with a background color
        int seppen[] = new int[6];
        int seppen_max = 0;        //Intercept(x,y both)Maximum value of
        int xy_max = 0;               //X in the graph,Maximum value of y
        int x_min = 0;                 //x minimum intercept
        int y_min = 0;                 //Minimum y-intercept
        int n_x_min = 0;              //x Number of the expression with the minimum intercept
        int n_y_min = 0;              //The number of the expression with the minimum y-intercept
        double d_masu = 0;         //Graph unit per pixel
        int select3 = 0;           //In step 3, store the numbers of the straight lines involved in the remaining intersections that have not yet moved.
        
        //Find the temporary x-intercept and y-intercept values and assign them to each variable.(Assign up to one decimal place to a variable by casting to int type over 10 times)
        int temporary_x1 = (int)(((double)info[4]/(double)info[2])*10);   //X-intercept of the first entered straight line
        int temporary_y1 = (int)(((double)info[4]/(double)info[3])*10);   //Y-intercept of the first entered straight line
        int temporary_x2 = (int)(((double)info[7]/(double)info[5])*10);   //The x-intercept of the second entered straight line
        int temporary_y2 = (int)(((double)info[7]/(double)info[6])*10);   //Y-intercept of the second entered straight line
        int temporary_x3 = (int)(((double)info[10]/(double)info[8])*10);  //The x-intercept of the third entered straight line
        int temporary_y3 = (int)(((double)info[10]/(double)info[9])*10);  //Y-intercept of the third input straight line
        
        //The intercept obtained above is sequenced seppen[]Store in
        seppen[0] = temporary_x1;
        seppen[1] = temporary_y1;
        seppen[2] = temporary_x2;
        seppen[3] = temporary_y2;
        seppen[4] = temporary_x3;
        seppen[5] = temporary_y3;
        
        //Find the maximum value of the intercept.
        seppen_max = seppen[0];
        for(int i=1; i<6; i++){
            if(seppen[i] > seppen_max){
                seppen_max = seppen[i];
            }
        }
        
        //Find the maximum value of the graph scale from the maximum value of the intercept.
        while(true){
            //When the maximum value of the scale of the graph exceeds the maximum value of the intercept
            if(xy_max >= seppen_max){
                break;                //Break out of the loop and xy at that point_Set max to the maximum value of the scale
            }
            xy_max = xy_max + 150;    //xy_Increase max by 150
                                      //(Since the intercept is multiplied by 10 at the top, the maximum value of the scale actually increases by 15)
        }
        
        //Find the graph unit per pixel
        d_masu = ((double)300)/((double)xy_max)*10;     //300 is the number of pixels in the vertical direction of the graph
                                                        //Divide 300 by the maximum value of the scale and multiply by 10 to obtain the value in graph units.
        
        //Find the coordinates of each intercept in the graph
        int decisive_x1 = (int)(((double)info[4]/(double)info[2])*d_masu);   //X-intercept of the first entered straight line
        int decisive_y1 = (int)(((double)info[4]/(double)info[3])*d_masu);   //Y-intercept of the first entered straight line
        int decisive_x2 = (int)(((double)info[7]/(double)info[5])*d_masu);   //The x-intercept of the second entered straight line
        int decisive_y2 = (int)(((double)info[7]/(double)info[6])*d_masu);   //Y-intercept of the second entered straight line
        int decisive_x3 = (int)(((double)info[10]/(double)info[8])*d_masu);  //The x-intercept of the third entered straight line
        int decisive_y3 = (int)(((double)info[10]/(double)info[9])*d_masu);  //Y-intercept of the third input straight line
        
        //Precautions when inputting
        g.drawString("* Be sure to enter a positive integer in all input fields.",120,640);
        
        
        g.drawLine(100,275,100,600);    //y axis
        g.drawLine(100,600,425,600);    //x axis
        
        g.drawLine(95,300,105,300);     //y-axis scale(First from the top)
        g.drawLine(95,400,105,400);     //y-axis scale(Second from the top)
        g.drawLine(95,500,105,500);     //y-axis scale(Third from the top)
        
        g.drawLine(200,595,200,605);    //x-axis scale(First from the left)
        g.drawLine(300,595,300,605);    //x-axis scale(Second from the left)
        g.drawLine(400,595,400,605);    //x-axis scale(Third from the left)
        
        //y-axis arrow
        g.drawLine(100,275,95,280);
        g.drawLine(100,275,105,280);
        //x-axis arrow
        g.drawLine(425,600,420,595);
        g.drawLine(425,600,420,605);
        
        //Draw a graph of the three input straight lines
        g.drawLine(decisive_x1+100,600,100,600-decisive_y1);
        g.drawLine(decisive_x2+100,600,100,600-decisive_y2);
        g.drawLine(decisive_x3+100,600,100,600-decisive_y3);
        
        //Calculate the value of each scale
        int n_xy1 = xy_max/30;
        int n_xy2 = (xy_max/30)*2;
        int n_xy3 = xy_max/10;
        
        //Convert the numerical value of each scale to a character string(To count the number of digits in each scale)
        String s_xy1 = String.valueOf(n_xy1);
        String s_xy2 = String.valueOf(n_xy2);
        String s_xy3 = String.valueOf(n_xy3);
        
        //Get the number of digits of each scale and substitute(To prevent the position of the numerical value on the scale from changing depending on the number of digits)
        int xy1_length = String.valueOf(n_xy1).length();
        int xy2_length = String.valueOf(n_xy2).length();
        int xy3_length = String.valueOf(n_xy3).length();
        
        //When bottan1 is entered(Write the value of each scale to the graph)
        if(scale_judge == 1){
            g.drawString(s_xy1, 200-(xy1_length*3), 620);
            g.drawString(s_xy2, 300-(xy2_length*3), 620);
            g.drawString(s_xy3, 400-(xy3_length*3), 620);
            g.drawString(s_xy1, 90-(xy1_length*6), 505);
            g.drawString(s_xy2, 90-(xy2_length*6), 405);
            g.drawString(s_xy3, 90-(xy3_length*6), 305);
        }
        
        //Find the minimum value of x-intercept and y-intercept. Also, the number of the straight line with the minimum value(What number was entered)Seeking
        x_min = decisive_x1;
        n_x_min = 1;
        y_min = decisive_y1;
        n_y_min = 1;
        
        if(x_min > decisive_x2){
            x_min = decisive_x2;
            n_x_min = 2;
        }
        if(x_min > decisive_x3){
            x_min = decisive_x3;
            n_x_min = 3;
        }
        
        
        if(y_min > decisive_y2){
            y_min = decisive_y2;
            n_y_min = 2;
        }
        if(y_min > decisive_y3){
            y_min = decisive_y3;
            n_y_min = 3;
        }
        
        //Each straight line(Straight line due to constraints)Upper limit value of(Constant term)To a value in graph units and substitute
        int upper1 = (int)(((double)info[4])*d_masu);
        int upper2 = (int)(((double)info[7])*d_masu);
        int upper3 = (int)(((double)info[10])*d_masu);
        
        
        //Find the x-coordinate of the intersection of each straight line
        x_intersection12 = (int)((((double)info[7]/(double)info[6])-((double)info[4]/(double)info[3]))/(((double)info[5]/(double)info[6])-((double)info[2]/(double)info[3]))*d_masu);
        x_intersection13 = (int)((((double)info[10]/(double)info[9])-((double)info[4]/(double)info[3]))/(((double)info[8]/(double)info[9])-((double)info[2]/(double)info[3]))*d_masu);
        x_intersection23 = (int)((((double)info[10]/(double)info[9])-((double)info[7]/(double)info[6]))/(((double)info[8]/(double)info[9])-((double)info[5]/(double)info[6]))*d_masu);
        
        //Find the y coordinate of the intersection of each straight line
        y_intersection12 = (int)((((double)info[7]/(double)info[5])-((double)info[4]/(double)info[2]))/(((double)info[6]/(double)info[5])-((double)info[3]/(double)info[2]))*d_masu);
        y_intersection13 = (int)((((double)info[10]/(double)info[8])-((double)info[4]/(double)info[2]))/(((double)info[9]/(double)info[8])-((double)info[3]/(double)info[2]))*d_masu);
        y_intersection23 = (int)((((double)info[10]/(double)info[8])-((double)info[7]/(double)info[5]))/(((double)info[9]/(double)info[8])-((double)info[6]/(double)info[5]))*d_masu);
        
        //If there is no intersection, the coordinates(-1,-1)Set to.(-1,-1)Is out of the feasible region
        //When the straight lines 1 and 2 are parallel
        if(x_intersection12>2000000000 || y_intersection12>2000000000){
            x_intersection12 = -1;
            y_intersection12 = -1;
        }
        //When the straight lines 1 and 3 are parallel
        if(x_intersection13>2000000000 || y_intersection13>2000000000){
            x_intersection13 = -1;
            y_intersection13 = -1;
        }
        //When the straight lines of 2 and 3 are parallel
        if(x_intersection23>2000000000 || y_intersection23>2000000000){
            x_intersection23 = -1;
            y_intersection23 = -1;
        }
        
        //When the intersection of 1 and 2 is outside the feasible region
        if(info[8]*x_intersection12+info[9]*y_intersection12 > upper3){
            x_intersection12 = -1;
            y_intersection12 = -1;
        }
        //When the intersection of 1 and 3 is outside the feasible region
        if(info[5]*x_intersection13+info[6]*y_intersection13 > upper2){
            x_intersection13 = -1;
            y_intersection13 = -1;
        }
        //When the intersection of 2 and 3 is outside the feasible region
        if(info[2]*x_intersection23+info[3]*y_intersection23 > upper1){
            x_intersection23 = -1;
            y_intersection23 = -1;
        }
        
        
        int answer_x = info[0]*x_min;    //x intercept(minimum value)Solution in
        int answer_y = info[1]*y_min;    //x intercept(minimum value)Solution in
        
        //Solution at the intersection of each straight line(The initial value is-1)
        int answer12 = -1;
        int answer13 = -1;
        int answer23 = -1;
        
        //If the solution at the intersection of each straight line is in the feasible region, assign that solution to each variable.
        if(x_intersection12>0 && y_intersection12>0){
            answer12 = info[0]*x_intersection12+info[1]*y_intersection12;
        }
        if(x_intersection13>0 && y_intersection13>0){
            answer13 = info[0]*x_intersection13+info[1]*y_intersection13;
        }
        if(x_intersection23>0 && y_intersection23>0){
            answer23 = info[0]*x_intersection23+info[1]*y_intersection23;
        }
        
        //The maximum solution from the solutions obtained above(Optimal solution)Look for and answer_Substitute for max
        int answer_max = answer_x;
        if(answer_max < answer_y){
            answer_max = answer_y;
        }
        if(answer_max < answer12){
            answer_max = answer12;
        }
        if(answer_max < answer13){
            answer_max = answer13;
        }
        if(answer_max < answer23){
            answer_max = answer23;
        }
        
        //When the button to start the simulation is clicked
        if(simu_judge == 1){
            
            g.setColor(Color.red);          //Set the color of the circle to draw to red
            
            g.fillOval(x-4, y-4, 8, 8);     //Draw a circle((x-4,y-4)Is the center of the circle)
            //step 1(Moving the intercept from the origin)
            if(step == 1){
                //When the coefficient of the y-intercept of the objective function is larger
                if(info[0] <= info[1]){
                    y = y-1;              //Move along the y-axis from the origin
                    //When the point reaches the minimum value of the y-intercept
                    if(y == 600-y_min){
                        //If that point gives the optimal solution
                        if(answer_max == answer_y){
                            step = 4;          //Move on to step 4
                        }
                        //If that point does not give the optimal solution
                        else{
                            step = 2;          //Move to step 2
                        }
                    }
                }
                //When the coefficient of the y-intercept of the objective function is smaller
                else if(info[0] > info[1]){
                    x = x+1;              //Move along the x-axis from the origin
                    //When the point reaches the minimum value of the x-intercept
                    if(x == x_min+100){
                        //If that point gives the optimal solution
                        if(answer_max == answer_x){
                            step = 4;         //Move on to step 4
                        }
                        //If that point does not give the optimal solution
                        else{
                            step = 2;         //Move to step 2
                        }
                    }
                }
            }
            //Step 2(Moving from intercept to intersection)
            else if(step == 2){
                //When the coefficient of the y-intercept of the objective function is larger
                if(info[0] <= info[1]){
                    //Straight line 1,When you reach the intersection of 2(Limited to the range of the feasible region)
                    if(x_intersection12>0 && x>x_intersection12+100){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x-(x - (x_intersection12+100));
                        y = y-(y - (600-y_intersection12));
                        //If that point gives the optimal solution
                        if(answer12 > answer13 && answer12 > answer23){
                            step = 4;          //Move on to step 4
                        }
                        //If that point does not give the optimal solution
                        else{
                            step = 3;       //Move on to step 3
                            select3 = 1;    //Remember straight line 1 as a clue to find the remaining intersections
                        }
                    }
                    //Straight line 1,When you reach the intersection of 3(Limited to the range of the feasible region)
                    else if(x_intersection13>0 && x>x_intersection13+100){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x-(x - (x_intersection13+100));
                        y = y-(y - (600-y_intersection13));
                        //If that point gives the optimal solution
                        if(answer13 > answer12 && answer13 > answer23){
                            step=4;          //Move on to step 4
                        }
                        //If that point does not give the optimal solution
                        else{
                            step = 3;       //Move on to step 3
                            select3 = 2;    //Remember straight line 2 as a clue to find the remaining intersections
                        }
                    }
                    //Straight line 2,When you reach the intersection of 3(Limited to the range of the feasible region)
                    else if(x_intersection23>0 && x>x_intersection23+100){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x-(x - (x_intersection23+100));
                        y = y-(y - (600-y_intersection23));
                        //If that point gives the optimal solution
                        if(answer23 > answer12 && answer23 > answer13){
                            step=4;          //Move on to step 4
                        }
                        //If that point does not give the optimal solution
                        else{
                            step = 3;       //Move on to step 3
                            select3 = 3;    //Remember straight line 3 as a clue to find the remaining intersections
                        }
                    }
                    //When the optimal solution is the x-intercept and the point is reached
                    else if(answer_max == answer_x && y>600){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x-(x - (x_min+100));
                        y = y-(y - 600);
                        step = 4;          //Move on to step 4
                    }
                    
                    //These three branches give the coordinate displacement corresponding to each straight line.(To move on that straight line)
                    else if(n_y_min == 1){
                        x = x+info[3];
                        y = y+info[2];
                    }
                    else if(n_y_min == 2){
                        x = x+info[6];
                        y = y+info[5];
                    }
                    else if(n_y_min == 3){
                        x = x+info[9];
                        y = y+info[8];
                    }
                }
                //When the coefficient of the y-intercept of the objective function is smaller
                else if(info[0] > info[1]){
                    //Straight line 1,When you reach the intersection of 2(Limited to the range of the feasible region)
                    if(y_intersection12>0 && y<600-y_intersection12){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x + ((x_intersection12+100)-x);
                        y = y + ((600-y_intersection12)-y);
                        //If that point gives the optimal solution
                        if(answer12 > answer13 && answer12 > answer23){
                            step=4;          //Move on to step 4
                        }
                        //If that point does not give the optimal solution
                        else{
                            step=3;         //Move on to step 3
                            select3 = 1;    //Remember straight line 1 as a clue to find the remaining intersections
                        }
                    }
                    //Straight line 1,When you reach the intersection of 3(Limited to the range of the feasible region)
                    else if(y_intersection13>0 && y<600-y_intersection13){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x + ((x_intersection13+100)-x);
                        y = y + ((600-y_intersection13)-y);
                        //If that point gives the optimal solution
                        if(answer13 > answer12 && answer13 > answer23){
                            step=4;          //Move on to step 4
                        }
                        //If that point does not give the optimal solution
                        else{
                            step=3;         //Move on to step 3
                            select3 = 2;    //Remember straight line 2 as a clue to find the remaining intersections
                        }
                    }
                    //Straight line 2,When you reach the intersection of 3(Limited to the range of the feasible region)
                    else if(y_intersection23>0 && y<600-y_intersection23){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x + ((x_intersection23+100)-x);
                        y = y + ((600-y_intersection23)-y);
                        //If that point gives the optimal solution
                        if(answer23 > answer12 && answer23 > answer13){
                            step=4;          //Move on to step 4
                        }
                        //If that point does not give the optimal solution
                        else{
                            step=3;         //Move on to step 3
                            select3 = 3;    //Remember straight line 3 as a clue to find the remaining intersections
                        }
                    }
                    //When the optimal solution is the y-intercept and the point is reached
                    else if(answer_max == answer_y && x<100){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x + (100-x);
                        y = y + ((600-y_min)-y);
                        step = 4;          //Move on to step 4
                    }
                    
                    //These three branches give the coordinate displacement corresponding to each straight line.(To move on that straight line)
                    else if(n_x_min == 1){
                        x = x-info[3];
                        y = y-info[2];
                    }
                    else if(n_x_min == 2){
                        x = x-info[6];
                        y = y-info[5];
                    }
                    else if(n_x_min == 3){
                        x = x-info[9];
                        y = y-info[8];
                    }
                }
            }
            //Step 3(Moving from intersection to intersection)
            else if(step == 3){
                //When the coefficient of the y-intercept of the objective function is larger
                if(info[0] <= info[1]){
                    //Straight line 1 which is the remaining intersection,When the intersection of 3 is reached and that point is the execution solution
                    if(select3 == 1 && x_intersection13>0 && x>x_intersection13+100){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x-(x - (x_intersection13+100));
                        y = y-(y - (600-y_intersection13));
                        step=4;          //Move on to step 4
                    }
                    //Straight line 2 which is the remaining intersection,When the intersection of 3 is reached and that point is the execution solution
                    else if(select3 == 1 && x_intersection23>0 && x>x_intersection23+100){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x-(x - (x_intersection23+100));
                        y = y-(y - (600-y_intersection23));
                        step=4;          //Move on to step 4
                    }
                    //Straight line 1 which is the remaining intersection,When the intersection of 2 is reached and that point is the execution solution
                    else if(select3 == 2 && x_intersection12>0 && x>x_intersection12+100){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x-(x - (x_intersection12+100));
                        y = y-(y - (600-y_intersection12));
                        step=4;          //Move on to step 4
                    }
                    //Straight line 2 which is the remaining intersection,When the intersection of 3 is reached and that point is the execution solution
                    else if(select3 == 2 && x_intersection23>0 && x>x_intersection23+100){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x-(x - (x_intersection23+100));
                        y = y-(y - (600-y_intersection23));
                        step=4;          //Move on to step 4
                    }
                    //Straight line 1 which is the remaining intersection,When the intersection of 2 is reached and that point is the execution solution
                    else if(select3 == 3 && x_intersection12>0 && x>x_intersection12+100){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x-(x - (x_intersection12+100));
                        y = y-(y - (600-y_intersection12));
                        step=4;          //Move on to step 4
                    }
                    //Straight line 1 which is the remaining intersection,When the intersection of 3 is reached and that point is the execution solution
                    else if(select3 == 3 && x_intersection13>0 && x>x_intersection13+100){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x-(x - (x_intersection13+100));
                        y = y-(y - (600-y_intersection13));
                        step=4;          //Move on to step 4
                    }
                    
                    //These three branches give the coordinate displacement corresponding to each straight line.(To move on that straight line)
                    else if(n_y_min == 1){
                        if(n_x_min == 2){
                            x = x+info[9];
                            y = y+info[8];
                        }
                        else if(n_x_min == 3){
                            x = x+info[6];
                            y = y+info[5];
                        }
                    }
                    else if(n_y_min == 2){
                        if(n_x_min == 1){
                            x = x+info[9];
                            y = y+info[8];
                        }
                        else if(n_x_min == 3){
                            x = x+info[3];
                            y = y+info[2];
                        }
                    }
                    else if(n_y_min == 3){
                        if(n_x_min == 1){
                            x = x+info[6];
                            y = y+info[5];
                        }
                        else if(n_x_min == 2){
                            x = x+info[3];
                            y = y+info[2];
                        }
                    }
                }
                //When the coefficient of the y-intercept of the objective function is smaller
                else if(info[0] > info[1]){
                    if(select3 == 1 && y_intersection13>0 && y<600-y_intersection13){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x + ((x_intersection13+100)-x);
                        y = y + ((600-y_intersection13)-y);
                        step=4;          //Move on to step 4
                    }
                    else if(select3 == 1 && y_intersection23>0 && y<600-y_intersection23){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x + ((x_intersection23+100)-x);
                        y = y + ((600-y_intersection23)-y);
                        step=4;          //Move on to step 4
                    }
                    else if(select3 == 2 && y_intersection12>0 && y<600-y_intersection12){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x + ((x_intersection12+100)-x);
                        y = y + ((600-y_intersection12)-y);
                        step=4;          //Move on to step 4
                    }
                    else if(select3 == 2 && y_intersection23>0 && y<600-y_intersection23){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x + ((x_intersection23+100)-x);
                        y = y + ((600-y_intersection23)-y);
                        step=4;          //Move on to step 4
                    }
                    else if(select3 == 3 && y_intersection12>0 && y<600-y_intersection12){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x + ((x_intersection12+100)-x);
                        y = y + ((600-y_intersection12)-y);
                        step=4;          //Move on to step 4
                    }
                    else if(select3 == 3 && y_intersection13>0 && y<600-y_intersection13){
                        //When it reaches, correct it so that the point is exactly the center coordinates of the circle.
                        x = x + ((x_intersection13+100)-x);
                        y = y + ((600-y_intersection13)-y);
                        step=4;          //Move on to step 4
                    }
                    
                    //These three branches give the coordinate displacement corresponding to each straight line.(To move on that straight line)
                    else if(n_x_min == 1){
                        if(n_y_min == 2){
                            x = x-info[9];
                            y = y-info[8];
                        }
                        else if(n_y_min == 3){
                            x = x-info[6];
                            y = y-info[5];
                        }
                    }
                    else if(n_x_min == 2){
                        if(n_y_min == 1){
                            x = x-info[9];
                            y = y-info[8];
                        }
                        else if(n_y_min == 3){
                            x = x-info[3];
                            y = y-info[2];
                        }
                    }
                    else if(n_x_min == 3){
                        if(n_x_min == 1){
                            x = x-info[6];
                            y = y-info[5];
                        }
                        else if(n_x_min == 2){
                            x = x-info[3];
                            y = y-info[2];
                        }
                    }
                }
            }
            //Step 4(point(Circle)Stops moving and the optimum solution is found. Display the optimal solution)
            else if(step == 4){
                //Set the color of the optimum solution value to be displayed to black.
                g.setColor(Color.black);
                
                //Set the font size to make it easier to see
                Font fo = new Font("Serif",Font.PLAIN,20);
                g.setFont(fo);
                
                //Convert the optimal solution in graph units to problem units
                double best_answer = ((double)answer_max/d_masu);
                //Convert the value of the optimal solution to a string
                String ba = String.valueOf(best_answer);
                
                //Display the optimal solution
                g.drawString("The optimal solution is approximate",450,325);
                g.drawString(String.format("%.2f", best_answer), 475, 350);
            }
            
        }
        
        
        
        
    }
    
    //Control the thread execution interval to move the circle
    public void run() {
        while(true) {
            repaint();
            try {
                Thread.sleep(300);       //Stop the thread for 100ms
            }catch(Exception e) {
                System.out.println(e);
            }
        }
    }
    
    public void actionPerformed(ActionEvent ae){
        //When button1 is clicked
        if(ae.getSource() == button1){
            //Store the value entered from the text field in the array info
            info[0] = Integer.parseInt(o_x.getText());
            info[1] = Integer.parseInt(o_y.getText());
            info[2] = Integer.parseInt(s_x1.getText());
            info[3] = Integer.parseInt(s_y1.getText());
            info[4] = Integer.parseInt(s_n1.getText());
            info[5] = Integer.parseInt(s_x2.getText());
            info[6] = Integer.parseInt(s_y2.getText());
            info[7] = Integer.parseInt(s_n2.getText());
            info[8] = Integer.parseInt(s_x3.getText());
            info[9] = Integer.parseInt(s_y3.getText());
            info[10] = Integer.parseInt(s_n3.getText());	
            scale_judge = 1;     //scale_judge=Set to 1 and write 3 straight lines of constraints on the graph
        }
        //When button2 is clicked
        else if(ae.getSource() == button2){
            //Set the coordinates of the circle to the origin of the graph
            x = 100;
            y = 600;
            
            step = 1;          //Set the simulation step to 1
            simu_judge = 1;    //simu_Set judge to 1 and start the simulation
        }
    }
}

6. Image

image.png

Recommended Posts

Simulate the simplex method with GUI
About the method
Come out with a suffix on the method
Come out with a suffix on the method 2
[Ruby] Exclude duplicate elements with the uniq method.
Finding pi with the Monte Carlo method? (Ruby)
About the mechanism when displaying the GUI with Docker
Output about the method # 2
About the length method
About the authenticate method.
About the map method
About the ancestors method
About the to_s method.
I immediately stumbled on the standard input method with AtCoder.
Note the StringUtils # containsAny method
What is the pluck method?
Change the port with SpringBoot
Output about the method Part 1
about the where method (rails)
Integer check method with ruby
Understand the helper method form_with
Various method tests with MockRestServiceServer
Mock the constructor with PowerMock
Consideration about the times method
Conditional branch with helper method
With the error Cordova-> Looper.prepare ()
Monitor the clipboard with JNA
[ruby] Method call with argument
What is the initialize method?
Connecting to a database with Java (Part 1) Maybe the basic method