Comments (6)
改变目标函数就可以了,有一个更复杂的案例 #26
from scikit-opt.
下面这个是起点终点不闭合,但是起点终点也由算法选取:
import numpy as np
from scipy import spatial
import matplotlib.pyplot as plt
num_points = 20
points_coordinate = np.random.rand(num_points, 2) # generate coordinate of points
distance_matrix = spatial.distance.cdist(points_coordinate, points_coordinate, metric='euclidean')
def cal_total_distance(routine):
'''The objective function. input routine, return total distance.
cal_total_distance(np.arange(num_points))
'''
num_points, = routine.shape
return sum([distance_matrix[routine[i], routine[i + 1]] for i in range(num_points-1)])
from sko.GA import GA_TSP
ga_tsp = GA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=50, max_iter=500, prob_mut=1)
best_points, best_distance = ga_tsp.run()
fig, ax = plt.subplots(1, 2)
best_points_coordinate = points_coordinate[best_points, :]
ax[0].plot(best_points_coordinate[:, 0], best_points_coordinate[:, 1], 'o-r')
ax[1].plot(ga_tsp.generation_best_Y)
plt.show()
下面这个是起点终点不闭合,且起点终点事先指定:
import numpy as np
from scipy import spatial
import matplotlib.pyplot as plt
num_points = 20
points_coordinate = np.random.rand(num_points, 2) # generate coordinate of points
start_point=[[0,0]]
end_point=[[1,1]]
points_coordinate=np.concatenate([points_coordinate,start_point,end_point])
distance_matrix = spatial.distance.cdist(points_coordinate, points_coordinate, metric='euclidean')
def cal_total_distance(routine):
'''The objective function. input routine, return total distance.
cal_total_distance(np.arange(num_points))
'''
num_points, = routine.shape
# start_point,end_point 本身不参与优化。给一个固定的值,参与计算总路径
routine=np.concatenate([[num_points],routine,[num_points+1]])
return sum([distance_matrix[routine[i], routine[i + 1]] for i in range(num_points+2-1)])
from sko.GA import GA_TSP
ga_tsp = GA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=50, max_iter=500, prob_mut=1)
best_points, best_distance = ga_tsp.run()
fig, ax = plt.subplots(1, 2)
best_points_ = np.concatenate([[num_points],best_points, [num_points+1]])
best_points_coordinate = points_coordinate[best_points_, :]
ax[0].plot(best_points_coordinate[:, 0], best_points_coordinate[:, 1], 'o-r')
ax[1].plot(ga_tsp.generation_best_Y)
plt.show()
from scikit-opt.
感谢大佬,我研究学习一下
from scikit-opt.
请问如果,中间点数量确定,位置不确定有办法解决么
from scikit-opt.
请问如果,中间点数量确定,位置不确定有办法解决么
那最优就是个直线吧
如果每段带权重的话,可以把每个点的坐标也加入到变量中参与优化。
from scikit-opt.
How should I define the function to fix this variant of the problem with ACA_TSP? Thank you very much
from scikit-opt.
Related Issues (20)
- 没有连续域蚁群算法的实现
- crossover.py文件中crossover_2point_bit算子起作用吗
- How to change crossover rate? HOT 1
- 请问GA在做整数规划的时候怎么设置才能保证每次n个变量间不重复 HOT 1
- Allow users to set number of process pool workers/threads
- 如何在初始化阶段将表现良好的个体加入种群?
- 规定GA起始点
- 约束条件不生效
- 请教多进程模式下,如何释放占用内存 HOT 3
- ACA算法发生除0问题
- 请问模拟退火算法出自于哪里?
- 希望增加进度条
- 如何设置遗传算法优化函数最大化?提供的一些demo都是目标函数最小化问题求解
- 请问,如何解决一个简单的VRP问题?
- 如何用已知的较好参数初始化种群 HOT 2
- 默认优化目标最大还是最小
- solve the problem that use grid route
- 计算加速
- 非优化参数的传入 HOT 2
- BUG: array shape mismatch between Y and pbest_y in PSO, error raised in update_pbest HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scikit-opt.