View Single Post
  #1 (permalink)  
Old November 10th, 2013, 01:54 AM
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
Default LinearProgrammingSolution

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
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]
Reply With Quote