| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 838 人关注过本帖
标题:哪个大神帮忙看一下代码呢???这个循环莫名的就中断了!!
取消只看楼主 加入收藏
zzia510
Rank: 1
等 级:新手上路
帖 子:25
专家分:3
注 册:2012-12-26
结帖率:100%
收藏
已结贴  问题点数:5 回复次数:6 
哪个大神帮忙看一下代码呢???这个循环莫名的就中断了!!
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 编辑 ]
搜索更多相关主题的帖子: double 声明 记录 
2013-01-12 11:52
zzia510
Rank: 1
等 级:新手上路
帖 子:25
专家分:3
注 册:2012-12-26
收藏
得分:0 
没人么???
2013-01-12 12:54
zzia510
Rank: 1
等 级:新手上路
帖 子:25
专家分:3
注 册:2012-12-26
收藏
得分:0 
求助各位潜水的大神!!!!
2013-01-12 15:26
zzia510
Rank: 1
等 级:新手上路
帖 子:25
专家分:3
注 册:2012-12-26
收藏
得分:0 
回复 4楼 mmxo
主要是这个循环啊!!!别的部分都没什么问题。。。只是这个循环一次之后就中断了。。
2013-01-12 22:53
zzia510
Rank: 1
等 级:新手上路
帖 子:25
专家分:3
注 册:2012-12-26
收藏
得分:0 
回复 4楼 mmxo
版主大神,我把全部代码发过来了,有人帮我看看吗????
真是太感谢了、、、、
2013-01-12 23:36
zzia510
Rank: 1
等 级:新手上路
帖 子:25
专家分:3
注 册:2012-12-26
收藏
得分:0 
回复 8楼 fily1314
我这个不是函数啊。。。是主程序。。
2013-01-14 14:46
zzia510
Rank: 1
等 级:新手上路
帖 子:25
专家分:3
注 册:2012-12-26
收藏
得分:0 
回复 7楼 mmxo
类在前面呢,应该没什么问题。。
2013-01-14 14:46
快速回复:哪个大神帮忙看一下代码呢???这个循环莫名的就中断了!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045262 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved