# constraints in a fitness function in MATLAB

I need to optimize weights in this fitness function:

f(x)=x(1)*a+x(2)*b+x(3)*c+x(4)*d;

(where a,b,c,d are numeral constants and x is the vector of 4 weights)

and i need to define these simple constraints in my function:

each weight (x(i)) must be between 0 and 1;

and

sum (x(i))=1; (i=1:4)

could anybody help me please?

## Answers

Assuming you have the Optimization Toolbox,

x= linprog([a,b,c,d],[],[],[1 1 1 1],1,[0 0 0 0],[1,1,1,1]);

if you minimize you fitness function, then check whether x is feasible (inside you search space) and whether it satisfies your constraint, if not then return +infinity (any big number) or infinity + some penalty, where the penalty is smaller for smaller violations of you constraints