The principle of the unfathomable genetic algorithm is so simple
Many people think that the algorithm is the content of mathematics and it is especially troublesome to learn. We cannot think that this view is wrong. But we also know that software is a composite technology. If one only knows the algorithm, but can't do it well in the programming language, then the excellent algorithm can't work.
Once, one person asked me:
"You are writing pediatric things, dozens of lines of code can be done, can you make a whole deep algorithm?"
I asked him what he understood as a sophisticated algorithm. He replied: "Like genetic algorithms, ant colony algorithms, etc." So I gave him an example of a genetic algorithm to solve the 0-1 knapsack problem and told him that This is the algorithm of dozens of lines of code, how to understand it as a deep algorithm? He didn't recognize this as a genetic algorithm at first, until I gave Denis Cormier the source code of the genetic algorithm on the server at North Carolina State University, he believed that he always thought that the principle of the unfathomable genetic algorithm was so simple.
Another person bluntly said that the problem of "dividing 8 liters of water in three buckets" is simply not an algorithm. He thinks that artificial intelligence like "Apha Dog" is an algorithm. I told him that the basic theory of computer chess is the game tree, or an expert system. But he thinks that the game tree is also a very deep algorithm, so I gave him a tic-tac-toe game and told him that this is the game tree search algorithm, very intelligent, you can never beat it (because the tic-tac-toe game is very simple, This algorithm will search all the states). I believe he must be shocked because this algorithm does not exceed 100 lines of code.
For the above mentioned examples, I think the main reason is that everyone has different understanding of the algorithm. Many people understand the algorithm too lopsided. Many people think that only the name containing the "XX algorithm" is the algorithm. And I think the essence of the algorithm is to solve the problem, as long as the code that can solve the problem is the algorithm.
Only when you have good computer knowledge and mathematical knowledge can you continue to progress in the study of algorithms. Regardless of the simplicity of the algorithm, you must practice it yourself. Only by constantly understanding the mistakes and constantly discovering the mistakes can you continuously improve your programming skills and continuously improve your business.
In fact, any algorithm has its own application environment and application scenarios, and no algorithm can be applied to all scenarios. I hope everyone understands this. At the same time, we must also understand that complex algorithms are composed of ordinary algorithms. Without ordinary algorithms, there is no complicated algorithm at all, so the complexity becomes simple and the size is small. This is the basic idea of ​​algorithmic recursion.
We can talk about the function of an array lookup below. Speaking one sentence at a time, first we start with the simplest function construct:
Int find(int array[], int length, int value)
2. {
3. int index = 0;
Return index;
5. }
Here we see that the lookup function is just a normal function, so the first thing to judge is the legality of the parameters:
1. static void test1()
2. {
3. int array[10] = {0};
4. assert(FALSE == find(NULL, 10, 10));
5. assert(FALSE == find(array, 0, 10));
6. }
It can be seen here that we have not judged the legality of the parameters, so how should the original search function be modified?
Int find(int array[], int length, int value)
2. {
3. if(NULL == array || 0 == length)
4. return FALSE;
5.
6. int index = 0;
Return index;
8. }
See the code above to show that we have already judged the entry parameters. Then let's start writing code.
Int find(int array[], int length, int value)
2. {
3. if(NULL == array || 0 == length)
4. return FALSE;
5.
6. int index = 0;
7. for(; index < length; index++){
8. if(value == array[index])
Return index;
10. }
11.
12. return FALSE;
13. }
The above code is almost complete, so how to write test cases?
1. static void test2()
2. {
3. int array[10] = {1, 2};
4. assert(0 == find(array, 10, 1));
5. assert(FALSE == find(array, 10, 10));
6. }
After running all the test cases, let's see if there is anything that can be optimized for the original code. In fact, we can turn an array into a pointer.
Int find(int array[], int length, int value)
2. {
3. if(NULL == array || 0 == length)
4. return FALSE;
5.
6. int* start = array;
7. int* end = array + length;
8. while(start < end){
9. if(value == *start)
10. return ((int)start - (int)array)/(sizeof(int));
11. start ++;
12. }
13.
14. return FALSE;
15. }
What if the above code parameter must be a generic data type?
1. template
Int find(type array[], int length, type value)
3. {
4. if(NULL == array || 0 == length)
5. return FALSE;
6.
7. type* start = array;
8. type* end = array + length;
9. while(start < end){
10. if(value == *start)
11. return ((int)start - (int)array)/(sizeof(type));
12. start ++;
13. }
14.
15. return FALSE;
16. }
At this point, is the test case also need to be revised?
1. static void test1()
2. {
3. int array[10] = {0};
4. assert(FALSE == find
5. assert(FALSE == find
6. }
7.
8. static void test2()
9. {
10. int array[10] = {1, 2};
11. assert(0 == find
12. assert(FALSE == find
13. }
Finally, let's summarize:
(1) Our algorithm requires verification of test cases
(2) Any optimization should be based on testing
(3) Testing and code writing should be done simultaneously
(4) The successful operation of the algorithm is carried out step by step, and the success of each step must be established on the basis of the original success.
Washing Machine Motor,Spin Motor Of Aluminium Wire,Washing Machine Motor Shaft,Automatic Washing Machine Spin Motor
WUJIANG JINLONG ELECTRIC APPLIANCE CO., LTD , https://www.jinlongmotor.com