Skip to content

Instantly share code, notes, and snippets.

@maekawatoshiki
Last active March 20, 2019 13:43
Show Gist options
  • Save maekawatoshiki/17d9ea3f9fe7e3774d69 to your computer and use it in GitHub Desktop.
Save maekawatoshiki/17d9ea3f9fe7e3774d69 to your computer and use it in GitHub Desktop.
Hello world! for Genetic Algorithm
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define I 15/***/
#define Q memcpy
#define PZ printf
int AG[I] [13];/**/
int C[13] ;;;;;;;;;;;;
int W[13] ;;;;;;;;;;;
int L[I];;;; /*IOC*/
char Y[13 ] = {
72,101 , 108 ,
108,111, 32,119
,111,114, 108,100
, 33}; int main() {
int TY= ( 12*12 /
12+ 12 ) >> 1 ;
int V, _ , JJ, PQ;
int H1 , H2,H3 ,
H4,H5 , H6,H7,
H8;for( V = 0; V
<I;V++) for (_=TY
;_;_-- ) AG[V][_]=
(16<<1)+ (rand()%
(0xFF/2));for (JJ = 0; ;
JJ++){ memset(L, 0 ,
sizeof(int)*I);for (V =0
; V<I; V ++)for (_
= 0; _< TY; _++)
L[V] += (Y[_] - AG
[V][_]) * (Y[_] - AG
[V][_]);H1 =H5=L[0];
H8=H7= H4=H3=0;
for (V = 0; V<I; V++)
{if (L[V] < H1){H2 =
H1 ;H1 = L[V]; H4 =
H3;H3=V; if(!H1){for(
_ = 0; _ < TY;_++)PZ
("%c",AG[V ][_]);return
puts(""); } } else if (L[V
] >= H5){H6 = H5; H5=
L[V];H8=H7; H7 = V;}}PQ=(int)
((double) TY/2+0.5)-1;
Q(C,&AG[H3 ],sizeof(
int)*TY); Q(W,&AG[H4
],sizeof( int)*TY);Q
(&C[PQ],& AG[H4][PQ] ,
sizeof(int )*TY>>1);Q
(&W[PQ] , &AG[H3][PQ
],sizeof( int)*TY>>1
);Q(&AG[ H7],C,sizeof
(int)*TY) ;Q(&AG[H8],
W,sizeof( int)*TY);for
(V=7;V; V--)AG[rand
()%I][rand ()% TY]+=rand
()%2?-1:1; } return 000;}
/* (C) 2015 MAEKAWA TOSHIKI. */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment