File: EstimationParameters.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (250 lines) | stat: -rw-r--r-- 9,525 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package tim.prune.function.estimate;

import java.text.NumberFormat;
import java.util.Locale;

import tim.prune.I18nManager;
import tim.prune.data.RangeStatsWithGradients;
import tim.prune.data.Unit;
import tim.prune.data.UnitSet;
import tim.prune.data.UnitSetLibrary;

/**
 * Class to hold, parse and convert the parameters for time estimation.
 * These are five (metric) values which can be loaded and saved from config,
 * and are used by the EstimateTime function
 */
public class EstimationParameters
{
	/** Minutes required for flat travel, fixed metric distance */
	private final double _flatMins;
	/** Minutes required for climbing, fixed metric distance */
	private final double _gentleClimbMins;
	private final double _steepClimbMins;
	/** Minutes required for descending, fixed metric distance */
	private final double _gentleDescentMins;
	private final double _steepDescentMins;

	/** Kilometres unit for comparison */
	private static final Unit KILOMETRES = UnitSetLibrary.UNITS_KILOMETRES;

	/** Default parameters */
	public static final EstimationParameters DEFAULT_PARAMS = new EstimationParameters(60.0, 12.0, 18.0, 0.0, 12.0);


	/**
	 * Populate with double metric values, for example the results of a Learning process
	 * @param inFlat time for 5km on the flat
	 * @param inGClimb time for 100m gentle climb
	 * @param inSClimb time for 100m steep climb
	 * @param inGDescent time for 100m gentle descent
	 * @param inSDescent time for 100m steep descent
	 */
	private EstimationParameters(double inFlat,
		double inGClimb, double inSClimb, double inGDescent, double inSDescent)
	{
		_flatMins = inFlat;
		_gentleClimbMins = inGClimb;
		_steepClimbMins  = inSClimb;
		_gentleDescentMins = inGDescent;
		_steepDescentMins  = inSDescent;
	}

	/**
	 * Constructor from config string
	 * @param inString single, semicolon-separated string from config with five params
	 * @return parameters object, or null if parsing failed
	 */
	public static EstimationParameters fromConfigString(String inString)
	{
		if (inString == null || inString.isBlank()) {
			return null;
		}
		String[] params = inString.trim().split(";");
		if (params == null || params.length != 5) {
			return null;
		}
		try
		{
			// Use fixed UK locale to parse these, because of fixed "." formatting
			NumberFormat twoDpFormatter = NumberFormat.getNumberInstance(Locale.UK);
			final double flatMins          = twoDpFormatter.parse(params[0]).doubleValue();
			final double gentleClimbMins   = twoDpFormatter.parse(params[1]).doubleValue();
			final double steepClimbMins    = twoDpFormatter.parse(params[2]).doubleValue();
			final double gentleDescentMins = twoDpFormatter.parse(params[3]).doubleValue();
			final double steepDescentMins  = twoDpFormatter.parse(params[4]).doubleValue();
			return new EstimationParameters(flatMins, gentleClimbMins, steepClimbMins, gentleDescentMins, steepDescentMins);
		}
		catch (Exception e) {
			return null;
		}
	}

	/**
	 * Populate the values using five user-entered values (now Units-specific!)
	 * @param inFlat minutes for flat
	 * @param inGClimb minutes for gentle climb
	 * @param inSClimb minutes for steep climb
	 * @param inGDescent minutes for gentle descent
	 * @param inSDescent minutes for steep descent
	 */
	public static EstimationParameters fromLocalUnits(UnitSet inUnitSet, double inFlat,
		double inGClimb, double inSClimb, double inGDescent, double inSDescent)
	{
		Unit distUnit = inUnitSet.getDistanceUnit();
		Unit altUnit  = inUnitSet.getAltitudeUnit();
		final double distFactor = (distUnit == KILOMETRES ? 1.0 : (5000/3.0 * distUnit.getMultFactorFromStd()));
		final double altFactor  = (altUnit.isStandard()  ? 1.0 : (1.0/3.0 * altUnit.getMultFactorFromStd()));

		double flatMins = inFlat * distFactor;
		double gentleClimbMins = inGClimb * altFactor;
		double steepClimbMins  = inSClimb * altFactor;
		double gentleDescentMins = inGDescent * altFactor;
		double steepDescentMins  = inSDescent * altFactor;
		return new EstimationParameters(flatMins, gentleClimbMins, steepClimbMins,
				gentleDescentMins, steepDescentMins);
	}

	/**
	 * Populate the values using five calculated values (metric)
	 * @param inFlat minutes for flat
	 * @param inGClimb minutes for gentle climb
	 * @param inSClimb minutes for steep climb
	 * @param inGDescent minutes for gentle descent
	 * @param inSDescent minutes for steep descent
	 */
	public static EstimationParameters fromMetricUnits(double inFlat,
		double inGClimb, double inSClimb, double inGDescent, double inSDescent)
	{
		return new EstimationParameters(inFlat, inGClimb, inSClimb, inGDescent, inSDescent);
	}

	/**
	 * @return true if this set of parameters is the same as the default set
	 */
	public boolean sameAsDefaults() {
		return toConfigString().equals(DEFAULT_PARAMS.toConfigString());
	}


	/** @return number of minutes for flat travel, local distance units */
	public double getFlatMinutesLocal(Unit inDistUnit) {
		return convertDistanceToLocal(_flatMins, inDistUnit);
	}

	/** @return the given number of minutes converted to the given distance units */
	private static double convertDistanceToLocal(double minutes, Unit inDistUnit)
	{
		double distFactor = (inDistUnit == KILOMETRES ? 1.0 : (5000/3.0 * inDistUnit.getMultFactorFromStd()));
		return minutes / distFactor;
	}

	/** @return number of minutes for gentle climb, local distance units */
	public double getGentleClimbMinutesLocal(Unit inAltUnit) {
		return convertAltitudeToLocal(_gentleClimbMins, inAltUnit);
	}

	/** @return number of minutes for steep climb, local distance units */
	public double getSteepClimbMinutesLocal(Unit inAltUnit) {
		return convertAltitudeToLocal(_steepClimbMins, inAltUnit);
	}

	/** @return number of minutes for gentle descent, local distance units */
	public double getGentleDescentMinutesLocal(Unit inAltUnit) {
		return convertAltitudeToLocal(_gentleDescentMins, inAltUnit);
	}

	/** @return number of minutes for steep descent, local distance units */
	public double getSteepDescentMinutesLocal(Unit inAltUnit) {
		return convertAltitudeToLocal(_steepDescentMins, inAltUnit);
	}

	/** @return the given number of minutes converted to local altitude units */
	private static double convertAltitudeToLocal(double minutes, Unit inAltUnit)
	{
		double altFactor  = (inAltUnit.isStandard() ? 1.0 : (1.0/3.0 * inAltUnit.getMultFactorFromStd()));
		return minutes / altFactor;
	}

	/**
	 * @return unit-specific string describing the distance for the flat time (5km/3mi/3NM)
	 */
	public static String getStandardDistance(Unit inDistUnit)
	{
		return (inDistUnit == KILOMETRES ? "5 " : "3 ") + I18nManager.getText(inDistUnit.getShortnameKey());
	}

	/**
	 * @return unit-specific string describing the height difference for the climbs/descents (100m/300ft)
	 */
	public static String getStandardClimb(Unit inAltUnit)
	{
		return (inAltUnit.isStandard() ? "100 " : "300 ") + I18nManager.getText(inAltUnit.getShortnameKey());
	}

	/**
	 * @return contents of parameters as a semicolon-separated (metric) string for the config
	 */
	public String toConfigString()
	{
		return "" + twoDp(_flatMins) + ";" + twoDp(_gentleClimbMins) + ";" + twoDp(_steepClimbMins) + ";"
			+ twoDp(_gentleDescentMins) + ";" + twoDp(_steepDescentMins);
	}

	/**
	 * @param inNum number to output
	 * @return formatted string to two decimal places, with decimal point
	 */
	public static String twoDp(double inNum)
	{
		if (inNum < 0.0) {
			return "-" + twoDp(-inNum);
		}
		int hundreds = (int) (inNum * 100 + 0.5);
		return "" + (hundreds / 100) + "." + ((hundreds/10) % 10) + (hundreds % 10);
	}

	/**
	 * Apply the parameters to the given range stats
	 * @param inStats stats of current range
	 * @return estimated number of minutes required
	 */
	public double applyToStats(RangeStatsWithGradients inStats)
	{
		if (inStats == null) {
			return 0.0;
		}
		final Unit METRES = UnitSetLibrary.UNITS_METRES;
		final double STANDARD_CLIMB = 100.0; // metres
		final double STANDARD_DISTANCE = 5.0; // kilometres
		return _flatMins * inStats.getMovingDistanceKilometres() / STANDARD_DISTANCE
			+ _gentleClimbMins * inStats.getGentleAltitudeRange().getClimb(METRES) / STANDARD_CLIMB
			+ _steepClimbMins  * inStats.getSteepAltitudeRange().getClimb(METRES) / STANDARD_CLIMB
			+ _gentleDescentMins * inStats.getGentleAltitudeRange().getDescent(METRES) / STANDARD_CLIMB
			+ _steepDescentMins  * inStats.getSteepAltitudeRange().getDescent(METRES) / STANDARD_CLIMB;
	}

	/**
	 * Combine two sets of parameters together
	 * @param inOther other set
	 * @param inFraction fractional weight
	 * @return combined set
	 */
	public EstimationParameters combine(EstimationParameters inOther, double inFraction)
	{
		if (inFraction >= 1.0 || inOther == null) {
			return this;
		}
		if (inFraction <= 0.0) {
			return inOther;
		}
		// inFraction is the weight of this one, weight of the other one is 1-inFraction
		final double fraction2 = 1 - inFraction;
		double flatMins = inFraction * _flatMins + fraction2 * inOther._flatMins;
		double gentleClimbMins = inFraction * _gentleClimbMins + fraction2 * inOther._gentleClimbMins;
		double gentleDescentMins = inFraction * _gentleDescentMins + fraction2 * inOther._gentleDescentMins;
		double steepClimbMins = inFraction * _steepClimbMins + fraction2 * inOther._steepClimbMins;
		double steepDescentMins = inFraction * _steepDescentMins + fraction2 * inOther._steepDescentMins;
		return new EstimationParameters(flatMins, gentleClimbMins, steepClimbMins, gentleDescentMins, steepDescentMins);
	}
}