View | Details | Raw Unified | Return to bug 346732 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/stem/core/graph/IntegrationLabelValue.java (-1 / +2 lines)
Lines 76-83 Link Here
76
	 * @param value reference value
76
	 * @param value reference value
77
	 * @return boolean True if changed
77
	 * @return boolean True if changed
78
	*/
78
	*/
79
	public boolean adjustDelta(IntegrationLabelValue value);
79
	public boolean avoidNegative(IntegrationLabelValue value);
80
	
80
	
81
	public double computeDeltaAdjustment(IntegrationLabelValue value);
81
	
82
	
82
	/**
83
	/**
83
	 * divide. Divide the input with this value
84
	 * divide. Divide the input with this value
(-)src/org/eclipse/stem/populationmodels/standard/provider/PopulationInitializerItemProvider.java (+1 lines)
Lines 18-23 Link Here
18
import org.eclipse.emf.common.notify.AdapterFactory;
18
import org.eclipse.emf.common.notify.AdapterFactory;
19
import org.eclipse.emf.common.notify.Notification;
19
import org.eclipse.emf.common.notify.Notification;
20
import org.eclipse.emf.common.util.ResourceLocator;
20
import org.eclipse.emf.common.util.ResourceLocator;
21
import org.eclipse.emf.common.util.URI;
21
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
22
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
22
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
23
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
23
import org.eclipse.emf.edit.provider.IItemLabelProvider;
24
import org.eclipse.emf.edit.provider.IItemLabelProvider;
(-)src/org/eclipse/stem/populationmodels/standard/impl/StandardPopulationModelLabelValueImpl.java (-22 / +32 lines)
Lines 249-291 Link Here
249
	 * <!-- end-user-doc -->
249
	 * <!-- end-user-doc -->
250
	 * @generated NOT
250
	 * @generated NOT
251
	 */
251
	 */
252
	public boolean adjustDelta(IntegrationLabelValue value) {
252
	public boolean avoidNegative(IntegrationLabelValue value) {
253
		StandardPopulationModelLabelValue sval = (StandardPopulationModelLabelValue)value;
253
		StandardPopulationModelLabelValue sval = (StandardPopulationModelLabelValue)value;
254
		boolean adjusted = false;
254
		boolean adjusted = false;
255
		double newCount = this.getCount() + sval.getCount();
255
		double newCount = this.getCount() + sval.getCount();
256
		double newBirths = this.getBirths() + sval.getBirths();
256
		double newBirths = this.getBirths() + sval.getBirths();
257
		double newDeaths = this.getDeaths() + sval.getDeaths();
257
		double newDeaths = this.getDeaths() + sval.getDeaths();
258
		
258
		
259
		double factor = 1.0;
259
		if(newCount<0.0) {
260
		if(newCount < newBirths && newCount < newDeaths && newCount < 0.0) {
261
			// Scale using S
262
			adjusted = true;
263
			factor = -sval.getCount()/this.getCount();
264
		} else if(newBirths < newDeaths && newBirths < 0.0) {
265
			// Scale using R
266
			adjusted = true;
267
			factor = -sval.getBirths()/this.getBirths();
268
		} else if (newDeaths < 0) {
269
			// Scale using R
270
			adjusted = true;
260
			adjusted = true;
271
			factor = -sval.getDeaths()/this.getDeaths();
261
			this.setCount(-sval.getCount());
272
		}
262
		}
273
		if(adjusted) this.scale(factor);
274
		// Due to precision limitations it is still possible the number if tiny negative. Adjust if necessary
275
		newCount = this.getCount() + sval.getCount();
276
		newBirths = this.getBirths() + sval.getBirths();
277
		newDeaths = this.getDeaths() + sval.getDeaths();
278
		
263
		
279
		if(newCount<0)
264
		if(newBirths<0.0) {
280
			this.setCount(-sval.getCount());
265
			adjusted = true;
281
		if(newBirths<0)
282
			this.setBirths(-sval.getBirths());
266
			this.setBirths(-sval.getBirths());
283
		if(newDeaths<0)
267
		}
268
		
269
		if(newDeaths<0.0) {
270
			adjusted = true;
284
			this.setDeaths(-sval.getDeaths());
271
			this.setDeaths(-sval.getDeaths());
272
		}
285
		
273
		
286
		return adjusted;
274
		return adjusted;
287
275
288
	}
276
	}
277
	
278
	public double computeDeltaAdjustment(IntegrationLabelValue value) {
279
		StandardPopulationModelLabelValue sval = (StandardPopulationModelLabelValue)value;
280
		double newCount = this.getCount() + sval.getCount();
281
		double newBirths = this.getBirths() + sval.getBirths();
282
		double newDeaths = this.getDeaths() + sval.getDeaths();
283
		double factor = 1.0;
284
		
285
		if(newCount < 0.0) {
286
			factor = Math.min(factor, -sval.getCount()/this.getCount());
287
		}
288
		
289
		if(newBirths < 0.0) {
290
			factor = Math.min(factor, -sval.getBirths()/this.getBirths());
291
		}
292
		
293
		if (newDeaths < 0.0) {
294
			factor = Math.min(factor, -sval.getDeaths()/this.getDeaths());
295
		}
296
		
297
		return factor;
298
	}
289
299
290
	private EList<Exchange>arrivals;
300
	private EList<Exchange>arrivals;
291
	private EList<Exchange>departures;
301
	private EList<Exchange>departures;
(-)src/org/eclipse/stem/populationmodels/standard/impl/AgingPopulationModelImpl.java (-2 / +1 lines)
Lines 28-34 Link Here
28
 * <em><b>Aging Population Model</b></em>'. <!-- end-user-doc -->
28
 * <em><b>Aging Population Model</b></em>'. <!-- end-user-doc -->
29
 * <p>
29
 * <p>
30
 * </p>
30
 * </p>
31
 * 
31
 *
32
 * @generated
32
 * @generated
33
 */
33
 */
34
public class AgingPopulationModelImpl extends DemographicPopulationModelImpl
34
public class AgingPopulationModelImpl extends DemographicPopulationModelImpl
Lines 44-50 Link Here
44
44
45
	/**
45
	/**
46
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
46
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
47
	 * 
48
	 * @generated
47
	 * @generated
49
	 */
48
	 */
50
	@Override
49
	@Override
(-)src/org/eclipse/stem/populationmodels/standard/StandardPopulationModelLabelValue.java (-8 lines)
Lines 139-150 Link Here
139
	 * @generated
139
	 * @generated
140
	 */
140
	 */
141
	void setDensity(double value);
141
	void setDensity(double value);
142
143
	/**
144
	 * <!-- begin-user-doc -->
145
	 * <!-- end-user-doc -->
146
	 * @model valueType="org.eclipse.stem.populationmodels.standard.IntegrationLabelValue"
147
	 * @generated
148
	 */
149
	boolean adjustDelta(IntegrationLabelValue value);
150
} // StandardPopulationModelLabelValue
142
} // StandardPopulationModelLabelValue
(-)model/standard.ecore (-3 lines)
Lines 37-45 Link Here
37
      abstract="true" interface="true"/>
37
      abstract="true" interface="true"/>
38
  <eClassifiers xsi:type="ecore:EClass" name="PopulationModelLabelValue" eSuperTypes="../../org.eclipse.stem.core/model/graph.ecore#//LabelValue"/>
38
  <eClassifiers xsi:type="ecore:EClass" name="PopulationModelLabelValue" eSuperTypes="../../org.eclipse.stem.core/model/graph.ecore#//LabelValue"/>
39
  <eClassifiers xsi:type="ecore:EClass" name="StandardPopulationModelLabelValue" eSuperTypes="#//PopulationModelLabelValue #//IntegrationLabelValue">
39
  <eClassifiers xsi:type="ecore:EClass" name="StandardPopulationModelLabelValue" eSuperTypes="#//PopulationModelLabelValue #//IntegrationLabelValue">
40
    <eOperations name="adjustDelta" eType="ecore:EDataType platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//EBoolean">
41
      <eParameters name="value" eType="#//IntegrationLabelValue"/>
42
    </eOperations>
43
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="count" eType="ecore:EDataType platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//EDouble"/>
40
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="count" eType="ecore:EDataType platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//EDouble"/>
44
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="births" eType="ecore:EDataType platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//EDouble"/>
41
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="births" eType="ecore:EDataType platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//EDouble"/>
45
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="deaths" eType="ecore:EDataType platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//EDouble"/>
42
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="deaths" eType="ecore:EDataType platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore#//EDouble"/>
(-)src/org/eclipse/stem/populationmodels/standard/impl/StandardPackageImpl.java (-3 lines)
Lines 949-957 Link Here
949
		initEAttribute(getStandardPopulationModelLabelValue_Deaths(), theEcorePackage.getEDouble(), "deaths", null, 0, 1, StandardPopulationModelLabelValue.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
949
		initEAttribute(getStandardPopulationModelLabelValue_Deaths(), theEcorePackage.getEDouble(), "deaths", null, 0, 1, StandardPopulationModelLabelValue.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
950
		initEAttribute(getStandardPopulationModelLabelValue_Density(), theEcorePackage.getEDouble(), "density", null, 0, 1, StandardPopulationModelLabelValue.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
950
		initEAttribute(getStandardPopulationModelLabelValue_Density(), theEcorePackage.getEDouble(), "density", null, 0, 1, StandardPopulationModelLabelValue.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
951
951
952
		EOperation op = addEOperation(standardPopulationModelLabelValueEClass, theEcorePackage.getEBoolean(), "adjustDelta", 0, 1, IS_UNIQUE, IS_ORDERED);
953
		addEParameter(op, this.getIntegrationLabelValue(), "value", 0, 1, IS_UNIQUE, IS_ORDERED);
954
955
		initEClass(stochasticStandardPopulationModelEClass, StochasticStandardPopulationModel.class, "StochasticStandardPopulationModel", !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS);
952
		initEClass(stochasticStandardPopulationModelEClass, StochasticStandardPopulationModel.class, "StochasticStandardPopulationModel", !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS);
956
		initEAttribute(getStochasticStandardPopulationModel_Gain(), theEcorePackage.getEDouble(), "gain", "0.01", 0, 1, StochasticStandardPopulationModel.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
953
		initEAttribute(getStochasticStandardPopulationModel_Gain(), theEcorePackage.getEDouble(), "gain", "0.01", 0, 1, StochasticStandardPopulationModel.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
957
954
(-)src/org/eclipse/stem/diseasemodels/experimental/impl/TBDiseaseModelLabelValueImpl.java (-1 / +5 lines)
Lines 312-320 Link Here
312
		return result.toString();
312
		return result.toString();
313
	}
313
	}
314
314
315
	public boolean adjustDelta(IntegrationLabelValue value) {
315
	public boolean avoidNegative(IntegrationLabelValue value) {
316
		// TODO Auto-generated method stub
316
		// TODO Auto-generated method stub
317
		return false;
317
		return false;
318
	}
318
	}
319
	
320
	public double computeDeltaAdjustment(IntegrationLabelValue value) {
321
		return 1.0;
322
	}
319
323
320
} //TBDiseaseModelLabelValueImpl
324
} //TBDiseaseModelLabelValueImpl
(-)src/org/eclipse/stem/diseasemodels/standard/impl/SIRLabelValueImpl.java (-29 / +42 lines)
Lines 279-285 Link Here
279
	 * @return boolean
279
	 * @return boolean
280
	 * @override
280
	 * @override
281
	 */
281
	 */
282
	public boolean adjustDelta(IntegrationLabelValue target) {
282
	public boolean avoidNegative(IntegrationLabelValue target) {
283
		SIRLabelValue sirValue = (SIRLabelValue)target;
283
		SIRLabelValue sirValue = (SIRLabelValue)target;
284
		boolean adjusted = false;
284
		boolean adjusted = false;
285
		double newS = this.getS() + sirValue.getS();
285
		double newS = this.getS() + sirValue.getS();
Lines 287-329 Link Here
287
		double newR = this.getR() + sirValue.getR();
287
		double newR = this.getR() + sirValue.getR();
288
		double newDD = this.getDiseaseDeaths() + sirValue.getDiseaseDeaths();
288
		double newDD = this.getDiseaseDeaths() + sirValue.getDiseaseDeaths();
289
		
289
		
290
		double factor = 1.0;
290
		if(newS<0.0) {
291
		if(newS < newI && newS < newR && newS < newDD && newS < 0.0) {
292
			// Scale using S
293
			adjusted = true;
291
			adjusted = true;
294
			factor = -sirValue.getS()/this.getS();
292
			this.setS(-sirValue.getS());
295
		} else if(newI < newR && newI < newDD && newI < 0.0) {
293
		}
296
			// Scale using I
294
		
295
		if(newI<0.0) {
297
			adjusted = true;
296
			adjusted = true;
298
			factor = -sirValue.getI()/this.getI();
297
			this.setI(-sirValue.getI());
299
		} else if(newR < newDD && newR < 0.0) {
298
		}
300
			// Scale using R
299
			
300
		if(newR<0.0) {
301
			adjusted = true;
301
			adjusted = true;
302
			factor = -sirValue.getR()/this.getR();
302
			this.setR(-sirValue.getR());
303
		} else if(newDD < 0.0) {
303
		}
304
			// Scale using DD
304
		
305
		if(newDD < 0.0) {
305
			adjusted = true;
306
			adjusted = true;
306
			factor = -sirValue.getDiseaseDeaths()/this.getDiseaseDeaths();
307
			this.setDiseaseDeaths(-sirValue.getDiseaseDeaths());
307
		}
308
		}
308
		if(adjusted) this.scale(factor);
309
		
309
		
310
		return adjusted;
311
	}
312
	
313
	public double computeDeltaAdjustment(IntegrationLabelValue value) {
314
		SIRLabelValue sirValue = (SIRLabelValue)value;
315
		double newS = this.getS() + sirValue.getS();
316
		double newI = this.getI() + sirValue.getI();
317
		double newR = this.getR() + sirValue.getR();
318
		double newDD = this.getDiseaseDeaths() + sirValue.getDiseaseDeaths();
319
		double factor = 1.0;
310
		
320
		
311
		// Due to precision limitations it is still possible the number if tiny negative. Adjust if necessary
321
		if(newS < 0.0) {
312
		newS = this.getS() + sirValue.getS();
322
			factor = Math.min(factor, -sirValue.getS()/this.getS());
313
		newI = this.getI() + sirValue.getI();
323
		}
314
		newR = this.getR() + sirValue.getR();
324
315
		newDD = this.getDiseaseDeaths() + sirValue.getDiseaseDeaths();
325
		if(newI < 0.0) {
326
			factor = Math.min(factor, -sirValue.getI()/this.getI());
327
		}
316
		
328
		
317
		if(newS<0)
329
		if(newR < 0.0) {
318
			this.setS(-sirValue.getS());
330
			factor = Math.min(factor, -sirValue.getR()/this.getR());
319
		if(newI<0)
331
		}
320
			this.setI(-sirValue.getI());
332
		
321
		if(newR<0)
333
		if(newDD < 0.0) {
322
			this.setR(-sirValue.getR());
334
			factor = Math.min(factor, -sirValue.getDiseaseDeaths()/this.getDiseaseDeaths());
323
		if(newDD < 0)
335
		}
324
			this.setDiseaseDeaths(-sirValue.getDiseaseDeaths());
336
		
325
		return adjusted;
337
		return factor;
326
	}
338
	}
339
	
327
	/**
340
	/**
328
	 * @see org.eclipse.stem.core.graph.impl.LabelValueImpl#reset()
341
	 * @see org.eclipse.stem.core.graph.impl.LabelValueImpl#reset()
329
	 */
342
	 */
(-)src/org/eclipse/stem/diseasemodels/standard/impl/SEIRLabelValueImpl.java (-36 / +51 lines)
Lines 266-274 Link Here
266
	 * @return boolean
266
	 * @return boolean
267
	 * @override
267
	 * @override
268
	 */
268
	 */
269
	public boolean adjustDelta(IntegrationLabelValue target) {
269
	public boolean avoidNegative(IntegrationLabelValue target) {
270
		SEIRLabelValue seirValue = (SEIRLabelValue)target;
270
		SEIRLabelValue seirValue = (SEIRLabelValue)target;	
271
		
272
		boolean adjusted = false;
271
		boolean adjusted = false;
273
		double newS = this.getS() + seirValue.getS();
272
		double newS = this.getS() + seirValue.getS();
274
		double newE = this.getE() + seirValue.getE();
273
		double newE = this.getE() + seirValue.getE();
Lines 276-324 Link Here
276
		double newR = this.getR() + seirValue.getR();
275
		double newR = this.getR() + seirValue.getR();
277
		double newDD = this.getDiseaseDeaths() +seirValue.getDiseaseDeaths();
276
		double newDD = this.getDiseaseDeaths() +seirValue.getDiseaseDeaths();
278
		
277
		
279
		double factor = 1.0;
278
		if(newS<0.0) {
280
		if(newS < newE && newS < newI && newS < newR && newS < newDD && newS < 0.0) {
281
			// Scale using S
282
			adjusted = true;
279
			adjusted = true;
283
			factor = -seirValue.getS()/this.getS();
280
			this.setS(-seirValue.getS());
284
		} else if(newE < newI && newE < newR && newE < newDD && newE < 0.0) {
285
			// Scale using E
286
			adjusted = true;
287
			factor = -seirValue.getE()/this.getE();
288
		} else if(newI < newR && newI < newDD && newI < 0.0) {
289
			// Scale using I
290
			adjusted = true;
291
			factor = -seirValue.getI()/this.getI();
292
		} else if(newR < newDD && newR < 0.0) {
293
			// Scale using R
294
			adjusted = true;
295
			factor = -seirValue.getR()/this.getR();
296
		} else if(newDD < 0) {
297
			// Scale using R
298
			adjusted = true;
299
			factor = -seirValue.getDiseaseDeaths()/this.getDiseaseDeaths();			
300
		}
281
		}
301
		if(adjusted) this.scale(factor);
302
		
282
		
303
		// Due to precision limitations it is still possible the number if tiny negative. Adjust if necessary
283
		if(newE<0.0) {
304
		newS = this.getS() + seirValue.getS();
284
			adjusted = true;
305
		newE = this.getE() + seirValue.getE();
306
		newI = this.getI() + seirValue.getI();
307
		newR = this.getR() + seirValue.getR();
308
		newDD = this.getDiseaseDeaths() + seirValue.getDiseaseDeaths();
309
		if(newS<0)
310
			this.setS(-seirValue.getS());
311
		if(newE<0)
312
			this.setE(-seirValue.getE());
285
			this.setE(-seirValue.getE());
313
		if(newI<0)
286
		}
287
		
288
		if(newI<0.0) {
289
			adjusted = true;
314
			this.setI(-seirValue.getI());
290
			this.setI(-seirValue.getI());
315
		if(newR<0)
291
		}
292
		
293
		if(newR<0.0) {
294
			adjusted = true;
316
			this.setR(-seirValue.getR());
295
			this.setR(-seirValue.getR());
317
		if(newDD < 0)
296
		}
297
		
298
		if(newDD < 0.0) {
299
			adjusted = true;
318
			this.setDiseaseDeaths(-seirValue.getDiseaseDeaths());
300
			this.setDiseaseDeaths(-seirValue.getDiseaseDeaths());
301
		}
302
		
319
		return adjusted;
303
		return adjusted;
320
	}
304
	}
321
305
306
	public double computeDeltaAdjustment(IntegrationLabelValue value) {
307
		SEIRLabelValue seirValue = (SEIRLabelValue)value;
308
		double newS = this.getS() + seirValue.getS();
309
		double newE = this.getE() + seirValue.getE();
310
		double newI = this.getI() + seirValue.getI();
311
		double newR = this.getR() + seirValue.getR();
312
		double newDD = this.getDiseaseDeaths() +seirValue.getDiseaseDeaths();
313
		double factor = 1.0;
314
		
315
		if(newS < 0.0) {
316
			factor = Math.min(factor, -seirValue.getS()/this.getS());
317
		} 
318
319
		if(newE < 0.0) {
320
			factor = Math.min(factor, -seirValue.getE()/this.getE());
321
		} 
322
323
		if(newI < 0.0) {
324
			factor = Math.min(factor, -seirValue.getI()/this.getI());
325
		} 
326
327
		if(newR < 0.0) {
328
			factor = Math.min(factor, -seirValue.getR()/this.getR());
329
		}
330
		
331
		if(newDD < 0.0) {
332
			factor = Math.min(factor, -seirValue.getDiseaseDeaths()/this.getDiseaseDeaths());			
333
		}
334
		
335
		return factor;
336
	}
322
	
337
	
323
	/**
338
	/**
324
	 * @see org.eclipse.stem.diseasemodels.standard.impl.SIRLabelValueImpl#reset()
339
	 * @see org.eclipse.stem.diseasemodels.standard.impl.SIRLabelValueImpl#reset()
(-)src/org/eclipse/stem/diseasemodels/standard/impl/SILabelValueImpl.java (-22 / +32 lines)
Lines 406-446 Link Here
406
	 * 
406
	 * 
407
	 * @return boolean 
407
	 * @return boolean 
408
	 */
408
	 */
409
	public boolean adjustDelta(IntegrationLabelValue target) {
409
	public boolean avoidNegative(IntegrationLabelValue target) {
410
		SILabelValue siValue = (SILabelValue)target;
410
		SILabelValue siValue = (SILabelValue)target;
411
		boolean adjusted = false;
411
		boolean adjusted = false;
412
		double newS = this.getS() + siValue.getS();
412
		double newS = this.getS() + siValue.getS();
413
		double newI = this.getI() + siValue.getI();
413
		double newI = this.getI() + siValue.getI();
414
		double newDD = this.getDiseaseDeaths() + siValue.getDiseaseDeaths();
414
		double newDD = this.getDiseaseDeaths() + siValue.getDiseaseDeaths();
415
		
415
		
416
		double factor = 1.0;
416
		if(newS<0.0) {
417
		if(newS < newI && newS < newDD && newS < 0.0) {
418
			// Scale using S
419
			adjusted = true;
420
			factor = -siValue.getS()/this.getS();
421
		} else if(newI < newDD && newI < 0.0) {
422
			// Scale using R
423
			adjusted = true;
424
			factor = -siValue.getI()/this.getI();
425
		} else if (newDD < 0) {
426
			// Scale using R
427
			adjusted = true;
417
			adjusted = true;
428
			factor = -siValue.getDiseaseDeaths()/this.getDiseaseDeaths();
418
			this.setS(-siValue.getS());
429
		}
419
		}
430
		if(adjusted) this.scale(factor);
431
		// Due to precision limitations it is still possible the number if tiny negative. Adjust if necessary
432
		newS = this.getS() + siValue.getS();
433
		newI = this.getI() + siValue.getI();
434
		newDD = this.getDiseaseDeaths() + siValue.getDiseaseDeaths();
435
		
420
		
436
		if(newS<0)
421
		if(newI<0.0) {
437
			this.setS(-siValue.getS());
422
			adjusted = true;
438
		if(newI<0)
439
			this.setI(-siValue.getI());
423
			this.setI(-siValue.getI());
440
		if(newDD<0)
424
		}
425
		
426
		if(newDD<0.0) {
427
			adjusted = true;
441
			this.setDiseaseDeaths(-siValue.getDiseaseDeaths());
428
			this.setDiseaseDeaths(-siValue.getDiseaseDeaths());
429
		}
442
		
430
		
443
		return adjusted;
431
		return adjusted;
444
	}
432
	}
445
	
433
	
434
	public double computeDeltaAdjustment(IntegrationLabelValue value) {
435
		SILabelValue siValue = (SILabelValue)value;
436
		double newS = this.getS() + siValue.getS();
437
		double newI = this.getI() + siValue.getI();
438
		double newDD = this.getDiseaseDeaths() + siValue.getDiseaseDeaths();
439
		double factor = 1.0;
440
		
441
		if(newS < 0.0) {
442
			factor = Math.min(factor, -siValue.getS()/this.getS());
443
		}
444
		
445
		if(newI < 0.0) {
446
			factor = Math.min(factor, -siValue.getI()/this.getI());
447
		}
448
		
449
		if (newDD < 0) {
450
			factor = Math.min(factor, -siValue.getDiseaseDeaths()/this.getDiseaseDeaths());
451
		}
452
		
453
		return factor;
454
	}
455
	
446
} // SILabelValueImpl
456
} // SILabelValueImpl
(-)src/org/eclipse/stem/solvers/fd/impl/FdJob.java (-2 / +16 lines)
Lines 13-18 Link Here
13
13
14
14
15
15
16
import org.eclipse.core.internal.runtime.LocalizationUtils;
16
import org.eclipse.core.runtime.IProgressMonitor;
17
import org.eclipse.core.runtime.IProgressMonitor;
17
import org.eclipse.core.runtime.IStatus;
18
import org.eclipse.core.runtime.IStatus;
18
import org.eclipse.core.runtime.Status;
19
import org.eclipse.core.runtime.Status;
Lines 30-35 Link Here
30
		this.threadnum = thread;
31
		this.threadnum = thread;
31
		this.solver = s;
32
		this.solver = s;
32
	}
33
	}
34
	
35
	public static int COMPUTE_DELTAS = 0;
36
	public static int APPLY_DELTAS = 1;
33
37
34
	protected double progress;
38
	protected double progress;
35
	protected double t;
39
	protected double t;
Lines 39-44 Link Here
39
	long timeDelta;
43
	long timeDelta;
40
	int cycle;
44
	int cycle;
41
	short threadnum;
45
	short threadnum;
46
	int step;
42
	
47
	
43
	public double getProgress() {
48
	public double getProgress() {
44
		return this.progress;
49
		return this.progress;
Lines 47-54 Link Here
47
		this.progress = p;
52
		this.progress = p;
48
	}
53
	}
49
	protected IStatus run(final IProgressMonitor monitor) {
54
	protected IStatus run(final IProgressMonitor monitor) {
50
		solver._step(time,timeDelta,cycle,threadnum);
55
		IStatus status = Status.CANCEL_STATUS;
51
		
56
		
52
		return Status.OK_STATUS;
57
		if (step == COMPUTE_DELTAS) {
58
			double adjustmentFactor = solver.computeDeltasStep(time,timeDelta,cycle,threadnum);
59
			
60
			status = new Status(IStatus.OK, "unknownId", adjustmentFactor + "");
61
		} else if (step == APPLY_DELTAS) {
62
			solver.applyDeltasStep(time, timeDelta, cycle, threadnum);
63
			status = new Status(IStatus.OK, "unknownId", "");
64
		}
65
		
66
		return status;
53
	}
67
	}
54
}
68
}
(-)src/org/eclipse/stem/solvers/fd/impl/FiniteDifferenceImpl.java (-26 / +117 lines)
Lines 97-103 Link Here
97
				} else currentStateLabelIter.next();
97
				} else currentStateLabelIter.next();
98
			}
98
			}
99
		}
99
		}
100
			
100
		
101
		if(jobs == null || jobs.length != num_threads) {
101
		if(jobs == null || jobs.length != num_threads) {
102
			// Initialize the jobs if not done yet or of the number of threads changes
102
			// Initialize the jobs if not done yet or of the number of threads changes
103
			jobs = new FdJob[num_threads];
103
			jobs = new FdJob[num_threads];
Lines 107-119 Link Here
107
				jobs[i] = new FdJob("Finite Difference Worker "+i, threadnum, this);
107
				jobs[i] = new FdJob("Finite Difference Worker "+i, threadnum, this);
108
			} // For each job
108
			} // For each job
109
		} // If not initialized
109
		} // If not initialized
110
110
		
111
		recursiveStep(time, timeDelta, cycle);
112
			
113
		return true;
114
	}
115
	
116
	protected void recursiveStep(STEMTime time, long timeDelta, int cycle) {
111
		// Initialize
117
		// Initialize
112
	
118
	
113
		for(FdJob j:jobs) {
119
		for(FdJob j:jobs) {
114
			j.cycle = cycle;
120
			j.cycle = cycle;
115
			j.time = time;
121
			j.time = time;
116
			j.timeDelta = timeDelta;
122
			j.timeDelta = timeDelta;
123
			j.step = FdJob.COMPUTE_DELTAS;
117
		}
124
		}
118
		
125
		
119
		// Schedule. Jobs can be rescheduled after finished
126
		// Schedule. Jobs can be rescheduled after finished
Lines 129-158 Link Here
129
			}
136
			}
130
		}
137
		}
131
		
138
		
132
		// Set the common time and step size here and validate everything is right
139
		double factor = 1.0;
133
		//double minStep = Double.MAX_VALUE;
134
		//double currentT = jobs[0].t;
135
		//for(SimJob j : jobs) {
136
			// The jobs have calculated new step sizes after they finished. Pick the
137
			// smallest one for the next cycle
138
		//	if(j.h < minStep) minStep = j.h;
139
		//	if(j.t != currentT) Activator.logError("Error, one thread was in misstep with other threads, its time was "+j.t+" versus "+currentT, new Exception());
140
		//}
141
		
140
		
142
		//this.setCurrentX(currentT);
141
		for (FdJob j : jobs) {
143
		//this.setStepSize(minStep); // smallest one from above.
142
			factor = Math.min(factor, Double.parseDouble(j.getResult().getMessage()));
144
		return true;
143
		}
144
		
145
		if (factor == 1.0) {
146
			// Apply deltas
147
			
148
			for(FdJob j:jobs)
149
				j.step = FdJob.APPLY_DELTAS;
150
			
151
			for(FdJob j:jobs) 
152
				j.schedule();
153
			
154
			for(FdJob j : jobs) {
155
				try {
156
					j.join();
157
				} catch(InterruptedException ie) {
158
					Activator.logError(ie.getMessage(), ie);
159
				}
160
			}
161
		} else {
162
			// Compute deltas for smaller time step
163
			long newTimeDelta = (long) Math.floor(factor*timeDelta);
164
			
165
			for(FdJob j:jobs)
166
				j.timeDelta = newTimeDelta;
167
			
168
			for(FdJob j:jobs) 
169
				j.schedule();
170
			
171
			for(FdJob j : jobs) {
172
				try {
173
					j.join();
174
				} catch(InterruptedException ie) {
175
					Activator.logError(ie.getMessage(), ie);
176
				}
177
			}
178
			
179
			// Apply deltas
180
			
181
			for(FdJob j:jobs)
182
				j.step = FdJob.APPLY_DELTAS;
183
			
184
			for(FdJob j:jobs) 
185
				j.schedule();
186
			
187
			for(FdJob j : jobs) {
188
				try {
189
					j.join();
190
				} catch(InterruptedException ie) {
191
					Activator.logError(ie.getMessage(), ie);
192
				}
193
			}
194
			
195
			// Call this method again for the rest of the time step
196
			
197
			for(Decorator decorator:this.getDecorators()) {
198
				EList<DynamicLabel>allLabels = decorator.getLabelsToUpdate();
199
				for (final Iterator<DynamicLabel> currentStateLabelIter = allLabels
200
						.iterator(); currentStateLabelIter.hasNext();) {
201
					if(decorator instanceof IntegrationDecorator) {
202
						// It's a standard disease model with a standard disease model label
203
						final IntegrationLabel iLabel = (IntegrationLabel) currentStateLabelIter.next();
204
						((IntegrationLabelValue)iLabel.getProbeValue()).set((IntegrationLabelValue)iLabel.getNextValue());
205
						((IntegrationLabelValue)iLabel.getTempValue()).set((IntegrationLabelValue)iLabel.getNextValue());
206
						((IntegrationLabelValue)iLabel.getTempValue()).prepareCycle();
207
						((IntegrationLabelValue)iLabel.getProbeValue()).prepareCycle();
208
					} else currentStateLabelIter.next();
209
				}
210
			}
211
			
212
			recursiveStep(time, timeDelta - newTimeDelta, cycle);
213
		}
145
	}
214
	}
146
	
215
	
147
	/**
216
	protected double computeDeltasStep(STEMTime time, long timeDelta, int cycle, short threadnum) {
148
	 * _step Do the step for a single thread
149
	 * 
150
	 * @param time
151
	 * @param timeDelta
152
	 * @param cycle
153
	 * @param threadnum
154
	 */
155
	protected void _step(STEMTime time, long timeDelta, int cycle, short threadnum) {
156
		// Now give each decorator a chance to update its dynamic
217
		// Now give each decorator a chance to update its dynamic
157
		// labels in the canonical graph, but only if it is enabled. A
218
		// labels in the canonical graph, but only if it is enabled. A
158
		// Decorator might not be enabled if it is the action of a Trigger
219
		// Decorator might not be enabled if it is the action of a Trigger
Lines 170-180 Link Here
170
		for(IntegrationDecorator imodel:iDecorators)
231
		for(IntegrationDecorator imodel:iDecorators)
171
			imodel.applyExternalDeltas(time, timeDelta, partitioner.partitionDecoratorLabels((Decorator)imodel, threadnum));
232
			imodel.applyExternalDeltas(time, timeDelta, partitioner.partitionDecoratorLabels((Decorator)imodel, threadnum));
172
		
233
		
234
		double factor = 1.0;
235
		
173
		for(IntegrationDecorator imodel:iDecorators)
236
		for(IntegrationDecorator imodel:iDecorators)
174
			updateStandardDiseaseModelLabels((Decorator)imodel, time, timeDelta, cycle, threadnum);
237
			factor = Math.min(factor, getDeltaAdjustment((Decorator)imodel, threadnum));
175
				
238
				
239
		return factor;
240
	}
241
	
242
	protected void applyDeltasStep(STEMTime time, long timeDelta, int cycle, short threadnum) {
243
		EList<IntegrationDecorator> iDecorators = new BasicEList<IntegrationDecorator>();
244
		for (final Iterator<Decorator> decoratorIter = this
245
				.getDecorators().iterator(); decoratorIter.hasNext();) {
246
			final Decorator decorator = decoratorIter.next();
247
			// Is the decorator enabled?
248
			if (decorator.isEnabled() && decorator instanceof IntegrationDecorator) iDecorators.add((IntegrationDecorator)decorator);
249
		}
250
		
251
		for(IntegrationDecorator imodel:iDecorators)
252
			updateStandardDiseaseModelLabels((Decorator)imodel, time, timeDelta, cycle, threadnum);
176
	}
253
	}
177
	
254
	
255
	protected double getDeltaAdjustment(Decorator model, short threadnum) {
256
		EList<DynamicLabel> myLabels = partitioner.partitionDecoratorLabels(model, threadnum);
257
		double factor = 1.0;
258
	
259
		for (final Iterator<DynamicLabel> currentStateLabelIter = myLabels
260
				.iterator(); currentStateLabelIter.hasNext();) {
261
			final IntegrationLabel label = (IntegrationLabel) currentStateLabelIter.next();
262
	
263
			IntegrationLabelValue delta = (IntegrationLabelValue)label.getDeltaValue();
264
			factor = Math.min(factor, delta.computeDeltaAdjustment((IntegrationLabelValue)label.getProbeValue()));
265
		}
266
		
267
		return factor;
268
	}
178
	
269
	
179
	protected void updateStandardDiseaseModelLabels(Decorator model, STEMTime time, long timeDelta, int cycle, short threadnum) {
270
	protected void updateStandardDiseaseModelLabels(Decorator model, STEMTime time, long timeDelta, int cycle, short threadnum) {
180
		
271
		
Lines 189-201 Link Here
189
		// Initialize the next value from the current value and add the delta
280
		// Initialize the next value from the current value and add the delta
190
		for (final Iterator<DynamicLabel> currentStateLabelIter = myLabels
281
		for (final Iterator<DynamicLabel> currentStateLabelIter = myLabels
191
				.iterator(); currentStateLabelIter.hasNext();) {
282
				.iterator(); currentStateLabelIter.hasNext();) {
192
			final DynamicLabel label = (DynamicLabel) currentStateLabelIter.next();
283
			final IntegrationLabel label = (IntegrationLabel) currentStateLabelIter.next();
193
			LabelValue nextState = label.getNextValue();
284
			LabelValue nextState = label.getNextValue();
194
			
285
			
195
			LabelValue delta = ((IntegrationLabel)label).getDeltaValue();
286
			LabelValue delta = ((IntegrationLabel)label).getDeltaValue();
196
			// For finite difference, we need to make sure we don't
287
			// For finite difference, we need to make sure we don't
197
			// move too many people from one state to another
288
			// move too many people from one state to another
198
			((IntegrationLabelValue)delta).adjustDelta((IntegrationLabelValue)label.getCurrentValue());
289
			((IntegrationLabelValue)delta).avoidNegative((IntegrationLabelValue)label.getProbeValue());
199
			
290
			
200
			nextState.reset();
291
			nextState.reset();
201
			// Add delta, this will also add the incidence
292
			// Add delta, this will also add the incidence
(-)src/org/eclipse/stem/populationmodels/standard/tests/StandardPopulationModelLabelValueTest.java (-8 / +2 lines)
Lines 15-26 Link Here
15
 * <!-- begin-user-doc -->
15
 * <!-- begin-user-doc -->
16
 * A test case for the model object '<em><b>Population Model Label Value</b></em>'.
16
 * A test case for the model object '<em><b>Population Model Label Value</b></em>'.
17
 * <!-- end-user-doc -->
17
 * <!-- end-user-doc -->
18
 * <p>
19
 * The following operations are tested:
20
 * <ul>
21
 *   <li>{@link org.eclipse.stem.populationmodels.standard.StandardPopulationModelLabelValue#adjustDelta(org.eclipse.stem.core.graph.IntegrationLabelValue) <em>Adjust Delta</em>}</li>
22
 * </ul>
23
 * </p>
24
 * @generated
18
 * @generated
25
 */
19
 */
26
public class StandardPopulationModelLabelValueTest extends PopulationModelLabelValueTest {
20
public class StandardPopulationModelLabelValueTest extends PopulationModelLabelValueTest {
Lines 104-113 Link Here
104
	}
98
	}
105
99
106
	/**
100
	/**
107
	 * Tests the '{@link org.eclipse.stem.populationmodels.standard.StandardPopulationModelLabelValue#adjustDelta(org.eclipse.stem.core.graph.IntegrationLabelValue) <em>Adjust Delta</em>}' operation.
101
	 * Tests the '{@link org.eclipse.stem.populationmodels.standard.StandardPopulationModelLabelValue#avoidNegative(org.eclipse.stem.core.graph.IntegrationLabelValue) <em>Adjust Delta</em>}' operation.
108
	 * <!-- begin-user-doc -->
102
	 * <!-- begin-user-doc -->
109
	 * <!-- end-user-doc -->
103
	 * <!-- end-user-doc -->
110
	 * @see org.eclipse.stem.populationmodels.standard.StandardPopulationModelLabelValue#adjustDelta(org.eclipse.stem.core.graph.IntegrationLabelValue)
104
	 * @see org.eclipse.stem.populationmodels.standard.StandardPopulationModelLabelValue#avoidNegative(org.eclipse.stem.core.graph.IntegrationLabelValue)
111
	 * @generated NOT
105
	 * @generated NOT
112
	 */
106
	 */
113
	public void testAdjustDelta__IntegrationLabelValue() {
107
	public void testAdjustDelta__IntegrationLabelValue() {

Return to bug 346732