How can I make a function be run with only teenager variables in matlab

2 vues (au cours des 30 derniers jours)
R@SH
R@SH le 21 Oct 2021
Commenté : dpb le 21 Oct 2021
Dear All,
I have a function with four variables. I want this function be run with only integer variabes. I want to define a bound for each variable and let it run within those span to find the best answer. I dont know how to define the variables to be limited to integers and not other values. for exaple:
Function (A)= optim(B,C,D,E)
B=1:1:10:
C=1:1:10:
D=2:2:10:
E=73:1:77:
It is noted that I am going to run this on Genetic algorithm toolbox. In there I have to determine the upper and lower bound of each parameter. But my problem is that it would be run with all poosible values between the upper and lower bound. I want them to be integer only. How can I do that?
I would appreciate your support in this matter
  2 commentaires
dpb
dpb le 21 Oct 2021
  1. There's no point in having B,C,D,E as input arguments to the function if you're going to define them internally -- either pass the input vectors or remove the arguments
  2. B=1:1:10 --> [1 2 3 ... 9 10] which are only integer values so I fail to see where the problem would be?
R@SH
R@SH le 21 Oct 2021
Thanks you so much dear dpb for your support.
the proble is that I have to find the minimize of A in GA, and in order to to that I have to define the allowed bound and values for B,C,D and E to look around and find the minimum value for A and its corresponding B,C,D and E.
Since in GA toolbox, I will define the lower and upper bound of each parameter, here in the m.file of matlab I only have to specify that these parameters are integer and also specify the lead value of each parameter. for example:
"B" is an integer and the lead value of that would be 2.
I previousely chose:
Function A=optim(B,C,D,E)
B=x(1);
C=x(2):
D=x(3):
E=x(4);
but when I specified the upper and lower bound for each parameter in GA toolbox, I found that it consider all possibe cases and not only integers. (for example 4.5 for "B" is not acceptable)
I hope it is clear now, and also hope to have your feedback on this matter.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 21 Oct 2021
Try this:
function A = optim(B, C, D, E)
% Clip values to a defined range
B = max([1, B]); % Clip so there are no values lower than 1.
B = min([10, B]); % Clip so there are no values more than 10.
% Round to nearest integer.
B = round(B);
% Clip values to a defined range
C = max([1, C]); % Clip so there are no values lower than 1.
C = min([10, C]); % Clip so there are no values more than 10.
% Round to nearest integer.
C = round(C);
% Clip values to a defined range
D = max([2, D]); % Clip so there are no values lower than 2.
D = min([10, D]); % Clip so there are no values more than 10.
% Round to nearest integer.
D = round(D);
% Clip values to a defined range
E = max([73, E]); % Clip so there are no values lower than 73.
E = min([77, E]); % Clip so there are no values more than 77.
% Round to nearest integer.
E = round(E);
  2 commentaires
R@SH
R@SH le 21 Oct 2021
I appreciate your respoce dear Image anayst, which is really valuable.
But the problem is that in GA toolbox, I have to define the maximum and minimum of each input variables (B,C,D,E) and let them to look around between them to find the minimum of objective (A).
In m.file code I only have to define that B,C,D and E are integer and specify their lead value.
In my previous code I specified them as below:
Function A=optim(B,C,D,E)
B=x(1);
C=x(2):
D=x(3):
E=x(4);
and the proble was that I got
B=4.5, C=2.2, D= 52.1 and E= 73 which provide the minimum of A.
I just want this code look around between integers to specify the minimum of A, and I have to determin it in my m.file.
I would appreciate you if you can help me how I should define my input variables in my m.File
dpb
dpb le 21 Oct 2021
I don't have the TB but what see the doc at https://www.mathworks.com/help/gads/mixed-integer-optimization.html?searchHighlight=integer%20optimization&s_tid=srchtitle_integer%20optimization_1 that illustrates how to set integer constraints on variables.
NB: the limitations on the types of problems that can be solved when doing this.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Produits


Version

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by