self-avoiding pathの続き。複数のagents

  • こちらでself-avoiding pathを書いてみた。データ型宣言も体系的に勉強せずに、みようみまね、検索・検索真似で書いているので、なかなか大変
  • コンパイルはするけれど、データ格納場所(アドレス)指定の破たんなどが頻発するので、しょっちゅう止まります
  • そうはいっても、少し前進したいので、複数のagentsがいて、相互に自身のいた場所も他agentのいた場所も避けるような酔歩にしてみる
  • さらに、通ってはいけないのは、有限の過去までの居場所であって、ある時間が経過したらそこは通れるようにしてみる

  • agentsの数を増やす

#include <RcppArmadilloExtensions/sample.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp ;

// [[Rcpp::export]]

using namespace Rcpp;

IntegerMatrix selfAvoidingPathCpp2_multi(int k,int past,int n,int L,int t,int maxt) {
	IntegerMatrix x(t,k*n);
	for(int i=0;i<(k*n);++i){
		for(int j=0;j<t;++j){
			x(j,i) = 0;
		}
	}
	IntegerVector p = seq_len(L);
	IntegerVector p2 = RcppArmadillo::sample(p, k*n, TRUE);
	for(int i=0;i<k*n;++i){
		x(0,i) = p2[i];
	}
	RNGScope scope;
	

	IntegerVector kv = seq_len(k);
	IntegerVector s(2);
	s[0] = -1;
	s[1] = 1;
	IntegerMatrix rm(maxt,n);
	IntegerMatrix rsm(maxt,n);
	for(int i=0;i<n;++i){
		IntegerVector r = RcppArmadillo::sample(kv, maxt, TRUE);
		IntegerVector rs = RcppArmadillo::sample(s, maxt, TRUE);
		for(int j=0;j<maxt;++j){
			rm(j,i) = r[j];
			rsm(j,i) = rs[j];
		}
	}
	
	int cnt = 0;
	for(int i=0;i<maxt;++i){
		if(cnt==t-2){
			break;
		}
		for(int ii=0;ii<n;++ii){
			int start = ii*k;
			int end = (ii+1)*k-1;
			IntegerVector here(k);
			for(int j=0;j<k;++j){
				here[j] = x(cnt,j+start);
			}
			IntegerVector toward(k);
			for(int j=0;j<k;++j){
				toward[j] = here[j];
			}
			toward[rm(i,ii)-1] = toward[rm(i,ii)-1]+rsm(i,ii);
			if(past == 0){
				for(int j=0;j<k;++j){
					x(cnt+1,j+start) = toward[j];
				}
			}else{
				int tmppast = 0;
				if(past == -1){
					
				}else{
					int tmp2 =cnt-past;
					if(tmppast<tmp2){
						tmppast = tmp2;
					}
				}
				bool b = TRUE;
				for(int jj=0;jj<n;++jj){
					int st2 = jj*k;
					int end2 = (jj+1)*k-1;
					for(int j=tmppast;j<cnt+1;++j){
						bool b2 = TRUE;
						for(int jjj=0;jjj<k;++jjj){
							if(x(j,jjj+st2) != toward[jjj]){
								b2 = FALSE;
								break;
							}
						}
						if(b2){
							b = FALSE;
							break;
						}
					}
					if(!b){
						break;
					}
				}
				if(b){
					for(int j=0;j<k;++j){
						x(cnt+1,j+start) = toward[j];
					}
				}else{
					for(int j=0;j<k;++j){
						x(cnt+1,j+start) = here[j];
					}
				}
			
			}
		}
		++cnt;
	}
	IntegerMatrix newx(t,k*n);
	for(int i=0;i<t;++i){
		for(int j=0;j<k*n;++j){
			newx(i,j) = x(i,j);
		}
	}
	return newx;
}
library(Rcpp)
library(RcppArmadillo)
library(rgl)

#sourceCpp("selfAvoidingPathCpp.cpp")
sourceCpp("selfAvoidingPathCpp2_multi.cpp")

k<-2
n<-10
max.t<-10000
past <- 20
t <- 1000
L <- 20
#B <- selfAvoidingWalkR(k,past=past,n=n,t=t,max.t=max.t)
#plot(B,type="b")
B2 <- selfAvoidingPathCpp2_multi(k,past,n,L,t,max.t)

B3 <- B2[,1:k]
for(i in 2:n){
	B3 <- rbind(B3,B2[,(1+(i-1)*k):(i*k)])
}
col <- rep(1:n,each=t)
plot(B3,col=col,pch=20,cex=1)