Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 days ago.
Improve this question
Could you pls. help with algorithm (I use C++) which seemed so simple at first glance.
I need a total enumeration of all combinations of N parameters to run some target function for all combination and choose one optimal combination.
Let say there are three parameters (it can be any number set by user but for this example let’s assume 3)
Possible values (can be any number for any parameter - NOT a fixed number - number of values set a user before program starts)
name
value
value
value
value
param1
1
2
..
param2
10
20
..
param3
100
200
300
..
So the number of combinations = 12 (2 * 2 * 3 = 12)
All combinations
#
param1
param2
param3
1
1
10
100
2
2
10
100
3
1
20
100
4
2
20
100
5
1
10
200
6
2
10
200
7
1
20
200
8
2
20
200
9
1
10
300
10
2
10
300
11
1
20
300
12
2
20
300
Ok – let say the order may be different
#
param1
param2
param3
1
1
10
100
2
1
20
100
3
1
10
200
4
1
20
200
5
1
10
300
6
1
20
300
7
2
10
100
8
2
20
100
9
2
10
200
10
2
20
200
11
2
10
300
12
2
20
300
However it’s obviously one counter should change while others fixed and after one counter finish increment next in the chain
This approach seems quite simple but I still can’t find an implementation. I thought use a list for the parameters and as one counter finish itereates it's values it call the next parameter and increment next parameter value and reset the first parameter counter. But how put it in couple of loops … ? I intuitively feels it should be quite simple solution.
The another approach I think – use all combinations to build a graph and after that traverse the whole graph and in the end get an optimal combination. But if I fill the graph it means I already solve this problem and building a graph just a waste of time and memory.
For now there is a sketch (in pseudo-code) like this:
std::list<param> params
bool isDone = false
func(node* n)
{
if(n->prev)
{
n->GetCurrentValue() // return current value and increment by one
n->prev->reset();
func(n->prev)
Run(); // target function
if(n->IsDone()) // finish end of the values
{
if(n->next)
func(n->next);
else
isDone = true;
}
}
else // first node in the list
{
while(n->IsDone()) // finish end of the values
{
n->GetCurrentValue() // return current value and increment by one
Run() // target function
}
n.reset() // set internal counter for the node to 0
func(n->next())
}
}
while(!isDone)
{
for(p : params)
{
func(p)
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
lets add some flavor of ingenuity to it.Addition operation requires cost now, and the cost is the summation of those two to be added. So,to add 1 and 10, you need a cost of 11. If you want to add 1, 2 and 3. There are several ways1 + 2 = 3, cost = 31 + 3 = 4, cost = 42 + 3 = 5, cost = 53 + 3 = 6, cost = 62 + 4 = 6, cost = 61 + 5 = 6, cost = 6Total = 9Total = 10Total = 11I hope you have understood already your mission, to add a set of integers so that the cost is minimal.Input Each test case will start with a positive number,N(2N5000) followed by N positive integers(all are less than 100000). Input is terminated by a case where the value of N is zero. This case should not be processed.Output For each case print the minimum total cost of addition in a single line.
Sample Input
3
1 2 3
4
1 2 3 4
0
Sample Output
9
19
i tried to sort the given array and then took another array for cumsum (CS) and summed all element of CS except cs[0].. i am getting WA for this approach, please explain
int n,i,hold=0;
while(1)
{
cin>>n;
if(n==0){break;}
int arr[n],cs[n];
for(i=0;i<n;i++) cin>>arr[i];
sort(arr,arr+i);
cs[0]=arr[0];
for(i=1;i<n;i++){cs[i]=arr[i]+cs[i-1]; }
cs[0]=0;
int sum=0;
for(i=1;i<n;i++){sum+=cs[i]; }
cout<<sum<<endl;
sum=0;
}
input:
9
66 85 52 22 44 1 59 88 67
0
my out:
1822
expected result(udebug):
1454
getting WA
Your idea is wrong to solve this problem.
after taking all the elements on a data structure you should repeat this 3 points:
1)sort.
2)sum 1st two value,and remove 1st two value from the data structure
3)add the sum to the cost and data structure.
you can use priority_queue as the data structure.
Use min heap and add 2 smallest element. Example:
1 2 3 -> 3 3 -> 6.
1 2 3 4 -> 3 3 4 -> 4 6 -> 10.
Hope it helps.
This question already has an answer here:
How to use arrayformula with formulas that do not seem to support arrayformulas?
(1 answer)
Closed 4 months ago.
Using an array formula I want to find the max value of each row of a range and get the resulting range to work with it further.
The problem occurs as soon as I add the MAX() statement since it does seem to behave strangely within an array formula. Even if you ad commands which will give you multiple values within the MAX() statement it does always only return one single value.
E.g. this will give you the ranges which I want to get the max of:
=ARRAYFORMULA(ADDRESS(ROW(E1:E11); COLUMN() + 1; 4) & ":" & ADDRESS(ROW(E1:E11); COLUMN() + 4; 4))
The result looks like the following:
F1:I1
F2:I2
F3:I3
F4:I4
F5:I5
F6:I6
F7:I7
F8:I8
F9:I9
F10:I10
F11:I11
If I now add INDIRECT() to make those to actual ranges and add MAX() it should return the max of each of those ranges since the array formula should go through the ROW(E1:11) as it did bevor. However, the result of this new formula
=ARRAYFORMULA(MAX(INDIRECT(ADDRESS(ROW(E1:E11); COLUMN() + 1; 4) & ":" & ADDRESS(ROW(E1:E11); COLUMN() + 4; 4))))
rather is one single value, the maximum of the first range.
I have even tried to bypass the problem by adding an IF() statement for the array formula to iterate through the rows. Doing so, it did give me a result for all 11 rows, however, the result always was the same (the max of the first row).
The new formula:
=ARRAYFORMULA(IF(ROW(E1:E11) = ROW(E1:E11); MAX(INDIRECT(ADDRESS(ROW(E1:E11); COLUMN() + 1; 4) & ":" & ADDRESS(ROW(E1:E11); COLUMN() + 4; 4))); ""))
The new result (left column are the results of the formula, trying to get the max of each row to its right):
10 7 10 4 1
10 10 8 1 2
10 4 5 9 4
10 10 10 2 2
10 10 10 5 10
10 10 6 9 5
10 4 5 7 3
10 6 9 4 7
10 5 5 7 3
10 9 2 3 10
10 10 3 9 10
=QUERY(TRANSPOSE(QUERY(TRANSPOSE(F1:I),
"select "®EXREPLACE(JOIN( , ARRAYFORMULA(IF(LEN(F1:F&G1:G&H1:H&I1:I),
"max(Col"&ROW(F1:F)-ROW(F1)+1&"),", ""))), ".\z", "")&"")),
"select Col2")
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
There is given array of size > n , we need to select n number of element from an array.
For example : array contain 112 element and n = 50, so select 50 number such that distance between every two selected number is more or less equal (equal distance is not possible of course except for size%n == 0 ).
If anyone suggest any idea that would work .
Example :
array = 1 2 3 4 5
n = 1
output : 1 or any another number depending on proposed algo.
n = 2
output : 1 3 or 2 4 or 1 4...
n = 3
output : 1 3 5
n = 4
output : 1 3 4 5 or 1 2 4 5
n = 5 :
output 1 2 3 4 5
Basically in case of n=1,2,4 there are more then one possible combination so I need to devise an algo which would pick numbers in uequally distributed manner.
One approach would be dividing the number of elements by the number of desired elements in the selection in floating point, and using rounding to determine the index:
double dist = ((double)size) / n;
int *res = new int[n];
for (int i = 0 ; i != n ; i++) {
res[i] = orig[round(dist*i)];
}
For your example of 112 and 50 the value of dist would be 2.24 and the sequence of indexes selected from the array would be
0 0
1 2
2 4
3 7
4 9
5 11
......
45 101
46 103
47 105
48 108
49 110
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have two workers.If I have configured a strategy that 60% tasks are allocate to A worker and the rest to B worker.
how do you implement with c++.
what is your suggestion?
map<string,float> m_percent;
m_percent.insert(make_pair("countA",0.6));
m_percent.insert(make_pair("countB",0.1));
m_percent.insert(make_pair("countC",0.3));
map<string,int> m_count;
m_count.insert(make_pair("total",0));
map<string,int>::iterator it = m_count.find("countA");
map<string,int>::iterator itc =m_count.find("total");
map<string,float>::iterator itp=m_percent.find("countA");
if(it== m_count.end())//use countA
{
m_count.insert(make_pair("countA",1));
}
else
{
int &c = it->second;
if(itc!=m_count.end()&&itp!=m_percent.end())
{
float f=(c+1)*100/(itc->second+1)*100.0
if (f<=itp->second)
{
it->second=it->second+1;
}
}
}
if(itc!=m_count.end())
{
itc->second=itc->second+1;
}
If you're talking about number of tasks with no regard to complexity, simply keep a count of how many jobs have been allocated to each. Let's call these counts countA for the jobs allocated to A, and count for the total number of jobs (for simplicity in calculations), and initialise them to zero.
Then, when a job comes in, allocate as per the following:
if count is equal to zero, allocate it to A and increment both countA and count.
otherwise, if countA / count is less than 0.6, allocate it to A and increment both countA and count.
otherwise allocate it to B and just increment count.
This will, over the long term, tend to average out allocations so that A gets 60%:
countA count countA/count allocateTo
------ ----- ------------ ----------
0 0 ? A
1 1 1.000 B
1 2 0.500 A
2 3 0.667 B
2 4 0.500 A
3 5 0.600 B
3 6 0.500 A
4 7 0.571 A
5 8 0.625 B
5 9 0.556 A
6 10 0.600 B
6 11 0.545 A
7 12 0.583 A
8 13 0.615 B
8 14 0.571 A
9 15 0.600 B
9 16 0.563 A
10 17 0.588 A
11 18 0.611 B
11 19 0.579 A
12 20 0.600
... and so on.
I'm trying to implement an ACO for 01MKP. My input values are from the OR-Library mknap1.txt. According to my algorithm, first I choose an item randomly. then i calculate the probabilities for all other items on the construction graph. the probability equation depends on pheremon level and the heuristic information.
p[i]=(tau[i]*n[i]/Σ(tau[i]*n[i]).
my pheremon matrix's cells have a constant value at initial (0.2). for this reason when i try to find the next item to go, pheremon matrix is becomes ineffective because of 0.2. so, my probability function determines the next item to go, checking the heuristic information. As you know, the heuristic information equation is
n[i]=profit[i]/Ravg.
(Ravg is the average of the resource constraints). for this reason my prob. functions chooses the item which has biggest profit value. (Lets say at first iteration my algorithm selected an item randomly which has 600 profit. then at the second iteration, chooses the 2400 profit value. But, in OR-Library, the item which has 2400 profit value causes the resource violation. Whatever I do, the second chosen is being the item which has 2400 profit.
is there anything wrong my algorithm? I hope ppl who know somethings about ACO, should help me. Thanks in advance.
Input values:
6 10 3800//no of items (n) / no of resources (m) // the optimal value
100 600 1200 2400 500 2000//profits of items (6)
8 12 13 64 22 41//resource constraints matrix (m*n)
8 12 13 75 22 41
3 6 4 18 6 4
5 10 8 32 6 12
5 13 8 42 6 20
5 13 8 48 6 20
0 0 0 0 8 0
3 0 4 0 8 0
3 2 4 0 8 4
3 2 4 8 8 4
80 96 20 36 44 48 10 18 22 24//resource capacities.
My algorithm:
for i=0 to max_ant
for j=0; to item_number
if j==0
{
item=rand()%n
ant[i].value+=profit[item]
ant[i].visited[j]=item
}
else
{
calculate probabilities for all the other items in P[0..n]
find the biggest P value.
item=biggest P's item.
check if it is in visited list
check if it causes resource constraint.
if everthing is ok:
ant[i].value+=profit[item]
ant[i].visited[j]=item
}//end of else
}//next j
update pheremon matrix => tau[a][b]=rou*tau[a][b]+deltaTou
}//next i