Added trajectory duration limit (stock only)

A new trajectory setting has been added to the editor to enter the
trajectory duration limit (in number of days). It adds a big
cost to the DE algorithm if the duration of a trajectory is longer
than the specified duration limit.
This commit is contained in:
Krafpy
2023-05-25 19:33:34 +02:00
parent f3fe516176
commit 7aee9ea9c5
9 changed files with 72 additions and 5 deletions

View File

@@ -62,6 +62,7 @@ editor:
defaultOrigin: 3 # default origin body on start (index of Kerbin in the selector)
defaultDest: 0 # default destination body on start (index of Moho in the selector)
defaultAltitude: 100 # default altitude from the default body (in km above surface)
defaultMaxDuration: 500 # default duration limit for a trajectory (in number of days)
workers:
progressStep: 250 # number of inputs processed per chunk before progress callback

View File

@@ -110,6 +110,11 @@ class TrajectoryCalculator {
}
return total;
}
get totalDuration() {
const start = this.steps[0].dateOfStart;
const end = this._lastStep.dateOfStart;
return end - start;
}
_computeLegDuration(infos) {
const exitedBody = this.system[infos.exitedBodyId];
const { durationParam } = infos;

View File

@@ -45,7 +45,10 @@ class TrajectoryOptimizer extends WorkerEnvironment {
const circDV = periVel - Physics3D.circularVelocity(finalBody, periapsis);
periVelCost = circDV;
}
return totDV + totDV * lastInc * 0.1 + periVelCost;
const duration = trajectory.totalDuration;
const durationOverflow = Math.max(0, duration - this._settings.maxDuration);
const durationCost = durationOverflow * totDV;
return totDV + totDV * lastInc * 0.1 + periVelCost + durationCost;
};
const trajConfig = this._config.trajectorySearch;
const { diffWeight } = trajConfig;

View File

@@ -136,6 +136,9 @@ export async function initEditorWithSystem(systems, systemIndex) {
const timeRangeEnd = new TimeSelector("end", config);
timeRangeStart.setToDefault();
timeRangeEnd.setToDefault();
const maxDuration = new IntegerInput("max-duration");
maxDuration.setMinMax(1, Infinity);
maxDuration.value = config.editor.defaultMaxDuration;
const depAltitude = new IntegerInput("start-altitude");
const destAltitude = new IntegerInput("end-altitude");
const updateAltitudeRange = (input, body) => {
@@ -247,13 +250,26 @@ export async function initEditorWithSystem(systems, systemIndex) {
throw new Error("Departure date range end must be greater than the start date.");
const depAltitudeVal = depAltitude.value * 1000;
const destAltitudeVal = destAltitude.value * 1000;
if (!maxDuration.validate()) {
throw new Error("Invalid duration limit.");
}
let maxDurationSeconds;
if (config.time.type == "base") {
const { hoursPerDay } = config.time;
const secondsPerDay = hoursPerDay * 3600;
maxDurationSeconds = maxDuration.value * secondsPerDay;
}
else {
maxDurationSeconds = maxDuration.value * 24 * 3600;
}
resetFoundTrajectory();
const userSettings = {
startDate: startDate,
endDate: endDate,
depAltitude: depAltitudeVal,
destAltitude: destAltitudeVal,
noInsertion: noInsertionBox.checked
noInsertion: noInsertionBox.checked,
maxDuration: maxDurationSeconds
};
const perfStart = performance.now();
await solver.searchOptimalTrajectory(sequence, userSettings);

View File

@@ -176,6 +176,13 @@
km (above surface level)
</div>
</div>
<div class="control-group">
<label class="control-label" for="max-duration">Max duration:</label>
<div class="controls">
<input class="number-input" name="max-duration" id="max-duration" type="number" value="0" min="1">
days
</div>
</div>
<div id="insertion-checkbox-container">
<div class="controls">
<input name="insertion-checkbox" id="insertion-checkbox" type="checkbox">

View File

@@ -160,6 +160,15 @@ class TrajectoryCalculator {
return total;
}
/**
* Returns the duration of the whole trajectory, in seconds.
*/
public get totalDuration(){
const start = this.steps[0].dateOfStart;
const end = this._lastStep.dateOfStart;
return end - start;
}
/**
* Completes the provided leg infos by calculating the leg duration from the already given
* parameters

View File

@@ -80,10 +80,15 @@ class TrajectoryOptimizer extends WorkerEnvironment {
periVelCost = circDV;
}
// Add a big cost value if the duration exceeds the duration limit.
const duration = trajectory.totalDuration;
const durationOverflow = Math.max(0, duration - this._settings.maxDuration);
const durationCost = durationOverflow*totDV;
// Attempt to force a minimal inclination of the
// circular orbit around the destination body
// FIX : doesn't work so well...
return totDV + totDV*lastInc*0.1 + periVelCost;
return totDV + totDV*lastInc*0.1 + periVelCost + durationCost;
};
const trajConfig = this._config.trajectorySearch;

View File

@@ -180,6 +180,11 @@ export async function initEditorWithSystem(systems: SolarSystemData[], systemInd
timeRangeStart.setToDefault();
timeRangeEnd.setToDefault();
// Max duration input
const maxDuration = new IntegerInput("max-duration");
maxDuration.setMinMax(1, Infinity);
maxDuration.value = config.editor.defaultMaxDuration;
// Numerical inputs
const depAltitude = new IntegerInput("start-altitude");
const destAltitude = new IntegerInput("end-altitude");
@@ -320,6 +325,19 @@ export async function initEditorWithSystem(systems: SolarSystemData[], systemInd
const depAltitudeVal = depAltitude.value * 1000;
const destAltitudeVal = destAltitude.value * 1000;
if(!maxDuration.validate()) {
throw new Error("Invalid duration limit.");
}
let maxDurationSeconds: number;
if(config.time.type == "base") {
const {hoursPerDay} = config.time;
const secondsPerDay = hoursPerDay * 3600;
maxDurationSeconds = maxDuration.value * secondsPerDay;
} else {
maxDurationSeconds = maxDuration.value * 24*3600;
}
resetFoundTrajectory();
@@ -328,7 +346,8 @@ export async function initEditorWithSystem(systems: SolarSystemData[], systemInd
endDate: endDate,
depAltitude: depAltitudeVal,
destAltitude: destAltitudeVal,
noInsertion: noInsertionBox.checked
noInsertion: noInsertionBox.checked,
maxDuration: maxDurationSeconds
};
const perfStart = performance.now();

4
src/types.d.ts vendored
View File

@@ -92,6 +92,7 @@ interface EditorSettings {
readonly defaultOrigin: number;
readonly defaultDest: number;
readonly defaultAltitude: number;
readonly defaultMaxDuration: number;
}
interface WorkersSettings {
@@ -317,7 +318,8 @@ type TrajectoryUserSettings = {
endDate: number,
depAltitude: number,
destAltitude: number,
noInsertion: boolean
noInsertion: boolean,
maxDuration: number
};
type ResultPannelItems = {