MATLAB Answers

How can i write a Objective function in vectorized form for GA optimization toolbox when the Objective function has many varialbess?

14 views (last 30 days)
Narin Sovann
Narin Sovann on 5 Dec 2020
Answered: Stephan on 6 Dec 2020
I am using Mixed Integer GA optimization toolbox for my model that has many variables e.g. x1,x2,x3,..............,x300.
I need to put them into a vectorized form e.g. x(:,1).*x(:,101) .*x(:,201) + x(:,2).*(:,102).*x(:,202) + .........+x(:,100).*x(:,200).*x(:,300)
So that is challenging for me to write this objective function. It should have a way to achieve it by iterative or other methods, any suggestion please.

Accepted Answer

Stephan
Stephan on 6 Dec 2020
str1 = ("x(:," + (1:100) + ") .*");
str2 = ("x(:," + (101:200) + ") .*");
str3 = ("x(:," + (201:300) + ") +");
str = vertcat(str1, str2, str3);
str = join(reshape(str,1,[]));
str = char("@(x) " + str);
str(end-1:end) = [];
fun = str2func(char(str))
results in:
fun =
function_handle with value:
@(x)x(:,1).*x(:,101).*x(:,201)+x(:,2).*x(:,102).*x(:,202)+x(:,3).*x(:,103).*x(:,203)+x(:,4).*x(:,104).*x(:,204)+x(:,5).*x(:,105).*x(:,205)+x(:,6).*x(:,106).*x(:,206)+x(:,7).*x(:,107).*x(:,207)+x(:,8).*x(:,108).*x(:,208)+x(:,9).*x(:,109).*x(:,209)+x(:,10).*x(:,110).*x(:,210)+x(:,11).*x(:,111).*x(:,211)+x(:,12).*x(:,112).*x(:,212)+x(:,13).*x(:,113).*x(:,213)+x(:,14).*x(:,114).*x(:,214)+x(:,15).*x(:,115).*x(:,215)+x(:,16).*x(:,116).*x(:,216)+x(:,17).*x(:,117).*x(:,217)+x(:,18).*x(:,118).*x(:,218)+x(:,19).*x(:,119).*x(:,219)+x(:,20).*x(:,120).*x(:,220)+x(:,21).*x(:,121).*x(:,221)+x(:,22).*x(:,122).*x(:,222)+x(:,23).*x(:,123).*x(:,223)+x(:,24).*x(:,124).*x(:,224)+x(:,25).*x(:,125).*x(:,225)+x(:,26).*x(:,126).*x(:,226)+x(:,27).*x(:,127).*x(:,227)+x(:,28).*x(:,128).*x(:,228)+x(:,29).*x(:,129).*x(:,229)+x(:,30).*x(:,130).*x(:,230)+x(:,31).*x(:,131).*x(:,231)+x(:,32).*x(:,132).*x(:,232)+x(:,33).*x(:,133).*x(:,233)+x(:,34).*x(:,134).*x(:,234)+x(:,35).*x(:,135).*x(:,235)+x(:,36).*x(:,136).*x(:,236)+x(:,37).*x(:,137).*x(:,237)+x(:,38).*x(:,138).*x(:,238)+x(:,39).*x(:,139).*x(:,239)+x(:,40).*x(:,140).*x(:,240)+x(:,41).*x(:,141).*x(:,241)+x(:,42).*x(:,142).*x(:,242)+x(:,43).*x(:,143).*x(:,243)+x(:,44).*x(:,144).*x(:,244)+x(:,45).*x(:,145).*x(:,245)+x(:,46).*x(:,146).*x(:,246)+x(:,47).*x(:,147).*x(:,247)+x(:,48).*x(:,148).*x(:,248)+x(:,49).*x(:,149).*x(:,249)+x(:,50).*x(:,150).*x(:,250)+x(:,51).*x(:,151).*x(:,251)+x(:,52).*x(:,152).*x(:,252)+x(:,53).*x(:,153).*x(:,253)+x(:,54).*x(:,154).*x(:,254)+x(:,55).*x(:,155).*x(:,255)+x(:,56).*x(:,156).*x(:,256)+x(:,57).*x(:,157).*x(:,257)+x(:,58).*x(:,158).*x(:,258)+x(:,59).*x(:,159).*x(:,259)+x(:,60).*x(:,160).*x(:,260)+x(:,61).*x(:,161).*x(:,261)+x(:,62).*x(:,162).*x(:,262)+x(:,63).*x(:,163).*x(:,263)+x(:,64).*x(:,164).*x(:,264)+x(:,65).*x(:,165).*x(:,265)+x(:,66).*x(:,166).*x(:,266)+x(:,67).*x(:,167).*x(:,267)+x(:,68).*x(:,168).*x(:,268)+x(:,69).*x(:,169).*x(:,269)+x(:,70).*x(:,170).*x(:,270)+x(:,71).*x(:,171).*x(:,271)+x(:,72).*x(:,172).*x(:,272)+x(:,73).*x(:,173).*x(:,273)+x(:,74).*x(:,174).*x(:,274)+x(:,75).*x(:,175).*x(:,275)+x(:,76).*x(:,176).*x(:,276)+x(:,77).*x(:,177).*x(:,277)+x(:,78).*x(:,178).*x(:,278)+x(:,79).*x(:,179).*x(:,279)+x(:,80).*x(:,180).*x(:,280)+x(:,81).*x(:,181).*x(:,281)+x(:,82).*x(:,182).*x(:,282)+x(:,83).*x(:,183).*x(:,283)+x(:,84).*x(:,184).*x(:,284)+x(:,85).*x(:,185).*x(:,285)+x(:,86).*x(:,186).*x(:,286)+x(:,87).*x(:,187).*x(:,287)+x(:,88).*x(:,188).*x(:,288)+x(:,89).*x(:,189).*x(:,289)+x(:,90).*x(:,190).*x(:,290)+x(:,91).*x(:,191).*x(:,291)+x(:,92).*x(:,192).*x(:,292)+x(:,93).*x(:,193).*x(:,293)+x(:,94).*x(:,194).*x(:,294)+x(:,95).*x(:,195).*x(:,295)+x(:,96).*x(:,196).*x(:,296)+x(:,97).*x(:,197).*x(:,297)+x(:,98).*x(:,198).*x(:,298)+x(:,99).*x(:,199).*x(:,299)+x(:,100).*x(:,200).*x(:,300)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!