View Single Post
  #2 (permalink)  
Old December 14th, 2013, 09:40 PM
muthukutta1 muthukutta1 is offline
Authorized User
Points: 209, Level: 4
Points: 209, Level: 4 Points: 209, Level: 4 Points: 209, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Java for Linearprogramming

Quote:
Originally Posted by muthukutta1 View Post
Hi

I have been trying to get a prg for the subject item.But i did not get it in the text book referered.Contextually
i request the authors to make the following code executable
******************************************

My main point of request is:-->

How do i reapeat its main function in place of usage(;;) in main

can i use java.util.Ireration; if yes; give demo please


************************************************
Thanks.
As
Muthukutta1

[code=java]

import java.io.*;
import java.text.*;
import java.awt.*;
import java.awt.event.ActionEvent.*;
import java.awt.AWTEvent;
import java.awt.event.WindowEvent;
import java.lang.Math.*;
import java.lang.Character;
import java.lang.Float;
import java.lang.Integer;
import java.lang.Object;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.lang.Boolean;

public class Linear2 extends Frame{


static final int M=15;
static final int N=30;
static double PENALTY =-900.00;
static final double EPS=0.00001;
static int j;
static int i;
static int kc;
static int kr;

static Frame lea=new Frame();
static char res_in_file;
static char yn;
static Linear2 lp;
static int mm;
static int k;
static int m;
static int n;

static float top[];
static float base[];
static float left[];
static char sign[];
static int basic[];

int rows,cols;

static float x[][];

static int kf;
float keyNo;
static float obj;




public static void readData() throws Exception{

String s1;
int i,j;
i=0;
j=0;
DecimalFormat df = new DecimalFormat("0.000000");


BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
s1=br.readLine();
System.out.println("\n Read data from keyboard or(1) or file (2)" );
s1=br.readLine();
System.in.read();
if(kf == 1)
{
System.out.println("\n Store data in discfile yn" );
s1=br.readLine();
System.in.read();

if(yn == 'y' || yn == 'Y')
{
System.out.println("\n Enter file to store data:myFile1 " );
s1=br.readLine();
System.in.read();
lp.writeFile();
}
else
if(yn == 'y' || yn == 'Y')
{
System.out.println("\n Enter file name to read data myFile1: " );
s1=br.readLine();
System.in.read();
lp.readFile();
}
}
System.out.print("\n Do you want to maximise(1) or minimize(2) mm: (1 or 2):");
s1=br.readLine();
System.in.read();

System.out.print("\n Enter no of variables k: " );
s1=br.readLine();
System.in.read();

System.out.print("\n Enter no of Constraints m: ");
s1=br.readLine();
System.in.read();
System.out.print("\n Enter > for >= and < for <=" );
s1=br.readLine();
System.in.read();

System.out.print("\n Enter the ObjectiveFunctions: obj " );
s1=br.readLine();
System.in.read();

System.out.println("\n Enter whether output required infile y/n : " );
s1=br.readLine();
System.in.read();
System.out.println("\n Enter file name to write result::res_in_file: myFile2:" );
s1=br.readLine();
System.in.read();

System.out.println("\n Enter file name to write result() myFile3:" );
s1=br.readLine();
System.in.read();

if(res_in_file == 'y')
{
lp.resultFile();


for(j=0;j<k;j++)
{
if(mm==2)
top[j]=0-top[j];

System.out.print("Enter top[]:");

}
System.out.print("Enter the constraints: ");
System.out.print("Enter > for > and <: ");
{
for(j=0;j<k;j++)
System.out.print(x[i][j]);
System.out.print(sign[i]);
System.out.print(left[i]);
}

}







}
public static void addSlack()
{
int i;
for(i=0;i<m;i++)
{
if(sign[i] == '<'|| sign[i] == '>')
{
n++;
for(int i1=0;i1<m;i1++)
x[i1][n-1]=0;
if(sign[i] == '<')
{
x[i][n-1]=1;
top[n-1]=0;
basic[i]=n-1;
}
else
{
x[i][n-1]=-1;
top[n-1]=0;
}
}
}
}
public static void addArti()
{
int i;
for(i=0;i<n;i++)
if (sign[i]== '='||sign[i]=='>')
{
n++;
for(int i1=0;i1<m;i1++)
x[i][n-1]=0;
x[i][n-1]=-900;
basic[i]=n-1;
}
}
public static void findbase()
{
int i,j;
for(j=0;j<n;j++)
{
base[j]=0;
for(i=0;i<m;i++)
base[j] += x[i][j]*top[basic[i]];
base[j]=base[j]-top[j];
}
}

public static int keyRow(int kc)
{
int i;
float ratio;
float min =100000;
int l=1;
for(i=0;i<m;i++)
{
if(x[i][k] < EPS && left[i] > EPS)
continue;
else
ratio = left[i]/x[i][k];
if(ratio < -EPS)
continue;
else
if(left[i]/x[i][k] < min)
{
min = left[i]/x[i][k];
l=i;
};
}
if (l == -1)
{
System.out.print("\n UnboundedSolution");

}
return(i);
}
public static int keyColumn()
{
int j;
float min = 0;
int l = -1;
for(j=0;j<n;j++)
{
if(base[j] < min)
{
min = base[j];
l =j;
}
}
return(i);
}
public static void display()
{
int i,j;
for(j=0;j<n;j++)
System.out.print("\n top[j]" + " " );
for(i=0;i<m;i++)
{
System.out.print("\n top[basic[i].basic[i]]+1"+ "\nleft[i]"+" "+" ");
for(j=0;j<n;j++)
System.out.print("\n x[i][j] "+ " ");
for(j=0;j<n;j++)
System.out.print("\n basic[j]"+" ");
}
}
public static void modify(int kr,int kc) throws Exception
{

// final int M=15;

int i;
int j;
float keyNo;
float temp[] = new float[M];

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

temp[i] = x[i][kr];
basic[kr]=kc;
keyNo=x[kr][kc];
for(j=0;j<n;j++)
x[kr][j]=x[kr][j]/keyNo;
left[kr]=left[kr]/keyNo;
for(i=0;i<m;i++)
{
if(i== kr)
continue;
left[i]=left[i]-left[kr]*top[i];
}
for(i=0;i<m;i++)
{
if(i==kr)
continue;
else
for(j=0;j<n;j++)
x[i][j]=x[i][j]-temp[i]*x[kr][j];
}
//float obj=0;
for (i=0;i<m;i++)
obj=obj+top[basic[i]]*left[i];
return;
}
public void result()throws Exception
{
int i;
int feasible =1;
{
RandomAccessFile raf3 = new RandomAccessFile("myFile3.txt","rw");
raf3.seek(raf3.length());

for(i=0;i<m;i++)
if(top[basic[i]] <(PENALTY +0.01)&& left[i]> EPS)
{
if(true);
break;
}
if(false)
{
for(i=0;i<m;i++)
System.out.print("\n basic[i]+1,left[i]"+" " +" ");
System.out.print("\nOptialValue of Objective function " +obj);
if(res_in_file == 'y')
{
raf3.writeInt(i);
for(i=0;i<m;i++){
obj=basic[i]+1;
obj=left[i];
}
}
else{
System.out.print("Solution is unfeasible");
if(res_in_file== 'y')
raf3.writeUTF("Solution not feasible");
}
}
}
}
public static void readFile() throws Exception
{
int i,j;
try
{


RandomAccessFile raf2= new RandomAccessFile("myFile2.txt","rw");
raf2.getFilePointer();
raf2.length();

while(raf2.getFilePointer()<raf2.length())

{
int mm=raf2.readInt();
k=raf2.readInt();
m=raf2.readInt();
n=k;
for(j=0;j<k;j++)
{
top[j]= j;
if(mm==2)
top[j]=0-top[j];
}
for(i=0;i<m;i++)
{
for(j=0;j<k;j++)
top[j] =raf2.readInt();
do{
sign[i]=raf2.readChar();

left[i]=raf2.readInt();
}while(true);

}
}
}
catch(Exception e)
{
}

}



public static void writeFile()
{

int i,j;

try
{
RandomAccessFile raf1= new RandomAccessFile("myFile1.txt","rw");
raf1.seek(raf1.length());
while(raf1.getFilePointer()<raf1.length()){


raf1.writeInt(mm);
raf1.writeInt(k);
raf1.writeInt(m);
n=k;
if(mm==1){

for(j=0;j<k;j++)
top[j]=j;
for (j=0;j<top.length;j++)
{

System.out.write(j);
}
}
else
for(i=0;i<m;i++)
for (j=0;j<k;j++)


x[i][j]=i;


for(i=0;i<x.length;i++){

System.out.write(i);
}
for(i=0;i<sign.length;i++)

{
System.out.write(i);

}

left[i]=i;
for(i=0;i<left.length;i++)
{
System.out.write(i);

}

}
raf1.close();

}
catch(Exception e)
{}

}



public static void resultFile()
{
int i,j;

try
{
RandomAccessFile raf2 = new RandomAccessFile("myFile2.txt","rw");
raf2.seek(raf2.length());
while(raf2.getFilePointer()<raf2.length())

for(j=0;j<n;j++)
top[j]=j;
for(j=0;j<top.length;j++){
System.out.write(j);
}

for(j=0;j<n;j++)
for(i=0;i<m;i++)
{
top[basic[i]] = i+1;

for(i=0;i<top.length;i++)
for(i=0;i<basic.length;i++)

{
System.out.write(i);

}

for(j=0;j<n;j++)
x[i][j]=i;
for(i=0;i<x.length;i++)
for(j=0;j<x.length;j++)
{

System.out.write(i);
}
for(j=0;j<n;j++)
//for(j=0;j<n;j++)
obj=j;
{
System.out.write(j);
}
for(j=0;j<n;j++)
base[j]=j;
{
System.out.write(j);
}
}
}
catch(Exception e)
{};
}



public static void main(String args[]) throws Exception
{
Linear2 lp = new Linear2();
try
{

lp.readData();

RandomAccessFile raf1= new RandomAccessFile("myFile1.txt","rw");

lp.addSlack();
lp.addArti();
lp.findbase();
lp.display();

if(res_in_file =='y')

lp.resultFile();







i=1;

for(;;)
{
if (kc == -1) break;
System.out.println("\n Liner2 Iteration :" + i);
{
System.out.println("\n *********************\n");

kc= lp.keyColumn();
System.out.println("kc" + kc);
keyRow(kc);
System.out.println("kr" + kr);
try
{
lp.modify( kc , kr );
lp.findbase();
lp.display();

if(res_in_file =='y')

lp.resultFile();
i++;
RandomAccessFile raf2= new RandomAccessFile("myFile2.txt","rw");


lp.result();
RandomAccessFile raf3 = new RandomAccessFile("myFile3.txt","rw");
}

catch(Exception e)
{
}
return;
}
}
}
catch(Exception e)
{
}
}
}
[/code]
Hi
As there is some more trys available the above code has been modified to get results.
However you may fine tune the same to go advanced
Code:
//package filereader;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public  class Iteration{


static int mm;
static int k;
static int m;
int n;
float top[];
float base[];
static float left[];
char sign[];
static int basic[];
static float x[][];
float augmented[][];
float keyno;
static float obj;
static int kc,kr=0;
static int kf=1;
static char yn=1;

 int PENALTY= -900;
 double  EPS =0.000001;
static Iteration lp;//=new
static char fdata[];
static char fres[];
static char res_in_file;
void  readdata()throws java.io.IOException
 {
  int i,j;
  int n;
  String s1;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
  System.out.print( "Read data from keyboard or(1) or file (2)");
  s1=br.readLine();
    System.in.read();

 System.out.print( " Do you want to maximise (1) or minimise (2) ") ;
 s1=br.readLine();
   System.in.read();
System.out.print("  No of variables                        k   :");
s1=br.readLine();
System.in.read();


  System.out.print( " No of constraints                      m   :");
  s1=br.readLine();
   System.in.read();

 n=k;
System.out.print( " Enter the ojective function               obj  : ");
 s1=br.readLine();
   System.in.read();
 for(j=0;j<k;j++)
{
System.out.print("top[j]"+top[j]);
 s1=br.readLine();
   System.in.read();

}
if(mm==2)
{
System.out.print("top[j]=0-top[j]" + " ");
 s1=br.readLine();
   System.in.read();

}

System.out.print( "Enter > for >= and < for <="  );
 s1=br.readLine();
    System.in.read();

{
 for (i =0; i < m; i++)
 {
 for ( j=0; j < k ; j++)
  System.out.println("x[i][j]"+x[i][j]);
   s1=br.readLine();
   System.in.read();
 augmented[i][j]=Float.parseFloat(s1);
System.out.println("sign[i]"+sign[i]);
   s1=br.readLine();
   System.in.read();
  System.out.println("left[i]"+left[i]);
   s1=br.readLine();
   System.in.read();

 }
}


if(kf == 1){

System.out.print( "store data in disc file y/n");
s1=br.readLine();
   System.in.read();
if(yn == 'y' || yn == 'Y')
{
System.out.print( "Name of file to store data    ");
s1=br.readLine();
   System.in.read();
lp.writefile();
}
}
else
{

System.out.print("file name to read data(file11.txt)");
	s1=br.readLine();
	System.in.read();
 lp.readfile();


}

System.out.print("Output required in file y/n");
s1=br.readLine();
   System.in.read();
System.out.print("Name of the file for result /res_in_file is ok:y or n");
 s1=br.readLine();
   System.in.read();

if(res_in_file =='y' || res_in_file == 'Y')
{
System.out.print(" File name to write result result");
 s1=br.readLine();
   System.in.read();
  RandomAccessFile raf=new RandomAccessFile("fileCRMKtxt", "w");

}


}


void  readfile()throws java.io.IOException
{

String fileName = "C:/jdk1.5.0_03/bin/bin/fileCRMK.txt";

 int mm;
 int k;
int m;
float x;
int n;



      // set up the basic random access input/output stream
    RandomAccessFile raf =
        new RandomAccessFile(fileName, "rw");

      // print the headings
    System.out.println("          Details");
    System.out.println("k           m         obj      x           ");
    System.out.println("-------   --------   ------   -------       ");

      // read and print the data

      k = raf.readInt();
      m = raf.readInt();
      obj = raf.readInt();

       x = raf.readFloat();

    // System.out.println(  k ,   m , obj , x[i][j]);


    raf.close();

      //System.exit(0);
  }

   void  writefile()throws java.io.IOException
  {
       String fileName = "fileCRMK.txt";


           //int mm;
            //int k;
           //int m;
           // int n;
            int j=3;

	         // set up the keyboard for string input
	       InputStreamReader isr =
	   	new InputStreamReader(System.in);
	       BufferedReader br =
	   	new BufferedReader(isr);

	         // set up the basic random access input/output stream
	       RandomAccessFile raf =
	           new RandomAccessFile(fileName, "rw");

	         // enter and store data for five records
	       for(int i = 1; i <= 1; i++)
	       {

	         raf.writeInt(k);


	         raf.writeInt(m);


	         	         raf.writeDouble(obj);


	   	       // raf.writeFloat(x[i][j]);






	       }

	       System.out.println("The file " + fileName + " has been written.");
	       System.out.println("The length of the file is "
	                         + raf.length() + " bytes.");
    raf.close();


	    }





void  addslack()
{
int i;
for(i=0;i<m;i++)
{
if(sign[i] == '<'|| sign[i] == '>')
{
  n++;
for(int i1=0;i1<m;i1++)
  x[i1][n-1]=0;
if(sign[i] == '<')
{
x[i][n-1]=1;
top[n-1]=0;
basic[i]=n-1;
}
else
{
x[i][n-1]=-1;
top[n-1]=0;
}
}
}
}
void  addarti()
{
 int i;
for(i=0;i<n;i++)
if (sign[i] == '='|| sign[i] == '>')
{
n++;
for(int i1=0;i1 < m;i1++)
x[i][n-1]=0;
x[i][n-1]=1;
top[n - 2] = -900;
basic[i]=n-1;
}
}
void  findbase()
{
int i,j;
for(j=0;j<n;j++)
{
base[j]=0;
for(i=0;i<m;i++)
base[j] += x[i][j] * top[basic[i]];
base[j]=base[j]-top[j];
}
}
void  resultfile()throws java.io.IOException
  {
	int i,j;
         String s1;
	    BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

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

       System.out.print("top[j]"+top[j]);
s1=br.readLine();
   System.in.read();


for(i =0; i< m; i++)
  {
   System.out.print("top[basic[i]]+basic[i]+1+left[i]"+ top[basic[i]]+basic[i]+1+left[i]);
s1=br.readLine();
   System.in.read();

  for (j =0;j < n; j++)
  System.out.print(  "x[i][j]"+x[i][j]);
  s1=br.readLine();
     System.in.read();
 augmented[i][j]=Float.parseFloat(s1);

}
  System.out.print( "--------------");
      s1=br.readLine();
   System.in.read();

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

  System.out.print("---------------");

   s1=br.readLine();
   System.in.read();
System.out.print( "z= obj"+obj);
s1=br.readLine();
   System.in.read();

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

  System.out.print( "base[j]"+base[j]);

   s1=br.readLine();
   System.in.read();

}

void  display()throws java.io.IOException
  {

	  int i,j;
	    String s1;
	      BufferedReader br= new BufferedReader(new InputStreamReader(System.in));


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

	    System.out.print("top[j]"+top[j]);
	  s1=br.readLine();
	     System.in.read();

	    System.out.print( "---------------");
	     s1=br.readLine();
	     System.in.read();
	  }
	        for(j=0;j<n;j++)
	        {
	    System.out.print("-----------------");

	    s1=br.readLine();
	       System.in.read();

	  }
	    for (i=0;i< m; i++)
	    {
	     System.out.print( "top[basic[i]]+basic[i] +1+left[i]"+top[basic[i]]+basic[i] + 1+left[i]);
	     s1=br.readLine();
	        System.in.read();
	    }
	         for(j = 0;j < n; j++)
	         {
	       System.out.print( "x[i][j]"+x[i][j]);
	       s1=br.readLine();
	  	    System.in.read();
	  }
	       System.out.print( "----------------");
	       s1=br.readLine();
	  	    System.in.read();
	        for(j = 0;j < n;j++){
	         System.out.print("base[j]"+base[j]);
	          s1=br.readLine();
	     System.in.read();

	    }
	  }
	  int  keycolumn()
 {
 int j;
 float min =0;
 int l=-1;
 for(j=0;j<n;j++)
 {
 if(base[j] < min)
 {
  min = base[j];
  l=j;
 }
 }
 return(l);
 }



int  Keyrow(int kc)
{
int i;
float ratio;
float min =100000;
int l=-1;
for(i=0;i < m;i++)
{
if(x[i][k] < EPS && left[i] > EPS)
continue;
else
ratio = left[i]/x[i][k];
if(ratio < -EPS)
continue;
else
if(left[i]/x[i][k] < min)
{
min = left[i]/x[i][k];
l=i;
};
}
if (l == -1)
{
System.out.print( "Unbounded Solution");
System.exit(0);
}
return(l);
}


void  modify(int kr,int kc)
{
  int i,j;
float keyno;
float temp[]={15};
for(i=0;i<m;i++)
temp[i] = x[i][kc];
basic[kr]=kc;
keyno=x[kr][kc];
for(j=0;j<n;j++)
x[kr][j]=x[kr][j]/keyno;
left[kr]=left[kr]/keyno;
for(i=0;i<m;i++)
{
if(i== kr)
continue;
left[i]=left[i]-left[kr] * temp[i];
}
for(i=0;i<m;i++)
{
  if(i == kr)
continue;
else
for(j=0;j<n;j++)
x[i][j]=x[i][j]-temp[i] * x[kr][j];
}
obj=0;
for (i=0;i<m;i++)
obj=obj+top[basic[i]]*left[i];
}


void  result()throws IOException
{
int i;
int feasible =1;
for(i=0;i<m;i++)
{
if(top[basic[i]] <(PENALTY +0.01)&& left[i]> EPS)
{
 feasible =0;
 break;
}


if(true)
{

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


System.out.println("basic[i]+1+left[i]\n"+basic[i]+1+left[i]);

System.out.print("\nOptimal vlue of objective function "+obj);

if(res_in_file == 'y')
{

System.out.println("\nOptimal values are as under\n");


for(i=0;i < m; i++)
System.out.print("basic[i] +1 +left[i]\n"+basic[i] +1 +left[i]);

System.out.print("\nOptimal vlue of objective function "+obj);

}
}

else
{
System.out.print("SOLUTION IS INFEASIBLE\n");


if(res_in_file == 'y')

System.out.print("SOLUTION IS INFEASIBLE\n");

}}}

public static void  main(String args[])throws java.io.IOException{

Iteration lp;
lp=new Iteration();
lp.readdata();
lp.writefile();
lp.readfile();

	lp.addslack();
	lp.addarti();
	lp.findbase();
	lp.display();
	//getch();
	if(res_in_file =='y')
     lp.resultfile();



 String s1;
  BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

int i=1;

for(;;)
{
 kc = lp.keycolumn();

if (kc == -1) break;

System.out.println("lp:" +i);
s1=br.readLine();
System.in.read();
System.out.println("\n *********************\n");
s1=br.readLine();
System.in.read();

System.out.println("kc" + kc);
s1=br.readLine();
System.in.read();
kr = lp.Keyrow(kc);

System.out.println("kr" + kr);
s1=br.readLine();
System.in.read();

lp.modify( kc ,  kr );

lp.findbase();

lp.display();

if(res_in_file =='y')

lp.resultfile();
i++;
}
lp.result();
}
}
Thanks
As
muthukutta1

Last edited by muthukutta1; May 7th, 2014 at 05:10 AM.. Reason: To know why main function does not repeat to obtain feasible solution
Reply With Quote