PyGAD genetic means – punishing cluster size

I am working on a problem which, from what I can tell from prior research, is troubling many people in the industry I work in. In civic engineering, particularly fibre-optic cables, house connections (HCs) need to be grouped into clusters hooked up to network distributors (NDs). Intuitively, one might assume this to be a problem for simple clustering algorithms such as kmeans, however, the issue arises with the infrastructure-imposed limit of 20 HCs per ND. If HCs are too dense in one area, kmeans etc. construct clusters exceeding such limitations.

From what I can tell, the only commercially available solution to this is ArcGIS’ build balanced zones algorithm, however, for the sake of FOSS, I’d personally want to avoid using ArcGIS. However, thankfully, ESRI does provide background information on how bbs works, which is why I’ve been reading up on genetic algorithms and ended up with PyGAD by Gad, A.F.. His tutorial helped me out greatly, however, I am struggling with adapting the fitness function for my needs, i.e. punishing solutions resulting in clusters exceeding populations of 20.

The original function is as follows and scores solutions based on the density of the clusters it results in, i.e., it is equivalent to kmeans:

def fitness_func(solution, solution_idx):
    cluster_centers, all_clusters_dists, cluster_indices, clusters, clusters_sum_dist = cluster_data(solution, solution_idx)

    fitness = 1.0 / (np.sum(clusters_sum_dist) + 0.00000001)

    return fitness
Best solution is [ 6.76857432 87.25666069 82.82788371 84.71676014 17.01672128 41.12036676
  6.71771791  9.91987939 83.17725224 28.87028453 87.64197362 53.89675135
 29.77371507 64.36472386 58.77172181 59.66950147 40.07732671 83.47739475
 59.29050269 13.28871487]
Fitness of the best solution is 0.0004500494908839888
Best solution found after 100 generations

K-means-clusters generated according to Gad, A.F.

I thought I’d add a punishment term for exceeding cluster sizes of 20, however, the way I did it, seems to punish all chromosomes (I hope I’m using the term properly, I’m still a bit fresh to genetic algorithms) equally.

def capped_clusters(solution, solution_idx):
    cluster_centers, all_clusters_dists, cluster_indices, clusters, clusters_sum_dist = cluster_data(solution, solution_idx)

    fitness = 1.0 / (np.sum(clusters_sum_dist) + 0.00000001) #1 is the optimum

    if any(element > 20 for element in [len(cluster) for cluster in clusters]):
        fitness = 0
    else:
        pass

    return fitness
Best solution is [66.16381384 94.42791405 34.78040731 64.66917082 90.50249179 24.68926304
  7.54327271 66.90232116 55.22703763 88.59661014  1.87161791 93.35067003
 39.58200881 57.61364249 65.91865205 16.62963165  4.22783108 67.18573653
 15.57759318  9.46596451]
Fitness of the best solution is 0
Best solution found after 0 generations

How can I selectively punish cluster sizes exceeding 20 to push the algorithm towards evolving in a way that favours sub-20-member solutions to the clustering problem?

Note: I have downgraded to PyGAD 2.10.0 for compatibility with the tutorial code from the link above. This required me to manually remove any mentions of the obsoltete numpy.int and numpy.float from the source code.

Cheers and thank you very much!

New contributor

Thomas Hoffmann is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật