commit 004187541ccd6638fd6f6d7fefd0a8f525c15b81 Author: Choi lab Date: Tue May 12 11:07:13 2026 +0900 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4ed587c --- /dev/null +++ b/.gitignore @@ -0,0 +1,359 @@ +# ---> C +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +# ---> TeX +## Core latex/pdflatex auxiliary files: +*.aux +*.lof +*.log +*.lot +*.fls +*.out +*.toc +*.fmt +*.fot +*.cb +*.cb2 +.*.lb + +## Intermediate documents: +*.dvi +*.xdv +*-converted-to.* +# these rules might exclude image files for figures etc. +# *.ps +# *.eps +# *.pdf + +## Generated if empty string is given at "Please type another file name for output:" +.pdf + +## Bibliography auxiliary files (bibtex/biblatex/biber): +*.bbl +*.bcf +*.blg +*-blx.aux +*-blx.bib +*.run.xml + +## Build tool auxiliary files: +*.fdb_latexmk +*.synctex +*.synctex(busy) +*.synctex.gz +*.synctex.gz(busy) +*.pdfsync + +## Build tool directories for auxiliary files +# latexrun +latex.out/ + +## Auxiliary and intermediate files from other packages: +# algorithms +*.alg +*.loa + +# achemso +acs-*.bib + +# amsthm +*.thm + +# beamer +*.nav +*.pre +*.snm +*.vrb + +# changes +*.soc + +# comment +*.cut + +# cprotect +*.cpt + +# elsarticle (documentclass of Elsevier journals) +*.spl + +# endnotes +*.ent + +# fixme +*.lox + +# feynmf/feynmp +*.mf +*.mp +*.t[1-9] +*.t[1-9][0-9] +*.tfm + +#(r)(e)ledmac/(r)(e)ledpar +*.end +*.?end +*.[1-9] +*.[1-9][0-9] +*.[1-9][0-9][0-9] +*.[1-9]R +*.[1-9][0-9]R +*.[1-9][0-9][0-9]R +*.eledsec[1-9] +*.eledsec[1-9]R +*.eledsec[1-9][0-9] +*.eledsec[1-9][0-9]R +*.eledsec[1-9][0-9][0-9] +*.eledsec[1-9][0-9][0-9]R + +# glossaries +*.acn +*.acr +*.glg +*.glo +*.gls +*.glsdefs +*.lzo +*.lzs +*.slg +*.slo +*.sls + +# uncomment this for glossaries-extra (will ignore makeindex's style files!) +# *.ist + +# gnuplot +*.gnuplot +*.table + +# gnuplottex +*-gnuplottex-* + +# gregoriotex +*.gaux +*.glog +*.gtex + +# htlatex +*.4ct +*.4tc +*.idv +*.lg +*.trc +*.xref + +# hyperref +*.brf + +# knitr +*-concordance.tex +# TODO Uncomment the next line if you use knitr and want to ignore its generated tikz files +# *.tikz +*-tikzDictionary + +# listings +*.lol + +# luatexja-ruby +*.ltjruby + +# makeidx +*.idx +*.ilg +*.ind + +# minitoc +*.maf +*.mlf +*.mlt +*.mtc[0-9]* +*.slf[0-9]* +*.slt[0-9]* +*.stc[0-9]* + +# minted +_minted* +*.pyg + +# morewrites +*.mw + +# newpax +*.newpax + +# nomencl +*.nlg +*.nlo +*.nls + +# pax +*.pax + +# pdfpcnotes +*.pdfpc + +# sagetex +*.sagetex.sage +*.sagetex.py +*.sagetex.scmd + +# scrwfile +*.wrt + +# svg +svg-inkscape/ + +# sympy +*.sout +*.sympy +sympy-plots-for-*.tex/ + +# pdfcomment +*.upa +*.upb + +# pythontex +*.pytxcode +pythontex-files-*/ + +# tcolorbox +*.listing + +# thmtools +*.loe + +# TikZ & PGF +*.dpth +*.md5 +*.auxlock + +# titletoc +*.ptc + +# todonotes +*.tdo + +# vhistory +*.hst +*.ver + +# easy-todo +*.lod + +# xcolor +*.xcp + +# xmpincl +*.xmpi + +# xindy +*.xdy + +# xypic precompiled matrices and outlines +*.xyc +*.xyd + +# endfloat +*.ttt +*.fff + +# Latexian +TSWLatexianTemp* + +## Editors: +# WinEdt +*.bak +*.sav + +# Texpad +.texpadtmp + +# LyX +*.lyx~ + +# Kile +*.backup + +# gummi +.*.swp + +# KBibTeX +*~[0-9]* + +# TeXnicCenter +*.tps + +# auto folder when using emacs and auctex +./auto/* +*.el + +# expex forward references with \gathertags +*-tags.tex + +# standalone packages +*.sta + +# Makeindex log files +*.lpz + +# xwatermark package +*.xwm + +# REVTeX puts footnotes in the bibliography by default, unless the nofootinbib +# option is specified. Footnotes are the stored in a file with suffix Notes.bib. +# Uncomment the next line to have this generated file ignored. +#*Notes.bib + +# vscode +.vscode/ \ No newline at end of file diff --git a/Makefile.txt b/Makefile.txt new file mode 100644 index 0000000..db2b3ce --- /dev/null +++ b/Makefile.txt @@ -0,0 +1,10 @@ +tsp : tsp.o tsp_jikken.o tsp_public.o tsp_private.o + gcc -Wall -o tsp tsp.o tsp_jikken.o tsp_public.o tsp_private.o -lm +tsp.o : tsp.c tsp.h + gcc -Wall -c tsp.c +tsp_jikken.o : tsp_jikken.c tsp.h + gcc -Wall -c tsp_jikken.c +tsp_public.o : tsp_public.c tsp.h tsp_public.h + gcc -Wall -c tsp_public.c +tsp_private.o : tsp_private.c tsp.h tsp_private.h + gcc -Wall -c tsp_private.c diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/check.dat b/check.dat new file mode 100644 index 0000000..ad5dc88 --- /dev/null +++ b/check.dat @@ -0,0 +1,12 @@ +#最短経路:0->9->1->2->5->3->4->6->7->8->0 +#最短経路長:35.63 +1 1 +6 0 +9 3 +9 8 +6 9 +7 5 +3 8 +0 9 +3 5 +4 2 diff --git a/compile.bat b/compile.bat new file mode 100644 index 0000000..2e69a8d --- /dev/null +++ b/compile.bat @@ -0,0 +1 @@ +gcc -Wall -lm tsp_jikken.c tsp_private.c tsp_public.c tsp.c -o tsp ; .\tsp \ No newline at end of file diff --git a/elete1.dat b/elete1.dat new file mode 100644 index 0000000..aa5bbb3 --- /dev/null +++ b/elete1.dat @@ -0,0 +1,101 @@ +# min avg max +41.5433, 56.2432, 70.8706 +41.5433, 52.9489, 64.8914 +41.5433, 50.2838, 70.5527 +41.0052, 47.7829, 65.7395 +41.0052, 44.9366, 63.8423 +39.2571, 43.9278, 64.3337 +39.2571, 43.7881, 63.8423 +39.2571, 42.2862, 59.5177 +39.2571, 42.0238, 54.3256 +39.2571, 42.1568, 63.0851 +36.464, 41.0749, 55.4945 +36.464, 41.1484, 66.1776 +36.464, 41.033, 62.1425 +36.464, 40.9938, 61.0389 +36.464, 40.4489, 56.0738 +36.464, 39.4724, 59.4337 +36.464, 39.2671, 58.8699 +36.464, 39.2704, 63.4688 +36.464, 38.1143, 53.2453 +36.464, 38.1057, 54.5408 +36.464, 37.7785, 53.2453 +36.464, 38.3985, 55.7743 +36.464, 37.9681, 52.3717 +35.6263, 37.8255, 55.7743 +35.6263, 38.7252, 63.4688 +35.6263, 39.0195, 63.4688 +35.6263, 37.8999, 62.4187 +35.6263, 37.9282, 55.7743 +35.6263, 37.4584, 53.0499 +35.6263, 37.4682, 52.3717 +35.6263, 37.2831, 50.3336 +35.6263, 36.7048, 50.3336 +35.6263, 37.561, 53.379 +35.6263, 37.7709, 53.379 +35.6263, 37.7804, 54.5408 +35.6263, 37.3412, 52.249 +35.6263, 36.5371, 53.379 +35.6263, 36.4896, 52.249 +35.6263, 37.4936, 61.626 +35.6263, 37.1807, 56.1744 +35.6263, 37.0677, 53.379 +35.6263, 36.6893, 51.7813 +35.6263, 37.1117, 54.5408 +35.6263, 36.7602, 50.3476 +35.6263, 37.0735, 56.2887 +35.6263, 36.877, 53.1389 +35.6263, 36.6941, 54.5408 +35.6263, 37.5088, 54.5408 +35.6263, 37.1173, 54.5408 +35.6263, 36.7332, 54.2678 +35.6263, 37.0174, 54.5408 +35.6263, 37.4219, 54.2678 +35.6263, 37.5632, 56.1744 +35.6263, 37.2171, 56.1744 +35.6263, 36.767, 54.5408 +35.6263, 37.8285, 56.1744 +35.6263, 37.0538, 54.5408 +35.6263, 36.4177, 52.6936 +35.6263, 36.9115, 51.7813 +35.6263, 37.3024, 56.1744 +35.6263, 36.7944, 47.2004 +35.6263, 37.5562, 53.379 +35.6263, 36.6323, 54.5408 +35.6263, 36.5994, 52.249 +35.6263, 37.1367, 53.379 +35.6263, 37.266, 54.3197 +35.6263, 37.5201, 53.379 +35.6263, 37.1902, 53.1389 +35.6263, 37.3239, 53.379 +35.6263, 36.5938, 53.2453 +35.6263, 36.602, 53.1389 +35.6263, 37.452, 56.1744 +35.6263, 36.9308, 56.1744 +35.6263, 36.7664, 54.5408 +35.6263, 37.0498, 54.5408 +35.6263, 37.4853, 54.5408 +35.6263, 36.9586, 53.1389 +35.6263, 36.9626, 56.1744 +35.6263, 36.9608, 54.2678 +35.6263, 36.242, 50.3336 +35.6263, 38.5092, 54.5408 +35.6263, 37.2976, 58.2788 +35.6263, 37.5366, 54.2678 +35.6263, 37.2887, 54.2258 +35.6263, 37.3455, 52.4206 +35.6263, 37.8723, 53.379 +35.6263, 37.3919, 53.2453 +35.6263, 37.351, 54.2678 +35.6263, 38.4408, 56.5808 +35.6263, 37.5935, 56.1744 +35.6263, 37.8466, 53.379 +35.6263, 37.4899, 52.6936 +35.6263, 36.936, 62.5186 +35.6263, 36.9946, 53.379 +35.6263, 36.9064, 54.9852 +35.6263, 36.7193, 49.9268 +35.6263, 36.5643, 54.2678 +35.6263, 38.0012, 54.5408 +35.6263, 37.5631, 54.2678 +35.6263, 37.3921, 56.1744 diff --git a/elete1.pdf b/elete1.pdf new file mode 100644 index 0000000..65a40c5 Binary files /dev/null and b/elete1.pdf differ diff --git a/elete1s.dat b/elete1s.dat new file mode 100644 index 0000000..717570d --- /dev/null +++ b/elete1s.dat @@ -0,0 +1,101 @@ +# min avg max +50.3476, 58.4281, 69.1819 +46.5294, 55.48, 69.5587 +46.5294, 54.737, 66.9029 +46.5294, 53.27, 66.4952 +46.5294, 52.7357, 65.9231 +46.5294, 51.7696, 65.707 +46.5294, 50.9023, 64.873 +44.2654, 50.1495, 59.565 +37.8771, 49.2242, 61.3018 +37.8771, 48.2633, 65.0095 +37.8771, 47.3929, 65.6401 +37.8771, 46.7169, 63.6148 +35.6263, 44.5643, 56.3844 +35.6263, 42.4969, 60.9607 +35.6263, 41.2202, 57.1282 +35.6263, 39.2793, 56.3017 +35.6263, 38.7866, 58.7506 +35.6263, 37.669, 51.7624 +35.6263, 37.0884, 49.9021 +35.6263, 37.4128, 58.7506 +35.6263, 37.2181, 53.379 +35.6263, 37.5561, 54.5408 +35.6263, 36.8713, 54.2678 +35.6263, 37.2001, 59.2887 +35.6263, 36.8783, 54.2678 +35.6263, 36.951, 52.4206 +35.6263, 37.3833, 52.4206 +35.6263, 37.1482, 54.2678 +35.6263, 37.1853, 54.2678 +35.6263, 37.337, 53.1389 +35.6263, 37.0323, 56.1744 +35.6263, 36.9269, 56.1744 +35.6263, 37.2367, 56.1744 +35.6263, 37.1072, 53.1389 +35.6263, 37.6884, 54.5408 +35.6263, 37.2595, 54.5408 +35.6263, 37.6769, 54.2678 +35.6263, 37.3518, 53.379 +35.6263, 37.0611, 53.379 +35.6263, 37.4466, 53.379 +35.6263, 37.6507, 54.2678 +35.6263, 36.9837, 53.1389 +35.6263, 36.8983, 53.2453 +35.6263, 36.7957, 49.6936 +35.6263, 36.7697, 50.3476 +35.6263, 37.0876, 52.6936 +35.6263, 37.7432, 59.8038 +35.6263, 37.7027, 56.1744 +35.6263, 37.573, 53.1389 +35.6263, 36.3228, 48.8634 +35.6263, 37.2041, 54.2678 +35.6263, 36.8178, 52.9372 +35.6263, 37.6996, 54.5408 +35.6263, 37.44, 53.379 +35.6263, 37.4074, 56.1744 +35.6263, 37.0781, 50.3476 +35.6263, 37.492, 51.7813 +35.6263, 36.7924, 52.6936 +35.6263, 37.3643, 54.5408 +35.6263, 36.7159, 56.1744 +35.6263, 36.7491, 56.1744 +35.6263, 36.3971, 53.1389 +35.6263, 36.6348, 50.3336 +35.6263, 37.1618, 54.5408 +35.6263, 37.56, 51.7813 +35.6263, 37.42, 60.5777 +35.6263, 36.6998, 50.3476 +35.6263, 37.1444, 54.2678 +35.6263, 37.2144, 54.5408 +35.6263, 36.652, 56.1744 +35.6263, 36.978, 53.379 +35.6263, 36.7629, 54.2678 +35.6263, 36.8705, 53.2453 +35.6263, 36.7832, 52.249 +35.6263, 36.7293, 52.249 +35.6263, 37.134, 53.2453 +35.6263, 37.8436, 54.2678 +35.6263, 36.8283, 52.249 +35.6263, 36.9295, 49.6936 +35.6263, 37.9173, 53.1389 +35.6263, 37.6842, 53.2453 +35.6263, 36.7822, 53.1389 +35.6263, 36.4904, 54.5408 +35.6263, 36.9093, 54.5408 +35.6263, 37.2125, 56.1744 +35.6263, 36.5104, 50.3336 +35.6263, 36.6669, 54.2678 +35.6263, 37.4681, 54.5408 +35.6263, 37.0171, 56.1744 +35.6263, 37.3532, 52.6936 +35.6263, 37.6471, 56.1744 +35.6263, 37.4166, 54.2678 +35.6263, 37.3508, 56.1744 +35.6263, 37.213, 56.1744 +35.6263, 36.586, 54.5408 +35.6263, 36.4517, 49.5115 +35.6263, 38.034, 54.2678 +35.6263, 37.5046, 54.5408 +35.6263, 37.249, 54.2678 +35.6263, 37.782, 56.1744 diff --git a/elete1s.pdf b/elete1s.pdf new file mode 100644 index 0000000..30ace43 Binary files /dev/null and b/elete1s.pdf differ diff --git a/elete2.dat b/elete2.dat new file mode 100644 index 0000000..9a53624 --- /dev/null +++ b/elete2.dat @@ -0,0 +1,101 @@ +# min avg max +45.7584, 52.2002, 67.9188 +42.0438, 47.24, 58.1833 +42.0438, 46.8329, 62.6684 +42.0438, 45.0048, 59.0662 +42.0438, 42.8209, 55.931 +42.0438, 43.2365, 59.4105 +41.821, 42.9002, 59.4105 +41.821, 43.3049, 58.9538 +41.821, 42.9492, 58.0016 +41.821, 43.3195, 58.1243 +41.821, 43.2495, 58.0016 +41.821, 42.5578, 52.2369 +41.821, 42.4344, 58.1243 +41.821, 42.4147, 56.5121 +41.821, 43.11, 57.8523 +41.821, 42.7043, 56.5121 +41.821, 43.1025, 56.5121 +41.821, 43.2106, 57.9163 +41.821, 42.9828, 58.1243 +41.821, 42.4807, 51.8157 +41.821, 43.1391, 57.8523 +41.821, 42.9974, 56.5121 +41.821, 43.0894, 58.1243 +41.821, 42.1836, 51.1641 +41.821, 42.7925, 57.8523 +41.821, 42.956, 58.1243 +41.821, 42.599, 52.9143 +41.821, 43.2927, 58.0016 +41.821, 42.5884, 56.5121 +41.821, 43.0709, 56.1044 +41.821, 42.4013, 50.2716 +41.821, 42.502, 57.8523 +41.821, 42.4258, 58.0016 +41.821, 42.8289, 58.1243 +41.821, 42.3256, 52.2369 +41.821, 42.7835, 57.8523 +41.821, 42.8929, 58.1243 +41.821, 42.7735, 54.1947 +41.821, 43.0711, 57.8523 +41.821, 42.8674, 58.0016 +41.821, 43.1045, 57.9163 +41.821, 42.626, 58.0016 +41.821, 42.5808, 58.1243 +41.821, 42.9069, 58.0016 +41.821, 42.56, 51.9019 +41.821, 42.7547, 57.9163 +41.821, 42.5633, 53.8268 +41.821, 43.4717, 57.9163 +41.821, 43.0858, 58.1243 +41.821, 42.7993, 57.8523 +41.821, 42.9181, 57.9163 +41.821, 43.3777, 56.5121 +41.821, 42.1784, 56.5121 +41.821, 43.5022, 58.1243 +41.821, 43.2801, 57.9163 +41.821, 42.6827, 53.8268 +41.821, 42.3316, 58.0016 +41.821, 42.5171, 57.8523 +41.821, 42.3879, 58.0016 +41.821, 42.9153, 58.1243 +41.821, 42.5566, 57.8523 +41.821, 42.9043, 57.8523 +41.821, 42.3516, 56.1044 +41.821, 42.9452, 57.3196 +41.821, 42.514, 54.1947 +41.821, 42.4125, 58.1243 +41.821, 43.021, 57.9163 +41.821, 42.429, 51.1641 +41.821, 42.6567, 56.4176 +41.821, 43.2515, 57.9163 +41.821, 43.3513, 57.3196 +41.821, 42.9148, 57.8523 +41.821, 43.1668, 58.1243 +41.821, 42.842, 58.1243 +41.821, 42.5273, 58.0016 +41.821, 42.5109, 53.8268 +41.821, 43.2986, 57.8523 +41.821, 42.9489, 56.1044 +41.821, 42.7382, 56.4176 +41.821, 43.0704, 57.9163 +41.821, 42.8327, 57.9163 +41.821, 42.7395, 57.8523 +41.821, 42.9599, 58.1243 +41.821, 42.664, 56.4176 +41.821, 42.4063, 56.4176 +41.821, 43, 58.1243 +41.821, 42.5897, 57.3196 +41.821, 43.2555, 57.9163 +41.821, 43.2943, 58.1243 +41.821, 43.0846, 58.1243 +41.821, 42.9521, 57.8523 +41.821, 43.251, 54.1947 +41.821, 42.3642, 57.8523 +41.821, 42.9014, 57.8523 +41.821, 43.0681, 57.8523 +41.821, 43.1667, 58.0016 +41.821, 42.9386, 58.1243 +41.821, 42.7052, 56.4176 +41.821, 42.5443, 52.2369 +41.821, 42.8319, 57.3196 diff --git a/elete2.pdf b/elete2.pdf new file mode 100644 index 0000000..edc9848 Binary files /dev/null and b/elete2.pdf differ diff --git a/em0.01.dat b/em0.01.dat new file mode 100644 index 0000000..ae1a4d3 Binary files /dev/null and b/em0.01.dat differ diff --git a/em0.05.dat b/em0.05.dat new file mode 100644 index 0000000..2c79f9e Binary files /dev/null and b/em0.05.dat differ diff --git a/em0.20.dat b/em0.20.dat new file mode 100644 index 0000000..6d38926 Binary files /dev/null and b/em0.20.dat differ diff --git a/em0.50.dat b/em0.50.dat new file mode 100644 index 0000000..c0db96b Binary files /dev/null and b/em0.50.dat differ diff --git a/generation.plt b/generation.plt new file mode 100644 index 0000000..0abc89b --- /dev/null +++ b/generation.plt @@ -0,0 +1,20 @@ +# 出力設定 +set terminal pdfcairo size 4in,3in font "Arial,12" +set output 'path_length_plot.pdf' + +set xlabel "generation" +set ylabel "path length" +set grid + +set xrange [0:100] +set yrange [0:80] + +set xtics 10 +set ytics 10 + +# データのプロット +# $0 は行番号(0から開始)を意味します +# 1列目:min, 2列目:avg, 3列目:max を想定 +plot 'output.dat' using 0:1 with lines title "Min", \ + '' using 0:2 with lines title "Avg", \ + '' using 0:3 with lines title "Max" diff --git a/only_mutation1.dat b/only_mutation1.dat new file mode 100644 index 0000000..4e6a392 --- /dev/null +++ b/only_mutation1.dat @@ -0,0 +1,101 @@ +# min avg max +45.5294, 61.9471, 75.2243 +45.5294, 61.5091, 75.2243 +45.5294, 61.1737, 75.5987 +45.5294, 61.2738, 75.5987 +45.5294, 61.4409, 75.5987 +45.5294, 61.7753, 75.5987 +45.5294, 61.5801, 75.5987 +50.8268, 61.4698, 75.5987 +48.4798, 61.4566, 75.5987 +46.5928, 61.5124, 75.5987 +46.5928, 61.3973, 75.5987 +46.5928, 61.5501, 75.5987 +46.5928, 61.5765, 74.1443 +46.5928, 61.5375, 74.1443 +46.5928, 61.3549, 74.1443 +46.5928, 61.0363, 74.1443 +49.2101, 61.1487, 73.9208 +48.5416, 61.2421, 73.9208 +48.5416, 61.2129, 73.2847 +49.2101, 61.1352, 72.831 +49.2101, 61.1545, 72.831 +49.2101, 61.374, 72.831 +49.2101, 61.1901, 72.831 +48.3827, 61.2704, 72.831 +48.3809, 61.2082, 72.831 +48.3809, 61.3082, 72.9551 +48.3809, 61.1667, 73.1866 +48.3809, 60.9059, 73.1866 +48.3809, 61.0146, 75.9349 +48.3809, 60.5673, 75.9349 +48.3809, 60.9728, 77.3301 +48.3809, 60.9296, 77.3301 +44.9001, 60.8398, 77.3301 +44.9001, 60.8155, 77.3301 +47.1546, 60.882, 77.3301 +48.4079, 60.9876, 77.3301 +48.4079, 60.9486, 72.831 +48.4079, 60.956, 73.9764 +48.4079, 60.955, 73.9764 +48.4079, 60.9306, 74.2388 +48.4079, 60.8439, 74.2388 +48.4079, 60.5127, 74.2388 +48.4079, 60.5748, 74.2388 +47.5294, 60.8237, 74.2388 +47.5294, 60.8721, 73.2931 +47.5294, 60.9409, 73.2931 +46.9184, 60.6378, 76.3645 +48.0827, 60.6986, 76.3645 +48.0249, 60.888, 76.3645 +48.0827, 60.9865, 76.3645 +48.0827, 60.741, 76.3645 +48.0827, 60.8003, 76.3645 +48.0827, 60.8745, 76.3645 +48.0827, 61.2306, 76.3645 +48.0827, 61.4272, 76.3645 +48.0827, 61.2962, 76.3645 +49.6497, 61.3978, 76.3645 +49.6497, 61.7179, 76.3645 +49.6497, 61.6371, 76.3645 +49.5208, 61.6281, 76.3645 +50.6103, 61.7684, 76.3645 +50.2895, 61.7677, 76.3645 +50.2895, 61.6216, 76.3645 +49.4399, 61.3769, 76.3645 +46.4688, 61.3879, 76.3645 +46.4688, 61.523, 76.3645 +46.4688, 62.007, 76.3645 +46.4688, 61.9853, 76.6304 +49.4399, 61.5991, 76.6304 +48.9993, 61.7025, 76.6304 +48.9993, 61.5724, 76.6304 +47.389, 61.6693, 76.6304 +47.389, 61.9294, 76.6304 +47.389, 61.9432, 72.3788 +47.389, 61.7124, 73.1473 +47.389, 61.2305, 73.1473 +47.389, 61.4225, 73.1473 +47.389, 61.4459, 73.1473 +47.389, 61.5575, 76.2133 +47.389, 61.6647, 76.2133 +47.2868, 61.6941, 76.2133 +47.2868, 61.469, 76.2133 +47.2868, 61.9245, 73.8426 +47.2868, 62.2466, 73.8426 +50.6103, 62.4955, 73.8426 +50.6103, 62.6961, 73.8426 +49.7479, 62.5457, 73.7954 +49.7479, 62.8831, 73.9072 +50.6103, 62.8054, 73.9072 +50.6103, 62.8534, 73.7954 +48.5602, 62.9654, 73.7954 +48.5602, 62.9501, 75.6723 +46.9305, 62.8084, 75.6723 +46.9305, 62.767, 75.6723 +46.9305, 63.0662, 77.3139 +45.757, 63.2588, 77.3139 +43.6158, 62.8181, 77.3139 +43.6158, 62.8254, 77.3139 +43.6158, 63.0695, 77.3139 +43.6158, 62.9746, 77.3139 diff --git a/only_mutation1.pdf b/only_mutation1.pdf new file mode 100644 index 0000000..129aa9f Binary files /dev/null and b/only_mutation1.pdf differ diff --git a/output.dat b/output.dat new file mode 100644 index 0000000..e4c5cbd Binary files /dev/null and b/output.dat differ diff --git a/tm0.01.dat b/tm0.01.dat new file mode 100644 index 0000000..b514c98 Binary files /dev/null and b/tm0.01.dat differ diff --git a/tm0.05.dat b/tm0.05.dat new file mode 100644 index 0000000..28e4848 Binary files /dev/null and b/tm0.05.dat differ diff --git a/tm0.20.dat b/tm0.20.dat new file mode 100644 index 0000000..0e1237f Binary files /dev/null and b/tm0.20.dat differ diff --git a/tm0.50.dat b/tm0.50.dat new file mode 100644 index 0000000..f390a53 Binary files /dev/null and b/tm0.50.dat differ diff --git a/tour1.dat b/tour1.dat new file mode 100644 index 0000000..ed156c2 --- /dev/null +++ b/tour1.dat @@ -0,0 +1,101 @@ +# min avg max +46.8854, 56.9165, 68.7222 +46.8854, 55.0716, 63.206 +46.8854, 53.675, 67.7632 +46.8854, 52.0257, 61.4154 +46.8854, 50.8136, 59.3177 +46.8854, 50.4592, 61.0442 +45.3803, 49.864, 69.0927 +42.3642, 48.8172, 62.9122 +45.3803, 49.0228, 64.9908 +45.2664, 48.2674, 63.9113 +42.9463, 47.1045, 59.5574 +43.7244, 47.2205, 64.3962 +42.3642, 47.0575, 62.752 +42.3642, 46.6641, 64.1491 +42.3642, 46.9167, 60.2728 +40.7055, 46.8007, 63.6319 +40.7055, 45.5017, 61.8452 +40.7055, 45.4721, 65.5437 +37.8771, 45.3018, 61.5584 +37.8771, 44.1674, 59.3705 +37.8771, 43.3751, 64.104 +40.7055, 42.9308, 59.3705 +37.8771, 41.9451, 57.2102 +37.8771, 41.8206, 62.7323 +37.8771, 41.9732, 57.5316 +37.8771, 42.1225, 56.7356 +37.8771, 41.4701, 60.3809 +37.8771, 41.8593, 66.0288 +37.8771, 41.259, 57.2102 +37.8771, 41.0926, 56.1029 +37.8771, 40.3113, 54.7447 +37.8771, 39.7817, 66.0288 +37.8771, 39.6595, 60.513 +37.8771, 39.7134, 53.453 +35.6263, 38.87, 57.5316 +35.6263, 39.3476, 57.5316 +35.6263, 39.6123, 54.7447 +37.8771, 39.4376, 54.7447 +37.8771, 39.2524, 52.2869 +37.8771, 38.9519, 54.7447 +35.6263, 39.2691, 66.0288 +35.6263, 39.8424, 66.0288 +35.6263, 40.177, 55.3093 +35.6263, 41.228, 66.0288 +35.6263, 39.6316, 66.0288 +35.6263, 38.9198, 60.513 +35.6263, 38.1649, 51.9005 +35.6263, 38.9737, 66.0288 +35.6263, 37.7844, 53.453 +35.6263, 37.5678, 51.2283 +35.6263, 37.9779, 52.3171 +35.6263, 37.5648, 59.4337 +35.6263, 37.2188, 59.4337 +35.6263, 36.8083, 53.6468 +35.6263, 36.6641, 51.2283 +35.6263, 36.7372, 58.9229 +35.6263, 37.2287, 58.9229 +35.6263, 37.1368, 53.6468 +35.6263, 36.8274, 58.9229 +35.6263, 36.201, 52.3171 +35.6263, 37.7288, 59.4337 +35.6263, 37.4702, 53.6468 +35.6263, 36.8762, 53.6468 +35.6263, 37.3096, 53.9934 +35.6263, 37.1608, 52.9149 +35.6263, 37.111, 59.4337 +35.6263, 36.9695, 53.6468 +35.6263, 38.0728, 53.6468 +35.6263, 37.2612, 52.9149 +35.6263, 37.8341, 53.6468 +35.6263, 37.8199, 58.9229 +35.6263, 37.9516, 53.9934 +35.6263, 37.6247, 58.9229 +35.6263, 37.0913, 55.0178 +35.6263, 37.5607, 58.9229 +35.6263, 36.8658, 53.6468 +35.6263, 37.4453, 58.9229 +35.6263, 36.8112, 52.8306 +35.6263, 37.1611, 59.4337 +35.6263, 37.3148, 58.9229 +35.6263, 37.4381, 54.9041 +35.6263, 37.5192, 59.4337 +35.6263, 37.7401, 53.9934 +35.6263, 36.4104, 52.4409 +35.6263, 36.9875, 58.9229 +35.6263, 38.1195, 58.9229 +35.6263, 37.5695, 58.9229 +35.6263, 36.7173, 53.9934 +35.6263, 36.7635, 53.9934 +35.6263, 38.0423, 59.4337 +35.6263, 38.2014, 58.9229 +35.6263, 36.8826, 58.2925 +35.6263, 36.9507, 58.9229 +35.6263, 37.5093, 54.9041 +35.6263, 37.3459, 59.4337 +35.6263, 37.5717, 53.9934 +35.6263, 37.5335, 53.9934 +35.6263, 37.2009, 53.4067 +35.6263, 37.388, 53.4067 +35.6263, 37.2049, 54.9041 diff --git a/tour1.pdf b/tour1.pdf new file mode 100644 index 0000000..c7372e1 Binary files /dev/null and b/tour1.pdf differ diff --git a/tour2.dat b/tour2.dat new file mode 100644 index 0000000..90c6ec2 --- /dev/null +++ b/tour2.dat @@ -0,0 +1,101 @@ +# min avg max +48.4051, 52.518, 66.0019 +47.9389, 49.1244, 63.1711 +47.8462, 48.8651, 59.8477 +40.7055, 48.2732, 54.5167 +40.7055, 47.3942, 54.4387 +40.7055, 43.9889, 56.9097 +40.7055, 42.4347, 55.4715 +40.7055, 41.6844, 54.1293 +40.7055, 41.7646, 55.2381 +40.7055, 41.7323, 55.4715 +40.7055, 41.6488, 55.4715 +40.7055, 41.4085, 52.5794 +40.7055, 41.8962, 52.1481 +40.7055, 41.6391, 54.1293 +40.7055, 42.2353, 55.2381 +40.7055, 41.8956, 55.2381 +40.7055, 41.4927, 52.1177 +40.7055, 41.6299, 55.4715 +40.7055, 42.1978, 55.2381 +40.7055, 42.0644, 55.2381 +40.7055, 42.1517, 55.4715 +40.7055, 41.963, 55.4715 +40.7055, 41.8481, 51.8485 +40.7055, 42.3403, 55.2381 +40.7055, 41.9751, 55.4715 +40.7055, 41.9968, 55.2381 +40.7055, 41.7708, 52.5313 +40.7055, 41.9835, 55.4715 +40.7055, 41.6743, 51.8485 +40.7055, 42.1242, 55.2381 +40.7055, 41.435, 52.5313 +40.7055, 41.1915, 52.2249 +40.7055, 42.0759, 52.5313 +40.7055, 41.6181, 52.5313 +40.7055, 41.558, 55.2381 +40.7055, 42.0292, 55.4715 +40.7055, 42.0138, 55.2381 +40.7055, 41.6111, 55.2381 +40.7055, 41.9351, 55.4715 +40.7055, 42.0643, 55.4715 +40.7055, 41.4954, 54.1293 +40.7055, 41.9374, 55.4715 +40.7055, 42.4952, 52.5794 +40.7055, 41.8668, 55.4715 +40.7055, 42.0516, 55.4715 +40.7055, 41.8136, 55.4715 +40.7055, 42.0586, 55.4715 +40.7055, 41.7465, 51.1446 +40.7055, 41.7004, 55.4715 +40.7055, 42.1399, 55.4715 +40.7055, 41.807, 52.1481 +40.7055, 41.3854, 52.5794 +40.7055, 42.3651, 52.5313 +40.7055, 41.8922, 54.1293 +40.7055, 41.2659, 51.8515 +40.7055, 42.3622, 55.4715 +40.7055, 41.9063, 52.5794 +40.7055, 41.9301, 55.4715 +40.7055, 41.9443, 55.4715 +40.7055, 41.5227, 55.4715 +40.7055, 41.778, 55.4715 +40.7055, 42.0409, 52.5313 +40.7055, 41.5331, 51.3631 +40.7055, 41.8774, 55.4715 +40.7055, 41.9248, 54.1293 +40.7055, 41.8726, 55.4715 +40.7055, 42.662, 55.4715 +40.7055, 42.4377, 55.2381 +40.7055, 42.0995, 52.5794 +40.7055, 42, 52.2249 +40.7055, 42.3283, 55.4715 +40.7055, 41.8004, 55.2381 +40.7055, 42.2284, 52.1481 +40.7055, 42.2798, 55.4715 +40.7055, 41.8208, 55.2381 +40.7055, 41.997, 52.1481 +40.7055, 41.8609, 54.1293 +40.7055, 42.0222, 55.4715 +40.7055, 41.853, 52.1481 +40.7055, 41.6148, 55.4715 +40.7055, 42.11, 55.4715 +40.7055, 41.712, 54.1293 +40.7055, 42.1994, 55.4715 +40.7055, 42.2112, 52.5794 +40.7055, 41.9974, 55.4715 +40.7055, 42.0251, 52.1481 +40.7055, 41.5503, 52.1177 +40.7055, 41.8666, 55.2381 +40.7055, 41.7091, 52.2249 +40.7055, 42.1531, 55.4715 +40.7055, 41.7287, 55.4715 +40.7055, 41.7526, 54.1293 +40.7055, 41.8032, 55.4715 +40.7055, 41.653, 55.2381 +40.7055, 41.9801, 54.1293 +40.7055, 42.1707, 55.2381 +40.7055, 41.5366, 51.8515 +40.7055, 41.8065, 52.1481 +40.7055, 42.1649, 55.4715 +40.7055, 41.3337, 55.4715 diff --git a/tour2.pdf b/tour2.pdf new file mode 100644 index 0000000..b3601c1 Binary files /dev/null and b/tour2.pdf differ diff --git a/tsp.c b/tsp.c new file mode 100644 index 0000000..c0b2cbb --- /dev/null +++ b/tsp.c @@ -0,0 +1,135 @@ +#include +#include +#include +#include +#include "tsp.h" + +#define PRINT_GENE NO /* 遺伝子を表示(本来はオプションで指示するもの) */ +#define PRINT_PROCESS NO /* 途中経過を表示(本来はオプションで指示するもの) */ + +void print_usage(char *command) /* 使用方法を表示する関数 */ +{ + fprintf(stderr, "Usage: %s \n", command); +} + +void print_data(Gene_set gene_group[GENE_NUM]) /* データの表示 */ +{ + double best, sum, worst; + int i; + + sum = 0; + best = worst = gene_group[0].length; + for(i = 0; i < GENE_NUM; i++) { + sum += gene_group[i].length; /* 平均を出すため合計を求める */ + if (best > gene_group[i].length) { + best = gene_group[i].length; /* 最良値 */ + } + if (worst < gene_group[i].length) { + worst = gene_group[i].length; /* 最悪値 */ + } + } + //printf("best = %g, ave = %g, worst = %g\n", best, sum/GENE_NUM, worst); + printf("%g, %g, %g\n", best, sum/GENE_NUM, worst); +} + +int main(int argc, char *argv[]) +{ + int i; /* カウンタ用 */ + Position city[CITY_NUM]; /* 都市座標 */ + Gene_set gene_group[GENE_NUM]; /* 遺伝子集団 */ + FILE *fp; /* 座標用ファイル */ + char string[BUFSIZ]; /* ファイル入力用バッファ */ + + srand((unsigned int)time(NULL)); /* rand() の種 */ + + /* コマンド引数処理 */ + if (argc > 2) { /* コマンド引数が多すぎる */ + print_usage(argv[0]); /* 使用方法を表示 */ + exit(1); /* 終了 */ + } + /* コマンド引数が無い時 */ + else if (argc == 1) { + print_usage(argv[0]); /* 使用方法を表示 */ + exit(1); /* 終了 */ + } +#if 0 + else if (argc == 1) { + initial_city(city); /* ランダムに都市を生成 */ + fprintf(stderr, "RANDOM COORDINATES ARE USED!!\n"); + } +#endif + /* コマンド引数が 1つ */ + /* ファイルから都市座標を読みこみ */ + else if(argc == 2) { + if((fp = fopen(argv[1], "r")) == NULL) { + /* ファイルオープン失敗 */ + fprintf(stderr, "%s: %s can not be found\n", argv[0], argv[1]); + exit(1); + } + for(i = 0; (i < CITY_NUM) && (fgets(string, BUFSIZ, fp) != NULL);) { /* ファイルの終わりか都市数が設定に達するまで */ + if((string[0] == '#')||(string[0] == '\n')) { + /* コメント行および空行 */ + continue; /* 何も処理しない */ + } + /* データ読み込み */ + sscanf(string, "%d %d", &(city[i].x), &(city[i].y)); + i++; /* 読みこんだ都市数を増やす */ + } + fclose(fp); + } + + printf("# Trial, MutationRate, SelectNum, BestLength, GenToBest\n"); + for (int aaa = 0; aaa < 10; aaa++) + { + + + /* 各種データの初期化 */ + initial_gene(gene_group); /* 遺伝子集団の生成 */ + initial_fitness(gene_group, city); /* 初期適合度の計算 */ + + /* 初期データの表示 */ + //print_city(city); +#if PRINT_GENE /* 1/2 */ + print_gene_group(gene_group); +#endif /* PRINT_GENE 1/2 */ + + // printf("INITIAL RESULT\n"); + // print_best_data(gene_group); + + double best = gene_group[0].length; + int bg = 0; + for(i = 0; i < GENERATION; i++) { + /* 選択 */ + selection(gene_group); /* この関数を実験で作成 */ + + /* 突然変異を起こす */ + mutation(gene_group); /* この関数を実験で作成 */ + + /* 適合度計算 */ + calculate_fitness(gene_group, city); + if (gene_group[0].length < best) + { + best = gene_group[0].length; + bg = i; + } + +#if PRINT_PROCESS + /* 進化中のデータ表示 */ + //printf("GENERATION %d\n", i); + //print_data(gene_group); + /* print_best_data(gene_group); */ +#endif /* PRINT_PROCESS */ + } + + printf("%d, %.2f, %d, %.2f, %d\n", aaa, MUTATION_RATIO, SELECT_NUM, best, bg); + + /* 進化終了時のデータ表示 */ + //printf("FINAL RESULT\n"); +#if PRINT_GENE /* 2/2 */ + print_gene_group(gene_group); +#endif /* PRINT_GENE 2/2 */ + //print_best_data(gene_group); + } + + return 0; +} diff --git a/tsp.h b/tsp.h new file mode 100644 index 0000000..821aac1 --- /dev/null +++ b/tsp.h @@ -0,0 +1,88 @@ +#ifndef TSP_H_INCLUDED +#define TSP_H_INCLUDED + +#define YES 1 +#define NO 0 +#define CITY_NUM 10 /* 都市数 */ +#define POSITION_MAX 10 /* 都市座標の最大値 */ + +/* 以下のパラメータを調節する */ +#define GENE_NUM 100 /* 個体数 */ +#define ELETE 1 /* エリート戦略用 */ +#define GENERATION 100 /* 進化世代数 */ +#define MUTATION_RATIO 0.05 /* 突然変異確率 */ +#define CROSSOVER_RATIO 0.3 /* 交叉確率 */ /* 本実験では不使用 */ +#define SELECT_NUM 2 /* トーナメント方式で選ぶ遺伝子の数 */ + +/****************/ +/* 構造体の定義 */ +/****************/ +/* 都市座標 */ +typedef struct position_tag { + int x; /* x座標 */ + int y; /* y座標 */ +} Position; + +/* 個体 */ +typedef struct gene_set_tag { + int gene[CITY_NUM]; /* 遺伝子 */ + double length; /* 経路長 */ + double fitness; /* 適合度 */ +} Gene_set; + +/********************************/ +/* 実験作成関数プロトタイプ宣言 */ +/********************************/ + +/* 比較関数 */ +int compare_fitness(const void *a, const void *b); + +/* 突然変異 */ +void mutation(Gene_set gene_group[GENE_NUM]); + +/* 選択 */ +void selection(Gene_set gene_group[GENE_NUM]); + +/* トーナメント方式 */ +void tournament(Gene_set gene_group[GENE_NUM]); + +/* エリート戦略 */ +void elete(Gene_set gene_group[GENE_NUM]); + +/* 遺伝子個体を複写 */ +void copy_gene_set(Gene_set *gene_set_to, Gene_set *gene_set_from); + +/* 全遺伝子個体を複写 */ +void copy_gene_group(Gene_set gene_group_to[GENE_NUM], Gene_set gene_group_from[GENE_NUM]); + +/****************************/ +/* 各関数のプロトタイプ宣言 */ +/* 実体は tsp_public.c 内 */ +/* main 関数内で使用 */ +/****************************/ + +/* 都市をランダムに設定 */ +void initial_city(Position city[CITY_NUM]); + +/* 遺伝子をランダムに設定 */ +void initial_gene(Gene_set gene_group[GENE_NUM]); + +/* 適応度の初期設定 */ +void initial_fitness(Gene_set *gene_ptr, Position city[CITY_NUM]); + +/* 適応度の計算 */ +void calculate_fitness(Gene_set *gene_ptr, Position city[CITY_NUM]); + +/* 都市の座標を表示 */ +void print_city(Position city[CITY_NUM]); + +/* 各個体のデータを表示 */ +void print_gene_group(Gene_set gene_group[GENE_NUM]); + +/* 最良解を表示 */ +void print_best_data(Gene_set gene_group[GENE_NUM]); + +/* 最良値、平均値、最悪値を表示 */ +void print_data(Gene_set gene_group[GENE_NUM]); + +#endif /* define TSP_H_INCLUDED */ diff --git a/tsp_jikken.c b/tsp_jikken.c new file mode 100644 index 0000000..671ba10 --- /dev/null +++ b/tsp_jikken.c @@ -0,0 +1,158 @@ +#include +#include +#include "tsp.h" + +#define ELETE_SELECTION NO /* エリート戦略で選択 */ + +/* 距離(または適合度)でソートするための比較関数 */ +int compare_fitness(const void *a, const void *b) { + Gene_set *ga = (Gene_set *)a; + Gene_set *gb = (Gene_set *)b; + // 適合度が良い(小さい)順に並べる + if (ga->fitness < gb->fitness) return -1; + if (ga->fitness > gb->fitness) return 1; + return 0; +} + +/* 遺伝子集団 gene_group の中で突然変異を起こす */ +void mutation(Gene_set gene_group[GENE_NUM]) +{ + int i, target; + + /* どの遺伝子を変異させるか */ +#if ELETE_SELECTION /* 1/2 */ + for (i = ELETE; i < GENE_NUM; i++ ) +#else + for (i = 0; i < GENE_NUM; i++ ) +#endif /* ELETE_SELECTION 1/2 */ + { + /* その遺伝子が突然変異対象に選ばれたら */ + if ((rand() / (double)RAND_MAX) < MUTATION_RATIO) + { + /* どの位置を変異させるか */ + target = rand() % CITY_NUM; + /* 残っている都市の数以内でランダムに選ぶ */ + gene_group[i].gene[target] = rand() % (CITY_NUM - target); + } + } +} + +/* 遺伝子集団 gene_group の中で選択を行う */ +void selection(Gene_set gene_group[GENE_NUM]) +{ +#if ELETE_SELECTION /* 2/2 */ + elete(gene_group); +#else + tournament(gene_group); +#endif /* ELETE_SELECTION 2/2 */ +} + +void tournament(Gene_set gene_group[GENE_NUM]) +{ + int i, j, k, l; + int min_fitness_index; + int random_gene_index[SELECT_NUM]; + Gene_set next_gene_group[GENE_NUM]; + + /* すべての候補がそろうまで */ + for (i = 0; i < GENE_NUM; i++) + { + for (j = 0; j < SELECT_NUM; j++) + { + /* 全遺伝子の中からランダムに被らないよう複数選ぶ */ + while (1) + { + k = rand() % GENE_NUM; + for (l = 0; l < j; l++) + { + if (k == random_gene_index[l]) + { + break; + } + } + /* 最後まで一致しなかったら採用 */ + if (l == j) break; + + } + random_gene_index[j] = k; + } + + min_fitness_index = random_gene_index[0]; + + /* 選んだ個体の中から適合度が最も良い(小さい)ものを選び,次の世代とする */ + for (j = 1; j < SELECT_NUM; j++) + { + if (gene_group[min_fitness_index].fitness > gene_group[random_gene_index[j]].fitness) + { + min_fitness_index = random_gene_index[j]; + } + } + copy_gene_set(&next_gene_group[i], &gene_group[min_fitness_index]); + } + copy_gene_group(gene_group, next_gene_group); +} + +void elete(Gene_set gene_group[GENE_NUM]) +{ + int i, j, k, l; + int min_fitness_index; + int random_gene_index[SELECT_NUM]; + Gene_set next_gene_group[GENE_NUM]; + + /* 全個体を適合度の良い順(小さい順)に並び替える */ + qsort(gene_group, GENE_NUM, sizeof(Gene_set), compare_fitness); + + /* 上位 ELETE 個を次世代にコピーする */ + for (i = 0; i < ELETE; i++) + { + copy_gene_set(&next_gene_group[i], &gene_group[i]); + } + + /* 残りの枠(ELETE ~ GENE_NUM)をトーナメント方式で埋める */ + for (i = ELETE; i < GENE_NUM; i++) { + for (j = 0; j < SELECT_NUM; j++) { + while (1) + { + k = rand() % GENE_NUM; + for (l = 0; l < j; l++) + { + if (k == random_gene_index[l]) break; + } + if (l == j) break; + } + random_gene_index[j] = k; + } + + min_fitness_index = random_gene_index[0]; + for (j = 1; j < SELECT_NUM; j++) + { + if (gene_group[min_fitness_index].fitness > gene_group[random_gene_index[j]].fitness) + { + min_fitness_index = random_gene_index[j]; + } + } + copy_gene_set(&next_gene_group[i], &gene_group[min_fitness_index]); + } + copy_gene_group(gene_group, next_gene_group); +} + +void copy_gene_set(Gene_set *gene_set_to, Gene_set *gene_set_from) +{ + int i; + for (i = 0; i < CITY_NUM; i++) + { + gene_set_to->gene[i] = gene_set_from->gene[i]; + } + + gene_set_to->length = gene_set_from->length; + gene_set_to->fitness = gene_set_from->fitness; +} + +void copy_gene_group(Gene_set gene_group_to[GENE_NUM], Gene_set gene_group_from[GENE_NUM]) +{ + int i; + for (i = 0; i < GENE_NUM; i++) + { + copy_gene_set(&gene_group_to[i], &gene_group_from[i]); + } +} diff --git a/tsp_private.c b/tsp_private.c new file mode 100644 index 0000000..4c03bbf --- /dev/null +++ b/tsp_private.c @@ -0,0 +1,132 @@ +#include +#include +#include +#include "tsp.h" +#include "tsp_private.h" + +/* 遺伝子型 g から表現型 p への変換 */ +/* g[] で与えた巡回表現の遺伝子型を都市の番号にして表現型 p[] に入れる */ +void gtype_to_ptype(int g[CITY_NUM], int p[CITY_NUM]) { + int i, j; + int count; + int check[CITY_NUM]; /* 既に表現型に現れたかどうか */ + + /* check[] の初期化 */ + for(i = 0; i < CITY_NUM; i++) { + check[i] = NO; + } + + for(i = 0; i < CITY_NUM; i++) { + /* i 番目の遺伝子を変換 */ + j = 0; /* 全都市のうち何番目か */ + count = 0; /* 残った都市のうち何番目か */ + do { + if(check[j] == NO) { /* 表現型に現れていない都市である */ + count++; + } + if(count > g[i]) { /* 遺伝子型に適合 */ + break; + } + j++; + } while(1); + p[i] = j; /* p 番目の遺伝子の都市番号は j */ + check[j] = YES; /* 表現型に現れたことをチェック */ + } +}; + +/* 都市間の距離を計算 */ +double distance(Position city1, Position city2) +{ + return sqrt((city1.x - city2.x)*(city1.x - city2.x) + + (city1.y - city2.y)*(city1.y - city2.y)); +}; + +/* 遺伝子の表す巡回路長の計算 */ +double path_length(Position city[CITY_NUM], int gene[CITY_NUM]) +{ + int i; + double sum = 0.0; + int route[CITY_NUM]; /* 巡回路の表現型 */ + + gtype_to_ptype(gene, route); /* 遺伝子型から表現型への変換 */ + + for(i = 0; i < CITY_NUM; i++) { + sum += distance(city[route[i%CITY_NUM]], city[route[(i+1)%CITY_NUM]]); + } + + return sum; +}; + +/* 遺伝子の表示 */ +void print_gene(int gene[CITY_NUM]) +{ + int i; + + printf("gene:"); + for(i = 0; i < CITY_NUM; i++) { + printf("%d.", gene[i]); + } + printf("\n"); +} + +/* 巡回路の表示 */ +void print_route(int route[CITY_NUM]) +{ + int i; + printf("route:"); + for(i = 0; i < CITY_NUM; i++) { + printf("%d->", route[i]); + } + printf("%d\n", route[0]); + +} + +/* 遺伝子集団の巡回路長の計算 */ +void calculate_length(Gene_set *gene_ptr, Position city[CITY_NUM]) { + int i; + for(i = 0; i < GENE_NUM; i++) { + (gene_ptr + i)->length = path_length(city, (gene_ptr + i)->gene); + } +} + +/* 遺伝子の複写 */ +void copy_gene(int gene_to[CITY_NUM], int gene_from[CITY_NUM]) +{ + int k; + + for(k = 0; k < CITY_NUM; k++) { + gene_to[k] = gene_from[k]; + } +} + +/* 遺伝子の入れ換え */ +void swap_gene_set(Gene_set gene_group[GENE_NUM], int i, int j) +{ + Gene_set gene_swap; + + gene_swap.fitness = gene_group[i].fitness; + gene_swap.length = gene_group[i].length; + copy_gene(gene_swap.gene, gene_group[i].gene); + + gene_group[i].fitness = gene_group[j].fitness; + gene_group[i].length = gene_group[j].length; + copy_gene(gene_group[i].gene, gene_group[j].gene); + + gene_group[j].fitness = gene_swap.fitness; + gene_group[j].length = gene_swap.length; + copy_gene(gene_group[j].gene, gene_swap.gene); +} + +/* 現時点の最適遺伝子の番号を得る */ +int get_best_gene(Gene_set gene_group[GENE_NUM]) +{ + int i, index = 0; + + for(i = 0; i < GENE_NUM; i++) { + if(gene_group[i].length < gene_group[index].length) { + index = i; + } + } + + return index; +} diff --git a/tsp_private.h b/tsp_private.h new file mode 100644 index 0000000..8339a88 --- /dev/null +++ b/tsp_private.h @@ -0,0 +1,26 @@ +#ifndef TSP_PRIVATE_H_INCLUDED +#define TSP_PRIVATE_H_INCLUDED +#include "tsp.h" + +/******************************/ +/* 各関数のプロトタイプ宣言 */ +/* 実体は tsp_private.c 内 */ +/* tsp_private.c 内でのみ使用 */ +/******************************/ + +/* 距離を計算 */ +double distance(Position city1, Position city2); + +/* 経路長を計算 */ +double path_length(Position city[CITY_NUM], int gene[CITY_NUM]); + +/* 遺伝子を複写 */ +void copy_gene(int gene_to[CITY_NUM], int gene_from[CITY_NUM]); + +/* 個体の交換 */ +void swap_gene_set(Gene_set gene_group[GENE_NUM], int i, int j); + +/* 最適解の遺伝子番号を得る */ +int get_best_gene(Gene_set gene_group[GENE_NUM]); + +#endif /* define TSP_PRIVATE_H_INCLUDED */ diff --git a/tsp_public.c b/tsp_public.c new file mode 100644 index 0000000..343f784 --- /dev/null +++ b/tsp_public.c @@ -0,0 +1,159 @@ +#include +#include +#include "tsp_public.h" + +/* 都市座標の初期化 */ +/* 配列 city[] にランダムに都市座標を設定 */ +void initial_city(Position city[CITY_NUM]) { + int i; + for(i = 0; i < CITY_NUM; i++) { + city[i].x = rand()%POSITION_MAX; + city[i].y = rand()%POSITION_MAX; + } +}; + +/* 遺伝子の初期化(ランダム) */ +/* 構造体 Gene_set 中の gene[] に遺伝子をランダムに設定 */ +void initial_gene(Gene_set gene_group[GENE_NUM]) { + int i, j; + for(i = 0; i < GENE_NUM; i++) { + for(j = 0; j < CITY_NUM; j++) { + /* 巡回表現を用いる */ + gene_group[i].gene[j] = rand()%(CITY_NUM - j); + } + } +} + +/* 適合度の初期化 */ +void initial_fitness(Gene_set *gene_ptr, Position city[CITY_NUM]) +{ + int i; /* カウンタ */ + /* スケーリングウィンドウ関連 */ + /* (スケーリングウィンドウ自体はstatic変数)*/ + double length_max; /* 経路長の最大値(スケーリングウィンドウ用)*/ + + /* 全遺伝子の経路長計算 */ + calculate_length(gene_ptr, city); + + for(i = 0; i < GENE_NUM; i++) { /* 経路長の最大値の計算 */ + if(i == 0) { + length_max = gene_ptr->length; + } + else if((gene_ptr + i)->length > length_max) { + length_max = (gene_ptr + i)->length; + } + } + + for(i = 0; i < WINDOW_SIZE; i++) { /* スケーリングウィンドウの設定 */ + /* 最初は全部同じ値にする */ + scaling_window[i] = length_max; + } + + for(i = 0; i < GENE_NUM; i++) { + /* 適合度は 経路長/スケーリングウィンドウ内の最大値 */ + (gene_ptr + i)->fitness = (gene_ptr + i)->length / length_max; + } + +} + +/* 適合度の計算 */ +void calculate_fitness(Gene_set *gene_ptr, Position city[CITY_NUM]) +{ + int i; + double length_max; + + /* 遺伝子集団の経路長を求める */ + calculate_length(gene_ptr, city); + + /* 現在の遺伝子集団の最大経路長を求める */ + for(i = 0; i < GENE_NUM; i++) { + if(i == 0) { + length_max = gene_ptr->length; + } + else if((gene_ptr + i)->length > length_max) { + length_max = (gene_ptr + i)->length; + } + } + + /* スケーリングウィンドウの更新 */ + for(i = 0; i < WINDOW_SIZE; i++) { + if (i < WINDOW_SIZE-1) { + scaling_window[i] = scaling_window[i+1]; + } + else { + scaling_window[i] = length_max; + } + } + + /* スケーリングウィンドウ中の最大経路長を求める */ + for(i = 0; i < WINDOW_SIZE; i++) { + if (scaling_window[i] > length_max) { + length_max = scaling_window[i]; + } + } + + /* 遺伝子集団の適合度を求める */ + for(i = 0; i < GENE_NUM; i++) { + (gene_ptr + i)->fitness = (gene_ptr + i)->length / length_max; + } + +} + +/* 都市座標の表示 */ +void print_city(Position city[CITY_NUM]) { + int i; + //printf("%d\n", CITY_NUM); + for(i = 0; i < CITY_NUM; i++) { + printf("city%03d %d %d\n", i, city[i].x, city[i].y); + } +} + + +/* 遺伝子のデータ表示 */ +void print_gene_group(Gene_set gene_group[GENE_NUM]) { + int i; + int route[CITY_NUM]; + + for(i = 0; i < GENE_NUM; i++) { + gtype_to_ptype(gene_group[i].gene, route); +// print_gene(gene_group[i].gene); /* 遺伝子型 */ + print_route(route); /* 表現型 */ + printf("length = %f (fitness = %f)\n", gene_group[i].length, gene_group[i].fitness); /* 経路長と適合度 */ + } +} + +/* 現時点の最良解の表示 */ +void print_best_data(Gene_set gene_group[GENE_NUM]) +{ + int route[CITY_NUM]; + int best_index; /* 最良解の遺伝子番号 */ + + best_index = get_best_gene(gene_group); + + //print_gene(gene_group[best_index].gene); /* 遺伝子表示 */ + gtype_to_ptype(gene_group[best_index].gene, route); + print_route(route); /* 巡回路表示 */ + printf("length = %f (fitness = %f)\n", gene_group[best_index].length, gene_group[best_index].fitness); /* 経路長・適合度表示 */ +} + +/* データの表示 */ +#if 0 +void print_data(Gene_set gene_group[GENE_NUM]) +{ + double best, sum, worst; + int i; + + sum = 0; + best = worst = gene_group[0].length; + for(i = 0; i < GENE_NUM; i++) { + sum += gene_group[i].length; /* 平均を出すため合計を求める */ + if (best > gene_group[i].length) { + best = gene_group[i].length; /* 最良値 */ + } + if (worst < gene_group[i].length) { + worst = gene_group[i].length; /* 最悪値 */ + } + } + printf("best = %g, ave = %g, worst = %g\n", best, sum/GENE_NUM, worst); +} +#endif diff --git a/tsp_public.h b/tsp_public.h new file mode 100644 index 0000000..dcbb9d9 --- /dev/null +++ b/tsp_public.h @@ -0,0 +1,33 @@ +#ifndef TSP_PUBLIC_H_INCLUDED +#define TSP_PUBLIC_H_INCLUDED + +#include "tsp.h" + +/***************/ +/* static 変数 */ +/***************/ +/* スケーリングウィンドウ */ +#define WINDOW_SIZE 5 /* スケーリングウィンドウサイズ */ +static double scaling_window[WINDOW_SIZE]; + +/****************************/ +/* 各関数のプロトタイプ宣言 */ +/* 実体は tsp_private.c 内 */ +/* tsp_public.c 内で使用 */ +/****************************/ + +/* 遺伝子型を表現型に変換 */ +void gtype_to_ptype(int g[CITY_NUM], int p[CITY_NUM]); + +/* 遺伝子を表示 */ +void print_gene(int gene[GENE_NUM]); + +/* 経路を表示 */ +void print_route(int route[CITY_NUM]); + +/* 各個体の経路長を計算 */ +void calculate_length(Gene_set *gene_ptr, Position city[CITY_NUM]); + +/* 最良の遺伝子番号を得る */ +int get_best_gene(Gene_set gene_group[GENE_NUM]); +#endif /* TSP_PUBLIC_H_INCLUDED */ diff --git a/出力ファイル一覧.md b/出力ファイル一覧.md new file mode 100644 index 0000000..dd829c7 --- /dev/null +++ b/出力ファイル一覧.md @@ -0,0 +1,60 @@ +# 出力一覧 + +## 収束曲線 + +### only_mutation1 + +- selection()をコメントアウト +- ELETE_SELECTION NO +- GENE_NUM 100 +- ELETE 1 +- GENERATION 100 +- MUTATION_RATIO 0.2 +- SELECT_NUM 2 + +### elete1 + +- ELETE_SELECTION YES +- GENE_NUM 100 +- ELETE 1 +- GENERATION 100 +- MUTATION_RATIO 0.2 +- SELECT_NUM 2 + +### elete1s + +elete1をtour1と同じような開始にしたバージョン + +- ELETE_SELECTION YES +- GENE_NUM 100 +- ELETE 1 +- GENERATION 100 +- MUTATION_RATIO 0.2 +- SELECT_NUM 2 + +### elete2 + +- ELETE_SELECTION YES +- GENE_NUM 100 +- ELETE 1 +- GENERATION 100 +- MUTATION_RATIO 0.2 +- SELECT_NUM 10 + +### tour1 + +- ELETE_SELECTION NO +- GENE_NUM 100 +- ELETE 1 +- GENERATION 100 +- MUTATION_RATIO 0.2 +- SELECT_NUM 2 + +### tour2 + +- ELETE_SELECTION NO +- GENE_NUM 100 +- ELETE 1 +- GENERATION 100 +- MUTATION_RATIO 0.2 +- SELECT_NUM 10