Commit 5cbbdcc6 authored by Claes Sjöfors's avatar Claes Sjöfors

MPC controller update with monitor object

parent c64855dc
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -40,6 +40,7 @@
#include <math.h>
#include "co_dcli.h"
#include "simul.h"
#include "rt_plc_msg.h"
typedef enum {
mlp_eActivation_No = 0,
......@@ -60,10 +61,12 @@ typedef struct {
double aval[20];
} mlp_sCtx, *mlp_tCtx;
#define mlp_scale(value, idx) ((value) * co->ScaleCoeff1[(idx)] + co->ScaleCoeff0[(idx)])
#define mlp_rescale(value, idx) (((value) - co->ScaleCoeff0[(idx)]) / co->ScaleCoeff1[(idx)])
int mlp_import(const char *file, mlp_sCtx *mlp);
void mlp_free(mlp_sCtx *mlp);
void mlp_model(mlp_tCtx mlp, double *x, double *out);
static int mlp_import(pwr_sClass_Sim_ModelMLP* co, const char *file, mlp_sCtx *mlp);
static void mlp_model(mlp_tCtx mlp, double *x, double *out);
void Sim_ModelMLP_Fo_init(pwr_sClass_Sim_ModelMLP_Fo* o)
{
......@@ -78,19 +81,33 @@ void Sim_ModelMLP_Fo_init(pwr_sClass_Sim_ModelMLP_Fo* o)
o->PlcConnectP = 0;
co = (pwr_sClass_Sim_ModelMLP *)o->PlcConnectP;
if (!co) {
co->Status = 2;
if (!co)
return;
}
o->ModelP = (mlp_tCtx)calloc(1, sizeof(mlp_sCtx));
co->Status = mlp_import(co->ModelFile, (mlp_tCtx)o->ModelP);
co->Status = mlp_import(co, co->ModelFile, (mlp_tCtx)o->ModelP);
if (EVEN(co->Status))
return;
co->Layers = ((mlp_tCtx)o->ModelP)->layers;
for (i = 0; i < MIN(co->Layers, sizeof(co->LayerSizes)/sizeof(co->LayerSizes[0])); i++)
co->LayerSizes[i] = ((mlp_tCtx)o->ModelP)->layer_sizes[i];
switch (((mlp_tCtx)o->ModelP)->activation) {
case mlp_eActivation_Tanh:
strcpy(co->Activation, "tanh");
break;
case mlp_eActivation_Identity:
strcpy(co->Activation, "identity");
break;
case mlp_eActivation_Relu:
strcpy(co->Activation, "relu");
break;
case mlp_eActivation_Logistic:
strcpy(co->Activation, "logistic");
break;
default:
strcpy(co->Activation, "unknown");
}
((mlp_tCtx)o->ModelP)->inputs = (double *)calloc(((mlp_tCtx)o->ModelP)->layer_sizes[0],
sizeof(double));
......@@ -106,20 +123,22 @@ void Sim_ModelMLP_Fo_exec(plc_sThread* tp, pwr_sClass_Sim_ModelMLP_Fo* o)
return;
for (i = 0; i < co->LayerSizes[0]; i++) {
((mlp_tCtx)o->ModelP)->inputs[i] = (double)(**(pwr_tFloat32 **)((char *)&o->In1P + pwr_cInputOffset * i));
((mlp_tCtx)o->ModelP)->inputs[i] = (double)mlp_scale((**(pwr_tFloat32 **)((char *)&o->In1P + pwr_cInputOffset * i)), i+1);
}
mlp_model((mlp_tCtx)o->ModelP, ((mlp_tCtx)o->ModelP)->inputs, &out);
o->Out = (pwr_tFloat32)out;
o->Out = (pwr_tFloat32)mlp_rescale(out, 0);
co->Value = o->Out;
}
int mlp_import(const char *file, mlp_sCtx *mlp)
static int mlp_import(pwr_sClass_Sim_ModelMLP* co, const char *file, mlp_sCtx *mlp)
{
pwr_tFileName fname;
FILE *fp;
char line[2000];
unsigned int i, j, k;
unsigned int size, num;
char *s;
dcli_translate_filename(fname, file);
......@@ -128,26 +147,48 @@ int mlp_import(const char *file, mlp_sCtx *mlp)
return 0;
while (dcli_read_line(line, sizeof(line), fp)) {
if (strncmp(line, "Layers ", 7) == 0) {
if (sscanf(&line[7], "%d", &mlp->layers) != 1) {
printf("Syntax error\n");
return 0;
if (strncmp(line, "Scaler ", 7) == 0) {
char sarray[20][40];
if (sscanf(&line[7], "%d", &size) != 1)
return PLC__FILESYNTAX;
dcli_read_line(line, sizeof(line), fp);
num = dcli_parse(line, " ", "", (char*)sarray,
sizeof(sarray) / sizeof(sarray[0]), sizeof(sarray[0]),
0);
if (num < size)
return PLC__FILESYNTAX;
for (i = 0; i < size; i++) {
if (sscanf(sarray[i], "%f", &co->ScaleCoeff0[i]) != 1)
return PLC__FILESYNTAX;
}
dcli_read_line(line, sizeof(line), fp);
num = dcli_parse(line, " ", "", (char*)sarray,
sizeof(sarray) / sizeof(sarray[0]), sizeof(sarray[0]),
0);
if (num < size)
return PLC__FILESYNTAX;
for (i = 0; i < size; i++) {
if (sscanf(sarray[i], "%f", &co->ScaleCoeff1[i]) != 1)
return PLC__FILESYNTAX;
}
}
if (strncmp(line, "Layers ", 7) == 0) {
if (sscanf(&line[7], "%d", &mlp->layers) != 1)
return PLC__FILESYNTAX;
}
else if (strncmp(line, "LayerSizes ", 11) == 0) {
mlp->layer_sizes = (unsigned int *)calloc(mlp->layers, sizeof(int));
s = line;
for (i = 0; i < mlp->layers; i++) {
s = strchr(s, ' ');
if (!s) {
printf("Syntax error\n");
return 0;
}
if (!s)
return PLC__FILESYNTAX;
s++;
if (sscanf(s, "%d", &mlp->layer_sizes[i]) != 1) {
printf("Syntax error\n");
return 0;
}
if (sscanf(s, "%d", &mlp->layer_sizes[i]) != 1)
return PLC__FILESYNTAX;
}
}
else if (strncmp(line, "Activation ", 11) == 0) {
......@@ -174,10 +215,8 @@ int mlp_import(const char *file, mlp_sCtx *mlp)
s = strchr(s, ' ');
s++;
}
if (sscanf(s, "%lf", &mlp->intercepts[i][j]) != 1) {
printf("Syntax error\n");
return 0;
}
if (sscanf(s, "%lf", &mlp->intercepts[i][j]) != 1)
return PLC__FILESYNTAX;
}
}
}
......@@ -197,7 +236,7 @@ int mlp_import(const char *file, mlp_sCtx *mlp)
dcli_read_line(line, sizeof(line), fp);
sscanf(line, "%d %d %d %lf", &i1, &j1, &k1, &mlp->coefs[i][j][k]);
if (i1 != i || j1 != j || k1 != k)
printf("Syntax error\n");
return PLC__FILESYNTAX;
}
}
}
......@@ -211,7 +250,7 @@ int mlp_import(const char *file, mlp_sCtx *mlp)
for (i = 0; i < mlp->layers - 1; i++) {
mlp->h[i] = (double *)calloc(mlp->layer_sizes[i+1], sizeof(double));
}
return 1;
return PLC__SUCCESS;
}
void mlp_free(mlp_sCtx *mlp)
......@@ -235,7 +274,7 @@ void mlp_free(mlp_sCtx *mlp)
free(mlp);
}
void mlp_model(mlp_tCtx mlp, double *x, double *out)
static void mlp_model(mlp_tCtx mlp, double *x, double *out)
{
unsigned int i, j, k;
......
......@@ -113,6 +113,43 @@ void Sim_SignalGeneratorFo_exec(plc_sThread* tp, pwr_sClass_Sim_SignalGeneratorF
if ( value > co->Bias + co->Amplitude)
value -= co->Amplitude;
break;
case pwr_eSim_SignalGeneratorType_Triangular: {
if ( co->Period < FLT_MIN)
break;
co->Accum += *o->ScanTime;
if ( co->Accum / co->Period > (int)(1000.0f / co->Period))
co->Accum -= co->Period * (int)(1000.0f / co->Period);
float p = co->Accum / co->Period - (int)(co->Accum / co->Period);
if ( p <= co->PulseWidth/100)
value += co->Amplitude * p / (co->PulseWidth/100);
else
value += co->Amplitude * (1 - p) / (1 - (co->PulseWidth/100));
break;
}
case pwr_eSim_SignalGeneratorType_StepPyramid: {
if ( co->Period < FLT_MIN)
break;
co->Accum += *o->ScanTime;
if ( co->Accum / co->Period > (int)(1000.0f / co->Period))
co->Accum -= co->Period * (int)(1000.0f / co->Period);
float p = co->Accum / co->Period - (int)(co->Accum / co->Period);
int steps = (int)(1.0f / (co->PulseWidth/100));
if (ODD(steps))
steps -= 1;
int current = (int)(p * steps);
int height = steps/2;
if ( current <= steps/2 - 1)
value += co->Amplitude / height * (current + 1);
else if (current < steps - 1)
value += co->Amplitude / height * (steps - current - 1);
break;
}
}
if ( co->FilterTime > 0.001f) {
......@@ -124,6 +161,11 @@ void Sim_SignalGeneratorFo_exec(plc_sThread* tp, pwr_sClass_Sim_SignalGeneratorF
value += (float)rand() / RAND_MAX * co->Noise * 2 - co->Noise;
}
if (co->RampUp != 0.0f && value - o->ActualValue > co->RampUp * tp->f_scan_time)
o->ActualValue += co->RampUp * tp->f_scan_time;
else if (co->RampDown != 0.0f && value - o->ActualValue < -co->RampDown * tp->f_scan_time)
o->ActualValue -= co->RampDown * tp->f_scan_time;
else
o->ActualValue = value;
co->ActualValue = o->ActualValue;
}
......
This diff is collapsed.
......@@ -29,22 +29,26 @@
100 20
135 20
101 20
102 92
103 7
104 2.9419
136 2.9419
102 234
103 258
104 2.73887
136 2.73887
105 100
106 13
107 1
106 28
107 2
108 53
109 10.5
110 40.8
111 0.831579
116 0
117 0
118 110
119 89
120 1
112 758
113 613
114 196
115 117
116 4
117 33
118 107
119 117
120 0
121 Claes context
122 0
126 0.5
......@@ -63,7 +67,7 @@
134
22
2200 0
2201 536
2201 540
2202 pwr_c_sim_signalgenerator
2203 310
2205 0
......@@ -3714,10 +3718,10 @@ pwr_exe:
99
30
3004 O75
3000 0.63148
3000 0.58148
3001 0.23148
3002 0.696505
3003 0.246505
3003 0.196505
3008 0
3010 4
3011 2
......@@ -8273,10 +8277,10 @@ pwr_exe:
99
30
3004 O376
3000 17.6556
3000 17.6056
3001 13.2056
3002 22.4097
3003 21.5597
3002 22.5097
3003 21.7097
3008 326
3010 4
3011 2
......@@ -8301,7 +8305,7 @@ pwr_exe:
2802 12.1056
2803 0
2804 1
2805 19.7597
2805 19.8597
2806 0
99
99
......@@ -9161,10 +9165,10 @@ pwr_exe:
99
30
3004 O503
3000 14.8056
3000 14.7556
3001 13.2056
3002 25.3597
3003 24.5097
3002 25.4597
3003 24.6597
3008 326
3010 4
3011 2
......@@ -9189,7 +9193,7 @@ pwr_exe:
2802 12.1056
2803 0
2804 1
2805 22.7097
2805 22.8097
2806 0
99
99
......@@ -9478,7 +9482,7 @@ pwr_exe:
6967
6968
6969 0
6900 31
6900 127
6901 Straight
6933 0
6902 Sine
......@@ -9489,6 +9493,10 @@ pwr_exe:
6936 3
6905 SawTooth
6937 4
6906 Triangular
6938 5
6907 StepPyramid
6939 6
99
99
99
......@@ -9497,7 +9505,7 @@ pwr_exe:
3000 14.9556
3001 13.2056
3002 23.8597
3003 23.0097
3003 23.0597
3008 326
3010 4
3011 2
......@@ -9803,10 +9811,10 @@ pwr_exe:
99
30
3004 O510
3000 17.0556
3000 17.1056
3001 13.2056
3002 27.5997
3003 26.7497
3002 27.6497
3003 26.8497
3008 326
3010 4
3011 2
......@@ -9831,7 +9839,7 @@ pwr_exe:
2802 12.1056
2803 0
2804 1
2805 24.9497
2805 24.9997
2806 0
99
99
......@@ -10263,10 +10271,10 @@ pwr_exe:
99
30
3004 O515
3000 15.6056
3000 15.5056
3001 13.2056
3002 29.8397
3003 28.9897
3003 29.0397
3008 326
3010 4
3011 2
......@@ -10723,10 +10731,10 @@ pwr_exe:
99
30
3004 O520
3000 17.5556
3000 17.4556
3001 13.2056
3002 32.0797
3003 31.2297
3002 32.1797
3003 31.3797
3008 326
3010 4
3011 2
......@@ -10751,7 +10759,7 @@ pwr_exe:
2802 12.1056
2803 0
2804 1
2805 29.4297
2805 29.5297
2806 0
99
99
......@@ -11183,10 +11191,10 @@ pwr_exe:
99
30
3004 O525
3000 15.3556
3000 15.2556
3001 13.2056
3002 34.3197
3003 33.4697
3002 34.4197
3003 33.6197
3008 326
3010 4
3011 2
......@@ -11211,7 +11219,7 @@ pwr_exe:
2802 12.1056
2803 0
2804 1
2805 31.6697
2805 31.7697
2806 0
99
99
......@@ -11643,10 +11651,10 @@ pwr_exe:
99
30
3004 O530
3000 16.8056
3000 16.9056
3001 13.2056
3002 36.5597
3003 35.7097
3003 35.7597
3008 326
3010 4
3011 2
......@@ -12318,5 +12326,375 @@ pwr_exe:
99
99
99
30
3004 O536
3000 28.9056
3001 25.6556
3002 22.5097
3003 21.7097
3008 326
3010 4
3011 2
3007 0
3006
3005
9
900 4
901 303
904 326
902 RampUp
903
7
700 1.1
701 2.5
99
99
3009
28
2800 1
2801 0
2802 24.5556
2803 0
2804 1
2805 19.8597
2806 0
99
99
27
2703 334
2704 330
2731 310
2722 338
2705 330
2723 338
2706 334
2732 10000
2708 0
2709 0
2710 0
2711 0
2712 0
2713 0
2714 0
2715 0
2720 0
2725 0
2726 0
2702 0
2701
2700
10
1000 pwrct_valueinputmediumbg
1002 O537
1005
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1006 36.594
1007 30.7458
1008 22.8467
1009 21.4902
1013 36.594
1014 30.7458
1015 22.8467
1016 21.4902
1003
0
0
0
0
0
0
0
0
0
0
1004
1001
7
700 8.5
701 7.9
99
1010
1011
1018
1019
1020
1021
1022
1023
1024
1025
1012 0
1017 9999
1027 9999
1026 35454972
1028 0
1029
99
2707
28
2800 1.77219
2801 0
2802 31.0116
2803 0
2804 1.04348
2805 21.6467
2806 0
99
2716 0
2718
2717
2719 0
2724 1
2727 0
2728 303
2729 4
2730 0
2721
1
100 1024
105 0
101 4096
106 0
102 6
103 0
12
1200 $object.RampUp##Float32
1201 %10.3f
1202 1
1203 1
1204 0
1205
1206 0
99
13
1302 0
1303 0
1304 0
1305 0
1306 0
1307
1308
1309 0
1310 0
1311 0
99
99
99
30
3004 O538
3000 30.0056
3001 25.6556
3002 23.9597
3003 23.1597
3008 326
3010 4
3011 2
3007 0
3006
3005
9
900 4
901 303
904 326
902 RampDown
903
7
700 1.1
701 2.5
99
99
3009
28
2800 1
2801 0
2802 24.5556
2803 0
2804 1
2805 21.3097
2806 0
99
99
27
2703 334
2704 330
2731 310
2722 338
2705 330
2723 338
2706 334
2732 10000
2708 0
2709 0
2710 0
2711 0
2712 0
2713 0
2714 0
2715 0
2720 0
2725 0
2726 0
2702 0
2701
2700
10
1000 pwrct_valueinputmediumbg
1002 O539
1005
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1006 36.594
1007 30.7458
1008 24.2967
1009 22.9402
1013 36.594
1014 30.7458
1015 24.2967
1016 22.9402
1003
0
0
0
0
0
0
0
0
0
0
1004
1001
7
700 8.5
701 7.9
99
1010
1011
1018
1019
1020
1021
1022
1023
1024
1025
1012 0
1017 9999
1027 9999
1026 35454972
1028 0
1029
99
2707
28
2800 1.77219
2801 0
2802 31.0116
2803 0
2804 1.04348
2805 23.0967
2806 0
99
2716 0
2718
2717
2719 0
2724 1
2727 0
2728 303
2729 4
2730 0
2721
1
100 1024
105 0
101 4096
106 0
102 6
103 0
12
1200 $object.RampDown##Float32
1201 %10.3f
1202 1
1203 1
1204 0
1205
1206 0
99
13
1302 0
1303 0
1304 0
1305 0
1306 0
1307
1308
1309 0
1310 0
1311 0
99
99
99
99
99
! Generated by wb_print_wbl 07-NOV-2019 13:42:48.29
! Generated by wb_print_wbl 10-FEB-2020 16:09:47.11
! Volume Simul
! Version V5.7.0
Volume Simul $ClassVolume 0.0.0.8
Body SysBody 01-JAN-1970 01:00:00.00
Attr NextOix = "_X80"
Attr NextOix = "_X82"
Attr NextCix = "_X28"
Attr NextTix[0] = "_X8"
EndBody
......@@ -81,6 +81,20 @@ Volume Simul $ClassVolume 0.0.0.8
Attr Value = 4
EndBody
EndObject
Object Triangular $Value 81 28-JAN-2020 15:27:17.79
Body SysBody 28-JAN-2020 15:27:31.71
Attr Text = "Triangular"
Attr PgmName = "Triangular"
Attr Value = 5
EndBody
EndObject
Object StepPyramid $Value 82 28-JAN-2020 15:31:53.16
Body SysBody 28-JAN-2020 15:32:05.53
Attr Text = "StepPyramid"
Attr PgmName = "StepPyramid"
Attr Value = 6
EndBody
EndObject
EndObject
EndObject
Object Class $ClassHier 21 28-FEB-2016 17:06:54.40
......@@ -1581,7 +1595,7 @@ Volume Simul $ClassVolume 0.0.0.8
Object RtBody $ObjBodyDef 1 06-FEB-2019 15:11:38.90
Body SysBody 06-FEB-2019 15:11:38.90
Attr StructName = "Sim_SignalGenerator"
Attr NextAix = "_X41"
Attr NextAix = "_X43"
EndBody
Object Description $Attribute 21 06-FEB-2019 15:11:38.90
Body SysBody 06-FEB-2019 15:11:38.90
......@@ -1651,6 +1665,20 @@ Volume Simul $ClassVolume 0.0.0.8
Attr TypeRef = "pwrs:Type-$Float32"
EndBody
EndObject
Object RampUp $Attribute 41 28-JAN-2020 17:17:09.14
Body SysBody 28-JAN-2020 17:17:10.50
Attr PgmName = "RampUp"
Attr Size = 4
Attr TypeRef = "pwrs:Type-$Float32"
EndBody
EndObject
Object RampDown $Attribute 42 28-JAN-2020 17:17:19.26
Body SysBody 28-JAN-2020 17:17:21.07
Attr PgmName = "RampDown"
Attr Size = 4
Attr TypeRef = "pwrs:Type-$Float32"
EndBody
EndObject
Object ActualValueMin $Attribute 30 06-FEB-2019 15:11:38.90
Body SysBody 06-FEB-2019 15:11:38.90
Attr PgmName = "ActualValueMin"
......@@ -2240,8 +2268,15 @@ Volume Simul $ClassVolume 0.0.0.8
Object RtBody $ObjBodyDef 1 07-NOV-2019 12:25:46.68
Body SysBody 07-NOV-2019 12:26:18.41
Attr StructName = "Sim_ModelMLP"
Attr NextAix = "_X22"
Attr NextAix = "_X28"
EndBody
Object Description $Attribute 27 10-FEB-2020 16:09:23.86
Body SysBody 10-FEB-2020 16:09:23.86
Attr PgmName = "Description"
Attr Size = 80
Attr TypeRef = "pwrs:Type-$String80"
EndBody
EndObject
!/**
! Model file exported from the sev_analyser.
!*/
......@@ -2252,6 +2287,24 @@ Volume Simul $ClassVolume 0.0.0.8
Attr TypeRef = "pwrs:Type-$String40"
EndBody
EndObject
Object ScaleCoeff0 $Attribute 22 04-FEB-2020 16:45:53.94
Body SysBody 04-FEB-2020 16:46:15.09
Attr PgmName = "ScaleCoeff0"
Attr Size = 68
Attr Flags = 2
Attr Elements = 17
Attr TypeRef = "pwrs:Type-$Float32"
EndBody
EndObject
Object ScaleCoeff1 $Attribute 23 04-FEB-2020 16:46:20.26
Body SysBody 04-FEB-2020 16:46:20.26
Attr PgmName = "ScaleCoeff1"
Attr Size = 68
Attr Flags = 2
Attr Elements = 17
Attr TypeRef = "pwrs:Type-$Float32"
EndBody
EndObject
!/**
! Number of hidden layers.
!*/
......@@ -2297,6 +2350,28 @@ Volume Simul $ClassVolume 0.0.0.8
Attr TypeRef = "pwrs:Type-$Status"
EndBody
EndObject
Object Value $Attribute 24 10-FEB-2020 15:05:29.56
Body SysBody 10-FEB-2020 15:05:45.84
Attr PgmName = "Value"
Attr Size = 4
Attr Flags = 1040
Attr TypeRef = "pwrs:Type-$Float32"
EndBody
EndObject
Object ValueMin $Attribute 26 10-FEB-2020 15:10:54.88
Body SysBody 10-FEB-2020 15:11:23.85
Attr PgmName = "ValueMin"
Attr Size = 4
Attr TypeRef = "pwrs:Type-$Float32"
EndBody
EndObject
Object ValueMax $Attribute 25 10-FEB-2020 15:11:05.33
Body SysBody 10-FEB-2020 15:11:31.33
Attr PgmName = "ValueMax"
Attr Size = 4
Attr TypeRef = "pwrs:Type-$Float32"
EndBody
EndObject
!/**
! @Summary The plc function object connected to this object.
! The plc function object connected to this object.
......@@ -2312,7 +2387,25 @@ Volume Simul $ClassVolume 0.0.0.8
EndObject
EndObject
Object Template Sim_ModelMLP 2154528768 07-NOV-2019 12:25:46.68
Body RtBody 01-JAN-1970 01:00:00.00
Body RtBody 10-FEB-2020 16:09:43.26
Attr ScaleCoeff1[0] = 1.000000e+00
Attr ScaleCoeff1[1] = 1.000000e+00
Attr ScaleCoeff1[2] = 1.000000e+00
Attr ScaleCoeff1[3] = 1.000000e+00
Attr ScaleCoeff1[4] = 1.000000e+00
Attr ScaleCoeff1[5] = 1.000000e+00
Attr ScaleCoeff1[6] = 1.000000e+00
Attr ScaleCoeff1[7] = 1.000000e+00
Attr ScaleCoeff1[8] = 1.000000e+00
Attr ScaleCoeff1[9] = 1.000000e+00
Attr ScaleCoeff1[10] = 1.000000e+00
Attr ScaleCoeff1[11] = 1.000000e+00
Attr ScaleCoeff1[12] = 1.000000e+00
Attr ScaleCoeff1[13] = 1.000000e+00
Attr ScaleCoeff1[14] = 1.000000e+00
Attr ScaleCoeff1[15] = 1.000000e+00
Attr ScaleCoeff1[16] = 1.000000e+00
Attr ValueMax = 1.000000e+02
EndBody
EndObject
EndObject
......
......@@ -8,6 +8,13 @@ class Error(Exception):
def __str__(self):
return self.str
class SyntaxError(Error):
def __init__(self, status, str):
self.str = str
super(Error, self).__init__(status, str)
def __str__(self):
return self.str
def translateFilename(name):
if name[:1] == '$':
idx = name.find('/')
......
......@@ -75,7 +75,47 @@ def hinton(matrix, max_weight=None, ax=None):
ax.autoscale_view()
ax.invert_yaxis()
def minmax_scale(ser):
"""MinMax scaler [0,1]."""
max = ser.max()
min = ser.min()
if min == max:
coeff0 = 0.0
coeff1 = 1.0 / max
ser = ser / max
else:
coeff0 = -min/(max - min)
coeff1 = 1.0/(max - min)
ser = ser * coeff1 + coeff0
return ser, coeff0, coeff1;
def minmax2_scale(ser):
"""MinMax scaler [-1,1]."""
max = ser.max()
min = ser.min()
if min == max:
coeff0 = 0.0
coeff1 = 1.0 / max
ser = ser / max
else:
coeff0 = -2.0*min/(max - min) - 1
coeff1 = 2.0/(max - min)
ser = ser * coeff1 + coeff0
return ser, coeff0, coeff1;
def standard_scale(ser):
"""Standard scaler."""
mean = ser.mean()
std = ser.std()
if std == 0:
coeff0 = 0.0
coeff1 = 1.0 / mean
ser = ser / mean
else:
coeff0 = -mean/std
coeff1 = 1.0/std
ser = ser * coeff1 + coeff0
return ser, coeff0, coeff1;
class WData:
"""WData class, container for measuring data.
......@@ -102,6 +142,8 @@ class WData:
OP_CURVE = 13
OP_CONSTANT = 14
OP_SHIFT = 15
OP_SCALE = 16
OP_MSHIFT = 17
operations = ((OP_NO, 'No'),
(OP_COPY, 'Copy'),
......@@ -118,7 +160,9 @@ class WData:
(OP_DIVIDE, 'Divide'),
(OP_CURVE, 'Curve'),
(OP_CONSTANT, 'Const'),
(OP_SHIFT, 'Shift'))
(OP_SHIFT, 'Shift'),
(OP_SCALE, 'Scale'),
(OP_MSHIFT, 'MShift'))
def __init__(self):
self.set = False
......@@ -382,7 +426,7 @@ class WData:
i += 1
if len(cix) != 1:
raise co.Error(0, "Select one attributes")
raise co.Error(0, "Select one attribute")
return
if cix[0] == len(self.wdname) - 1:
......@@ -586,7 +630,7 @@ class WData:
i = len(opv) - 1
for op in reversed(opv):
self.exec_op(op, par1v[i], par2v[i])
self.exec_op(op, par1v[i], par2v[i], 0)
i -= 1
self.formula_trim(formulas)
......@@ -631,7 +675,7 @@ class WData:
i += 1
self.wd.columns = self.wdcol
def exec_op(self, op, par1, par2):
def exec_op(self, op, par1, par2, par3):
arg1 = None
found1 = False
......@@ -646,6 +690,9 @@ class WData:
if op == self.OP_SHIFT:
arg1 = par2
if op == self.OP_MSHIFT:
arg1 = par2
arg2 = par3
elif op == self.OP_CURVE:
arg1 = par2
elif op == self.OP_CONSTANT:
......@@ -658,9 +705,9 @@ class WData:
break
i2 += 1
self.op_exec(op, i1, i2, arg1)
self.op_exec(op, i1, i2, arg1, 0)
def op_exec(self, op, cix1, cix2, arg1):
def op_exec(self, op, cix1, cix2, arg1, arg2):
if cix1 != -1:
ser = pd.Series(self.wd[self.wdcol[cix1]])
......@@ -755,7 +802,7 @@ class WData:
elif op == self.OP_CURVE:
if arg1 == '':
raise co.Error(0, 'No curve file is supplied')
raise co.SyntaxError(0, 'No curve file is supplied')
fname = co.translateFilename(arg1)
curveframe = pd.read_csv(fname, header=None)
......@@ -766,14 +813,20 @@ class WData:
colname = "Curve(" + self.wdname[cix1] + "," + arg1 + ")"
elif op == self.OP_CONSTANT:
try:
value = float(arg1)
except:
raise co.SyntaxError(0, 'Missing value or sytax error')
const = [value] * len(self.wd)
ser = pd.Series(const)
colname = "Const(" + str(value) + ")"
elif op == self.OP_SHIFT:
try:
shiftvalue = int(arg1)
except:
raise co.SyntaxError(0, 'Missing value or syntax error')
shift = []
i = 0
......@@ -792,6 +845,47 @@ class WData:
i += 1
ser = pd.Series(shift)
colname = "Shift(" + self.wdname[cix1] + "," + str(shiftvalue) + ")"
elif op == self.OP_MSHIFT:
try:
shiftnum = int(arg2)
shiftarg1 = int(arg1)
except:
raise co.SyntaxError(0, 'Missing value or syntax error')
j = 0
while j < shiftnum:
shiftvalue = shiftarg1 * (j + 1)
shift = []
i = 0
for val in ser.values:
if shiftvalue > 0:
if i < shiftvalue:
val = 0
else:
val = ser.values[i-shiftvalue]
else:
if i - shiftvalue >= len(self.wd):
val = 0
else:
val = ser.values[i-shiftvalue]
shift.append(val)
i += 1
ser_shift = pd.Series(shift)
colname = "Shift(" + self.wdname[cix1] + "," + str(shiftvalue) + ")"
if j < shiftnum - 1:
idx = len(self.wdname)
self.wdname.append(colname)
colname = "A" + str(idx+1)
self.wdcol.append(colname)
self.wd[self.wdcol[idx]] = ser_shift
j += 1
ser = ser_shift
elif op == self.OP_SCALE:
ser, coeff0, coeff1 = minmax_scale(ser)
colname = "Scale(" + self.wdname[cix1] + ")"
print 'Scale Coeff0', coeff0, 'Coeff1', coeff1
else:
raise co.Error(0, 'No action is selected')
......
......@@ -76,4 +76,5 @@ reduinit <Redundancy init error> /error
reduconfig <Redundancy not configured> /error
filesyntax <Syntax error in file> /error
file <No such file> /error
mpc_shiftval <Number of shift values not matching model size> /error
.end
......@@ -290,6 +290,9 @@ static wbl_sSym classes[] = { { "pwr_eClass_ClassDef", pwr_eClass_ClassDef },
{ "BaseComponent:Class-CompModePID", pwr_cClass_CompModePID },
{ "BaseComponent:Class-CompPID", pwr_cClass_CompPID },
{ "BaseComponent:Class-CompModeIMC", pwr_cClass_CompModeIMC },
{ "BaseComponent:Class-CompMPC", pwr_cClass_CompMPC },
{ "BaseComponent:Class-CompMPC_MLP", pwr_cClass_CompMPC_MLP },
{ "BaseComponent:Class-CompMPC_Monitor", pwr_cClass_CompMPC_Monitor },
{ "BaseComponent:Class-CompIMC", pwr_cClass_CompIMC }, { 0, 0 } };
/* Datatypes */
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment