Skip to content

Commit

Permalink
- fix an issue with SetOperatingPoint for GainSchedModelParameters
Browse files Browse the repository at this point in the history
- add a flag to GainSchedFittingSpecs to determine how to set operating point,
- fix an issue when attempting to set the mean of the dataset as the operating point.
  • Loading branch information
Steinar Elgsæter committed Nov 26, 2024
1 parent 7f7b2dd commit b8ad379
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 207 deletions.
20 changes: 14 additions & 6 deletions Dynamic/Identification/FitScoreCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static double Calc(double[] meas, double[] sim)
static public double GetPlantWideSimulated(PlantSimulator plantSimObj, TimeSeriesDataSet inputData,
TimeSeriesDataSet simData)
{
const string disturbanceSignalPrefix = "_D";
// const string disturbanceSignalPrefix = "_D";
List<double> fitScores = new List<double>();
foreach (var modelName in plantSimObj.modelDict.Keys)
{
Expand All @@ -68,13 +68,17 @@ static public double GetPlantWideSimulated(PlantSimulator plantSimObj, TimeSerie
if (outputName == "" || outputName == null)
continue;


//
// Step 1: get the output signals of individual models in the measured dataset
//

if (plantSimObj.modelDict[modelName].GetProcessModelType() == ModelType.PID)
{
if (inputData.ContainsSignal(outputName))
{
measY = inputData.GetValues(outputName);
}

}
else //if (plantSimObj.modelDict[modelName].GetProcessModelType() == ModelType.SubProcess)
{
Expand All @@ -84,25 +88,29 @@ static public double GetPlantWideSimulated(PlantSimulator plantSimObj, TimeSerie
{
measY = inputData.GetValues(outputIdentName);
}
else if (simData.ContainsSignal(outputIdentName))
/* else if (simData.ContainsSignal(outputIdentName))
{
measY = simData.GetValues(outputIdentName);
}
}*/
}
// add in fit of process output, but only if "additive output signal" is not a
// locally identified "_D_" signal, as then output matches 100% always (any model error is put into disturbance signal as well)
else if (modelObj.GetAdditiveInputIDs() != null)
/* else if (modelObj.GetAdditiveInputIDs() != null)
{
if (!modelObj.GetAdditiveInputIDs()[0].StartsWith(disturbanceSignalPrefix))
{
measY = inputData.GetValues(outputName);
}
}
}*/
else if (inputData.ContainsSignal(outputName))
{
measY = inputData.GetValues(outputName);
}
}

//
// Step 2: get the corresponding signal from the simulated dataset
//
if (simData.ContainsSignal(outputName))
{
simY = simData.GetValues(outputName);
Expand Down
9 changes: 9 additions & 0 deletions Dynamic/Identification/GainSchedFittingSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,14 @@ public GainSchedFittingSpecs()
/// Default is 0, i.e. first input is used for gain-scheduling.
/// </summary>
public int uGainScheduledInputIndex = 0;

/// <summary>
/// If set to false, the model starts in the same value as the tuning set starts in,
/// if set to true, the model passes through the mean of the dataset.
/// </summary>
public bool DoSetOperatingPointToDatasetMean = false;



}
}
32 changes: 7 additions & 25 deletions Dynamic/Identification/GainSchedIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public static GainSchedModel IdentifyForGivenThresholds(UnitDataSet dataSet, Gai
if (idParams.Fitting.WasAbleToIdentify)
{
// simulate the model and determine the optimal bias term:
DetermineOperatingPointAndSimulate(ref idParams, ref dataSet);
DetermineOperatingPointAndSimulate(ref idParams, ref dataSet, gsFittingSpecs.DoSetOperatingPointToDatasetMean);

if (doTimeDelayEstimation)
EstimateTimeDelay(ref idParams, ref dataSet);
Expand Down Expand Up @@ -493,12 +493,12 @@ static private (GainSchedParameters, int) ChooseBestModelFromSimulationList(List
/// </summary>
/// <param name="gsParams">the gain-scheduled parameters that are to be updated with an operating point.</param>
/// <param name="dataSet">tuning dataset to be updated with Y_sim </param>
/// <param name="doMeanU">set the operating point to be the centre of the tuning set, if set to false models is set to match the start of the dataset</param>
///
/// <returns>true if able to estiamte bias, otherwise false</returns>
private static bool DetermineOperatingPointAndSimulate(ref GainSchedParameters gsParams, ref UnitDataSet dataSet)
private static bool DetermineOperatingPointAndSimulate(ref GainSchedParameters gsParams, ref UnitDataSet dataSet, bool doMeanU)
{
// if set to true, then the operating point is set to the average U in the dataset, otherwise it is set to equal the start
const bool doMeanU = false; // can be true or false, makes little difference?

var gsIdentModel = new GainSchedModel(gsParams, "ident_model");
var vec = new Vec(dataSet.BadDataID);

Expand All @@ -508,7 +508,7 @@ private static bool DetermineOperatingPointAndSimulate(ref GainSchedParameters g
var val = vec.Mean(vec.GetValues(dataSet.U.GetColumn(gsParams.GainSchedParameterIndex), dataSet.IndicesToIgnore));
if (val.HasValue && doMeanU)
{
desiredOpU = dataSet.U.GetColumn(gsParams.GainSchedParameterIndex).First();
desiredOpU = val.Value; //dataSet.U.GetColumn(gsParams.GainSchedParameterIndex).First();
}
gsParams.MoveOperatingPointUWithoutChangingModel(desiredOpU);

Expand Down Expand Up @@ -768,35 +768,17 @@ private static GainSchedParameters EvaluateMultipleTimeConstantsForGivenGainThre
{
curGainSchedParams_separateTc.TimeConstant_s = curTimeConstants;
curGainSchedParams_separateTc.TimeConstantThresholds = new double[] { candidateTcThresholds[i] };
DetermineOperatingPointAndSimulate(ref curGainSchedParams_separateTc, ref DS_separateTc);
DetermineOperatingPointAndSimulate(ref curGainSchedParams_separateTc, ref DS_separateTc,false);
candModelsStep2.Add(new GainSchedParameters(curGainSchedParams_separateTc));
// candYsimStep2.Add((double[])DS_separateTc.Y_sim.Clone());
}
else
{
curGainSchedParams_separateTc.TimeConstant_s = new double[] { curTimeConstants.First() };
curGainSchedParams_separateTc.TimeConstantThresholds = null;
DetermineOperatingPointAndSimulate(ref curGainSchedParams_separateTc, ref DS_separateTc);
DetermineOperatingPointAndSimulate(ref curGainSchedParams_separateTc, ref DS_separateTc,false);
candModelsStep2.Add(new GainSchedParameters(curGainSchedParams_separateTc));
}
/*
{
var curGainSchedParams_commonTc1 = new GainSchedParameters(curGainSchedParams_separateTc);
curGainSchedParams_commonTc1.TimeConstant_s = new double[] { curTimeConstants[0] };
curGainSchedParams_commonTc1.TimeConstantThresholds = null;
DetermineOperatingPointAndSimulate(ref curGainSchedParams_commonTc1, ref DS_commonTc1);
candModelsStep2.Add(new GainSchedParameters(curGainSchedParams_commonTc1));
// candYsimStep2.Add((double[])DS_commonTc1.Y_sim.Clone());
}
{
var curGainSchedParams_commonTc2 = new GainSchedParameters(curGainSchedParams_separateTc);
curGainSchedParams_commonTc2.TimeConstant_s = new double[] { curTimeConstants[1] };
curGainSchedParams_commonTc2.TimeConstantThresholds = null;
DetermineOperatingPointAndSimulate(ref curGainSchedParams_commonTc2, ref DS_commonTc2);
candModelsStep2.Add(new GainSchedParameters(curGainSchedParams_commonTc2));
// candYsimStep2.Add((double[])DS_commonTc2.Y_sim.Clone());
}
*/

bool doDebugPlot = false;
if (doDebugPlot)
Expand Down
Loading

0 comments on commit b8ad379

Please sign in to comment.