필터 지우기
필터 지우기

constraint on some output of a fitfunc in genetic algorithm

조회 수: 11 (최근 30일)
sajad mhmzd
sajad mhmzd 2023년 4월 21일
답변: Sameer 2024년 8월 16일 9:23
hello dears
I have implemented genetic algorithm by:
options = optimoptions('ga','FunctionTolerance',1e-2,'PlotFcn', @gaplotbestf,'MaxGenerations',50);
[x, fval,exitflag,output]=ga(@(x)fitfun(x),nvars,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
I share you part of my fitfun
function y=nonIdealgas_test(x)
kisi=x(1);
phi=x(2);
psi=x(3);
.
.
U4=sqrt(deltaH0/psi)
Cm4=phi*kisi*U4;
beta4=atand((Cu4-U4)/Cm4
.
.
y=-deltaH0/(deltaH0+loss);
end
it's clear my objective function is y and I want optimize that but I want another output of this problem named beta4 that sould be in a rang like:
A<beta4<B
so how shoud I implement this constraint on ga?

답변 (1개)

Sameer
Sameer 2024년 8월 16일 9:23
Hi Sajad
To implement a constraint on the variable beta4 such that it lies within a specific range ( A < beta4 < B ) in a genetic algorithm using MATLAB, you can use nonlinear constraints. The ga function allows you to specify nonlinear constraints using a function handle.
Here's how you can modify your setup to include this constraint:
1.Define a Nonlinear Constraint Function:
Create a function that returns two outputs: c and ceq. The c output is for inequality constraints (i.e., constraints of the form ( c(x) <= 0 )), and ceq is for equality constraints (i.e., constraints of the form ( ceq(x) = 0 )). For your requirement, you will only need to use c.
function [c, ceq] = nonlcon(x)
% Extract your variables
kisi = x(1);
phi = x(2);
psi = x(3);
% Calculate U4, Cm4, and beta4 as in your fitfun
U4 = sqrt(deltaH0/psi);
Cm4 = phi * kisi * U4;
beta4 = atand((Cu4 - U4) / Cm4);
% Define the inequality constraints for beta4
A = ...; % Specify the lower bound
B = ...; % Specify the upper bound
c = [A - beta4; beta4 - B]; % Ensure A < beta4 < B
% No equality constraints
ceq = [];
end
2. Update the ga Function Call:
Ensure that you pass this nonlinear constraint function to the ga function.
options = optimoptions('ga', 'FunctionTolerance', 1e-2, 'PlotFcn', @gaplotbestf, 'MaxGenerations', 50);
[x, fval, exitflag, output] = ga(@(x)fitfun(x), nvars, A, b, Aeq, beq, lb, ub, @nonlcon, IntCon, options);
By following these steps, you can enforce the constraint on beta4 within your genetic algorithm optimization process.
I hope this helps!
Sameer

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by