/// <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; }
/// <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 override BehaviorReturnCode Behave() { try{ UtilityVector func_vector = this._utility_function.Invoke(); 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(); return(this.ReturnCode); }catch (Exception e) { #if DEBUG Console.WriteLine(e.ToString()); #endif ReturnCode = BehaviorReturnCode.Failure; return(BehaviorReturnCode.Failure); } }
public UtilityPair(UtilityVector vector, BehaviorComponent behavior) { this.vector = vector; this.behavior = behavior; }