Genetic Algorithm and direct search toolbox study NOTE 2: compile your own objective functions

Source: Internet
Author: User
Tags scalar

 This section describes how to write your own target functions. What is the target function? You use geneticsAlgorithmThe Toolbox mainly aims to find the optimal solution of a function, so this function is the target function. You must write this function as an M file. In this way, we can meet the requirements of the Genetic Algorithm Toolbox. Of course, there are not only these requirements, but also the following requirements:

This function should acceptVectorAs an input parameter, the length of the vector should be equal to the number of independent variables in the target function. This function should returnScalar, This scalar should be the function value corresponding to this input parameter. The following describes how to write a target function. Suppose you want to optimize the following functions.

This function has two independent variables X1 and x2. therefore, the input parameter of your function should be a 1X2 vector. Okay. Now that you understand it, write the function below.

1. Click "file" and select "function M-file" in "New". The text editor page of MATLAB appears. Change your content to the correct one.

2. Save the file.

3. Test your function.

Enter mytest ([2 3]) in MATLAB and press Enter. Appears

Ans =

-5
Congratulations, the function you wrote is correct. Otherwise, you have to check the M file of your target function.

By the way, the image of the target function is also drawn.

 

When using the Genetic Algorithm Toolbox, what is the optimal direction for the objective function? That is, do these tools calculate the maximum value or the minimum value for the target function? The answer is: minimum value. So if you want to calculate the maximum value of a function, you must modify the original target function so that the minimum value of this function is exactly the maximum value you want. This is very simple, add a negative sign (-) to the front, and then change it. Of course there are still many methods. Believe in yourself, you will have a better way, haha. I will not talk about the details. Wait for the next timeArticleRight.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.