多AGV调度两阶段算法模拟源码
多AGV调度系统实现,代码包含5个AGV基于A*算法搜路,基于两阶段算法进行调度
主界面.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Timers;
namespace AGVS
{
public partial class 主界面 : Form
{
/// 定义单元格类
public class mybutton : Button
{
int x;
int y;
public int X
{
set { x = value; }
get { return x; }
}
public int Y
{
set { y = value; }
get { return y; }
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);
}
}
/// <summary>
/// 定义枚举变量
/// </summary>
enum mycc
{
wall,
start,
des
}
byte[,] R = new byte[10, 10]
{
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
mybutton[,] mybut = new mybutton[21, 11];
byte[,] myR = new byte[21, 11];/////////////建立我的地图
MyPoint pa1 = new MyPoint();
MyPoint pb1 = new MyPoint();
MyPoint pa2 = new MyPoint();
MyPoint pb2 = new MyPoint();
MyPoint pa3 = new MyPoint();
MyPoint pb3 = new MyPoint();
MyPoint pa4 = new MyPoint();
MyPoint pb4 = new MyPoint();
MyPoint pa5 = new MyPoint();
MyPoint pb5 = new MyPoint();
MyPoint pa1_1 = new MyPoint();
MyPoint pa2_2 = new MyPoint();
MyPoint pa3_3 = new MyPoint();
MyPoint pa4_4 = new MyPoint();
MyPoint pa5_5 = new MyPoint();
List<MyPoint> myp1 = new List<MyPoint>();
List<MyPoint> myp2 = new List<MyPoint>();
List<MyPoint> myp3 = new List<MyPoint>();
List<MyPoint> myp4 = new List<MyPoint>();
List<MyPoint> myp5 = new List<MyPoint>();
int error1 = 0;
int error2 = 0;
int error3 = 0;
int error4 = 0;
int error5 = 0;
public 主界面()
{
InitializeComponent();
}
System.Timers.Timer aTimer = new System.Timers.Timer();//////实例化Timer类用于运行
/// <summary>
/// 定时时间到需要执行的函数
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private void test1(object source, System.Timers.ElapsedEventArgs e)
{
foreach (MyPoint p in myp1)
{
if (p.father == pa1_1)
{
if (myR[p.x, p.y] == 1)
{
mybut[p.x, p.y].BackColor = Color.Yellow;
myR[p.x, p.y] = 0;
myR[pa1_1.x, pa1_1.y] = 1;
if ((pa1_1.x == pb2.x && pa1_1.y == pb2.y) ||( pa1_1.x == pb3.x && pa1_1.y == pb3.y)
|| (pa1_1.x == pb4.x && pa1_1.y == pb4.y)||(pa1_1.x == pb5.x && pa1_1.y == pb5.y))
{
mybut[pa1_1.x, pa1_1.y].BackColor = Color.Red;
}
else
{
mybut[pa1_1.x, pa1_1.y].BackColor = SystemColors.Control;
mybut[pa1_1.x, pa1_1.y].UseVisualStyleBackColor = true;
}
pa1_1 = p;
}
else
error1++;
}
}
foreach (MyPoint p in myp2)
{
if (p.father == pa2_2)
{
if (myR[p.x, p.y] == 1)
{
mybut[p.x, p.y].BackColor = Color.Yellow;
myR[p.x, p.y] = 0;
myR[pa2_2.x, pa2_2.y] = 1;
if ((pa2_2.x == pb1.x && pa2_2.y == pb1.y) || (pa2_2.x == pb3.x && pa2_2.y == pb3.y)
|| (pa2_2.x == pb4.x && pa2_2.y == pb4.y) || (pa2_2.x == pb5.x && pa2_2.y == pb5.y))
{
mybut[pa2_2.x, pa2_2.y].BackColor = Color.Red;
}
else
{
mybut[pa2_2.x, pa2_2.y].BackColor = SystemColors.Control;
mybut[pa2_2.x, pa2_2.y].UseVisualStyleBackColor = true;
}
pa2_2 = p;
}
else
error2++;
}
}
foreach (MyPoint p in myp3)
{
if (p.father == pa3_3)
{
if (myR[p.x, p.y] == 1)
{
mybut[p.x, p.y].BackColor = Color.Yellow;
myR[p.x, p.y] = 0;
myR[pa3_3.x, pa3_3.y] = 1;
if ((pa3_3.x == pb1.x && pa3_3.y == pb1.y) || (pa3_3.x == pb2.x && pa3_3.y == pb2.y)
|| (pa3_3.x == pb4.x && pa3_3.y == pb4.y) || (pa3_3.x == pb5.x && pa3_3.y == pb5.y))
{
mybut[pa3_3.x, pa3_3.y].BackColor = Color.Red;
}
else
{
mybut[pa3_3.x, pa3_3.y].BackColor = SystemColors.Control;
mybut[pa3_3.x, pa3_3.y].UseVisualStyleBackColor = true;
}
pa3_3 = p;
}
else
error3++;
}
}
foreach (MyPoint p in myp4)
{
if (p.father == pa4_4)
{
if (myR[p.x, p.y] == 1)
{
mybut[p.x, p.y].BackColor = Color.Yellow;
myR[p.x, p.y] = 0;
myR[pa4_4.x, pa4_4.y] = 1;
if ((pa4_4.x == pb1.x && pa4_4.y == pb1.y) || (pa4_4.x == pb2.x && pa4_4.y == pb2.y)
|| (pa4_4.x == pb3.x && pa4_4.y == pb3.y) || (pa4_4.x == pb5.x && pa4_4.y == pb5.y))
{
mybut[pa4_4.x, pa4_4.y].BackColor = Color.Red;
}
else
{
mybut[pa4_4.x, pa4_4.y].BackColor = SystemColors.Control;
mybut[pa4_4.x, pa4_4.y].UseVisualStyleBackColor = true;
}
pa4_4 = p;
}
else
error4++;
}
}
foreach (MyPoint p in myp5)
{
if (p.father == pa5_5)
{
if (myR[p.x, p.y] == 1)
{
mybut[p.x, p.y].BackColor = Color.Yellow;
myR[p.x, p.y] = 0;
myR[pa5_5.x, pa5_5.y] = 1;
if ((pa5_5.x == pb1.x && pa5_5.y == pb1.y) || (pa5_5.x == pb2.x && pa5_5.y == pb2.y)
|| (pa5_5.x == pb3.x && pa5_5.y == pb3.y) || (pa5_5.x == pb4.x && pa5_5.y == pb4.y))
{
mybut[pa5_5.x, pa5_5.y].BackColor = Color.Red;
}
else
{
mybut[pa5_5.x, pa5_5.y].BackColor = SystemColors.Control;
mybut[pa5_5.x, pa5_5.y].UseVisualStyleBackColor = true;
}
pa5_5 = p;
}
else
error5++;
}
}
if (error1 > 2)
{
error1 = 0;
AxingTest axx = new AxingTest(myR);
myR[pa1_1.x, pa1_1.y] = 0;
myp1.Clear();
myp1 = axx.FindeWay(pa1_1, pb1);
}
}
//判断开始列表是否包含某个坐标的点
public void SetaTimerParam()
{
//到时间的时候执行事件
aTimer.Elapsed += new ElapsedEventHandler(test1);
aTimer.Interval = 1000;
aTimer.AutoReset = true;//执行一次 false,一直执行true
//是否执行System.Timers.Timer.Elapsed事件
aTimer.Enabled = true;
}
private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void 主界面_Load(object sender, EventArgs e)
{
////////////////////////////
//////////////////////////////场景地图建模
for (int i = 0; i < 21; i++)
{
for (int j = 0; j < 11; j++)
{
mybut[i, j] = new mybutton();
mybut[i, j].X= i;
mybut[i, j].Y = j;
mybut[i, j].Size = new Size(40, 40);
mybut[i, j].Location = new Point(450+i * 40, 100+j * 40); ////////定位起点450 100
this.Controls.Add(mybut[i, j]);
myR[i, j] = 1; //////表示当前单元格空闲
mybut[i, j].MouseDown += 主界面_MouseDown;
}
}
}
void init()
{
for (int i = 0; i < 21; i++)
{
for (int j = 0; j < 11; j++)
{
myR[i, j] = 1;
mybut[i, j].BackColor = Color.Transparent;
}
}
aTimer.Enabled = false;
}
void 主界面_MouseDown(object sender, MouseEventArgs e)
{
mybutton myb = (mybutton)sender;
int x = myb.X;
int y = myb.Y;
}
private void 生成地图ToolStripMenuItem_Click(object sender, EventArgs e)
{
AxingTest ax = new AxingTest(myR);
pa1.x = 5; pa1.y = 0;
pb1.x = 5; pb1.y = 8;
pa2.x = 5; pa2.y = 9;
pb2.x = 5; pb2.y = 1;
pa3.x = 2; pa3.y = 0;
pb3.x = 14; pb3.y = 7;
pa4.x = 0; pa4.y = 0;
pb4.x = 15; pb4.y = 5;
pa5.x = 4; pa5.y = 1;
pb5.x = 8; pb5.y = 4;
pa1_1 = pa1;
pa2_2 = pa2;
pa3_3 = pa3;
pa4_4 = pa4;
pa5_5 = pa5;
mybut[pa1.x, pa1.y].BackColor = Color.Green;
mybut[pb1.x, pb1.y].BackColor = Color.Red;
mybut[pa2.x, pa2.y].BackColor = Color.Green;
mybut[pb2.x, pb2.y].BackColor = Color.Red;
mybut[pa3.x, pa3.y].BackColor = Color.Green;
mybut[pb3.x, pb3.y].BackColor = Color.Red;
mybut[pa4.x, pa4.y].BackColor = Color.Green;
mybut[pb4.x, pb4.y].BackColor = Color.Red;
mybut[pa5.x, pa5.y].BackColor = Color.Green;
mybut[pb5.x, pb5.y].BackColor = Color.Red;
myp1 = ax.FindeWay(pa1, pb1);
myp2 = ax.FindeWay(pa2, pb2);
myp3 = ax.FindeWay(pa3, pb3);
myp4 = ax.FindeWay(pa4, pb4);
myp5 = ax.FindeWay(pa5, pb5);
}
//private void 障碍点ToolStripMenuItem_Click(object sender, EventArgs e)
//{
// mychoose = mycc.wall;
//}
private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.SetaTimerParam();
}
private void 初始化ToolStripMenuItem_Click(object sender, EventArgs e)
{
init();
}
private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e)
{
aTimer.Enabled = false;
}
}
}
© 版权声明
1. 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长 QQ2766242327进行删除处理。
2. 本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
THE END
暂无评论内容