Average Optimization using GA or intlinprog algorithms
Show older comments
Guys i need to figure out the algorithm to model a question. Question is here: I have to pickup 30 balls that are in 10 different colors. Number of the balls are in the first column of the input matrice. Only requirement here is i have to pick at least one for each color.Every balls have different numbers of holes and spike on them. These are column 2 and 3 input respectively. I want to solve the problem for min and max average spike count. What is the algorithm here? Intlingprog does not seem to help, used ga solver but it takes about 5 mins. I want to drop the runtime to 10 secs at worst. Thanks!
1 Comment
Answers (2)
Torsten
on 16 Mar 2024
0 votes
9 Comments
Torsten
on 16 Mar 2024
Why is the intlinprog code I linked to not applicable ? When reading your question, it seems nothing has changed.
Mehmet
on 16 Mar 2024
Please include the code you used for the other objective.
Note that your problem description from above does not mention any condition on the number of holes. If they don't matter, just take the packages of each color with maximum/minimum number of spikes on the respective balls.
Mehmet
on 20 Mar 2024
But I gave you the solution above since you don't mention a condition on the number of holes. So just take the packages of each color with maximum/minimum number of spikes on the respective balls.
If you mean the problem from below, I don't think you can refomulate it as a linear integer problem. So "ga" will be the only way to go.
a = [3 5 36;1 5 25;2 8 2;4 7 23;8 22 8;8 2 7;5 12 98;6 15 5];
b = [3 3 3;1 6 6;2 8 2;3 5 2;3 11 5;8 5 31;7 6 7;9 3 8;8 4 9;2 20 3;3 4 51];
number_of_yellow_balls_packages = size(a,1);
number_of_black_balls_packages = size(b,1);
total_number_of_balls_packages = number_of_yellow_balls_packages + number_of_black_balls_packages;
ab = [a;b];
balls_vector = ab(:,1);
hole_vector = ab(:,2);
spike_vector = ab(:,3);
ballchoice = optimproblem;
selection_array = optimvar('selection_array',total_number_of_balls_packages,'Type','integer','LowerBound',0,'UpperBound',1);
ballchoice.Objective = sum(selection_array.*balls_vector.*spike_vector)/sum(selection_array.*balls_vector);
ballchoice.Constraints.c0 = sum(selection_array.*balls_vector) == 30;
ballchoice.Constraints.c1 = sum(selection_array.*balls_vector.*hole_vector) == 100;
ballchoice.Constraints.c2 = sum(selection_array(1:number_of_yellow_balls_packages).*balls_vector(1:number_of_yellow_balls_packages)) >= 1;
ballchoice.Constraints.c3 = sum(selection_array(number_of_yellow_balls_packages+1:total_number_of_balls_packages).*balls_vector(number_of_yellow_balls_packages+1:total_number_of_balls_packages)) >= 1;
x = solve(ballchoice);
sum(x.selection_array)
sum(x.selection_array.*balls_vector.*spike_vector)/sum(x.selection_array.*balls_vector)
sum(x.selection_array.*balls_vector.*hole_vector)
Maybe minimizing or maximizing
sum(selection_array.*spike_vector./balls_vector)
could be a compromise. But it's not the same, of course.
Or you try to restart from the solution of the linear integer problem:
a = [3 5 36;1 5 25;2 8 2;4 7 23;8 22 8;8 2 7;5 12 98;6 15 5];
b = [3 3 3;1 6 6;2 8 2;3 5 2;3 11 5;8 5 31;7 6 7;9 3 8;8 4 9;2 20 3;3 4 51];
number_of_yellow_balls_packages = size(a,1);
number_of_black_balls_packages = size(b,1);
total_number_of_balls_packages = number_of_yellow_balls_packages + number_of_black_balls_packages;
ab = [a;b];
balls_vector = ab(:,1);
hole_vector = ab(:,2);
spike_vector = ab(:,3);
ballchoice = optimproblem;
selection_array = optimvar('selection_array',total_number_of_balls_packages,'Type','integer','LowerBound',0,'UpperBound',1);
ballchoice.Objective = sum(selection_array.*balls_vector.*spike_vector);
ballchoice.Constraints.c0 = sum(selection_array.*balls_vector) == 30;
ballchoice.Constraints.c1 = sum(selection_array.*balls_vector.*hole_vector) == 100;
ballchoice.Constraints.c2 = sum(selection_array(1:number_of_yellow_balls_packages).*balls_vector(1:number_of_yellow_balls_packages)) >= 1;
ballchoice.Constraints.c3 = sum(selection_array(number_of_yellow_balls_packages+1:total_number_of_balls_packages).*balls_vector(number_of_yellow_balls_packages+1:total_number_of_balls_packages)) >= 1;
x = solve(ballchoice);
ballchoice.Objective = sum(selection_array.*balls_vector.*spike_vector)/sum(selection_array.*balls_vector);
x0.selection_array = x.selection_array;
x = solve(ballchoice,x0)
sum(x.selection_array)
sum(x.selection_array.*balls_vector.*spike_vector)/sum(x.selection_array.*balls_vector)
sum(x.selection_array.*balls_vector.*hole_vector)
Mehmet
on 23 Mar 2024
Then you will either use "ga" right from the beginning or start "ga" from a solution obtained by "intlinprog" that will be at least feasible and where the objective is a linear approximation of the nonlinear "average function".
Just out of interest: How large is aa*bb*cc*...*ii ?
John D'Errico
on 20 Mar 2024
0 votes
This is not an optimization problem. You only look at it in that way. It is purely a problem of a Monte Carlo simulation, to compute the distribution of average spike count. It sounds like you want min and max.
You need to choose 30 balls, from 10 different colors. The only requirement as you state is that you need to choose at least ONE of each color. The solution seems simple. Choose ONE of each color ball FIRST. Remove them from the set of unchosen balls.
Having done that, now you need to choose 20 more balls, but there is no constraint on them. So choose randomly from those that remain.
Now just compute the desired information on that chosen set. Repeat as many times as you wish. The above scheme can be done in a tiny fraction of a second, not minutes, or even seconds.
If this does not solve your problem, then you need to explain what in your question was incomplete.
2 Comments
Mehmet
on 23 Mar 2024
John D'Errico
on 23 Mar 2024
Sorry. I cannot/will not chase a moving target, especially one that is highly likely to continue its rapid motion. I've left my answer because it does answer the question you initially posed.
Categories
Find more on Linear Programming and Mixed-Integer Linear Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!