2010-02-11 2 views
4

저는 NetLogo에서 게임 이론 시뮬레이션을 실행 해왔고 크로스 테이블 데이터를 포함하는 많은 데이터 파일을 보유하고 있습니다. 각 열에는 다른 변수의 값이 저장되어 있습니다. 거기에 c. 데이터가 포함 된 1000 개의 행 나는이 파일을 가져 와서 각 열의 평균값을 계산하는 프로그램을 작성하려고합니다.NetLogo : 행 수가 변하는 입력 파일에서 데이터 읽기

각 파일에 일정한 수의 데이터 행이있는 한 작동하는 프로그램이 있습니다. 이 프로그램은 파일 읽기 명령 루프를 사용하여 누적 합계를 계산 한 다음 모든 행을 읽은 후 읽은 행의 누벨로 나눕니다.

그러나 실제 데이터 파일에는 여러 행이 있습니다. file-at-end를 사용하여 코드를 수정하려고 노력하고 있습니까? 마지막 행 다음에 실행중인 전체 루프를 종료하게하려면 작동하지만 작동시키는 방법을 찾지 못했습니다. 파일이 끝났음을 알리는 오류가 발생합니다.

누군가가이 문제를 해결할 방법을 제안 할 수 있습니까? 아래에 작업 코드를 붙여 넣었습니다.

-

globals [target-file-name 
current-tally-file 
lines-read 
coops-fixed-run cheats-fixed-run either-fixed-run coop-freq-min-run 
coop-freq-max-run coop-freq-mean-run ticks-run 
num-lines 
] 

to setup 
set target-file-name user-input "Type a name for the target file" 
file-open target-file-name 
file-print("TallyFile Reps pFixCoop pFixCheat pFixEither MeanMinCoop MeanMaxCoop MeanMeanCoop") 
file-close 
set num-lines read-from-string user-input "How many lines in the file to be processed?" 
end 

to go 
set current-tally-file user-file 
file-open current-tally-file 
set lines-read 0 

while [lines-read < num-lines][ 
let in1 file-read set coops-fixed-run (coops-fixed-run + in1) 
let in2 file-read set cheats-fixed-run (cheats-fixed-run + in2) 
let in3 file-read set either-fixed-run (either-fixed-run + in3) 
let in4 file-read set coop-freq-min-run (coop-freq-min-run + in4) 
let in5 file-read set coop-freq-max-run (coop-freq-max-run + in5) 
let in6 file-read set coop-freq-mean-run (coop-freq-mean-run + in6) 
let in7 file-read set ticks-run (ticks-run + in7)  
set lines-read (lines-read + 1) 
] 

stop-and-clear 
end 


to stop-and-clear 

let pfixcoop (coops-fixed-run/lines-read) 
let pfixcheat (cheats-fixed-run/lines-read) 
let pfixeither (either-fixed-run/lines-read) 
let mean-of-mins (coop-freq-min-run/lines-read) 
let mean-of-maxs (coop-freq-max-run/lines-read) 
let mean-of-means (coop-freq-mean-run/lines-read) 
let mean-of-ticks (ticks-run/lines-read) 

file-open target-file-name 
file-print (word current-tally-file " " lines-read " " pfixcoop " " pfixcheat " 
" pfixeither " " mean-of-mins " " mean-of-maxs " " mean-of-means " " 
mean-of-ticks) 
file-close 


set coops-fixed-run 0 
set cheats-fixed-run 0 
set either-fixed-run 0 
set coop-freq-min-run 0 
set coop-freq-max-run 0 
set coop-freq-mean-run 0 
set ticks-run 0 
set lines-read 0 

stop 

end 

답변

4

파일이 보이는에서처럼 모든 라인을 읽을 수있는 방법이 모든 행이 데이터 항목의 이름과 정확하게 번호를 가지고, 당신이 알고 있다고 가정

to read-file [filename] 
    file-open filename 
    while [not file-at-end?][ 
    ;read one line 
    let in1 file-read 
    let in2 file-read 
    ;and so one, at the end you will probably want to put these into some global variable 
    set global-in1 fput in1 global-in1 
    ] 
    file-close filename 
end 

무엇 그 숫자는 그렇지 않으면 파일 읽기 대신 파일 읽기 라인을 사용하십시오.

+1

대단히 감사합니다! 나는 파일을 사용하지 않을 수 있다는 것을 깨닫지 못했습니다. 필자는 행당 일련의 데이터 항목을 가지고 있으므로 완벽 해 보입니다. –