This topic has been permanently closed and transferred to MATLAB Answers.

General
Follow


仟仟
4 views (last 30 days)

Parameter output problem of genetic algorithm

仟仟 on 7 Nov 2023
Latest activity Reply by Walter Roberson on 29 Nov 2023

When I apply genetic algorithm to find the optimal parameter, before the parameter enters the algorithm, I have controlled the parameter to keep two decimals in their respective ranges, but when the final output, the optimal parameter returned is really six decimals. Why? What am I supposed to do? (Note: I need to keep two decimal places in the parameter before entering the algorithm, because a change of 0.001 in the parameter has a great effect on the result, so I can't round the result directly.)Look forward to your reply~
Walter Roberson
Walter Roberson on 7 Nov 2023

The only way to control a ga parameter to two decimal places is to use an integer parameter with a range exactly 100 times larger than you ordinarily would, and then divide the parameter by 100 inside the objective function.

仟仟
仟仟 on 7 Nov 2023
Thank you for your answer, but I don't quite understand what you mean, and what I do is related to fractional order, so the parameter needs to be decimal. If it had to do with fractional order, would it work the way you said? If it's all right, I'd like you to tell me more about it
Walter Roberson
Walter Roberson on 7 Nov 2023
When you have a ga parameter that is not an integer data-type, then ga will automatically try continuous parameter values between the upper and lower bound for the parameter. There is no way to tell ga to only try the parameter by 0.1 increments.
If you need a parameter to be a multiple of a fraction f (such as 1/10) then use integer parameter there, but divide the upper and lower bound for that position by f. Then inside of your objective function, multiply the received value by f.
For example
f = 0.1;
ga(@(x) (x*f - pi)^2, 1, [], [], [], [], -1/f, 5/f, [], 1) * f
ga stopped because the average change in the penalty function value is less than options.FunctionTolerance and the constraint violation is less than options.ConstraintTolerance.
ans =
3.1
The objective here was to find the multiple of 0.1 that is closest to pi
仟仟
仟仟 on 8 Nov 2023
But my function is in another very complicated file, my God, my head is about to explode
Walter Roberson
Walter Roberson on 8 Nov 2023
Inside that complicated file, you can put in a line very close to the top, something like
x(7) = x(7)/10;
and then none of the rest of the file has to change.
仟仟
仟仟 on 9 Nov 2023
But this also modifies the number before entering the algorithm. Because for example, the parameter a=1.123456, the error at this time may be 0.00001. But if you round a=0.12, the error may become particularly large. So I need to keep this number two decimal places before I do the algorithm.
Walter Roberson
Walter Roberson on 29 Nov 2023
You will need to write your own genetic optimizer that executes the way you want the code to execute.
I personally do not think that what you want to do is consistent, but I will leave that up to you.
You might want to look in the File Exchange as there are some genetic algorithm implementations there that you might want to start with and then make your modifications.