List <OptionN> createOptFromSubGoals(List <SubGoal> subgoals) { List <OptionN> options = new List <OptionN>(); for (int i = 0; i < subgoals.Count; i++) { SubGoal s = subgoals[i]; OptionN o = new OptionN(s.State, this); options.Add(o); for (int j = 0; j < Model[s.State].Count; j++) { if (Model[s.State][j] == null) { continue; } if (Q[s.State].Actions[j].O != null) { continue; } o.AddSASp(s.State, j, Model[s.State][j].sP); constOptionN(subgoals, s, o, Model[s.State][j].sP); } } return(options); }
private void m_comboOptions_SelectedIndexChanged(object sender, EventArgs e) { OptionN o = (OptionN)m_comboOptions.SelectedItem; if (o == null) { return; } List <int> used = new List <int>(); Bitmap bmp = new Bitmap(m_graph.Width * 2, m_graph.Height * 2); Graphics g = Graphics.FromImage(bmp); draw(o, used, o.SubgoalState, g, new PointF(m_graph.Width + offsetX, m_graph.Height + offsetY)); g.Dispose(); m_graph.BackgroundImageLayout = ImageLayout.Stretch; m_graph.BackgroundImage = bmp; }
void constOptionN(List <SubGoal> subgoals, SubGoal s, OptionN o, int state) { if (o.initSet.Keys.Contains(state)) { return; } if (isSubgoal(state)) { return; } Q[state].Actions.Add(new Action(o)); for (int i = 0; i < Model[state].Count; i++) { if (Model[state][i] != null && Q[state].Actions[i].O == null) { o.AddSASp(state, i, Model[state][i].sP); constOptionN(subgoals, s, o, Model[state][i].sP); } } }
void draw(OptionN o, List <int> used, int state, Graphics g, PointF pos) { Color colArrow = Color.Black, colNode = Color.LightGray, colValue = Color.Black, colState = Color.Black; float arrow = 5, node = 20, space = 60, hnode = 10; arrow *= size; node *= size; space *= size; hnode *= size; Font f = new Font(this.Font.FontFamily, this.Font.Size * size); if (!o.V.Keys.Contains(state)) { return; } if (used.Contains(state)) { return; } used.Add(state); g.FillEllipse(new SolidBrush(colNode), new RectangleF(pos, new SizeF(node, node))); g.DrawString(state.ToString(), f, new SolidBrush(colState), pos); g.DrawString(o.V[state].ToString("0.00"), f, new SolidBrush(colValue), new PointF(pos.X + node, pos.Y + node)); foreach (int i in o.initSet[state].aSp.Keys) { PointF p = new PointF(0, 0); if (i == 0) { p = new PointF(pos.X, pos.Y - space); } else if (i == 1) { p = new PointF(pos.X, pos.Y + space); } else if (i == 2) { p = new PointF(pos.X - space, pos.Y); } else if (i == 3) { p = new PointF(pos.X + space, pos.Y); } if (o.initSet[state].aSp[i] != null) { PointF pA = new PointF(); PointF pB = new PointF(); Pen pn = new Pen(colArrow); if (pos.X == p.X) { pA.X = pos.X + hnode; pB.X = p.X + hnode; if (pos.Y > p.Y) { pA.Y = pos.Y; pB.Y = p.Y + node; g.DrawLine(pn, pB, new PointF(pB.X - arrow, pB.Y + arrow)); g.DrawLine(pn, pB, new PointF(pB.X + arrow, pB.Y + arrow)); } else { pA.Y = pos.Y + node; pB.Y = p.Y; g.DrawLine(pn, pB, new PointF(pB.X - arrow, pB.Y - arrow)); g.DrawLine(pn, pB, new PointF(pB.X + arrow, pB.Y - arrow)); } } if (pos.Y == p.Y) { pA.Y = pos.Y + hnode; pB.Y = p.Y + hnode; if (p.X > pos.X) { pA.X = pos.X + node; pB.X = p.X; g.DrawLine(pn, pB, new PointF(pB.X - arrow, pB.Y - arrow)); g.DrawLine(pn, pB, new PointF(pB.X - arrow, pB.Y + arrow)); } else { pA.X = pos.X; pB.X = p.X + node; g.DrawLine(pn, pB, new PointF(pB.X + arrow, pB.Y - arrow)); g.DrawLine(pn, pB, new PointF(pB.X + arrow, pB.Y + arrow)); } } g.DrawLine(Pens.Blue, pA, pB); draw(o, used, o.initSet[state].aSp[i], g, p); } } }
public void RunResult() { double oldRho = Agent.rho; Agent.rho = 0.1; if (Agent == null) { MessageBox.Show("Learn first!"); return; } for (int i = 0; i < Agent.contexts.Count; i++) { Agent.contexts[i].Em = 0; } while (true) { Point p = new Point(m_mouseLocation.X, m_mouseLocation.Y); int s = locationToState(p); int a = Agent.Q[s].GetMaxAction(); int flow = data.getFlow(p.X, p.Y); if (a < 4) { if (flow == 5) { if (a == 1) //UP { p.Y--; } if (a == 0) //Down { p.Y++; } if (a == 3) //Left { p.X--; } if (a == 2) //Right { p.X++; } } else if (flow == 6) { if (a == 2) { p.Y--; } if (a == 3) { p.Y++; } if (a == 1) { p.X--; } if (a == 0) { p.X++; } } else if (flow == 7) { if (a == 3) { p.Y--; } if (a == 2) { p.Y++; } if (a == 0) { p.X--; } if (a == 1) { p.X++; } } else { if (a == 0) //UP { p.Y--; } if (a == 1) //Down { p.Y++; } if (a == 2) //Left { p.X--; } if (a == 3) //Right { p.X++; } } if (p.X < 0) { p.X = 0; } if (p.Y < 0) { p.Y = 0; } if (p.X >= GridSize) { p.X = GridSize - 1; } if (p.Y >= GridSize) { p.Y = GridSize - 1; } if (isBlock(p)) { p = new Point(m_mouseLocation.X, m_mouseLocation.Y); } Point pTmp = p; if (flow > 0) { if (flow == 1 && a != 1) { p.Y--; } if (flow == 2 && a != 0) { p.Y++; } if (flow == 3 && a != 3) { p.X--; } if (flow == 4 && a != 2) { p.X++; } if (p.X < 0 || p.Y < 0 || p.X >= GridSize || p.Y >= GridSize) { p = pTmp; } if (isBlock(p)) { p = pTmp; } } if (checkSwitchContext) { Agent.checkAndSwitchContextNoNew(s, a, locationToState(p), 0); } if (StepPassed != null) { StepPassed(); } } else if (a >= 4) { OptionN op = Agent.Q[s].Actions[a].O; int sP = locationToState(p); while (sP != op.SubgoalState) { flow = data.getFlow(p.X, p.Y); Point pr = p; if (!op.initSet.Keys.Contains(sP)) { break; } int[] temp = op.maxA(sP); if (flow == 5) { if (temp[1] == 1) //UP { p.Y--; } if (temp[1] == 0) //Down { p.Y++; } if (temp[1] == 3) //Left { p.X--; } if (temp[1] == 2) //Right { p.X++; } } else if (flow == 6) { if (temp[1] == 2) { p.Y--; } if (temp[1] == 3) { p.Y++; } if (temp[1] == 1) { p.X--; } if (temp[1] == 0) { p.X++; } } else if (flow == 7) { if (temp[1] == 3) { p.Y--; } if (temp[1] == 2) { p.Y++; } if (temp[1] == 0) { p.X--; } if (temp[1] == 1) { p.X++; } } else { if (temp[1] == 0) //UP { p.Y--; } if (temp[1] == 1) //Down { p.Y++; } if (temp[1] == 2) //Left { p.X--; } if (temp[1] == 3) //Right { p.X++; } } if (p.X < 0) { p.X = 0; } if (p.Y < 0) { p.Y = 0; } if (p.X >= GridSize) { p.X = GridSize - 1; } if (p.Y >= GridSize) { p.Y = GridSize - 1; } if (isBlock(p)) { p = pr; } m_mouseLocation = p; if (checkSwitchContext) { if (Agent.checkAndSwitchContextNoNew(sP, temp[1], locationToState(p), 0)) { break; } } if (StepPassed != null) { StepPassed(); } sP = locationToState(p); drawMe(); Thread.Sleep(100); } } if (!(p.X < 0 || p.Y < 0 || p.X >= GridSize || p.Y >= GridSize || isBlock(p))) { m_mouseLocation = p; } drawMe(); if (data.goalLocation == m_mouseLocation) { break; } Thread.Sleep(100); } if (RunFinished != null) { RunFinished(); } Agent.rho = oldRho; }
public Action(OptionN o) { O = o; }