public BingController(IBingManager context, IConfiguration configuration, IGetTrails trailContext, ITrail trail) { _context = context; Configuration = configuration; _getTrails = trailContext; _trail = trail; }
/// <summary> /// Backtracks to the current choice point /// </summary> public void BacktrackToChoicePoint() { // Restore the environment permanent variables if (!ReferenceEquals(_choicePoint.Environment, _environment)) { for (var varIndex = 0; varIndex < _choicePoint.Environment.Variables.Length; ++varIndex) { _registers[varIndex] = _choicePoint.Environment.Variables[varIndex]; } for (var varIndex = _choicePoint.Environment.Variables.Length; varIndex < _environment.Variables.Length; ++varIndex) { _registers[varIndex] = new SimpleReference(); } } // Restore the argument registers var argumentIndex = _choicePoint.Environment.Variables.Length; foreach (var argument in _choicePoint.Arguments) { _registers[argumentIndex].SetTo(argument); ++argumentIndex; } // Environment is reset to the choice point environment _environment = _choicePoint.Environment; // Reset the trail _choicePoint.Trail.Reset(); _trail = _choicePoint.Trail; }
/// <summary> /// Allocates a new choice point with the current execution state /// </summary> public void TryMeElse(int nextClause) { var argumentValues = _registers.Skip(_environment.Variables.Length).Take(_environment.NumberOfArguments); var newChoice = new ChoicePoint(_choicePoint, _environment, argumentValues, new BasicTrail(), nextClause); _choicePoint = newChoice; _trail = _choicePoint.Trail; }
/// <summary> /// Unifies a value on the heap /// </summary> public static bool Unify(this IReferenceLiteral address1, IReferenceLiteral address2, ITrail trail) { var unifyStack = new Stack<IReferenceLiteral>(); // Push the addresses that we're going to unify unifyStack.Push(address1); unifyStack.Push(address2); // Iterate until the stack is empty while (unifyStack.Count > 0) { // Deref the values on the stack var value1 = unifyStack.Pop().Dereference(); var value2 = unifyStack.Pop().Dereference(); if (!ReferenceEquals(value1, value2)) { if (value1.IsReference()) { // Bind references trail.Record(value1); value1.SetTo(value2); } else if (value2.IsReference()) { trail.Record(value2); value2.SetTo(value1); } else { if (Equals(value1.Term, value2.Term)) { // Process the rest of the structure var structurePos1 = value1.Reference; var structurePos2 = value2.Reference; while (structurePos1 != null) { unifyStack.Push(structurePos1); unifyStack.Push(structurePos2); structurePos1 = structurePos1.NextArgument; structurePos2 = structurePos2.NextArgument; } } else { // Structures do not match: fail return false; } } } } return true; }
public ChoicePoint(ChoicePoint previousChoice, ByteCodeEnvironment environment, IEnumerable<IReferenceLiteral> arguments, ITrail trail, int nextClause) { if (arguments == null) throw new ArgumentNullException(nameof(arguments)); if (trail == null) throw new ArgumentNullException(nameof(trail)); _previousChoicePoint = previousChoice; _arguments = arguments.Select(arg => new SimpleReference(arg)).ToArray(); _trail = trail; _nextClause = nextClause; _environment = environment; }
private void ValidateCurves(ITrail referenceSamples, ITrail movingSamples) { if (validator.Validate(referenceSamples, movingSamples, allowedError)) { Debug.LogFormat("Curves are close enough"); } else { Debug.LogFormat("Curves are too far"); } }
public bool Validate(ITrail expected, ITrail actual, float threshold) { var expectedSamples = expected.GetSampledLocations(); var actualSamples = expected.GetSampledLocations(); var diff = expectedSamples.Zip(actualSamples, (e, a) => e - a); var normDiff = diff.Select(d => d.magnitude); var sumNormDiff = normDiff.Aggregate(0.0, (x, y) => x + y); var mse = sumNormDiff / diff.Count(); bool isValid = mse < threshold; return(isValid); }
public bool Validate(ITrail expected, ITrail actual, float threshold) { var expectedSamples = expected.GetSampledLocations(); var actualSamples = actual.GetSampledLocations(); var diff = expectedSamples.Zip(actualSamples, (e, a) => e - a); var normDiff = diff.Select(d => d.magnitude); var maxNormDiff = normDiff.Aggregate(Mathf.NegativeInfinity, (x, y) => Mathf.Max(x, y)); bool isValid = maxNormDiff < threshold; return(isValid); }
/// <summary> /// Backtracks to the current choice point and discards it /// </summary> public void TrustMe() { BacktrackToChoicePoint(); _programCounter = _choicePoint.NextClause; _choicePoint = _choicePoint.PreviousChoicePoint; if (_choicePoint == null) { _trail = new NoTrail(); } else { _trail = _choicePoint.Trail; } }
public ChoicePoint(ChoicePoint previousChoice, ByteCodeEnvironment environment, IEnumerable <IReferenceLiteral> arguments, ITrail trail, int nextClause) { if (arguments == null) { throw new ArgumentNullException(nameof(arguments)); } if (trail == null) { throw new ArgumentNullException(nameof(trail)); } _previousChoicePoint = previousChoice; _arguments = arguments.Select(arg => new SimpleReference(arg)).ToArray(); _trail = trail; _nextClause = nextClause; _environment = environment; }
public void NewLevel() { loadingScreen.enabled = true; levelPlayable.SetValue(false); levelGenerator.GenerateNewLevel(); levelGenerator.LoadGeneratedLevel(); var config = levelConfigurationReference.GetLevelConfiguration(); var solutionBall = Array.Find(world.GetComponentsInChildren <ItemAnimator>(), item => item.GetId() == config.solutionItemId); solutionTrailGenerator.transform.position = solutionBall.transform.position + config.solutionStartPositionOnItem; solutionTrailGenerator.StartSimulation(samples => { solutionTrail = samples; levelGenerator.LoadGeneratedLevel(); loadingScreen.enabled = false; levelPlayable.SetValue(true); }, solutionBall.transform, config.solutionStartPositionOnItem); }
public bool Validate(ITrail expected, ITrail actual, float threshold) { var expectedSamples = expected.GetSampledLocations(); var actualSamples = actual.GetSampledLocations(); var(np_exp, np_act) = prepare_np_input(expectedSamples, actualSamples); // we know that the curves are sampled at same time intervals. If we didn't we would need to interpolate // both of them (cubic spline or other) and sample at equal times or referably at equal distances. // Since we do know, we can skip this phase. // Resampling at same-spacial-distances could help, but it would be negligible here. //normalize both curves to zero mean to be able to find curves that look the same but are far away var one = new int[1]; one[0] = 1; var exp_mean = np.mean(np_exp, one); var exp_normalized = np_exp - exp_mean; var act_mean = np.mean(np_act, one); var act_normalized = np_act - act_mean; // now use rigid body transform optimization, to find the matrix that registers both curves. // this can be done because we have ordering on all points. // http://nghiaho.com/?page_id=671 var H = np.dot(exp_normalized, act_normalized.T); // H is 2x2, if it is nxn then flip the order, and get R_act_to_exp var(U, S, V) = np.linalg.svd(H); if ((double)np.linalg.det(V) < 0) { V[":, 2"] = -V[":, 2"]; } var R_exp_to_act = np.dot(V, U.T); //apply rotation on exp and calculate mse var exp_rotated = np.dot(R_exp_to_act, exp_normalized); var error = np.linalg.norm(exp_rotated - act_normalized); return(error < threshold); }
public bool Validate(ITrail expected, ITrail actual, float threshold) { var expectedSamples = expected.GetSampledLocations(); var actualSamples = actual.GetSampledLocations(); int count = expectedSamples.Count(); for (int i = 0; i < count / 2; i++) { var diff = expectedSamples.Skip(i).Zip(actualSamples.Take(count - i), (e, a) => e - a); var normDiff = diff.Select(d => d.magnitude); var sumNormDiff = normDiff.Aggregate(0.0, (x, y) => x + y); var mse = sumNormDiff / diff.Count(); bool isValid = mse <= threshold; Debug.Log($"mse: {mse}"); if (isValid) { return(true); } } return(false); }
/// <summary> /// Pull Database /// </summary> /// <param name="trail">Trail Table</param> public TrailController(ITrail trail) { _trail = trail; }
public void AttemptPlayerSolution(ITrail playerTrail) { StartCoroutine(validator.Validate(solutionTrail, playerTrail, validatorThreshold) ? LoadNextLevelDelay() : TryAgainDelay()); }
/// <summary> /// Unifies a value on the heap /// </summary> public static bool Unify(this IReferenceLiteral address1, IReferenceLiteral address2, ITrail trail) { var unifyStack = new Stack <IReferenceLiteral>(); // Push the addresses that we're going to unify unifyStack.Push(address1); unifyStack.Push(address2); // Iterate until the stack is empty while (unifyStack.Count > 0) { // Deref the values on the stack var value1 = unifyStack.Pop().Dereference(); var value2 = unifyStack.Pop().Dereference(); if (!ReferenceEquals(value1, value2)) { if (value1.IsReference()) { // Bind references trail.Record(value1); value1.SetTo(value2); } else if (value2.IsReference()) { trail.Record(value2); value2.SetTo(value1); } else { if (Equals(value1.Term, value2.Term)) { // Process the rest of the structure var structurePos1 = value1.Reference; var structurePos2 = value2.Reference; while (structurePos1 != null) { unifyStack.Push(structurePos1); unifyStack.Push(structurePos2); structurePos1 = structurePos1.NextArgument; structurePos2 = structurePos2.NextArgument; } } else { // Structures do not match: fail return(false); } } } } return(true); }
public bool Validate(ITrail expected, ITrail actual, float threshold) { return(true); }