Moved NaN check to the optimizer script.

This commit is contained in:
Krafpy
2021-12-24 17:35:12 +01:00
parent c7cc922ba6
commit 01a733fbd1
4 changed files with 52 additions and 61 deletions
+1 -25
View File
@@ -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;
+22 -1
View File
@@ -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);
@@ -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
+28 -1
View File
@@ -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);