哪个大神帮忙看一下代码呢???这个循环莫名的就中断了!!
class Program{
static void Main(string[] args)
{
int tt = 10000;//初始循环不记录数据步数
int TT = 60000;//总的循环步数
int N;//道路上车辆数
double q=0;//道路上车辆密度
Road road = new Road(2,1000, 5, 0.3); //初始化道路参数
int[,] Location = new int[2,1000];//声明数组记录车辆信息
int[,] Temp_Location = new int[2,1000];//声明中间数组记录车辆信息
int[,] changeflag = new int[2, 1000];//声明换道指示变量;
double[] Ave_eachTT = new double[50000];//声明数组记录每步更新的平均速度
double[] Ave_eachk = new double[100];//声明数组记录第个密度对应的平均速度
double[] flow_average = new double[100];//声明数组记录平均流量
Random randkey = new Random();
//采用周期性边界条件
//一次放入20辆车,一共放2000辆
for (N = 20; N <2* road.Length; N += 20)
{
int i, j;
for ( i = 0; i < road.lanenum; i++)//初始化道路状态
{
for ( j = 0; j < road.Length; j++)
{
Location [i,j] = -1;
Temp_Location[i,j] = -1;
changeflag[i,j] = 0;
}
}
//在道路上放车
int cars = 0;
int pos,laneid;
while (cars < N)
{
pos = randkey.Next(0, 1000);
laneid=randkey.Next(0,2);
if (Location[laneid,pos] == -1)
{
Location[laneid,pos] = road.Vmax;
cars++;
}
}
q = (double)N /( 2*road.Length);//由道路上的车辆计算密度
for (int t = 0; t < TT; t++) //对应于给定密度的60000步更新
{
int gap=0, gap_b=0, gap_f=0;
int next=0;
int speed=0,speed_n=0,speed_b=0,speed_f=0;
int tolane=0;
for (int k = 0; k < road.lanenum; k++)
{
i = -1;
if (k == 0)
tolane = 1;//目标车道
if (k == 1)
tolane = 0;
while (++i < road.Length && Location[k,i] == -1) ;
while (i < road.Length)
{
gap = i; //k车道上第i车的位置
gap_b = i;
gap_f = i;
speed = Location[k,i];//k车道上第i车的速度
while (++gap < road.Length && Location[k,gap] == -1) ;
next = gap;
//k车道上第i车与前车的间距
if (gap < road.Length)
{
speed_n = Location[k,gap];
gap = gap - i - 1;
}
else
{
int m = -1;
while (++m < road.Length && Location[k,m] == -1) ;
speed_n = Location[k,gap];
gap = road.Length - i + m - 1;
}
//k车道上第i车与另一车道上前车的间距
while (++gap_f < road.Length && Location[tolane,gap_f]==-1) ;
if (gap_f < road.Length)
{
speed_f = Location[tolane,gap_f];
gap_f = gap_f - i - 1;
}
else
{
int n = -1;
while (++n < road.Length && Location[tolane,n] == -1) ;
speed_f=Location[tolane,n];
gap_f = road.Length - i + n - 1;
}
//k车道上第i车与另一车道上后车的间距
while (--gap_b >-1&& Location[k,gap_b]==-1) ;
if (gap_b > -1)
{
speed_b = Location[tolane,gap_b];
gap_b = i - gap_b - 1;
}
else
{
int m = road.Length - 1;
while (--m > -1 && Location[tolane,m] == -1) ;
speed_b = Location[tolane,m];
gap_b = i + road.Length - m - 1;
}
//判断是否换道
if(k==0)//在第一车道
{
if(Location[tolane,i]==-1&&gap<Math.Min(speed+1,road.Vmax)&&gap_f>gap&&gap_b>road.Vmax)
changeflag[k,i]=1;
}
if(k==1)//在第二车道
{
if(Location[tolane,i]==-1&&gap<Math.Min(speed+1,road.Vmax)&&gap_f>gap&&gap_b>road.Vmax)
changeflag[k,i]=-1;
}
i = next;
}
for (i = 0; i < road.lanenum; i++)
{
for (j = 0; j < road.Length; j++)
{
double pchange = randkey.Next(0, 100);
pchange /= 100;//车辆以一定的概率进行换道
if (changeflag[i,j] != 0 && pchange < 0.5)
Location[i + changeflag[i,j],j] = Location[i,j];
Location[i,j] = -1;
}
}
}
for (i = 0; i < road.lanenum; i++)
{
j = 0;
while (j < road.Length)
{
int pc = 0; //声明一个控制参量
int[] a = new int[2];
int Headway;
while (j < road.Length && pc < 2)//搜索相邻车辆位置
{
if (Location[i,j] > -1)
{
a[pc] = j;
pc++;
}
j++;
}
if (pc <= 1)
{
int jj = -1;
while (++jj < road.Length && Location[i,jj] == -1) ;
Headway = jj + road.Length - a[0] - 1;//当车辆为头车时的间距
}
else
{
Headway = a[1] - a[0] - 1;//当车辆不是头车时的车间距
j = j - 1;
}
//NaSch更新四部曲
//加速
Location[i,a[0]] = Math.Min(Location[i,a[0]] + 1, road.Vmax);
//减速
if (Location[i,a[0]] >= Headway)
Location[i,a[0]] = Math.Min(Location[i,a[0]], Headway);
//随机慢化
double slowrate = randkey.Next(0, 1000);
slowrate /= 1000;
if (slowrate <= road.P)
Location[i,a[0]] = Math.Max(0, Location[i,a[0]] - 1);
}
}
//位置更新步
for(i=0;i<road.lanenum;i++)
{
for(j=0;j<road .Length;j++)
{
if (Location[i,j] > -1)
{
if (j +Location[i,j] >= road.Length)
Temp_Location[i,j + Location[i,j] - road.Length] = Location[i,j];
else
Temp_Location[i,j + Location[i,j]] = Location[i,j];
}
}
Array.Copy(Temp_Location, Location, Temp_Location.Length);
for (j = 0; j < road.Length; j++)
Temp_Location[i,j] = -1;
}
//更新10000步后记录数据
//记录每次更新的平均速度
if (t >= tt)
{
double Aver_v = 0;
for (i = 0; i < road.lanenum; i++)
{
for (j = 0; j < road.Length; j++)
{
if (Location[i,j] > -1)
{
Aver_v += Location[i,j];
}
}
}
Aver_v /= N;
Ave_eachTT[t - tt] = Aver_v;
}
}
for (i = 0; i < TT - tt; i++)
{
Ave_eachk[N/10] += Ave_eachTT[i];
}
Ave_eachk[N / 10] /= (TT - tt);
flow_average[N/10] = Ave_eachk[N / 10] * q;
Console.WriteLine("Processing....");
}
//输出数据流量
FileStream fc = File.Create("D:\\flowSTNS.txt");
StreamWriter fw = new StreamWriter(()fc);
for (int i = 0; i < 100; i++)
{
fw.Write("{0} ", flow_average[i]);
}
fw.Close();
Console.WriteLine("Perfect");
Console.ReadLine();
想问下,这个循环能执行下去么????
我调试的时候它就中断了!!什么情况
[ 本帖最后由 zzia510 于 2013-1-12 23:35 编辑 ]