Question

hey I need help finishing this simplex program. public class Main {       /**       *...

hey

I need help finishing this simplex program.

public class Main {

      /**

      * @param args

      */

      public static void main(String[] args) {

            // TODO Auto-generated method stub

            //FAIRE LES TODO dans Simplex

            test1();test2();

           

      }

      private static void test1() {

            double[][] A = {

                        { -1, 1, 0 },

                        { 1, 4, 0 },

                        { 2, 1, 0 },

                        { 3, -4, 0 },

                        { 0, 0, 1 },

            };

            double[] c = { 1, 1, 1 };

            double[] b = { 5, 45, 27, 24, 4 };

            Simplex ss = new Simplex(A,b,c);

      }

      // x0 = 12, x1 = 28, opt = 800

      private static void test2() {

            double[] c = { 13.0, 23.0 };

            double[] b = { 480.0, 160.0, 1190.0 };

            double[][] A = {

                        { 5.0, 15.0 },

                        { 4.0, 4.0 },

                        { 35.0, 20.0 },

            };

            Simplex ss = new Simplex(A,b,c);

      }

}

public class Simplex {

    private static final double EPSILON = 1.0E-10;

    private double[][] a;   // tableaux

    private int m;          // number of constraints

    private int n;          // number of original variables

    private int[] basis;    // basis[i] = basic variable corresponding to row i

                            // only needed to print out solution, not book

    /**

     * Determines an optimal solution to the linear program

     * { max cx : Ax ; b, x ; 0 }, where A is a m-by-n

     * matrix, b is an m-length vector, and c is an n-length vector.

     *

     * @param A matrix

     * @param b ressource vector

     * @param c cost vector

     * @throws IllegalArgumentException unless {@code b[i] >= 0} for each {@code i}

     * @throws ArithmeticException if the linear program is unbounded

     */

    public Simplex(double[][] A, double[] b, double[] c) {

        m = b.length;

        n = c.length;

        for (int i = 0; i < m; i++)

            if (!(b[i] >= 0)) throw new IllegalArgumentException("ressources must be nonnegative");

        a = new double[m+1][n+m+1];

        for (int i = 0; i < m; i++)

            for (int j = 0; j < n; j++)

                a[i][j] = A[i][j];

        for (int i = 0; i < m; i++)

            a[i][n+i] = 1.0;

        for (int j = 0; j < n; j++)

            a[m][j] = c[j];

        for (int i = 0; i < m; i++)

            a[i][m+n] = b[i];

        basis = new int[m];

        for (int i = 0; i < m; i++)

            basis[i] = n + i;

        solve();

        show();

    }

    // run simplex algorithm starting from initial BFS

    private void solve() {

        while (true) {

            // find entering column q

            int q = dantzig();

            if (q == -1) break; // optimal

            // find leaving row p

            int p = minRatioRule(q);

            if (p == -1) throw new ArithmeticException("Linear program is unbounded");

            // pivot

            pivot(p, q);

            // update basis

            basis[p] = q;

        }

    }

    /**

     * Trouver l'index de la variable entrante

     * @return l'index de la variable entrante

     */

    private int dantzig() {

       //TODO

    return -1;

    }

    /**

     * Trouver l'index de la variable sortante

     * @param q

     * @return

     */

    private int minRatioRule(int q) {

      

    //TODO

    return -1;

    }

    //

    /**

     * Pivot sur le point (p, q) utilisant l'élimination de Gauss-Jordan (comme vue en classes)

     * @param p index de la variable entrante

     * @param q index de la variable sortante

     */

    private void pivot(int p, int q) {

        //TODO

    }

    /**

     * Returns the optimal value of this linear program.

     *

     * @return the optimal value of this linear program

     *

     */

    public double value() {

        return -a[m][m+n];

    }

    /**

     * Returns the optimal primal solution to this linear program.

     *

     * @return the optimal primal solution to this linear program

     */

    public double[] primal() {

        double[] x = new double[n];

        for (int i = 0; i < m; i++)

            if (basis[i] < n) x[basis[i]] = a[i][m+n];

        return x;

    }

    // print tableaux

    private void show() {

        System.out.println("m = " + m);

        System.out.println("n = " + n);

        for (int i = 0; i <= m; i++) {

            for (int j = 0; j <= m+n; j++) {

                  System.out.printf("%7.2f ", a[i][j]);

                // StdOut.printf("%10.7f ", a[i][j]);

            }

            System.out.println();

        }

        System.out.println("value = " + value());

        for (int i = 0; i < m; i++)

            if (basis[i] < n) System.out.println("x_" + basis[i] + " = " + a[i][m+n]);

        System.out.println();

    }

}


0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Main {

      /**

      * @param args

      */

      public static void main(String[] args) {

            // TODO Auto-generated method stub

            //FAIRE LES TODO dans Simplex

            test1();test2();

         

      }

      private static void test1() {

            double[][] A = {

                        { -1, 1, 0 },

                        { 1, 4, 0 },

                        { 2, 1, 0 },

                        { 3, -4, 0 },

                        { 0, 0, 1 },

            };

            double[] c = { 1, 1, 1 };

            double[] b = { 5, 45, 27, 24, 4 };

            Simplex ss = new Simplex(A,b,c);

      }

      // x0 = 12, x1 = 28, opt = 800

      private static void test2() {

            double[] c = { 13.0, 23.0 };

            double[] b = { 480.0, 160.0, 1190.0 };

            double[][] A = {

                        { 5.0, 15.0 },

                        { 4.0, 4.0 },

                        { 35.0, 20.0 },

            };

            Simplex ss = new Simplex(A,b,c);

      }

}

class Simplex {

    private static final double EPSILON = 1.0E-10;

    private double[][] a;   // tableaux

    private int m;          // number of constraints

    private int n;          // number of original variables

    private int[] basis;    // basis[i] = basic variable corresponding to row i

                            // only needed to print out solution, not book

    /**

     * Determines an optimal solution to the linear program

     * { max cx : Ax ; b, x ; 0 }, where A is a m-by-n

     * matrix, b is an m-length vector, and c is an n-length vector.

     *

     * @param A matrix

     * @param b ressource vector

     * @param c cost vector

     * @throws IllegalArgumentException unless {@code b[i] >= 0} for each {@code i}

     * @throws ArithmeticException if the linear program is unbounded

     */

    public Simplex(double[][] A, double[] b, double[] c) {

        m = b.length;

        n = c.length;

        for (int i = 0; i < m; i++)

            if (!(b[i] >= 0)) throw new IllegalArgumentException("ressources must be nonnegative");

        a = new double[m+1][n+m+1];

        for (int i = 0; i < m; i++)

            for (int j = 0; j < n; j++)

                a[i][j] = A[i][j];

        for (int i = 0; i < m; i++)

            a[i][n+i] = 1.0;

        for (int j = 0; j < n; j++)

            a[m][j] = c[j];

        for (int i = 0; i < m; i++)

            a[i][m+n] = b[i];

        basis = new int[m];

        for (int i = 0; i < m; i++)

            basis[i] = n + i;

        solve();

        show();

    }

    // run simplex algorithm starting from initial BFS

    private void solve() {

        while (true) {

            // find entering column q

            int q = dantzig();

            if (q == -1) break; // optimal

            // find leaving row p

            int p = minRatioRule(q);

            if (p == -1) throw new ArithmeticException("Linear program is unbounded");

            // pivot

            pivot(p, q);

            // update basis

            basis[p] = q;

        }

    }

    /**

     * Trouver l'index de la variable entrante

     * @return l'index de la variable entrante

     */

    private int dantzig() {

       //TODO

    return -1;

    }

    /**

     * Trouver l'index de la variable sortante

     * @param q

     * @return

     */

    private int minRatioRule(int q) {

    

    //TODO

    return -1;

    }

    //

    /**

     * Pivot sur le point (p, q) utilisant l'élimination de Gauss-Jordan (comme vue en classes)

     * @param p index de la variable entrante

     * @param q index de la variable sortante

     */

    private void pivot(int p, int q) {

        //TODO

    }

    /**

     * Returns the optimal value of this linear program.

     *

     * @return the optimal value of this linear program

     *

     */

    public double value() {

        return -a[m][m+n];

    }

    /**

     * Returns the optimal primal solution to this linear program.

     *

     * @return the optimal primal solution to this linear program

     */

    public double[] primal() {

        double[] x = new double[n];

        for (int i = 0; i < m; i++)

            if (basis[i] < n) x[basis[i]] = a[i][m+n];

        return x;

    }

    // print tableaux

    private void show() {

        System.out.println("m = " + m);

        System.out.println("n = " + n);

        for (int i = 0; i <= m; i++) {

            for (int j = 0; j <= m+n; j++) {

                  System.out.printf("%7.2f ", a[i][j]);

                // StdOut.printf("%10.7f ", a[i][j]);

            }

            System.out.println();

        }

        System.out.println("value = " + value());

        for (int i = 0; i < m; i++)

            if (basis[i] < n) System.out.println("x_" + basis[i] + " = " + a[i][m+n]);

        System.out.println();

    }

}


The above code is the solution.

And the problem with your given code was:-

1)Add a default package to the class

2)There was 2 public classes i.e Main and Simplex.We can not have two public classes in one class file.So make the Simplex class Default.

Sorry for your trouble.But you asked to fix the problem and the only problem was there are two public classes.

Add a comment
Know the answer?
Add Answer to:
hey I need help finishing this simplex program. public class Main {       /**       *...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help with a question in Java: What is the output from the following program? public class...

    Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQ...

    Complete the code below, test cases to use are at the bottom package Labs; public class ResizingQueue { private final int INITIAL_SIZE = 10;   private int [] A = new int[INITIAL_SIZE]; // array to hold queue: front is always at Q[0] // and rear at A[next-1] int next = 0; // location of next available unused slot // interface methods public void enqueue(int key) { //TODO: push the key onto the back of the queue // YOUR CODE HERE! }...

  • this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea {...

    this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);    Triangle triangle1 = new Triangle(); Triangle triangle2 = new Triangle(); // Read and set base and height for triangle1 (use setBase() and setHeight())    // Read and set base and height for triangle2 (use setBase() and setHeight())    // Determine larger triangle (use getArea())    private int base; private int height; private...

  • Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {...

    Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {         int[] array = {7, 1, 3, 2, 42, 76, 9};         insertionSort(array);     }     public static int[] insertionSort(int[] input) {         int temp;         for (int i = 1; i < input.length; i++) {             for (int j = i; j > 0; j--) {                 if (input[j] < input[j - 1]) {                     temp = input[j];                     input[j] = input[j - 1];                     input[j - 1] = temp;                 }             }             display(input, i);...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{       ...

    Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{        File prg8 = new File("program8.txt");        Scanner reader = new Scanner(prg8);        String cName = "";        int cID = 0;        double bill = 0.0;        String email = "";        double nExempt = 0.0;        String tExempt = "";        int x = 0;        int j = 1;        while(reader.hasNextInt()) {            x = reader.nextInt();}        Customers c1 [] = new Customers [x];        for (int...

  • Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void...

    Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void main(String[] args) { int currentVertex, userChoice; Scanner input = new Scanner(System.in); // create graph using your WeightedGraph based on author's Graph WeightedGraph myGraph = new WeightedGraph(4); // add labels myGraph.setLabel(0,"Spot zero"); myGraph.setLabel(1,"Spot one"); myGraph.setLabel(2,"Spot two"); myGraph.setLabel(3,"Spot three"); // Add each edge (this directed Graph has 5 edges, // so we add 5 edges) myGraph.addEdge(0,2,9); myGraph.addEdge(1,0,7); myGraph.addEdge(2,3,12); myGraph.addEdge(3,0,15); myGraph.addEdge(3,1,6); // let's pretend we are on...

  • need help to write the main method as the template import java.util.*; public class oBST {...

    need help to write the main method as the template import java.util.*; public class oBST { static float optimalBST(String words[], float freq[], int n) { //2D dp matrix float dp[][] = new float[n + 1][n + 1]; int root[][] = new int[n+1][n+1]; // For a single word, cost is equal to frequency of the word for (int i = 0; i < n; i++) dp[i][i] = freq[i]; // Now consider for size 2, 3, ... . for (int L =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT