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]

[5474 byte] By [nitinp_bioinfa] at [2007-9-29 11:42:09]
# 1

Sorry if I seem to be criticising your style rather than

content, but you will find it easier to see what your

program is doing if you indent each nested loop or whatever

a bit more as the depth of nesting increases, and then align

each "}" with the start of the loop.

And the closing code tag uses a forward slash, not a back slash.

You don't say what the question is with the code.

Now, to content...

It's not clear why you form the particular sets of

producer consumer pairs. Why not

5,2 5,3 5,4 5,5

6,2 6,3 6,4 6,5 6,6

7,2 7,3 7,4 7,5 7,6

8,2 8,3 8,4 8,5 8,6

9,2 9,3 9,4 8,5 9,6

?

(I used code tags to make alignment work by the way).

This is the same thing, but in a different order. I think

if you figure out the most systematic way to group the

pairs (if there is a systematic way) half the problem

will be solved.

TerryMoorea at 2007-7-15 1:15:01 > top of Java-index,Other Topics,Algorithms...
# 2

> It's not clear why you form the particular sets of

> producer consumer pairs. Why not

> > 5,2 5,3 5,4 5,5

> 6,2 6,3 6,4 6,5 6,6

> 7,2 7,3 7,4 7,5 7,6

> 8,2 8,3 8,4 8,5 8,6

> 9,2 9,3 9,4 8,5 9,6

>

I've just noticed that you might have been more consistent

than I thought. The lack of code tags probably spoiled your

alignment--shift your last row along one pair and you get

the transpose of my arrangement. The question is still:

"Why did you label your rows (my columns) S1, S2 etc.?" Is

there any reason for any particular grouping? Perhaps, for

example the consumers are subordinate to the producers and

should, therefore, be grouped by producers (the same producer

in each group) rather than by consumer.

Sort out the logic of what you want to do first.

TerryMoorea at 2007-7-15 1:15:01 > top of Java-index,Other Topics,Algorithms...
# 3
Hello Terry,Will do that.Thanks.Regards.NP.
nitinp_bioinfa at 2007-7-15 1:15:01 > top of Java-index,Other Topics,Algorithms...
# 4

Hello Terry,

I will explain why I paired it that way.

Firstly for every consumer we need to select all possible producers.

suppose we have this.

P0 P1 P2 P3 P4

9 5 22 8

C0 C1 C2 C3 C4

65 4 5 3

for one Ci we select all possible Pi with the condition Pi>Ci

So we have the sets

S0 =(C0,P0) and (C0,P4)

S1 =(C1,P0),(C1,P1) and (C1,P4).

S2=...

S3=...

S4=....

We need to select some of the sets from each of these sets with the condition that no two producers should be allocted to the same consumer

and vice versa.

To make this easier.We make us of a distance function d.

from SO we select (CO,P0) and not (CO,P4).

Next we eliminate (CO,P0) from S1,S2,S3,S4.

Similarly we select select (C1,P1) from S1 and perform elimination.

Finally we arrive at the solution.

I guess this is better for you to help me.

Thanks.

Regards.

NP

nitinp_bioinfa at 2007-7-15 1:15:01 > top of Java-index,Other Topics,Algorithms...
# 5

In your example you selected the pair with the greatest

distance from the first set, and the pair with the least

distance from the second. I don't see how the distance

function was used.

Also it's not clear to me that there is always a solution,

and, if there is, whether consistently choosing the

furthest or the closest pairs will always work.

The most obvious approach is to systematically try

all possibilities until the answer is reached, or there

are no possibilities left. This means backtracking whenever

a point is reached where you cannot continue. In this case

backtrack one step and try another possibility at this

step. After all possible choices of the previous step,

backtrack one more step and so on.

This seems rather involved, and it probably is.

Interestingly, if you know Prolog, it is ridiculously

easy because Prolog does all the backtracking for you.

In Java, you can implement the algorithm in much the same

way as Prolog implementations do it--keep a list of all the

choice points and work through them until success or there

are none left.

If you do know Prolog, you could generate lots of random

problems and see if there is always a solution.

TerryMoorea at 2007-7-15 1:15:01 > top of Java-index,Other Topics,Algorithms...
# 6
Hello there,When I mean CO I mean the index Zero in C and not COth element.DO you get it now CO < C4 because the index Zero is less than 4.Is it clear now.Thanks.Regards.NP.
nitinp_bioinfa at 2007-7-15 1:15:01 > top of Java-index,Other Topics,Algorithms...