Click here to Skip to main content
15,798,200 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
Input : Requests of each agent req1,req2,req3……
1. SUM <- sum of all requests.
2. while SUM > 0 do (for each data request)
3. i <- SelectAgent( )
4. k <- SelectObject( )
5. Add k to the allocated set of i.
6. SUM <- SUM – 1.
let agents are a1,a2,a3 and data objects are d1,d2,d3,d4,d5
a1 got d1,d2,d3
a2 got d4,d5
a3 got d1,d4

i will itself do the coding for select agent and select object. now i want to know how i allocate these objects(k) to agents(i)????

What I have tried:

i considered the array of 2-d by taking a[i][k]..
now the problem is this when agent get any object then only its row should be shifted. but row of all the agent will shift...
for eg) a1(i.e i=1) got d1(i.e k=1) we allocate at a[1][1] then a2(i.e i=2) got d4(i.e k=4).it will be allocated at a[2][4]...but t should be allocated at a[2][1]. how can i do this.please explain the code for this.
Posted
Updated 18-Mar-16 21:25pm

1 solution

Particularly, I've got just one possible solution of the problem you've claimed:

I don't actually know what SelectAgent and SelectObject functions are supposed to do, that's why I replaced them with two "trap" functions generating random values, which you'll need to substitute with the ones the perform the right things for you.

C++
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#define AGENTS_N  5  // The number of agents
#define OBJECTS_N 10 // The number of objects

// These are two "trap" functions which immitate selecting either agent or object
// You'll have to replace these two functions with your own to provide instant
// agent and object selection
int SelectAgent() { return rand() % AGENTS_N; }
int SelectObject() { return rand() % OBJECTS_N; }

// Another "trap" function which returns the sum of all requests
int GetSum() { return rand() % (AGENTS_N * OBJECTS_N) + 1; }

int main()
{
	// Allocating 2-d pointer array to store requests values
	// for each agent and object being selected
	int** a = new int*[AGENTS_N];

	// Allocating array of each row sizes 
	// and initially set each size to AGENTS_N;
	int* sizes = new int[AGENTS_N];
	for (int t = 0; t < AGENTS_N; t++)
		sizes[t] = OBJECTS_N;

	// Initializing each pointer of the 2-d 
	// array to store objects requests per agent
	for (int i = 0; i < AGENTS_N; i++)
	{
		a[i] = new int[OBJECTS_N];
		memset((void*)a[i], 0x00, sizeof(int) * OBJECTS_N);
	}

	// Obtaining the total sum value for all requests
	int SUM = GetSum();

	// For each data request dispatched we're performing 
	while (SUM > 0)
	{
		int i = SelectAgent();  // Obtaining the value of agent
		int k = SelectObject(); // Obtaining the value of object

		// Generating the random "trap" value for an object being selected
		int rand_val = rand() % 0xFF + 1;

		// Printing out the values of agent, object and value associated with it
		printf("agent = %d object = %d value = %d\n", i, k, rand_val);

		// Now, let's check if the value of the corresponding object of a certain agent
		// has not yet been assigned. If not, simply assign random value to a specific
		// data item of 2-d array
		if (a[i][k] <= 0)
		{
			a[i][k] = rand_val;

			// Printing out all data items of a row being selected
			for (int x = 0; x < sizes[i]; x++)
				printf("%d ", a[i][x]);

			printf("\n");
		}

		else
		{
			// Otherwise, if the value of the object has been already assigned
			// just shift all the values on the right to insert the value
			int new_size = sizes[i] + 1;
			int* temp = new int[new_size];
			memcpy((void*)temp, (const void*)a[i], sizeof(int) * sizes[i]);
			for (int t = sizes[i]; t >= k; t--)
				a[i][t + 1] = a[i][t];

			// Assigning the new random value and update the size of the row being selected
			a[i][k] = rand_val; sizes[i] = new_size;

			// Printing out all data items of a row being selected
			for (int x = 0; x < sizes[i]; x++)
				printf("%d ", a[i][x]);

			printf("\n");
		}

		// Decrement SUM variable value
		SUM = SUM - 1;
	}

    return 0;
}</conio.h></stdlib.h></stdio.h>


P.S. If you've got any questions, just post your comments under my solution for your question. That's all folks.
 
Share this answer
 
Comments
Member 12402139 20-Mar-16 10:16am    
I am very thankful to you for this...sir
Thanks a lot.
Arthur V. Ratz 20-Mar-16 10:20am    
O'key, no problem.
Arthur V. Ratz 20-Mar-16 10:31am    
Is it working for you or not ?
Member 12402139 2-Apr-16 10:08am    
Sir please tell me the coding of following algorithm in c++

Consider that agents A1, A2, . . . , An request the data from the distributor possessing a set of objects, say D = { d1, d2, . . . ,dm }.

Here mi shows the request of agent Ai. │T│ is the no of data objects from D1 to Dm. Ri shows the allocation of data object to agent Ai. Here a is array in which ak shows object Dk is allocated to how many agents.

Algorithm 1: Allocation for Sample Data Requests
Input: m1,m2, . . . ,mn,│T│ ⊳ Assuming mi ≤ │T│
Output: R1, . . . ,Rn
1. a ← 0│T│ ⊳ a[k] : number of agents who have received object tk
2. R1 ← ∅, . . . , Rn ← ∅
3: remaining ← ∑mi (sum from i=1 to n)
4: while remaining > 0 do
5: i ← SELECTAGENT()
6: k ←SELECTOBJECT(i , Ri) . ⊳ May also use additional parameters
7: Ri ←Ri ∪ {tk}
8: a[k] ← a[k] + 1
9: remaining ← remaining – 1

Algorithm 2. Object Selection for s-random
1: function SELECTOBJECT(i , Ri)
2: k ← select at random an element from set { k' │ tk ∉ Ri}
3: return k

Algorithm 3. Object Selection for s-overlap
1: function SELECTOBJECT (i , Ri , a)
2: K ← { k │ k = argmin a[k']}
k'

3: k ← select at random an element from set {k' │ k' ∈ K ∧ tk' ∉ Ri}
4: return k


Algorithm 4. Object Selection for s-max
1: function SELECTOBJECT (i, R1, . . . ,Rn, m1, . . . ,mn)
2: min overlap ← 1 . ⊳ the minimum out of the maximum relative overlaps that the allocations of different objects to Ui yield
3: for k ∈ {k' │ tk ∉ Ri} do
4: max_rel_ov ← 0 . ⊳ the maximum relative overlap between Ri and any set Rj that the allocation of tk to Ui yields
5: for all j = 1, . . . ,n : j ≠ i and tk ∈ Rj do
6: abs_ov ←│Ri ∩ Rj│+ 1
7: rel_ov ← abs ov/min(mi , mj)
8: max_rel_ov ← Max(max_rel_ov , rel_ov)
9: if max_ rel_ov ≤ min_overlap then
10: min_overlap ← max_rel_ov
11: ret k ← k
12: return ret k

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900