{"id":373129,"date":"2024-05-21T05:28:53","date_gmt":"2024-05-21T05:28:53","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=373129"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-21T05:28:53","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=373129","title":{"rendered":"<span>\u041f\u043e\u0438\u0441\u043a \u0441 \u043f\u043e\u0434\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u0438\u0435\u043c \u043d\u0430 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0445 \u0433\u0440\u0430\u0444\u0430\u0445<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p> \u041c\u0438\u0440 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438 \u0432\u044b\u0445\u043e\u043b\u043e\u0449\u0435\u043d\u043e \u0447\u0438\u0441\u0442. \u0420\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u043d\u0435 \u043f\u043e\u043b\u043d\u0430, \u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u0430.<\/p>\n<p>\u0412 \u0441\u0442\u0443\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u0438\u0435 90-\u0435 \u0433\u043e\u0434\u044b \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0432 \u0440\u0430\u0439\u043e\u043d\u0435 \u0433. \u041f\u0435\u0447\u0435\u0440\u044b \u041f\u0441\u043a\u043e\u0432\u0441\u043a\u043e\u0439 \u043e\u0431\u043b. \u0411\u044b\u043b \u043d\u043e\u044f\u0431\u0440\u044c. \u0421\u043f\u0443\u0441\u0442\u0438\u043b\u0441\u044f \u0440\u0430\u043d\u043d\u0438\u043c \u0443\u0442\u0440\u043e\u043c \u0441 \u0445\u043e\u043b\u043c\u0430. \u041f\u0435\u0440\u0435\u0434 \u0440\u0435\u0447\u043a\u043e\u0439 \u0431\u043e\u043b\u043e\u0442\u0438\u0441\u0442\u0430\u044f \u043c\u0435\u0441\u0442\u043d\u043e\u0441\u0442\u044c. \u041f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0440\u0430\u0437\u0434\u0435\u0432\u0430\u0442\u044c\u0441\u044f \u0438, \u0434\u0435\u0440\u0436\u0430 \u043e\u0434\u0435\u0436\u0434\u0443 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0440\u0443\u043a\u0435, \u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043f\u043e \u043f\u043e\u044f\u0441, \u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u043f\u043b\u044b\u0432\u0430\u0442\u044c. \u0421\u043b\u044b\u0448\u0430\u043b \u043f\u043e \u043b\u0430\u044e \u0441\u043e\u0431\u0430\u043a, \u043a\u0430\u043a \u043d\u0430\u0448\u0438 \u043f\u043e\u0433\u0440\u0430\u043d\u0446\u044b \u043f\u043e\u0448\u043b\u0438 \u043f\u043e \u0441\u043b\u0435\u0434\u0443. \u041d\u043e \u043d\u0438 \u0441\u043e\u0431\u0430\u043a\u0430\u043c, \u043d\u0438 \u0441\u043b\u0443\u0436\u0438\u0432\u044b\u043c \u0432 \u0432\u043e\u0434\u0443 \u043b\u0435\u0437\u0442\u044c \u0443\u0436\u0435 \u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c. \u041f\u043e\u0442\u043e\u043c \u0432\u0435\u0447\u0435\u0440. \u0422\u0435\u043c\u043d\u043e. \u0423\u0448\u0435\u043b \u043f\u043e \u043f\u043e\u043b\u044f\u0440\u043d\u043e\u0439 \u0437\u0432\u0435\u0437\u0434\u0435 <a href=\"https:\/\/ru-ru.topographic-map.com\/map-vw9x51\/%D0%9F%D0%B5%D1%87%D0%BE%D1%80%D1%8B\/?center=57.85425%2C27.64332&amp;zoom=12\" rel=\"noopener noreferrer nofollow\">\u043a \u0436\u0434 \u043d\u0430 \u0441\u0435\u0432\u0435\u0440\u043e-\u0437\u0430\u043f\u0430\u0434<\/a>. \u041d\u0430\u0437\u0430\u0434 \u0447\u0435\u0440\u0435\u0437 2 \u043c\u0435\u0441\u044f\u0446\u0430 \u043f\u043e\u0447\u0442\u0438 \u0442\u0430\u043a\u0436\u0435.<\/p>\n<p>\u0425\u043e\u0440\u043e\u0448\u043e, \u043a\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0447\u0451\u0442\u043a\u0438\u0435 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u044b. \u0418\u0441\u043a\u0430\u0442\u044c \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u044b, \u043d\u0435 \u0438\u043c\u0435\u044f \u0432\u0441\u0435\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 &#8212; \u0442\u0438\u043f\u0438\u0447\u043d\u0430\u044f \u0438 \u0432\u043e\u0441\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430. \u041d\u0430 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0445 \u0433\u0440\u0430\u0444\u0430\u0445 \u0441 \u043d\u0435\u0439 \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b. \u041d\u043e \u044d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043e\u0431\u0445\u043e\u0434\u0430 &#171;\u0432\u043e\u043b\u043d\u043e\u0439&#187; \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u0442\u043e, \u043a\u0430\u043a \u0434\u0432\u0438\u0436\u0435\u0442\u0441\u044f \u0447\u0435\u043b\u043e\u0432\u0435\u043a.<\/p>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0440\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a \u0441 \u043f\u043e\u0434\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u0438\u0435\u043c \u0438 \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0435 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0433\u0440\u0430\u0444\u044b.<\/p>\n<p>\u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0441\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0432\u0438\u0441\u0435\u0442\u044c \u043e\u0442 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u044b \u043d\u0430\u0433\u0440\u0430\u0434\u044b (\u0432\u0435\u043b\u0438\u0447\u0438\u043d\u044b \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u0430) \u0438 \u0434\u0438\u0441\u043a\u043e\u043d\u0442\u0430 \u0440\u0435\u0431\u0435\u0440 \u0433\u0440\u0430\u0444\u0430. \u0415\u0441\u043b\u0438 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 \u043d\u0430\u0433\u0440\u0430\u0434\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u0441\u0443\u043c\u043c\u044b \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 \u0440\u0435\u0431\u0435\u0440 \u043f\u043e \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0443, \u0442\u043e \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442. \u042d\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u0442\u043e, \u043a\u0430\u043a \u0432\u043d\u0430\u0447\u0430\u043b\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043e\u0438\u0441\u043a \u0438, \u0435\u0441\u043b\u0438 \u0446\u0435\u043b\u044c \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043d\u0430 \u044d\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u0441\u0442\u0430\u0432\u0438\u043c \u0433\u043e\u0440\u0443 \u0410\u043b\u0442\u0430\u044f \u0411\u0435\u043b\u0443\u0445\u0443. \u041f\u043e \u0441\u0443\u0442\u0438, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0442\u043e\u043f\u043e\u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u043a\u0430\u0440\u0442\u0443 \u043c\u0435\u0441\u0442\u043d\u043e\u0441\u0442\u0438. \u0413\u0440\u0430\u0434\u0438\u0435\u043d\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043a \u0446\u0435\u043b\u0438.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/3fe\/45e\/00d\/3fe45e00de67bf91381c240645e49284.jpg\" alt=\"\u0412\u044b\u0441\u043e\u0442\u0430 \u043d\u0430\u0434 \u0443\u0440\u043e\u0432\u043d\u0435\u043c \u043c\u043e\u0440\u044f: 4 506 \u043c\" title=\"\u0412\u044b\u0441\u043e\u0442\u0430 \u043d\u0430\u0434 \u0443\u0440\u043e\u0432\u043d\u0435\u043c \u043c\u043e\u0440\u044f: 4 506 \u043c\" width=\"2560\" height=\"1732\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3fe\/45e\/00d\/3fe45e00de67bf91381c240645e49284.jpg\" data-blurred=\"true\"\/><\/p>\n<div><figcaption>\u0412\u044b\u0441\u043e\u0442\u0430 \u043d\u0430\u0434 \u0443\u0440\u043e\u0432\u043d\u0435\u043c \u043c\u043e\u0440\u044f: 4 506 \u043c<\/figcaption><\/div>\n<\/figure>\n<p> \u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u043e q. \u0418, \u043a\u0430\u043a \u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435, \u0446\u0438\u043a\u043b\u044b \u0431\u0435\u0437 \u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u043e\u0441\u0442\u0438 \u043a \u0446\u0435\u043b\u0438, \u0435\u0441\u043b\u0438 \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u0443: \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0441 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c.<\/p>\n<p>\u0412\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441: \u0435\u0441\u043b\u0438 \u0446\u0435\u043b\u044c \u0432\u0441\u0435 \u0436\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043d\u043e \u043d\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u043a\u0430\u0440\u0442\u0443, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0432\u0441\u0435 \u0436\u0435 \u043f\u0440\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0445\u043e\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043c\u0430\u0440\u0448\u0440\u0443\u0442, \u043f\u0443\u0441\u0442\u044c \u0438 \u043d\u0435\u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439? \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0435\u0441\u0442\u044c. \u0423\u0434\u0430\u043b\u0438\u043c \u0432 \u0442\u0430\u043a\u043e\u043c \u0435\u0449\u0435 loop-\u044b \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u0430\u043a \u0437\u0434\u0435\u0441\u044c: [1, 2, 3, 5, 2, 4, 9] -> [1, 2, 4, 9]<\/p>\n<pre><code class=\"python\">import random  zero = 0  roundLim = 3 #\u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 \u043e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 visited = {} #\u0434\u043b\u044f \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u0439 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 (\u0432\u0435\u0440\u0448\u0438\u043d) cGraph = {} #\u043f\u043e\u0448\u0430\u0433\u043e\u0432\u043e \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044b \u0433\u0440\u0430\u0444\u0430 G qGraph = {} #\u043f\u043e\u0448\u0430\u0433\u043e\u0432\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e\u0442\u0441\u044f q: currentSate: {nextState:q}) routes = {} #\u0445\u0440\u0430\u043d\u0438\u0442 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u044b, \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0435 \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 targetState limit = 100 #\u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0433\u0440\u0430\u0444\u0435 epoch = 100 #\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u043f\u044b\u0442\u043e\u043a \u043f\u043e\u0438\u0441\u043a\u0430 \u0446\u0435\u043b\u0438 targetCost = 4506 #\u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 \u043d\u0430\u0433\u0440\u0430\u0434\u044b (reward) #\u0433. \u0411\u0435\u043b\u0443\u0445\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043d\u0430\u0434 \u0443\u0440\u043e\u0432\u043d\u0435\u043c \u043c\u043e\u0440\u044f: 4 506 \u043c targetState = 19 #\u0446\u0435\u043b\u0435\u0432\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u0434\u043b\u044f \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0438\u0436\u0435)  k = len(str(targetCost)) gamma = int('9'*k)\/int('1'+'0'*k)#\u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0434\u0438\u0441\u043a\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f  def set_q(currentState, nextStates):     for nextState in nextStates:         cMax = zero         if nextState in qGraph:             nnextStates = [nnextState for nnextState in qGraph[nextState]]             for nnextState in nnextStates:                 if gamma*qGraph[nextState][nnextState] > cMax:                     cMax = gamma*qGraph[nextState][nnextState]             if cMax - cGraph[currentState][nextState] > zero:                 qGraph[currentState][nextState] = \\                   round(cMax - gamma*cGraph[currentState][nextState], \\                   roundLim)             else: qGraph[currentState][nextState] = zero         if nextState == targetState:             qGraph[currentState][nextState] = targetCost - \\               cGraph[currentState][nextState]             return          def get_path(currentState):     lim = limit     path = []     while lim > 0 and currentState != None:         path += [currentState];         visited[currentState] +=1         lim -=1         if currentState == targetState:             return path           nextStateQ = best_q(currentState)              if len(path) ==1:             currentState = best_q(currentState)             continue         currentState = nextStateQ     return []   def get_first_route(startState):     for state in routes:         if targetState in routes[state] and startState in routes[state]:             startStateInd = routes[state].index(startState)             return routes[state][startStateInd:]              def best_q(currentState):     try:         toSorted = [(nextState, qGraph[currentState][nextState]) \\           for nextState in qGraph[currentState]]         nextState = sorted(toSorted, key = lambda t: t[1], \\           reverse=True)[0][0]         return  nextState     except:          return       def next_states(currentState):     if currentState not in cGraph:         cGraph[currentState] = G[currentState]         qGraph[currentState] = {}         for nextState in cGraph[currentState]:             qGraph[currentState][nextState] = zero     if currentState not in visited:         visited[currentState] = 1     else:          visited[currentState] += 1     nextStates = [nextState for nextState in G[currentState]]     for nextState in nextStates:        if nextState not in visited:            visited[nextState] = zero     set_q(currentState, nextStates)     return nextStates        def less_visited(currentState, nextStates):     if not nextStates: return     toSorted = [(nextState, visited[nextState]) \\       for nextState in nextStates]     sortedStates = sorted(toSorted, key = lambda t: t[1])     minVisited = [nextState for nextState in sortedStates \\       if sortedStates[0][1] == nextState[1]]     nextState = random.choice(minVisited)[0]     visited[nextState] +=1     return nextState      def search_target(currentState, route):     lim = limit     route.append(currentState)     nextStates = next_states(currentState)     while lim > 0:         if not nextStates:              return []         lim -=1         if currentState == targetState:             return route          currentState = less_visited(currentState, nextStates)         route.append(currentState)         nextStates = next_states(currentState)         return []      def delete_loop(route):     sRoute = []     i = 0     while i &lt; len(route)-1:         if route[i] in route[i+1:]:             i = len(route) - route[::-1].index(route[i]) -1          sRoute.append(route[i])         i +=1     if route[-1] not in sRoute:          sRoute.append(route[-1])     return sRoute  #\u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e pyrgg.     G = {   0 : {6: 7, 14: 7, 0: 16},   16 : {3: 9, 17: 1},   6 : {18: 0, 11: 7, 17: 3},   14 : {18: 9, 17: 3, 8: 4},   1 : {17: 5, 2: 2, 12: 2, 3: 1, 5: 6, 18: 4, 1: 12},   12 : {7: 2, 19: 6, 3: 8, 2: 2},   18 : {10: 8, 2: 6},   5 : {16: 9, 2: 6, 13: 2, 9: 2, 19: 9, 15: 2, 10: 4},   3 : {15: 5, 19: 2, 8: 0, 10: 6},   17 : {},   2 : {11: 5},   11 : {0: 0, 13: 0, 17: 9},   10 : {13: 3, 12: 7, 2: 6},   8 : {9: 0, 15: 6, 14: 9},   19 : {15: 6, 0: 6},   15 : {16: 6},   4 : {8: 2, 18: 4, 11: 9, 16: 0, 10: 8, 15: 5, 14: 0, 9: 6, 5: 9, 4: 14},   9 : {13: 8, 18: 7, 12: 8, 17: 0, 2: 4, 16: 0},   13 : {18: 7},   7 : {19: 6, 9: 7, 7: 12}, }       def main():     gStates = [state for state in G]         route = []     startState = 0     for i in range(1, epoch+1):         initState = random.choice(gStates)         #initState = startState         rRoute = search_target(initState, route.copy())         if rRoute:             routes[rRoute[0]] = delete_loop(rRoute)          currentState = startState      path = get_path(currentState)     #print(rRoute, \"\\n\\n\", visited, \"\\n\\n\", cGraph, \"\\n\\n\", qGraph)       if path:         print(\"\\npath =\", path)     else:         path = get_first_route(startState)         print(\"get_first_route =\", path)      main() <\/code><\/pre>\n<p>\u041d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0447\u0430\u0441\u0442\u043e\u0442\u043d\u044b\u0439 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u043f\u0443\u0442\u044c: path = [0, 14, 8, 9, 16, 3, 19]<\/p>\n<p>\u0418\u0437\u043c\u0435\u043d\u0438\u043c targetCost. \u041f\u0440\u0438\u0440\u0430\u0432\u043d\u044f\u0435\u043c \u043f\u044f\u0442\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438 &#171;\u0442\u043e, \u0447\u0442\u043e \u0435\u0441\u0442\u044c&#187;. \u041c\u0430\u0440\u0448\u0440\u0443\u0442 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d \u0432\u043e\u043e\u0431\u0449\u0435. \u0418\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d \u0445\u043e\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u0442\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a\u043e\u0439: path = [0, 14, 8, 15, 16, 3, 19]<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/bda\/0fa\/147\/bda0fa147eec747a403348524a237aa6.png\" alt=\"\u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0438\" title=\"\u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0438\" width=\"1280\" height=\"984\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bda\/0fa\/147\/bda0fa147eec747a403348524a237aa6.png\"\/><\/p>\n<div><figcaption>\u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0438<\/figcaption><\/div>\n<\/figure>\n<p><a href=\"https:\/\/algodaily.com\/lessons\/an-illustrated-guide-to-dijkstras-algorithm\/python\" rel=\"noopener noreferrer nofollow\">\u0414\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f<\/a> &#8230; \u0438\u0437 \u043c\u0438\u0440\u0430 \u0438\u0441\u0442\u0438\u043d\u044b.<\/p>\n<pre><code class=\"python\">import math  graph = { 0 : {6: 7, 14: 7, 0: 16} , 16 : {3: 9, 17: 1} , 6 : {18: 0, 11: 7, 17: 3} , 14 : {18: 9, 17: 3, 8: 4} , 1 : {17: 5, 2: 2, 12: 2, 3: 1, 5: 6, 18: 4, 1: 12} , 12 : {7: 2, 19: 6, 3: 8, 2: 2} , 18 : {10: 8, 2: 6} , 5 : {16: 9, 2: 6, 13: 2, 9: 2, 19: 9, 15: 2, 10: 4} , 3 : {15: 5, 19: 2, 8: 0, 10: 6} , 17 : {} , 2 : {11: 5} , 11 : {0: 0, 13: 0, 17: 9} , 10 : {13: 3, 12: 7, 2: 6} , 8 : {9: 0, 15: 6, 14: 9} , 19 : {15: 6, 0: 6} , 15 : {16: 6} , 4 : {8: 2, 18: 4, 11: 9, 16: 0, 10: 8, 15: 5, 14: 0, 9: 6, 5: 9, 4: 14} , 9 : {13: 8, 18: 7, 12: 8, 17: 0, 2: 4, 16: 0} , 13 : {18: 7} , 7 : {19: 6, 9: 7, 7: 12} , }  source = 0 destination = 19  unvisited = graph shortest_distances = {} route = [] path_nodes = {}  for nodes in unvisited:     shortest_distances[nodes] = math.inf shortest_distances[source] = 0  while unvisited:     min_node = None     for current_node in unvisited:         if min_node is None:             min_node = current_node         elif shortest_distances[min_node] \\             > shortest_distances[current_node]:             min_node = current_node     for (node, value) in unvisited[min_node].items():         if value + shortest_distances[min_node] \\             &lt; shortest_distances[node]:             shortest_distances[node] = value \\                 + shortest_distances[min_node]             path_nodes[node] = min_node     unvisited.pop(min_node) node = destination  while node != source:     try:         route.insert(0, node)         node = path_nodes[node]     except Exception:         print('Path not reachable')         break route.insert(0, source)  print(route) #[0, 14, 8, 9, 16, 3, 19] print(\"\\n\", shortest_distances)<\/code><\/pre>\n<p>PS \u0423\u0434\u0430\u0447\u0438.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/806445\/\"> https:\/\/habr.com\/ru\/articles\/806445\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p> \u041c\u0438\u0440 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438 \u0432\u044b\u0445\u043e\u043b\u043e\u0449\u0435\u043d\u043e \u0447\u0438\u0441\u0442. \u0420\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u043d\u0435 \u043f\u043e\u043b\u043d\u0430, \u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u0430.<\/p>\n<p>\u0412 \u0441\u0442\u0443\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u0438\u0435 90-\u0435 \u0433\u043e\u0434\u044b \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0432 \u0440\u0430\u0439\u043e\u043d\u0435 \u0433. \u041f\u0435\u0447\u0435\u0440\u044b \u041f\u0441\u043a\u043e\u0432\u0441\u043a\u043e\u0439 \u043e\u0431\u043b. \u0411\u044b\u043b \u043d\u043e\u044f\u0431\u0440\u044c. \u0421\u043f\u0443\u0441\u0442\u0438\u043b\u0441\u044f \u0440\u0430\u043d\u043d\u0438\u043c \u0443\u0442\u0440\u043e\u043c \u0441 \u0445\u043e\u043b\u043c\u0430. \u041f\u0435\u0440\u0435\u0434 \u0440\u0435\u0447\u043a\u043e\u0439 \u0431\u043e\u043b\u043e\u0442\u0438\u0441\u0442\u0430\u044f \u043c\u0435\u0441\u0442\u043d\u043e\u0441\u0442\u044c. \u041f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0440\u0430\u0437\u0434\u0435\u0432\u0430\u0442\u044c\u0441\u044f \u0438, \u0434\u0435\u0440\u0436\u0430 \u043e\u0434\u0435\u0436\u0434\u0443 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0440\u0443\u043a\u0435, \u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043f\u043e \u043f\u043e\u044f\u0441, \u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u043f\u043b\u044b\u0432\u0430\u0442\u044c. \u0421\u043b\u044b\u0448\u0430\u043b \u043f\u043e \u043b\u0430\u044e \u0441\u043e\u0431\u0430\u043a, \u043a\u0430\u043a \u043d\u0430\u0448\u0438 \u043f\u043e\u0433\u0440\u0430\u043d\u0446\u044b \u043f\u043e\u0448\u043b\u0438 \u043f\u043e \u0441\u043b\u0435\u0434\u0443. \u041d\u043e \u043d\u0438 \u0441\u043e\u0431\u0430\u043a\u0430\u043c, \u043d\u0438 \u0441\u043b\u0443\u0436\u0438\u0432\u044b\u043c \u0432 \u0432\u043e\u0434\u0443 \u043b\u0435\u0437\u0442\u044c \u0443\u0436\u0435 \u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c. \u041f\u043e\u0442\u043e\u043c \u0432\u0435\u0447\u0435\u0440. \u0422\u0435\u043c\u043d\u043e. \u0423\u0448\u0435\u043b \u043f\u043e \u043f\u043e\u043b\u044f\u0440\u043d\u043e\u0439 \u0437\u0432\u0435\u0437\u0434\u0435 <a href=\"https:\/\/ru-ru.topographic-map.com\/map-vw9x51\/%D0%9F%D0%B5%D1%87%D0%BE%D1%80%D1%8B\/?center=57.85425%2C27.64332&amp;zoom=12\" rel=\"noopener noreferrer nofollow\">\u043a \u0436\u0434 \u043d\u0430 \u0441\u0435\u0432\u0435\u0440\u043e-\u0437\u0430\u043f\u0430\u0434<\/a>. \u041d\u0430\u0437\u0430\u0434 \u0447\u0435\u0440\u0435\u0437 2 \u043c\u0435\u0441\u044f\u0446\u0430 \u043f\u043e\u0447\u0442\u0438 \u0442\u0430\u043a\u0436\u0435.<\/p>\n<p>\u0425\u043e\u0440\u043e\u0448\u043e, \u043a\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0447\u0451\u0442\u043a\u0438\u0435 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u044b. \u0418\u0441\u043a\u0430\u0442\u044c \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u044b, \u043d\u0435 \u0438\u043c\u0435\u044f \u0432\u0441\u0435\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 &#8212; \u0442\u0438\u043f\u0438\u0447\u043d\u0430\u044f \u0438 \u0432\u043e\u0441\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430. \u041d\u0430 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0445 \u0433\u0440\u0430\u0444\u0430\u0445 \u0441 \u043d\u0435\u0439 \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b. \u041d\u043e \u044d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043e\u0431\u0445\u043e\u0434\u0430 &#171;\u0432\u043e\u043b\u043d\u043e\u0439&#187; \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u0442\u043e, \u043a\u0430\u043a \u0434\u0432\u0438\u0436\u0435\u0442\u0441\u044f \u0447\u0435\u043b\u043e\u0432\u0435\u043a.<\/p>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0440\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a \u0441 \u043f\u043e\u0434\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u0438\u0435\u043c \u0438 \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0435 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0433\u0440\u0430\u0444\u044b.<\/p>\n<p>\u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0441\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0432\u0438\u0441\u0435\u0442\u044c \u043e\u0442 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u044b \u043d\u0430\u0433\u0440\u0430\u0434\u044b (\u0432\u0435\u043b\u0438\u0447\u0438\u043d\u044b \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u0430) \u0438 \u0434\u0438\u0441\u043a\u043e\u043d\u0442\u0430 \u0440\u0435\u0431\u0435\u0440 \u0433\u0440\u0430\u0444\u0430. \u0415\u0441\u043b\u0438 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 \u043d\u0430\u0433\u0440\u0430\u0434\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u0441\u0443\u043c\u043c\u044b \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 \u0440\u0435\u0431\u0435\u0440 \u043f\u043e \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0443, \u0442\u043e \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442. \u042d\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u0442\u043e, \u043a\u0430\u043a \u0432\u043d\u0430\u0447\u0430\u043b\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043e\u0438\u0441\u043a \u0438, \u0435\u0441\u043b\u0438 \u0446\u0435\u043b\u044c \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043d\u0430 \u044d\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u0441\u0442\u0430\u0432\u0438\u043c \u0433\u043e\u0440\u0443 \u0410\u043b\u0442\u0430\u044f \u0411\u0435\u043b\u0443\u0445\u0443. \u041f\u043e \u0441\u0443\u0442\u0438, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0442\u043e\u043f\u043e\u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u043a\u0430\u0440\u0442\u0443 \u043c\u0435\u0441\u0442\u043d\u043e\u0441\u0442\u0438. \u0413\u0440\u0430\u0434\u0438\u0435\u043d\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043a \u0446\u0435\u043b\u0438.<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0412\u044b\u0441\u043e\u0442\u0430 \u043d\u0430\u0434 \u0443\u0440\u043e\u0432\u043d\u0435\u043c \u043c\u043e\u0440\u044f: 4 506 \u043c<\/figcaption><\/div>\n<\/figure>\n<p> \u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u043e q. \u0418, \u043a\u0430\u043a \u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435, \u0446\u0438\u043a\u043b\u044b \u0431\u0435\u0437 \u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u043e\u0441\u0442\u0438 \u043a \u0446\u0435\u043b\u0438, \u0435\u0441\u043b\u0438 \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u0443: \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0441 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c.<\/p>\n<p>\u0412\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441: \u0435\u0441\u043b\u0438 \u0446\u0435\u043b\u044c \u0432\u0441\u0435 \u0436\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043d\u043e \u043d\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u043a\u0430\u0440\u0442\u0443, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0432\u0441\u0435 \u0436\u0435 \u043f\u0440\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0445\u043e\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043c\u0430\u0440\u0448\u0440\u0443\u0442, \u043f\u0443\u0441\u0442\u044c \u0438 \u043d\u0435\u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439? \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0435\u0441\u0442\u044c. \u0423\u0434\u0430\u043b\u0438\u043c \u0432 \u0442\u0430\u043a\u043e\u043c \u0435\u0449\u0435 loop-\u044b \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u0430\u043a \u0437\u0434\u0435\u0441\u044c: [1, 2, 3, 5, 2, 4, 9] -> [1, 2, 4, 9]<\/p>\n<pre><code class=\"python\">import random  zero = 0  roundLim = 3 #\u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 \u043e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 visited = {} #\u0434\u043b\u044f \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u0439 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 (\u0432\u0435\u0440\u0448\u0438\u043d) cGraph = {} #\u043f\u043e\u0448\u0430\u0433\u043e\u0432\u043e \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044b \u0433\u0440\u0430\u0444\u0430 G qGraph = {} #\u043f\u043e\u0448\u0430\u0433\u043e\u0432\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e\u0442\u0441\u044f q: currentSate: {nextState:q}) routes = {} #\u0445\u0440\u0430\u043d\u0438\u0442 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u044b, \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0435 \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 targetState limit = 100 #\u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0433\u0440\u0430\u0444\u0435 epoch = 100 #\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u043f\u044b\u0442\u043e\u043a \u043f\u043e\u0438\u0441\u043a\u0430 \u0446\u0435\u043b\u0438 targetCost = 4506 #\u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 \u043d\u0430\u0433\u0440\u0430\u0434\u044b (reward) #\u0433. \u0411\u0435\u043b\u0443\u0445\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043d\u0430\u0434 \u0443\u0440\u043e\u0432\u043d\u0435\u043c \u043c\u043e\u0440\u044f: 4 506 \u043c targetState = 19 #\u0446\u0435\u043b\u0435\u0432\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u0434\u043b\u044f \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0438\u0436\u0435)  k = len(str(targetCost)) gamma = int('9'*k)\/int('1'+'0'*k)#\u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0434\u0438\u0441\u043a\u043e\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f  def set_q(currentState, nextStates):     for nextState in nextStates:         cMax = zero         if nextState in qGraph:             nnextStates = [nnextState for nnextState in qGraph[nextState]]             for nnextState in nnextStates:                 if gamma*qGraph[nextState][nnextState] > cMax:                     cMax = gamma*qGraph[nextState][nnextState]             if cMax - cGraph[currentState][nextState] > zero:                 qGraph[currentState][nextState] = \\                   round(cMax - gamma*cGraph[currentState][nextState], \\                   roundLim)             else: qGraph[currentState][nextState] = zero         if nextState == targetState:             qGraph[currentState][nextState] = targetCost - \\               cGraph[currentState][nextState]             return          def get_path(currentState):     lim = limit     path = []     while lim > 0 and currentState != None:         path += [currentState];         visited[currentState] +=1         lim -=1         if currentState == targetState:             return path           nextStateQ = best_q(currentState)              if len(path) ==1:             currentState = best_q(currentState)             continue         currentState = nextStateQ     return []   def get_first_route(startState):     for state in routes:         if targetState in routes[state] and startState in routes[state]:             startStateInd = routes[state].index(startState)             return routes[state][startStateInd:]              def best_q(currentState):     try:         toSorted = [(nextState, qGraph[currentState][nextState]) \\           for nextState in qGraph[currentState]]         nextState = sorted(toSorted, key = lambda t: t[1], \\           reverse=True)[0][0]         return  nextState     except:          return       def next_states(currentState):     if currentState not in cGraph:         cGraph[currentState] = G[currentState]         qGraph[currentState] = {}         for nextState in cGraph[currentState]:             qGraph[currentState][nextState] = zero     if currentState not in visited:         visited[currentState] = 1     else:          visited[currentState] += 1     nextStates = [nextState for nextState in G[currentState]]     for nextState in nextStates:        if nextState not in visited:            visited[nextState] = zero     set_q(currentState, nextStates)     return nextStates        def less_visited(currentState, nextStates):     if not nextStates: return     toSorted = [(nextState, visited[nextState]) \\       for nextState in nextStates]     sortedStates = sorted(toSorted, key = lambda t: t[1])     minVisited = [nextState for nextState in sortedStates \\       if sortedStates[0][1] == nextState[1]]     nextState = random.choice(minVisited)[0]     visited[nextState] +=1     return nextState      def search_target(currentState, route):     lim = limit     route.append(currentState)     nextStates = next_states(currentState)     while lim > 0:         if not nextStates:              return []         lim -=1         if currentState == targetState:             return route          currentState = less_visited(currentState, nextStates)         route.append(currentState)         nextStates = next_states(currentState)         return []      def delete_loop(route):     sRoute = []     i = 0     while i &lt; len(route)-1:         if route[i] in route[i+1:]:             i = len(route) - route[::-1].index(route[i]) -1          sRoute.append(route[i])         i +=1     if route[-1] not in sRoute:          sRoute.append(route[-1])     return sRoute  #\u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e pyrgg.     G = {   0 : {6: 7, 14: 7, 0: 16},   16 : {3: 9, 17: 1},   6 : {18: 0, 11: 7, 17: 3},   14 : {18: 9, 17: 3, 8: 4},   1 : {17: 5, 2: 2, 12: 2, 3: 1, 5: 6, 18: 4, 1: 12},   12 : {7: 2, 19: 6, 3: 8, 2: 2},   18 : {10: 8, 2: 6},   5 : {16: 9, 2: 6, 13: 2, 9: 2, 19: 9, 15: 2, 10: 4},   3 : {15: 5, 19: 2, 8: 0, 10: 6},   17 : {},   2 : {11: 5},   11 : {0: 0, 13: 0, 17: 9},   10 : {13: 3, 12: 7, 2: 6},   8 : {9: 0, 15: 6, 14: 9},   19 : {15: 6, 0: 6},   15 : {16: 6},   4 : {8: 2, 18: 4, 11: 9, 16: 0, 10: 8, 15: 5, 14: 0, 9: 6, 5: 9, 4: 14},   9 : {13: 8, 18: 7, 12: 8, 17: 0, 2: 4, 16: 0},   13 : {18: 7},   7 : {19: 6, 9: 7, 7: 12}, }       def main():     gStates = [state for state in G]         route = []     startState = 0     for i in range(1, epoch+1):         initState = random.choice(gStates)         #initState = startState         rRoute = search_target(initState, route.copy())         if rRoute:             routes[rRoute[0]] = delete_loop(rRoute)          currentState = startState      path = get_path(currentState)     #print(rRoute, \"\\n\\n\", visited, \"\\n\\n\", cGraph, \"\\n\\n\", qGraph)       if path:         print(\"\\npath =\", path)     else:         path = get_first_route(startState)         print(\"get_first_route =\", path)      main() <\/code><\/pre>\n<p>\u041d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0447\u0430\u0441\u0442\u043e\u0442\u043d\u044b\u0439 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u043f\u0443\u0442\u044c: path = [0, 14, 8, 9, 16, 3, 19]<\/p>\n<p>\u0418\u0437\u043c\u0435\u043d\u0438\u043c targetCost. \u041f\u0440\u0438\u0440\u0430\u0432\u043d\u044f\u0435\u043c \u043f\u044f\u0442\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438 &#171;\u0442\u043e, \u0447\u0442\u043e \u0435\u0441\u0442\u044c&#187;. \u041c\u0430\u0440\u0448\u0440\u0443\u0442 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d \u0432\u043e\u043e\u0431\u0449\u0435. \u0418\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d \u0445\u043e\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u0442\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a\u043e\u0439: path = [0, 14, 8, 15, 16, 3, 19]<\/p>\n<figure class=\"full-width\">\n<div><figcaption>\u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0438<\/figcaption><\/div>\n<\/figure>\n<p><a href=\"https:\/\/algodaily.com\/lessons\/an-illustrated-guide-to-dijkstras-algorithm\/python\" rel=\"noopener noreferrer nofollow\">\u0414\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f<\/a> &#8230; \u0438\u0437 \u043c\u0438\u0440\u0430 \u0438\u0441\u0442\u0438\u043d\u044b.<\/p>\n<pre><code class=\"python\">import math  graph = { 0 : {6: 7, 14: 7, 0: 16} , 16 : {3: 9, 17: 1} , 6 : {18: 0, 11: 7, 17: 3} , 14 : {18: 9, 17: 3, 8: 4} , 1 : {17: 5, 2: 2, 12: 2, 3: 1, 5: 6, 18: 4, 1: 12} , 12 : {7: 2, 19: 6, 3: 8, 2: 2} , 18 : {10: 8, 2: 6} , 5 : {16: 9, 2: 6, 13: 2, 9: 2, 19: 9, 15: 2, 10: 4} , 3 : {15: 5, 19: 2, 8: 0, 10: 6} , 17 : {} , 2 : {11: 5} , 11 : {0: 0, 13: 0, 17: 9} , 10 : {13: 3, 12: 7, 2: 6} , 8 : {9: 0, 15: 6, 14: 9} , 19 : {15: 6, 0: 6} , 15 : {16: 6} , 4 : {8: 2, 18: 4, 11: 9, 16: 0, 10: 8, 15: 5, 14: 0, 9: 6, 5: 9, 4: 14} , 9 : {13: 8, 18: 7, 12: 8, 17: 0, 2: 4, 16: 0} , 13 : {18: 7} , 7 : {19: 6, 9: 7, 7: 12} , }  source = 0 destination = 19  unvisited = graph shortest_distances = {} route = [] path_nodes = {}  for nodes in unvisited:     shortest_distances[nodes] = math.inf shortest_distances[source] = 0  while unvisited:     min_node = None     for current_node in unvisited:         if min_node is None:             min_node = current_node         elif shortest_distances[min_node] \\             > shortest_distances[current_node]:             min_node = current_node     for (node, value) in unvisited[min_node].items():         if value + shortest_distances[min_node] \\             &lt; shortest_distances[node]:             shortest_distances[node] = value \\                 + shortest_distances[min_node]             path_nodes[node] = min_node     unvisited.pop(min_node) node = destination  while node != source:     try:         route.insert(0, node)         node = path_nodes[node]     except Exception:         print('Path not reachable')         break route.insert(0, source)  print(route) #[0, 14, 8, 9, 16, 3, 19] print(\"\\n\", shortest_distances)<\/code><\/pre>\n<p>PS \u0423\u0434\u0430\u0447\u0438.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/806445\/\"> https:\/\/habr.com\/ru\/articles\/806445\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-373129","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/373129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=373129"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/373129\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=373129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=373129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=373129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}