still stuck with the same old producer consumer weight problem need help
Hello All,
This is the problem I am stuck with right now.
I have two array lists one producer array list and one consumer array list denoted by a and b
P1 P2 P3 P4 P5
5 6 7 8 9
C1 C2 C3 C4 C5
2 3 4 5 6
Now we find all those producer consumer pairs which satisfy the criteria Pi>=Ci
We have the following sets
(5,2)(6,2)(7,2),(8,2),(9,2)
(5,3)(6,3)(7,3),(8,3),(9,3)
(5,4)(6,4)(7,4),(8,4),(9,4)
(5,5)(6,5)(7,5),(8,5),(9,5)
(6,6)(7,6)(8,6),(9,6)
Let us done each of them with Si
so we have S1,S2,S3,S4,S5
we assign a third parameter called weight to each element in Si which has satisfied the condition Pi>=Ci;
so we we will have
(5,2,ai),(6,2,bi),(7,2,ci)....etc for S1
similarly for S2 and so on.
We need to find in each set Si the the pair which has the smallest weight.
if we have (5,2,3) and (6,2,4) then 5,2,3 should be chosen.We should make sure that there is only one pair in every set which is finally chosen on the basis of weight.
Suppose we get a pair (5,2,3) in S1 and (5,2,3) in S2 we should see that (5,2,3) is not used to compare to compare with any other elements in the same set S2,
Finally we should arrive at the best element pair in each set.They should be non repeating in other sets.
Given a problem
P0 P1 P2 P3 P4
9 5 2 2 8
6 5 4 5 3
C0 C1 C2 C3 C4
we have So as (P0,C0) and (P4,C0)
assuming that the one with the smaller index has lesser weight PO is selected.In the program I have used random weights.from set S1 we select the pair PO,CO
S1 =(P0,C1),(P1,C1) and (P4,C1)
since P0 and P4 are already used in previous set we dont use them for checking in S1 so we have (P1,C1) as best.
S2=(P0,C2),(P1,C2) and (P4,C2) so we dont use P0,C2 and P1 and C2 because PO and P1 are already used in S1.
So we choose P4,C2
in S3 and S4 ae have (P0,C3),(P1,C3),(P4,C3) so we dont choose anything
and same in S4 also.
So answer is
(P0,C0),(P1,C1) and (P4,C2).
My program is trying to assign weights and I am trying to print the weights along with the sets.It doesnt work fine.I need help to write this program to do this.
Thanks.
Regards.
NP
What I have tried till now.
[code]
I have one more question could you help me with this.
I have an array list of this form.
package mypackage1;
import java.util.*;
public class DD
{
private int P;
private int C;
private int weight;
public void set_p(int P1)
{
P=P1;
}
public void set_c(int C1)
{
C=C1;
}
public void set_weight(int W1)
{
weight=W1;
}
public int get_p()
{
return P;
}
public int get_c()
{
return C;
}
public int get_x()
{
return weight;
}
public static void main(String args[])
{
ArrayList a=new ArrayList();
ArrayList min_weights_int=new ArrayList();
ArrayList rows=new ArrayList();
ArrayList temp=new ArrayList();
Hashtable h=new Hashtable();
String v;
int o=0;
DD[] d=new DD[5];
for(int i=0;i<4;i++)
d=new DD();
for(int i=0;i<4;i++)
{
d.set_p(((int)(StrictMath.random()*10 + 1)));
d.set_c((int)(StrictMath.random()*10 + 1));
d.set_weight(0);
}
System.out.println("Producers");
for(int i=0;i<4;i++)
{
System.out.println(d.get_p());
}
System.out.println("Consumers");
for(int i=0;i<4;i++)
{
System.out.println(d.get_c());
}
System.out.println("Weights");
for(int i=0;i<4;i++)
{
System.out.println(d.get_x());
}
for(int i=0;i<4;i++ )
{
int bi =d.get_c();
ArrayList row=new ArrayList();
for(int j=0;j<4;j++)
{
if( d[j].get_p() >=bi)
{
d[j].set_weight((int)(StrictMath.random()*10 + 1));
row.add("(" + bi + "," + d[j].get_p() + "," +d[j].get_x() + ")");
}
else
{
d[j].set_weight(0);
row.add("null");
}
}
rows.add(row);
}
System.out.println(rows);
int f=0;
for(Iterator p=rows.iterator();p.hasNext();)
{
temp=(ArrayList)p.next();
String S="S" +f;
h.put(S,temp);
String tt=new String();
for(int j=0;j<4;j++)
{
if(temp.get(j).toString() !="null")
{
// System.out.println("In if loop");
//System.out.println(temp.get(j).toString());
String l=temp.get(j).toString();
System.out.println(l);
//System.out.println("Comma matches" + l.lastIndexOf(","));
//System.out.println(min_weights);
}
}
f++;
}
for(Enumeration e=h.keys();e.hasMoreElements();)
{
//System.out.println("I am here");
int ii=0;
int smallest=0;
String key=(String)e.nextElement();
System.out.println("key=" + key);
temp=(ArrayList)h.get(key);
System.out.println("Array List" + temp);
for( int j=0;j<4;j++)
{
String l=(temp.get(j).toString());
if(l!="null")
{
System.out.println("l=" +l);
}
}
}
}
}
[\code]

