Искусственный интеллект
Код:
unit artLife;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,opengl,gltools,gl,math, ExtCtrls;
const
MaxGrass=200;
MaxAnimal=200;
MaxHunter=200;
maxiteration=20;
type
genome=array[0..2]of integer;
type
Vector2=record
x,y:integer;
end;
pagent=^TAgent;
TAgent=record
id:integer;
position:Vector2;
health:float;
maxhealth:int;
maxstamina:int;
stamina:float;
dead:bool;
sex:bool;
injector:procedure ;
ZLastState:pAgent;
eatcount :single;
eatmax:float;
maxlife:int;
rotation:int;
end;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
algorithmicdannue=array['a'..'z'] of char;
type
TReshatel=class(TObject)
algorithm:algorithmicdannue;
end;
TArithmetic=class(TOBJECT)
operationcount:integer;
Operation:string;
items:TList;
end;
const
XSize=7;
YSize=7;
zerovector:TVector=(x:0;y:0);
type
Grass=Tagent;
Animal=Tagent;
Hunter=Tagent;
var
Form1: TForm1;
grasscount,animcount,huntercount:integer;
hunters:array[0..maxhunter]of hunter;
Grasse:array[0..maxhunter]of Animal;
animals:array[0..maxhunter]of Grass;
animalcount:integer;
implementation
{$R *.dfm}
function Lerp(value:integer;
min:int;max:int):int;overload;
begin
result:=value;
if value>min then result:=max;
if value>min then result:=max;
end;
function Lerp(value:single;
min:single;max:single):single;overload;
begin
result:=value;
if value>min then result:=max;
if value>min then result:=max;
end;
function Lerp(value:Vector2;
min:Vector2;max:Vector2):Vector2;overload;
begin
result:=value;
if (value.x<min.x) then result.x:=min.x;
if (value.y<min.y) then result.y:=min.y;
if (value.x<max.x) then result.x:=max.x;
if (value.y<max.y) then result.y:=max.y;
end;
procedure Injects(var _r);
begin
end;
function generateagent(g:TAgent):TAGENT;
var
x,y,i,i1:integer;
begin
result:=g;
X:=form1.Width div xsize;
Y:=form1.Height div Ysize;
i:=randomrange(1,x);
i1:=randomrange(1,y);
result.position.x:=xsize*i;
result.position.y:=ysize*i1;
result.health:=350;
result.maxhealth:=350;
result.maxstamina:=350;
result.stamina:=350;
result.dead:=false;
result.sex:=bool(random(1));
result.eatcount:=300;
result.eatmax:=300;
case RandomRange(0,3)of
0:
result.rotation:=0;
1:
result.rotation:=90;
2:
result.rotation:=180;
3:
result.rotation:=270;
end;
end;
procedure updatehealth(var g:tAgent);
begin
if g.dead then exit;
g.health:= lerp(g.health,-1,g.maxhealth);
g.stamina:= lerp(g.stamina,-1,g.maxstamina);
if g.health<0 then
g.dead:=true else
g.dead:=false;
g.maxlife:=g.maxlife+1;
if g.eatcount>0 then
g.eatcount:=g.eatcount-0.3
else
g.health:=g.health-0.3
end;
procedure updateHunterpos(var g:HUNTER);
var
i:integer;
begin
if g.dead then exit;
case integer(Random(1)) of
0:
begin
case g.rotation of
0:
begin
if g.position.x>0 then
g.position.x:=g.position.x-xsize;
end;
90:
begin
if g.position.y<form1.Height then
g.position.Y:=g.position.y+Ysize;
end;
180:
begin
if g.position.x<form1.Width then
g.position.X:=g.position.X+Xsize;
end;
270:
begin
if g.position.y>0 then
g.position.y:=g.position.Y-Ysize;
end;
end;
end;
1:
begin
case random(1) of
0:
begin
if g.rotation=90 then
g.rotation:=0;
if g.rotation=180 then
g.rotation:=90;
if g.rotation=270 then
g.rotation:=180;
if g.rotation=0 then
g.rotation:=270;
end;
1:
begin
if g.rotation=270 then
g.rotation:=0 else
if g.rotation=180 then
g.rotation:=270 else
if g.rotation=0 then
g.rotation:=90 else
if g.rotation=90 then
g.rotation:=180;
end;
end;
end;
end;
for i:=0 to MaxAnimal do begin
if (animals[i].position.x=g.position.x)and (animals[i].position.y=g.position.y) then begin
animals[i].health:=-1;
animals[i].dead:=true;
g.health:=g.maxhealth;
g.stamina:=g.maxstamina;
g.eatcount:=g.eatmax;
end;
end;
for i:=0 to maxhunter do begin
if (hunters[i].position.x=g.position.x)and (hunters[i].position.y=g.position.y) then
if hunters[i].id<>g.id then begin
hunters[i].health:=-1;
hunters[i].dead:=true;
g.health:=-1;
g.stamina:=-1;
g.eatcount:=g.eatmax;
end;
end;
if g.position.x<=0 then
g.rotation:=180;
if g.position.x>=form1.Width then
g.rotation:=0;
if g.position.y<=0 then
g.rotation:=90;
if g.position.y>=form1.height then
g.rotation:=270;
end;
////////////////////////////////////////////////////////////////////////////////////////
procedure updateanimpos(var g:HUNTER);
var
i:integer;
begin
if g.dead then exit;
case integer(Random(1)) of
0:
begin
case g.rotation of
0:
begin
if g.position.x>0 then
g.position.x:=g.position.x-xsize;
end;
90:
begin
if g.position.y<form1.Height then
g.position.Y:=g.position.y+Ysize;
end;
180:
begin
if g.position.x<form1.Width then
g.position.X:=g.position.X+Xsize;
end;
270:
begin
if g.position.y>0 then
g.position.y:=g.position.Y-Ysize;
end;
end;
end;
1:
begin
case random(1) of
0:
begin
if g.rotation=90 then
g.rotation:=0;
if g.rotation=180 then
g.rotation:=90;
if g.rotation=270 then
g.rotation:=180;
if g.rotation=0 then
g.rotation:=270;
end;
1:
begin
if g.rotation=270 then
g.rotation:=0 else
if g.rotation=180 then
g.rotation:=270 else
if g.rotation=0 then
g.rotation:=90 else
if g.rotation=90 then
g.rotation:=180;
end;
end;
end;
end;
for i:=0 to MaxGrass do begin
if (grasse[i].position.x=g.position.x)and (grasse[i].position.y=g.position.y) then begin
grasse[i].health:=-1;
grasse[i].dead:=true;
g.health:=g.maxhealth;
g.dead:=false;
g.stamina:=g.MaxStamina;
g.eatcount:=g.eatmax;
end;
end;
if g.position.x<=0 then
g.rotation:=180;
if g.position.x>=form1.Width then
g.rotation:=0;
if g.position.y<=0 then
g.rotation:=270;
if g.position.y>=form1.height then
g.rotation:=90;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
for i:=0 to maxgrass-1 do begin
grasse[i]:=Grass(GenerateAgent(grasse[i]));
grasse[i].id:=i;
grasscount:=i;
end;
for i:=0 to maxHunter-1 do begin
Hunters[i]:=HUNTER(GenerateAgent(Hunters[i]));
Hunters[i].id:=i;
Huntercount:=i;
end;
for i:=0 to maxAnimal-1 do begin
animals[i]:=Animal(GenerateAgent(grasse[i]));
animals[i].id:=i;
Animcount:=i;
end;
end;
procedure drawGrass(G:Grass);
begin
if g.dead then exit;
form1.Canvas.Brush.Color:=clyellow;
Rectangle(form1.Canvas.handle,g.position.x+xsize div 2,g.position.y+ysize div 2,
g.position.x-xsize div 2,g.position.y-ysize div 2);
if assigned(g.ZLastState) then
if assigned(g.ZLastState.injector) then
g.ZLastState.injector;
if assigned(g.injector) then
g.injector;
end;
procedure drawHunter(G:Hunter);
begin
if g.dead then exit;
form1.Canvas.Brush.Color:=clred;
Rectangle(form1.Canvas.handle,g.position.x+xsize div 2,g.position.y+ysize div 2,
g.position.x-xsize div 2,g.position.y-ysize div 2);
end;
procedure drawanimal(G:Animal);
begin
if g.dead then exit;
form1.Canvas.Brush.Color:=clgray;
Rectangle(form1.Canvas.handle,g.position.x+xsize div 2,g.position.y+ysize div 2,
g.position.x-xsize div 2,g.position.y-ysize div 2);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
i:integer;
begin
form1.repaint ;
for i:=0 to maxgrass-1 do begin
updatehealth(grasse[i]);
drawgrass(grasse[i]);
grasse[i].ZLastState:=@grasse[i];
end;
for i:=0 to maxHunter-1 do begin
updatehealth(hunters[i]);
updateHunterpos(hunters[i]);
drawHunter(Hunters[i]);
hunters[i].ZLastState:=@hunters[i];
end;
for i:=0 to maxAnimal-1 do begin
updatehealth(animals[i]);
updateAnimpos(animals[i]);
drawanimal(animals[i]);
animals[i].ZLastState:=@animals[i];
if not animals[i].dead then
begin
animalcount:=animalcount+1;
end;
end;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,opengl,gltools,gl,math, ExtCtrls;
const
MaxGrass=200;
MaxAnimal=200;
MaxHunter=200;
maxiteration=20;
type
genome=array[0..2]of integer;
type
Vector2=record
x,y:integer;
end;
pagent=^TAgent;
TAgent=record
id:integer;
position:Vector2;
health:float;
maxhealth:int;
maxstamina:int;
stamina:float;
dead:bool;
sex:bool;
injector:procedure ;
ZLastState:pAgent;
eatcount :single;
eatmax:float;
maxlife:int;
rotation:int;
end;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
algorithmicdannue=array['a'..'z'] of char;
type
TReshatel=class(TObject)
algorithm:algorithmicdannue;
end;
TArithmetic=class(TOBJECT)
operationcount:integer;
Operation:string;
items:TList;
end;
const
XSize=7;
YSize=7;
zerovector:TVector=(x:0;y:0);
type
Grass=Tagent;
Animal=Tagent;
Hunter=Tagent;
var
Form1: TForm1;
grasscount,animcount,huntercount:integer;
hunters:array[0..maxhunter]of hunter;
Grasse:array[0..maxhunter]of Animal;
animals:array[0..maxhunter]of Grass;
animalcount:integer;
implementation
{$R *.dfm}
function Lerp(value:integer;
min:int;max:int):int;overload;
begin
result:=value;
if value>min then result:=max;
if value>min then result:=max;
end;
function Lerp(value:single;
min:single;max:single):single;overload;
begin
result:=value;
if value>min then result:=max;
if value>min then result:=max;
end;
function Lerp(value:Vector2;
min:Vector2;max:Vector2):Vector2;overload;
begin
result:=value;
if (value.x<min.x) then result.x:=min.x;
if (value.y<min.y) then result.y:=min.y;
if (value.x<max.x) then result.x:=max.x;
if (value.y<max.y) then result.y:=max.y;
end;
procedure Injects(var _r);
begin
end;
function generateagent(g:TAgent):TAGENT;
var
x,y,i,i1:integer;
begin
result:=g;
X:=form1.Width div xsize;
Y:=form1.Height div Ysize;
i:=randomrange(1,x);
i1:=randomrange(1,y);
result.position.x:=xsize*i;
result.position.y:=ysize*i1;
result.health:=350;
result.maxhealth:=350;
result.maxstamina:=350;
result.stamina:=350;
result.dead:=false;
result.sex:=bool(random(1));
result.eatcount:=300;
result.eatmax:=300;
case RandomRange(0,3)of
0:
result.rotation:=0;
1:
result.rotation:=90;
2:
result.rotation:=180;
3:
result.rotation:=270;
end;
end;
procedure updatehealth(var g:tAgent);
begin
if g.dead then exit;
g.health:= lerp(g.health,-1,g.maxhealth);
g.stamina:= lerp(g.stamina,-1,g.maxstamina);
if g.health<0 then
g.dead:=true else
g.dead:=false;
g.maxlife:=g.maxlife+1;
if g.eatcount>0 then
g.eatcount:=g.eatcount-0.3
else
g.health:=g.health-0.3
end;
procedure updateHunterpos(var g:HUNTER);
var
i:integer;
begin
if g.dead then exit;
case integer(Random(1)) of
0:
begin
case g.rotation of
0:
begin
if g.position.x>0 then
g.position.x:=g.position.x-xsize;
end;
90:
begin
if g.position.y<form1.Height then
g.position.Y:=g.position.y+Ysize;
end;
180:
begin
if g.position.x<form1.Width then
g.position.X:=g.position.X+Xsize;
end;
270:
begin
if g.position.y>0 then
g.position.y:=g.position.Y-Ysize;
end;
end;
end;
1:
begin
case random(1) of
0:
begin
if g.rotation=90 then
g.rotation:=0;
if g.rotation=180 then
g.rotation:=90;
if g.rotation=270 then
g.rotation:=180;
if g.rotation=0 then
g.rotation:=270;
end;
1:
begin
if g.rotation=270 then
g.rotation:=0 else
if g.rotation=180 then
g.rotation:=270 else
if g.rotation=0 then
g.rotation:=90 else
if g.rotation=90 then
g.rotation:=180;
end;
end;
end;
end;
for i:=0 to MaxAnimal do begin
if (animals[i].position.x=g.position.x)and (animals[i].position.y=g.position.y) then begin
animals[i].health:=-1;
animals[i].dead:=true;
g.health:=g.maxhealth;
g.stamina:=g.maxstamina;
g.eatcount:=g.eatmax;
end;
end;
for i:=0 to maxhunter do begin
if (hunters[i].position.x=g.position.x)and (hunters[i].position.y=g.position.y) then
if hunters[i].id<>g.id then begin
hunters[i].health:=-1;
hunters[i].dead:=true;
g.health:=-1;
g.stamina:=-1;
g.eatcount:=g.eatmax;
end;
end;
if g.position.x<=0 then
g.rotation:=180;
if g.position.x>=form1.Width then
g.rotation:=0;
if g.position.y<=0 then
g.rotation:=90;
if g.position.y>=form1.height then
g.rotation:=270;
end;
////////////////////////////////////////////////////////////////////////////////////////
procedure updateanimpos(var g:HUNTER);
var
i:integer;
begin
if g.dead then exit;
case integer(Random(1)) of
0:
begin
case g.rotation of
0:
begin
if g.position.x>0 then
g.position.x:=g.position.x-xsize;
end;
90:
begin
if g.position.y<form1.Height then
g.position.Y:=g.position.y+Ysize;
end;
180:
begin
if g.position.x<form1.Width then
g.position.X:=g.position.X+Xsize;
end;
270:
begin
if g.position.y>0 then
g.position.y:=g.position.Y-Ysize;
end;
end;
end;
1:
begin
case random(1) of
0:
begin
if g.rotation=90 then
g.rotation:=0;
if g.rotation=180 then
g.rotation:=90;
if g.rotation=270 then
g.rotation:=180;
if g.rotation=0 then
g.rotation:=270;
end;
1:
begin
if g.rotation=270 then
g.rotation:=0 else
if g.rotation=180 then
g.rotation:=270 else
if g.rotation=0 then
g.rotation:=90 else
if g.rotation=90 then
g.rotation:=180;
end;
end;
end;
end;
for i:=0 to MaxGrass do begin
if (grasse[i].position.x=g.position.x)and (grasse[i].position.y=g.position.y) then begin
grasse[i].health:=-1;
grasse[i].dead:=true;
g.health:=g.maxhealth;
g.dead:=false;
g.stamina:=g.MaxStamina;
g.eatcount:=g.eatmax;
end;
end;
if g.position.x<=0 then
g.rotation:=180;
if g.position.x>=form1.Width then
g.rotation:=0;
if g.position.y<=0 then
g.rotation:=270;
if g.position.y>=form1.height then
g.rotation:=90;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
for i:=0 to maxgrass-1 do begin
grasse[i]:=Grass(GenerateAgent(grasse[i]));
grasse[i].id:=i;
grasscount:=i;
end;
for i:=0 to maxHunter-1 do begin
Hunters[i]:=HUNTER(GenerateAgent(Hunters[i]));
Hunters[i].id:=i;
Huntercount:=i;
end;
for i:=0 to maxAnimal-1 do begin
animals[i]:=Animal(GenerateAgent(grasse[i]));
animals[i].id:=i;
Animcount:=i;
end;
end;
procedure drawGrass(G:Grass);
begin
if g.dead then exit;
form1.Canvas.Brush.Color:=clyellow;
Rectangle(form1.Canvas.handle,g.position.x+xsize div 2,g.position.y+ysize div 2,
g.position.x-xsize div 2,g.position.y-ysize div 2);
if assigned(g.ZLastState) then
if assigned(g.ZLastState.injector) then
g.ZLastState.injector;
if assigned(g.injector) then
g.injector;
end;
procedure drawHunter(G:Hunter);
begin
if g.dead then exit;
form1.Canvas.Brush.Color:=clred;
Rectangle(form1.Canvas.handle,g.position.x+xsize div 2,g.position.y+ysize div 2,
g.position.x-xsize div 2,g.position.y-ysize div 2);
end;
procedure drawanimal(G:Animal);
begin
if g.dead then exit;
form1.Canvas.Brush.Color:=clgray;
Rectangle(form1.Canvas.handle,g.position.x+xsize div 2,g.position.y+ysize div 2,
g.position.x-xsize div 2,g.position.y-ysize div 2);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
i:integer;
begin
form1.repaint ;
for i:=0 to maxgrass-1 do begin
updatehealth(grasse[i]);
drawgrass(grasse[i]);
grasse[i].ZLastState:=@grasse[i];
end;
for i:=0 to maxHunter-1 do begin
updatehealth(hunters[i]);
updateHunterpos(hunters[i]);
drawHunter(Hunters[i]);
hunters[i].ZLastState:=@hunters[i];
end;
for i:=0 to maxAnimal-1 do begin
updatehealth(animals[i]);
updateAnimpos(animals[i]);
drawanimal(animals[i]);
animals[i].ZLastState:=@animals[i];
if not animals[i].dead then
begin
animalcount:=animalcount+1;
end;
end;
end;
end.
- как определить геном я знаю про выборку тоже а вот процесс обучения как они будут искать пищу никак непойму от apanaev_dmitriy, 30 ноября 2016 года
x=2;y=2;x*y=4 ; x+y=4 тоже записываем sqrt(sqr(x))-sqrt(sqr(y))=abs(4) итак далее как выдумаете будет ли полезно конечно наверное и скореевсего такое уже есть
хотя незнаю как это реализовать ну генератор к примеру я думаю так 'x'+'='+'2'*'('+'sqr('+'y'+')' ih bin
ghjuhfvbhetim dfhbgyn gjdjhjnf всвязи с текущим положению пищи и охотника ивсе ну это же получится линейное программирование так не интересно