Python uses genetic algorithms to solve the maximum flow problem, and python Genetic Algorithms

Source: Internet
Author: User

Python uses genetic algorithms to solve the maximum flow problem, and python Genetic Algorithms

This article shares with you the Python Genetic Algorithm for Solving the biggest stream problem. The specific content is as follows:

Generate_matrix

def Generate_matrix(x,y): import numpy as np import random return np.ceil(np.array([random.random()*10 for i in range(x*y)]).reshape(x,y))

Max_road

Def Max_road (A, degree, start): import random import numpy as np import copy def change (M, number, start): # number controls the degree of variation start controls The amount of variation x, y = M. shape for I in range (start, x): Line = zip (range (len (M [I]), M [I]) index_0 = [t [0] for t in Line if t [1] = 0] # obtain the subscript index_1 = [t [0] for t in Line if t [1] = 1] # obtain the subscript M [I] [random. sample (index_0, number) [0] = 1 # randomly change the number values in the sequence 0-> 1 M [I] [random. sample (index_1, number) [0] = 0 # randomly change the number values in the sequence 1-> 0 return M x, y =. shape n = x generation = y # initialize a solution matrix with n conditions init_solve = np. zeros ([n, x + y-2]) init = [1] * (x-1) + [0] * (Y-1) for I in range (n): random. shuffle (init) init_solve [I,:] = init #1 indicates that 0 indicates solve = copy to the right. copy (init_solve) for loop in range (generation): Sum = [A [] * n # used to record the total traffic of each solution for I in range (n ): j = 0; k = 0; for m in solve [I,:]: if m = 1: k = k + 1 else: j = j + 1 Sum [I] = Sum [I] + A [k, j] Sum_index = zip (range (len (Sum), Sum) sort_sum_index = sorted (Sum_index, key = lambda d: d [1], reverse = True) # Sort the scheme by traffic sum. Max = sort_sum_index [0] [1] # maximum traffic # print Max solve_index_half = [a [0] for a in sort_sum_index [: n/2] # retain the half solve of the sorting scheme = np. concatenate ([solve [solve_index_half], solve [solve_index_half]) # copy half of the retained scheme, and copy the portion for mutating change (solve, int (x + y-2) * degree) + 1, start) # variation return solve [0], Max

Draw_road

Def Draw_road (road, A): import pylab as plt import seaborn. set () x, y =. shape # map the move down and right to the drawing coordinate. Road = [(1, x)] # initial coordinate j = 1; k = x; for m in road: if m = 1: k = K-1 else: j = j + 1 Road. append (j, k) # print Road for I in range (len (road): plt. plot ([Road [I] [0], Road [I + 1] [0], [Road [I] [1], road [I + 1] [1])

Actual running example

In [119]: A = Generate_matrix(4,6)In [120]: AOut[120]: array([[ 10., 1., 7., 10., 8., 8.],  [ 4., 8., 8., 4., 8., 2.],  [ 9., 8., 8., 3., 9., 8.],  [ 7., 2., 5., 9., 3., 8.]])In [121]: road , M=Max_road(A,0.1,2)In [122]: Draw_road(road,A)

Large Scale

In [105]: A = Generate_matrix(40,60)In [106]: road , M=Max_road(A,0.1,4)In [107]: roadOut[107]: array([ 0., 0., 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0.,  1., 0., 0., 0., 1., 0., 0., 1., 0., 1., 1., 1., 1.,  1., 0., 0., 0., 0., 0., 1., 0., 0., 1., 0., 0., 0.,  1., 0., 0., 0., 1., 0., 1., 0., 0., 1., 0., 0., 1.,  0., 0., 0., 1., 0., 0., 1., 1., 1., 1., 0., 0., 0.,  0., 0., 0., 1., 0., 1., 1., 1., 1., 0., 1., 0., 1.,  1., 1., 0., 1., 0., 1., 0., 1., 0., 1., 0., 0., 1.,  0., 1., 0., 0., 1., 0., 1.])In [108]: Draw_road(road,A)

In [109]: A = generate_Matrix(100,200)In [110]: road , M=Max_road(A,0.1,10)In [111]: draw_road(road,A)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.