public void test_vector() { _log.enterScope(); UtilityVector vec1 = new UtilityVector(0, 1, 2, 3, 4, 5); float mag = vec1.magnitude; _log.logDebug("mag: " + mag); Verify.VerifyTrue("verify mag gte 0", true, mag >= 0); Verify.VerifyTrue("norm is not null", true, vec1.normalize() != null); UtilityVector vec2 = new UtilityVector(5, 4, 3, 2, 1, 0); float dot = vec1.dot(vec2); _log.logDebug("dot: " + dot); Verify.VerifyTrue("dot between 1 and -1", true, (dot <= 1) && (dot >= -1)); dot = vec1.dot(vec1); _log.logDebug("self dot: " + dot); Verify.VerifyTrue("dot with itself should be 1", true, dot == 1); _log.exitScope(); }
public void selector_1() { _log.enterScope(); UtilityVector vector = new UtilityVector(0, 1, 0, 2); BehaviorAction action = new BehaviorAction(delegate() { return(BehaviorReturnCode.Success); }); UtilityPair pair = new UtilityPair(vector, action); UtilitySelector sel = new UtilitySelector(delegate(){ return(new UtilityVector(0, 1, 1, 2)); }, pair, pair); BehaviorReturnCode result = sel.Behave(); Verify.VerifyNotEquals("basic vector compare", true, result, BehaviorReturnCode.Failure); _log.exitScope(); }
/// <summary> /// Dot between this and another specified vector. (based on normalized vectors) /// </summary> /// <param name="vector">Vector.</param> public float dot(UtilityVector vector) { if (this.magnitude == 0 || vector.magnitude == 0) { return(-2); } UtilityVector a = this.normalize(); UtilityVector b = vector.normalize(); float val = 0; for (int i = 0; i < this.values.Length; i++) { val += a.values [i] * b.values [i]; } return(val); }
/// <summary> /// Return a new vector based on the normalization of this instance. /// </summary> public UtilityVector normalize() { if (this.values.Length <= 0) { return(null); } UtilityVector vec = new UtilityVector(); vec.values = new float[this.values.Length]; this.values.CopyTo(vec.values, 0); float mag = vec.magnitude; for (int i = 0; i < vec.values.Length; i++) { vec.values [i] = vec.values [i] / mag; } return(vec); }
public BehaviorReturnCode Behave(Entity entity) { try{ UtilityVector func_vector = this._utility_function(); float min = -2.0f; UtilityPair best_match = null; //find max pair match foreach (UtilityPair pair in this._utility_pairs) { float val = func_vector.dot(pair.vector); if (val > min) { min = val; best_match = pair; } } //make sure we found a match if (best_match == null) { #if DEBUG Console.WriteLine("best_match not defined..."); #endif this.ReturnCode = BehaviorReturnCode.Failure; return(this.ReturnCode); } //execute best pair match and return result this.ReturnCode = best_match.behavior.Behave(entity); return(this.ReturnCode); }catch (Exception e) { #if DEBUG Console.WriteLine(e.ToString()); #endif this.ReturnCode = BehaviorReturnCode.Failure; return(BehaviorReturnCode.Failure); } }
public void test_vector(){ _log.enterScope (); UtilityVector vec1 = new UtilityVector (0, 1, 2, 3, 4, 5); float mag = vec1.magnitude; _log.logDebug ("mag: " + mag); Verify.VerifyTrue ("verify mag gte 0", true, mag >= 0); Verify.VerifyTrue ("norm is not null", true, vec1.normalize () != null); UtilityVector vec2 = new UtilityVector (5, 4, 3, 2, 1, 0); float dot = vec1.dot (vec2); _log.logDebug ("dot: " + dot); Verify.VerifyTrue ("dot between 1 and -1", true, (dot <= 1) && (dot >= -1)); dot = vec1.dot (vec1); _log.logDebug ("self dot: " + dot); Verify.VerifyTrue ("dot with itself should be 1", true, dot == 1); _log.exitScope (); }
public UtilityPair(UtilityVector vector, IBehavior behavior) { this.vector = vector; this.behavior = behavior; }
public void selector_1() { _log.enterScope(); UtilityVector vector = new UtilityVector(0, 1, 0, 2); BehaviorAction action = new BehaviorAction(delegate() { return BehaviorReturnCode.Success; }); UtilityPair pair = new UtilityPair(vector, action); UtilitySelector sel = new UtilitySelector(delegate(){return new UtilityVector(0,1,1,2);},pair,pair); BehaviorReturnCode result = sel.Behave(); Verify.VerifyNotEquals("basic vector compare", true, result, BehaviorReturnCode.Failure); _log.exitScope(); }
public void selector_3(){ _log.enterScope(); //build vectors UtilityVector a = new UtilityVector(0, 1, 0, 1); UtilityVector b = new UtilityVector(1, 1, 0, 0); UtilityVector c = new UtilityVector(1, 0, 1, 0); UtilityVector d = new UtilityVector(0, 0, 1, 1); string choice = ""; //build actions that change choice if called BehaviorAction aa = new BehaviorAction (delegate() { choice = "a"; return BehaviorReturnCode.Success; }); BehaviorAction ba = new BehaviorAction (delegate() { choice = "b"; return BehaviorReturnCode.Success; }); BehaviorAction ca = new BehaviorAction (delegate() { choice = "c"; return BehaviorReturnCode.Success; }); BehaviorAction da = new BehaviorAction (delegate() { choice = "d"; return BehaviorReturnCode.Success; }); //build the appropraite pairs UtilityPair ap = new UtilityPair (a, aa); UtilityPair bp = new UtilityPair (b, ba); UtilityPair cp = new UtilityPair (c, ca); UtilityPair dp = new UtilityPair (d, da); //execute tests UtilitySelector sel = new UtilitySelector (delegate() { return new UtilityVector (0.5f,0.7f,0.4f,0.8f); }, ap, bp, cp, dp); sel.Behave (); Verify.VerifyTrue ("a chosen", true, choice == "a"); sel = new UtilitySelector (delegate() { return new UtilityVector (0.7f,0.8f,0.5f,0.4f); }, ap, bp, cp, dp); sel.Behave (); Verify.VerifyTrue ("b chosen", true, choice == "b"); sel = new UtilitySelector (delegate() { return new UtilityVector (0.7f,0.5f,0.8f,0.4f); }, ap, bp, cp, dp); sel.Behave (); Verify.VerifyTrue ("c chosen", true, choice == "c"); sel = new UtilitySelector (delegate() { return new UtilityVector (0.5f,0.4f,0.7f,0.8f); }, ap, bp, cp, dp); sel.Behave (); Verify.VerifyTrue ("d chosen", true, choice == "d"); _log.exitScope (); }
public void selector_3() { _log.enterScope(); //build vectors UtilityVector a = new UtilityVector(0, 1, 0, 1); UtilityVector b = new UtilityVector(1, 1, 0, 0); UtilityVector c = new UtilityVector(1, 0, 1, 0); UtilityVector d = new UtilityVector(0, 0, 1, 1); string choice = ""; //build actions that change choice if called BehaviorAction aa = new BehaviorAction(delegate() { choice = "a"; return(BehaviorReturnCode.Success); }); BehaviorAction ba = new BehaviorAction(delegate() { choice = "b"; return(BehaviorReturnCode.Success); }); BehaviorAction ca = new BehaviorAction(delegate() { choice = "c"; return(BehaviorReturnCode.Success); }); BehaviorAction da = new BehaviorAction(delegate() { choice = "d"; return(BehaviorReturnCode.Success); }); //build the appropraite pairs UtilityPair ap = new UtilityPair(a, aa); UtilityPair bp = new UtilityPair(b, ba); UtilityPair cp = new UtilityPair(c, ca); UtilityPair dp = new UtilityPair(d, da); //execute tests UtilitySelector sel = new UtilitySelector(delegate() { return(new UtilityVector(0.5f, 0.7f, 0.4f, 0.8f)); }, ap, bp, cp, dp); sel.Behave(); Verify.VerifyTrue("a chosen", true, choice == "a"); sel = new UtilitySelector(delegate() { return(new UtilityVector(0.7f, 0.8f, 0.5f, 0.4f)); }, ap, bp, cp, dp); sel.Behave(); Verify.VerifyTrue("b chosen", true, choice == "b"); sel = new UtilitySelector(delegate() { return(new UtilityVector(0.7f, 0.5f, 0.8f, 0.4f)); }, ap, bp, cp, dp); sel.Behave(); Verify.VerifyTrue("c chosen", true, choice == "c"); sel = new UtilitySelector(delegate() { return(new UtilityVector(0.5f, 0.4f, 0.7f, 0.8f)); }, ap, bp, cp, dp); sel.Behave(); Verify.VerifyTrue("d chosen", true, choice == "d"); _log.exitScope(); }