Система не надоел тебе этот Диснейленд взрослый вроде человек
Кармак я детей обучаю программировать нате дети
Print("select screen mode");
Print("1 640x480");
Print("2 800x600");
g_mode=Input("> ");
If g_mode=1 Then g_width=640:g_height=480
If g_mode=2 Then g_width=800:g_height=600
Graphics g_width, g_height, 0, 2 ; AAH I REMEMBER TYPING THIS
SetFont(LoadFont("system"));
Global n_weights=10000
Global n_link=100
Dim weights%(n_weights,n_link)
Global MAX_WORDS=0
Dim knowledge$(100000);
Global g_text_pos = 0;
Global g_text$;
Global g_text_word$;
; #################################
; Pretty bad, so let us train the network
Global s_out$=""
Global s_dice$=""
Global max_res#=0
Global last_word$=""
f = FileOpen ("loc.txt")
Global i_word_$=ReadLine(f); load rus "i"
CloseFile(f)
;AddToKnowledgeWord("test")
ReadKnowledge();
LoadNet()
Global word$;
Dim words$(100)
Global words_n
Function Weights_add_weight(n,v2)
For i=1 To n_link
If weights(n,i)=v2 Then Return
Next
For i=1 To n_link
If weights(n,i)=0 Then weights(n,i)=v2:Return
Next
End Function
While (1)
; Print "Press key to train the network!"
;WaitKey()
ReadKnowledge();
Locate(0,GraphicsHeight())
user_text$=Input("> ")
TextBegin(user_text$)
While TextReadWord()
AddToKnowledgeWord(g_text_word)
Wend
SeedRnd(MilliSecs())
Log_add("> "+user_text)
TextBegin(user_text$)
;last_word$=""
words_n=0
While TextReadWord()
last_word=g_text_word;
words_n=words_n+1
words(words_n)=g_text_word
If g_text_word="locate" Then Print location;
If g_text_word="dice" Then d=Rand(1,6):Print "dice "+d : Log_add("dice "+d)
next_word$=Text_get_next_word$()
;If next_word$<>"" Then
Weights_add_weight(GetVec(g_text_word),GetVec(next_word))
Wend
; now our loss function should be much smaller
; If it isn't try playing around with the parameters
; #################################
; #################################
; The network is trained
; Print "Press key for a test run of the trained network!"
;WaitKey()
s_dice$=""
s_out$="";
r=8;Rand(1,7)
dice=0
word$=last_word;
; Print "max res "+max_res
For i = 1 To r
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;word=if(word1<>""
n_attemp=1
.a1
n_attemp=n_attemp+1
;res% = ProcessInputData(GetVec(word)) ; get network results
;If res<0.0 Then res=res*-1;
;If res>max_res Then max_res=res;
;res=res/Float(max_res)*Float(MAX_WORDS);
For j=1 To n_link
If weights(GetVec(word),j)=0 Exit
Next
j=j-1
If word=i_word_ Then Print "i_word_ "+j
res=weights(GetVec(word),Rand(1,j))
s_$=VecToString(res)
If s_="" And n_attemp<100 Then word=words(Rand(1,words_n)):Goto a1;
If s_="" Then s_=knowledge(Rand(1,MAX_WORDS)):Print "use Rand": Print res
word$=s_$
last_word=word$
; Print "res "+res
s_out=s_out+s_+" ";
If s_="dice" Then s_out=s_out+dice=Rand(1,6)+" ";
Next
Log_add(s_out)
Print s_out
If dice Then Print dice
; s_out=""
; If s_dice Then s_out="AI dice "+s_dice;
SaveNet()
Wend
; Print "Press key to end the program!"
;WaitKey()
; See? much better now!
; #################################
End
; bye!
Function Log_add(s$)
ls$=""
TextBegin(s$)
While TextReadWord()
ls=ls+g_text_word+" ";
Wend
If ls="" Or ls=" " Then Return
f=FileOpen("log.txt")
SeekFile(f, FileSize("log.txt"));
WriteLine(f, ls)
CloseFile(f);
End Function
Function GetVec(s$)
For i=1 To MAX_WORDS
If knowledge(i)=s$ Return i; Print "Get_vec "+i:
Next
Return 0
End Function
Function VecToString$(v%)
Return knowledge(v)
If v<0 Then v=v*-1
If v>MAX_WORDS Then v=0
If v>=0 And v<=MAX_WORDS Then Return knowledge(v)
Return ""
End Function
Function LoadNet()
f=ReadFile("weights.sav")
If f=0 Return;
CloseFile (f)
file1 = FileOpen("max_res.sav");
max_res=ReadFloat(file1)
CloseFile(file1)
file1 = FileOpen("weights.sav");
For i=1 To n_weights;
For j=1 To n_link;
weights(i,j)=ReadInt(file1)
Next
Next
CloseFile(file1);
End Function
Function SaveNet()
file1 = FileOpen("max_res.sav");
WriteFloat(file1,max_res)
CloseFile(file1)
file1 = FileOpen("weights.sav");
For i=1 To n_weights;
For j=1 To n_link;
WriteInt(file1,weights(i,j))
Next
Next
CloseFile(file1);
End Function
Function ReadKnowledge()
MAX_WORDS=0
file1=FileOpen("knowledge.txt");
While(Eof(file1)<>1)
ReadLine(file1)
MAX_WORDS=MAX_WORDS+1
Wend
CloseFile(file1)
file1 = FileOpen("knowledge.txt");
For i=1 To MAX_WORDS
knowledge(i) = ReadLine(file1);
Next
CloseFile(file1);
End Function
Function AddToKnowledgeWord(s$)
If Word_Qu(s)>0 Return
If(s = "") Return
f=FileOpen("knowledge.txt")
If MAX_WORDS SeekFile(f, FileSize("knowledge.txt"));
WriteLine(f, s)
MAX_WORDS=MAX_WORDS+1
; Print MAX_WORDS
CloseFile(f);
ReadKnowledge()
End Function
Function Word_Qu(w$)
q=0
ReadKnowledge()
For i=1 To MAX_WORDS
If w=knowledge(i) q=q+1
Next
Return q
End Function
Function FileOpen(fn$)
file1 = OpenFile(fn$);
If(file1=0)file1=WriteFile(fn$);
Return file1;
End Function
;------------------
Function TextBegin(text_$)
g_text=text_
g_text_pos = 1
g_text_prev_word_pos = 1
g_text_word=""
End Function
Function TextReadWord()
g_text_word =""
If (g_text_pos > Len(g_text))Return 0;
While (Mid(g_text, g_text_pos, 1) = " ") : g_text_pos = g_text_pos + 1 : Wend
While (Mid(g_text,g_text_pos,1) <> " " And g_text_pos<=Len(g_text))
g_text_word =g_text_word+ Mid(g_text,g_text_pos,1);
g_text_pos=g_text_pos+1
Wend
g_text_pos=g_text_pos+1;
Return 1;
End Function
Function Text_get_next_word$()
g_text_prev_word_pos=g_text_pos
prev_word$=g_text_word
TextReadWord()
w$=g_text_word
g_text_word=prev_word
g_text_pos=g_text_prev_word_pos
Return w
End Function
; simple min function
Function min#(a#, b#)
If a < b
Return a
Else
Return b
EndIf
End Function
; simple max function
Function max#(a#, b#)
If a > b
Return a
Else
Return b
EndIf
End Function
; to_approximate
; INPUT:
; x# - the function input value
; RETURNS:
; the value that the function computes
;
; This is the function we want our network to approximate
; I used a simple x^2, you can try different ones
; CHANGEME
Function to_approximate#(x#)
Return x*x
End Function
; ################################# <- I had to insert this or I would have gone mad