<br>&nbsp;&nbsp;&nbsp; The problem of using Math formulas in the LHS of a rule is that the expression can not be indexed by the engine and so it is much heavier to execute. So, yes, if your rules LHS can work only on previously calculated values, performance will increase.
<br>&nbsp;&nbsp;&nbsp; One thing you may try is instead of logically asserting a new fact with the distance, you may set an attribute in the Tile or the Agent facts themselves.<br><br>&nbsp;&nbsp;&nbsp;&nbsp; []s<br>&nbsp;&nbsp;&nbsp;&nbsp; Edson<br><br><div><span class="gmail_quote">
2007/12/8, velven &lt;<a href="mailto:velven83@hotmail.com">velven83@hotmail.com</a>&gt;:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>Hi, thanks for&nbsp;&nbsp;replying.<br><br>your 2.b. is what I have done currently, but theres a problem as I have<br>thousands of Agents and Tiles. and my range can go up to quite high, so i<br>suspect that there are too much assertions of the&nbsp;&nbsp;&quot;TileInRange&quot; for each
<br>and every tick on the simulation and eventually compromising performace<br>again (it almost hangs in one of my my test case)<br><br>I have thought of 2.a. too but the performance is poor as you mentioned.<br><br>BTW, Can I gather that I shouldn&#39;t include any calculations in my LHS rule
<br>else the performace of the engine will deteriorate greatly with more<br>calculations and perhaps not utilising rete?<br><br>If this is true, then perhaps I can convince my Boss to do this part of the<br>problem in Java, which is faster actually.
<br>Thanks<br><br><br>Edson Tirelli-3 wrote:<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; Your reasoning on how to solve the problem is correct. I don&#39;t know<br>&gt; about your implementation, but my suggestion is:<br>&gt;<br>&gt; 1. Create a function to calculate the distance between 2 points. I would
<br>&gt; prefer to create it as a static method in a helper class, but you can<br>&gt; define<br>&gt; it in the DRL if you prefer:<br>&gt;<br>&gt; function double distance( long originX, long originY, long actualX, long<br>
&gt; actualY ) {<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; // return the calculated distance<br>&gt; }<br>&gt;<br>&gt; 2.a. If you want to do it in a single rule, write something like this:<br>&gt;<br>&gt; when<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; $a: Agent( $x1:x, $y1:y, $r:range)
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; $t1: Tile( $x2: x, $y2 : y, eval( distance( $x1, $y1, $x2, $y2 ) &lt;= $r<br>&gt; )<br>&gt; )<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; not Tile( eval( distance( $x1, $y1, x, y ) &lt; distance( $x1, $y1, $x2,<br>&gt; $y2 ) ) )<br>&gt; then
<br>&gt; end<br>&gt;<br>&gt; The downside of such approach is that it recalculates the distance many<br>&gt; times and the performance would be poor.<br>&gt;<br>&gt; 2.b. If you want best performance, try a more relational approach with 2
<br>&gt; rules:<br>&gt;<br>&gt; rule &quot;calculate distance&quot;<br>&gt; when<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; $a: Agent( $x1 : x, $y1 : y, $r : range )<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; $t: Tile( eval( distance( $x1, $y1, x, y ) &lt; $r ) )<br>&gt; then<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; assertLogical( new TileInRange( $a, $t, distance( $x1, $y1, x, y ) )
<br>&gt; );<br>&gt; end<br>&gt;<br>&gt; rule &quot;find the closest&quot;<br>&gt; when<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; $a: Agent()<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; $tr TileInRange( agent == $a, $tile : tile, $dist : distance )<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; not TileInRange( agent == $a, distance &lt; $dist )
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; $t : Tile( this == $tile ) // this last pattern may be removed if you<br>&gt; don&#39;t need to constrain any other attribute<br>&gt; then<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; // do something<br>&gt; end<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;Hope it helps
<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; Edson<br>&gt;<br>&gt; 2007/12/6, velven &lt;<a href="mailto:velven83@hotmail.com">velven83@hotmail.com</a>&gt;:<br>&gt;&gt;<br>&gt;&gt;<br>&gt;&gt; HI, i have a problem<br>&gt;&gt; using tileworld model, i&#39;m supposed to write a rule which will cause the
<br>&gt;&gt; agents to choose the tile in the agent&#39;s range which is closest to the<br>&gt;&gt; agent.<br>&gt;&gt;<br>&gt;&gt; so a rough idea is<br>&gt;&gt;<br>&gt;&gt; when<br>&gt;&gt;<br>&gt;&gt; agent: Agent(x1:x, y1:y, r:range)
<br>&gt;&gt;<br>&gt;&gt; tile1: Tile(&lt;tile&#39;s x to be between (x1+r) and (x1-r)&gt; &amp;&amp; &lt;tile&#39;s y to be<br>&gt;&gt; between (y1+r and y1-r)&gt;)<br>&gt;&gt;<br>&gt;&gt; tile2: not Tile( &lt; which is closer than the tile1 to agent&gt; )
<br>&gt;&gt;<br>&gt;&gt; desperately need help,<br>&gt;&gt; alternatives tried::<br>&gt;&gt; i tried to use a query to return a list of tiles which is within the<br>&gt;&gt; agent&#39;s<br>&gt;&gt; range then compared which one is closer and other similar implementation,
<br>&gt;&gt; but this too slow for my simulation...<br>&gt;&gt;<br>&gt;&gt; I need one single good rule or efficient way of implementing the above<br>&gt;&gt; rule<br>&gt;&gt;<br>&gt;&gt;<br>&gt;&gt; then i tried to do multiple constraints too, say&nbsp;&nbsp;&nbsp;&nbsp; x (&gt; 30 &amp;&amp; &lt; 40) is
<br>&gt;&gt; ok<br>&gt;&gt; but<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x (&gt;<br>&gt;&gt; ($x-$r) &amp;&amp; &lt; ($x+$r)) gives errors ,due to extra parenthesis? i dunno..<br>&gt;&gt; any
<br>&gt;&gt; workarounds?<br>&gt;&gt;<br>&gt;&gt; Thanks!<br>&gt;&gt;<br>&gt;&gt;<br>&gt;&gt; --<br>&gt;&gt; View this message in context:<br>&gt;&gt; <a href="http://www.nabble.com/How-to-write-a-rule-which-only-fires-for-a-closest-value--tf4947931.html#a14166650">
http://www.nabble.com/How-to-write-a-rule-which-only-fires-for-a-closest-value--tf4947931.html#a14166650</a><br>&gt;&gt; Sent from the drools - user mailing list archive at <a href="http://Nabble.com">Nabble.com</a>.<br>&gt;&gt;
<br>&gt;&gt; _______________________________________________<br>&gt;&gt; rules-users mailing list<br>&gt;&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users">
https://lists.jboss.org/mailman/listinfo/rules-users</a><br>&gt;&gt;<br>&gt;<br>&gt;<br>&gt;<br>&gt; --<br>&gt;&nbsp;&nbsp; Edson Tirelli<br>&gt;&nbsp;&nbsp; JBoss Drools Core Development<br>&gt;&nbsp;&nbsp; Office: +55 11 3529-6000<br>&gt;&nbsp;&nbsp; Mobile: +55 11 9287-5646
<br>&gt;&nbsp;&nbsp; JBoss, a division of Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a><br>&gt;<br>&gt; _______________________________________________<br>&gt; rules-users mailing list<br>&gt; <a href="mailto:rules-users@lists.jboss.org">
rules-users@lists.jboss.org</a><br>&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>&gt;<br>&gt;<br><br>--<br>View this message in context: <a href="http://www.nabble.com/How-to-write-a-rule-which-only-fires-for-a-closest-value--tf4947931.html#a14227138">
http://www.nabble.com/How-to-write-a-rule-which-only-fires-for-a-closest-value--tf4947931.html#a14227138</a><br>Sent from the drools - user mailing list archive at <a href="http://Nabble.com">Nabble.com</a>.<br><br>_______________________________________________
<br>rules-users mailing list<br><a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/rules-users">https://lists.jboss.org/mailman/listinfo/rules-users
</a><br></blockquote></div><br><br clear="all"><br>-- <br>&nbsp;&nbsp;Edson Tirelli<br>&nbsp;&nbsp;JBoss Drools Core Development<br>&nbsp;&nbsp;Office: +55 11 3529-6000<br>&nbsp;&nbsp;Mobile: +55 11 9287-5646<br>&nbsp;&nbsp;JBoss, a division of Red Hat @ <a href="http://www.jboss.com">
www.jboss.com</a>