From 01a733fbd1663e80f0f79b1a2529f94dedb92ff4 Mon Sep 17 00:00:00 2001 From: Krafpy Date: Fri, 24 Dec 2021 17:35:12 +0100 Subject: [PATCH] Moved NaN check to the optimizer script. --- .../libs/trajectory-calculator.js | 26 +------------- .../dedicated-workers/trajectory-optimizer.js | 23 +++++++++++- .../libs/trajectory-calculator.ts | 35 +------------------ src/dedicated-workers/trajectory-optimizer.ts | 29 ++++++++++++++- 4 files changed, 52 insertions(+), 61 deletions(-) diff --git a/dist/dedicated-workers/libs/trajectory-calculator.js b/dist/dedicated-workers/libs/trajectory-calculator.js index 17e4ca3..40fdce8 100644 --- a/dist/dedicated-workers/libs/trajectory-calculator.js +++ b/dist/dedicated-workers/libs/trajectory-calculator.js @@ -8,7 +8,6 @@ class TrajectoryCalculator { this._legs = []; this._flybys = []; this._secondArcsData = []; - this.mathError = false; const attractorId = this._departureBody.orbiting; this._mainAttractor = this.system[attractorId]; } @@ -40,11 +39,10 @@ class TrajectoryCalculator { this._getFlybySettings(); } reset() { + this._secondArcsData = []; this._legs = []; this._flybys = []; this.steps = []; - this._secondArcsData = []; - this.mathError = false; } _getDepartureSettings() { this._departureInfos = { @@ -93,7 +91,6 @@ class TrajectoryCalculator { const last = this._legs[numOfLegs - 1]; this._computeFirstLegArc(last); this._computeLegSecondArcSimple(last); - this.mathError = this._hasNaNValues(); } get totalDeltaV() { let total = 0; @@ -107,27 +104,6 @@ class TrajectoryCalculator { } return total; } - _hasNaNValues() { - const hasNaN = obj => { - for (const value of Object.values(obj)) { - if (typeof value == "object") { - if (hasNaN(value)) - return true; - } - else if (typeof value == "number") { - if (isNaN(value)) - return true; - } - } - return false; - }; - for (let i = this.steps.length - 1; i >= 0; i--) { - if (hasNaN(this.steps[i])) { - return true; - } - } - return false; - } _computeLegDuration(infos) { const exitedBody = this.system[infos.exitedBodyId]; const { durationParam } = infos; diff --git a/dist/dedicated-workers/trajectory-optimizer.js b/dist/dedicated-workers/trajectory-optimizer.js index 4d61e08..fc07353 100644 --- a/dist/dedicated-workers/trajectory-optimizer.js +++ b/dist/dedicated-workers/trajectory-optimizer.js @@ -68,7 +68,7 @@ class TrajectoryOptimizer extends WorkerEnvironment { catch { failed = true; } - if (failed || trajectory.mathError) { + if (failed || this._hasNaNValuesInSteps(trajectory)) { this._evolver.randomizeAgent(agent); trajectory.reset(); } @@ -79,5 +79,26 @@ class TrajectoryOptimizer extends WorkerEnvironment { } throw new Error("Impossible to compute the trajectory."); } + _hasNaNValuesInSteps(trajectory) { + const hasNaN = obj => { + for (const value of Object.values(obj)) { + if (typeof value == "object") { + if (hasNaN(value)) + return true; + } + else if (typeof value == "number") { + if (isNaN(value)) + return true; + } + } + return false; + }; + const { steps } = trajectory; + for (let i = steps.length - 1; i >= 0; i--) { + if (hasNaN(steps[i])) + return true; + } + return false; + } } WorkerEnvironment.init(TrajectoryOptimizer); diff --git a/src/dedicated-workers/libs/trajectory-calculator.ts b/src/dedicated-workers/libs/trajectory-calculator.ts index 7114cfe..27c78a0 100644 --- a/src/dedicated-workers/libs/trajectory-calculator.ts +++ b/src/dedicated-workers/libs/trajectory-calculator.ts @@ -20,8 +20,6 @@ class TrajectoryCalculator { private _secondArcsData: SecondArcData[] = []; - public mathError: boolean = false; - constructor(public readonly system: IOrbitingBody[], public readonly config: TrajectorySearchSettings, public readonly sequence: number[]){ const attractorId = this._departureBody.orbiting; this._mainAttractor = this.system[attractorId]; @@ -62,11 +60,10 @@ class TrajectoryCalculator { } public reset(){ + this._secondArcsData = []; this._legs = []; this._flybys = []; this.steps = []; - this._secondArcsData = []; - this.mathError = false; } /** @@ -137,10 +134,6 @@ class TrajectoryCalculator { const last = this._legs[numOfLegs-1]; this._computeFirstLegArc(last); this._computeLegSecondArcSimple(last); - - // Check for math error that may have occured during the - // calculation - this.mathError = this._hasNaNValues(); } public get totalDeltaV(){ @@ -156,32 +149,6 @@ class TrajectoryCalculator { return total; } - /** - * Checks if there is a NaN value in the computed steps (caused by a math error) - * @returns true if there is a NaN value in the computed steps, false otherwise. - */ - private _hasNaNValues(){ - const hasNaN: (obj: Object) => boolean = obj => { - for(const value of Object.values(obj)){ - if(typeof value == "object"){ - if(hasNaN(value)) - return true; - } else if(typeof value == "number") { - if(isNaN(value)) - return true; - } - } - return false; - }; - - for(let i = this.steps.length - 1; i >= 0; i--){ - if(hasNaN(this.steps[i])){ - return true; - } - } - return false; - } - /** * Completes the provided leg infos by calculating the leg duration from the already given * parameters diff --git a/src/dedicated-workers/trajectory-optimizer.ts b/src/dedicated-workers/trajectory-optimizer.ts index ac7a714..ee15ec0 100644 --- a/src/dedicated-workers/trajectory-optimizer.ts +++ b/src/dedicated-workers/trajectory-optimizer.ts @@ -111,7 +111,7 @@ class TrajectoryOptimizer extends WorkerEnvironment { failed = true; } - if(failed || trajectory.mathError) { + if(failed || this._hasNaNValuesInSteps(trajectory)) { this._evolver.randomizeAgent(agent); trajectory.reset(); } else { @@ -123,6 +123,33 @@ class TrajectoryOptimizer extends WorkerEnvironment { throw new Error("Impossible to compute the trajectory."); } + + /** + * Checks if there is a NaN value in the computed steps (caused by a math error) + * @param trajectory The trajectory we want to check its steps for NaN values. + * @returns true if there is a NaN value in the computed steps, false otherwise. + */ + private _hasNaNValuesInSteps(trajectory: TrajectoryCalculator){ + const hasNaN: (obj: Object) => boolean = obj => { + for(const value of Object.values(obj)){ + if(typeof value == "object"){ + if(hasNaN(value)) + return true; + } else if(typeof value == "number") { + if(isNaN(value)) + return true; + } + } + return false; + }; + + const {steps} = trajectory; + for(let i = steps.length - 1; i >= 0; i--){ + if(hasNaN(steps[i])) + return true; + } + return false; + } } WorkerEnvironment.init(TrajectoryOptimizer); \ No newline at end of file