예제 #1
0
        public void GenerateVoltageSwingEvent()
        {
            float fPreFaultMW  = 0;
            float fPostFaultMW = 0;

            for (int iPoint = 0; iPoint < this.m_listMWPoint.Count; iPoint++)
            {
                AnalogPoint point = this.m_listMWPoint[iPoint];
                fPreFaultMW  += point.PreFaultValue;
                fPostFaultMW += point.ActualValue;
            }

            StringBuilder strBuilder = new StringBuilder();

            string strEvent = string.Format("\\nVoltage Swing", fPreFaultMW - fPostFaultMW);

            strBuilder.Append(strEvent);

            for (int iPoint = 0; iPoint < this.m_listKVPoint.Count; iPoint++)
            {
                AnalogPoint point = this.m_listKVPoint[iPoint];
                if (point.Update)
                {
                    if ((point.AlarmType == (UInt32)AlarmEvent.ANALOG_LOW2) ||
                        (point.AlarmType == (UInt32)AlarmEvent.ANALOG_HIGH2))
                    {
                        strEvent = string.Format("\\n - {0} {1} {2}", point.Time, point.StationName, point.PointName);
                        strBuilder.Append(strEvent);
                    }
                    point.Update = false;
                }
            }
            this.m_listGenerateEvent.Add(strBuilder.ToString());
        }
예제 #2
0
        private static Dictionary <Tuple <RegisterType, int>, BasePoint> GetScadaModel(bool test = false)
        {
            var dict = new Dictionary <Tuple <RegisterType, int>, BasePoint>();

            if (!test)
            {
                var analog = new AnalogPoint()
                {
                    Alarm = AlarmType.NO_ALARM, ClassType = ClassType.CLASS_0, Direction = FTN.Common.SignalDirection.ReadWrite, Index = 1, MeasurementType = FTN.Common.MeasurementType.ActiveEnergy, Mrid = "TEST_ANALOG", ObjectMrid = null, RegisterType = RegisterType.ANALOG_OUTPUT, TimeStamp = DateTime.Now.ToString(), MaxValue = 5, MinValue = 0, NormalValue = 0, Value = 10
                };
                var binary = new DiscretePoint()
                {
                    Alarm = AlarmType.NO_ALARM, ClassType = ClassType.CLASS_0, Direction = FTN.Common.SignalDirection.ReadWrite, Index = 3, MeasurementType = FTN.Common.MeasurementType.Status, Mrid = "TEST_BINARY", ObjectMrid = null, RegisterType = RegisterType.BINARY_OUTPUT, TimeStamp = DateTime.Now.ToString(), MaxValue = 1, MinValue = 0, NormalValue = 0, Value = 1
                };
                dict[Tuple.Create(analog.RegisterType, analog.Index)] = analog;
                dict[Tuple.Create(binary.RegisterType, binary.Index)] = binary;
            }
            else
            {
                var analog = new AnalogPoint()
                {
                    Alarm = AlarmType.NO_ALARM, ClassType = ClassType.CLASS_0, Direction = FTN.Common.SignalDirection.ReadWrite, Index = 1, MeasurementType = FTN.Common.MeasurementType.ActiveEnergy, Mrid = "TEST_ANALOG", ObjectMrid = null, RegisterType = RegisterType.ANALOG_OUTPUT, TimeStamp = DateTime.Now.ToString(), MaxValue = 7, MinValue = 0, NormalValue = 0, Value = 7
                };
                var binary = new DiscretePoint()
                {
                    Alarm = AlarmType.NO_ALARM, ClassType = ClassType.CLASS_0, Direction = FTN.Common.SignalDirection.ReadWrite, Index = 3, MeasurementType = FTN.Common.MeasurementType.Status, Mrid = "TEST_BINARY", ObjectMrid = null, RegisterType = RegisterType.BINARY_OUTPUT, TimeStamp = DateTime.Now.ToString(), MaxValue = 1, MinValue = 0, NormalValue = 0, Value = 0
                };
                dict[Tuple.Create(analog.RegisterType, analog.Index)] = analog;
                dict[Tuple.Create(binary.RegisterType, binary.Index)] = binary;
            }
            return(dict);
        }
예제 #3
0
        public AnalogPoint GetAnalogByFkIndex(UInt32 nFkIndex, string strStationName, string strPointName)
        {
            for (int iPoint = 0; iPoint < this.m_listMWPoint.Count; iPoint++)
            {
                AnalogPoint point = this.m_listMWPoint[iPoint];
                if (point.RecIndex == nFkIndex)
                {
                    if (point.StationName == strStationName)
                    {
                        if (point.PointName == strPointName)
                        {
                            return(point);
                        }
                    }
                    return(null);
                }
            }

            for (int iPoint = 0; iPoint < this.m_listKVPoint.Count; iPoint++)
            {
                AnalogPoint point = this.m_listKVPoint[iPoint];
                if (point.RecIndex == nFkIndex)
                {
                    if (point.StationName == strStationName)
                    {
                        if (point.PointName == strPointName)
                        {
                            return(point);
                        }
                    }
                    return(null);
                }
            }
            return(null);
        }
예제 #4
0
        public void GenerateMWLossEvent()
        {
            float fPreFaultMW  = 0;
            float fPostFaultMW = 0;

            for (int iPoint = 0; iPoint < this.m_listMWPoint.Count; iPoint++)
            {
                AnalogPoint point = this.m_listMWPoint[iPoint];
                fPreFaultMW  += point.PreFaultValue;
                fPostFaultMW += point.ActualValue;
            }

            StringBuilder strBuilder = new StringBuilder();

            string strEvent = string.Format("\\nTotal MW Loss {0}", fPreFaultMW - fPostFaultMW);

            strBuilder.Append(strEvent);

            for (int iPoint = 0; iPoint < this.m_listMWPoint.Count; iPoint++)
            {
                AnalogPoint point = this.m_listMWPoint[iPoint];
                if (point.ActualValue != point.PreFaultValue)
                {
                    if (point.ActualValue > this.m_fMWThreshold)
                    {
                        continue;
                    }
                    strEvent = string.Format("\\n - {0} {1}", point.StationName, point.PointName, point.PreFaultValue);
                    strBuilder.Append(strEvent);
                }
            }
            this.m_listGenerateEvent.Add(strBuilder.ToString());
        }
예제 #5
0
        public List <Point> ConvertToPoints(List <dnp3_protocol.dnp3types.sServerDatabasePoint> mangagedArray)
        {
            var list = new List <Point>();

            foreach (var item in mangagedArray)
            {
                if (item.eGroupID == dnp3_protocol.dnp3types.eDNP3GroupID.ANALOG_INPUT || item.eGroupID == dnp3_protocol.dnp3types.eDNP3GroupID.ANALOG_OUTPUTS)
                {
                    var point = new AnalogPoint()
                    {
                        GroupId   = item.eGroupID,
                        Index     = item.u16IndexNumber,
                        TimeStamp = new DateTime(item.sTimeStamp.u16Year, item.sTimeStamp.u8Month, item.sTimeStamp.u8Day, item.sTimeStamp.u8Hour, item.sTimeStamp.u8Minute, item.sTimeStamp.u8Seconds).ToString()
                    };
                    SingleInt32Union value = new SingleInt32Union(Marshal.ReadInt32(item.pvData, 0));
                    point.Value = CheckAnalogValueExp(value.f, point.GroupId, point.Index);
                    list.Add(point);
                }
                else
                {
                    var point = new BinaryPoint()
                    {
                        GroupId   = item.eGroupID,
                        Index     = item.u16IndexNumber,
                        TimeStamp = new DateTime(item.sTimeStamp.u16Year, item.sTimeStamp.u8Month, item.sTimeStamp.u8Day, item.sTimeStamp.u8Hour, item.sTimeStamp.u8Minute, item.sTimeStamp.u8Seconds).ToString()
                    };
                    SingleInt32Union value = new SingleInt32Union(Marshal.ReadInt32(item.pvData, 0));
                    point.Value = CheckBinaryValueExp(value.i, point.GroupId, point.Index);;
                    list.Add(point);
                }
            }
            return(list);
        }
        public AnalogPoint ProccessAnalog(AnalogPoint point)
        {
            point.TimeStamp = DateTime.Now.ToString();
            var newPoint = ProccessAnalogAlarm(point);

            newPoint = ProccessEGUValue(point);
            return(newPoint);
        }
예제 #7
0
        private void PumpRunning(AnalogPoint pumpFlow, AnalogPoint pumpTemp, AnalogPoint tapChanger, AnalogPoint voltage, AnalogPoint current)
        {
            pumpFlow.Value = tapChanger.Value * VoltageFactor * ConstPumpFlow;

            if (fluidLever.Value - pumpFlow.Value >= EmptyTank)
            {
                voltage.Value     = tapChanger.Value * VoltageFactor;
                current.Value     = 1000 / voltage.Value;
                voltage.Value     = tapChanger.Value * VoltageFactor;
                pumpTemp.Value   += HeatingConst * voltage.Value;
                fluidLever.Value -= pumpFlow.Value / 4; // 100/60 = 1,667 / s
            }
        }
예제 #8
0
        public Dictionary <Tuple <RegisterType, int>, BasePoint> Convert(List <PointDbModel> result)
        {
            var dict = new Dictionary <Tuple <RegisterType, int>, BasePoint>();

            foreach (var item in result)
            {
                if (item.RegisterType == RegisterType.ANALOG_INPUT || item.RegisterType == RegisterType.ANALOG_OUTPUT)
                {
                    var point = new AnalogPoint()
                    {
                        Alarm           = item.Alarm,
                        ClassType       = item.ClassType,
                        Direction       = item.Direction,
                        Index           = item.Index,
                        MaxValue        = item.MaxValue,
                        MeasurementType = item.MeasurementType,
                        MinValue        = item.MinValue,
                        NormalValue     = item.NormalValue,
                        Mrid            = item.Mrid,
                        ObjectMrid      = item.ObjectMrid,
                        RegisterType    = item.RegisterType,
                        TimeStamp       = item.TimeStamp,
                        Value           = item.Value
                    };
                    dict.Add(Tuple.Create(point.RegisterType, point.Index), point);
                }
                else
                {
                    var point = new DiscretePoint()
                    {
                        Alarm           = item.Alarm,
                        ClassType       = item.ClassType,
                        Direction       = item.Direction,
                        Index           = item.Index,
                        MaxValue        = (int)item.MaxValue,
                        MeasurementType = item.MeasurementType,
                        MinValue        = (int)item.MinValue,
                        NormalValue     = (int)item.NormalValue,
                        Mrid            = item.Mrid,
                        ObjectMrid      = item.ObjectMrid,
                        RegisterType    = item.RegisterType,
                        TimeStamp       = item.TimeStamp,
                        Value           = (int)item.Value
                    };
                    dict.Add(Tuple.Create(point.RegisterType, point.Index), point);
                }
            }
            return(dict);
        }
예제 #9
0
 public void ProccessAnalogAlarm(AnalogPoint point)
 {
     if (point.Value > point.MaxValue)
     {
         point.Alarm = AlarmType.HIGH_ALARM;
     }
     else if (point.Value < point.MinValue)
     {
         point.Alarm = AlarmType.LOW_ALARM;
     }
     else
     {
         point.Alarm = AlarmType.NO_ALARM;
     }
 }
예제 #10
0
 public AnalogPoint GetKVPoint(string strStationName, string strPointName)
 {
     for (int iPoint = 0; iPoint < this.m_listKVPoint.Count; iPoint++)
     {
         AnalogPoint point = this.m_listKVPoint[iPoint];
         if (point.StationName == strStationName)
         {
             if (point.PointName == strPointName)
             {
                 return(point);
             }
         }
     }
     return(null);
 }
예제 #11
0
        private void ParseAnalogInputObject(byte[] analogInputObject, ushort typeField, ref Dictionary <Tuple <RegisterType, int>, BasePoint> points)
        {
            RegisterType registerType = RegisterType.ANALOG_INPUT;

            int currentIndex  = analogInputObject[3];
            int numberOfitems = analogInputObject[4] - analogInputObject[3] + 1;

            for (int i = 0; i < numberOfitems; i++)
            {
                AnalogPoint point = new AnalogPoint();
                point.Value        = (ushort)BitConverter.ToUInt16(analogInputObject, (5 + i * 2)); //na 5 je prva vrednost, i * 2 idemo short po short
                point.Index        = currentIndex;
                point.RegisterType = registerType;
                points.Add(new Tuple <RegisterType, int>(registerType, currentIndex++), point);
            }
        }
예제 #12
0
        private void GetPointsRightBranch()
        {
            simulator.MarshalUnmananagedArray2Struct(db.psServerDatabasePoint, (int)db.u32TotalPoints, out List <dnp3_protocol.dnp3types.sServerDatabasePoint> points);

            var result = simulator.ConvertToPoints(points);

            foreach (var item in result)
            {
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.BINARY_OUTPUT && item.Index == pairs["Breaker_13Status"])
                {
                    breaker13 = item as BinaryPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.BINARY_OUTPUT && item.Index == pairs["Breaker_23Status"])
                {
                    breaker23 = item as BinaryPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.BINARY_OUTPUT && item.Index == pairs["Discrete_Disc13"])
                {
                    dis13 = item as BinaryPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.BINARY_OUTPUT && item.Index == pairs["Discrete_Disc23"])
                {
                    dis23 = item as BinaryPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.ANALOG_INPUT && item.Index == pairs["Flow_AM3"])
                {
                    pump3flow = item as AnalogPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.ANALOG_INPUT && item.Index == pairs["Temp_AM3"])
                {
                    pump3temp = item as AnalogPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.ANALOG_OUTPUTS && item.Index == pairs["Discrete_Tap3"])
                {
                    tapChanger3 = item as AnalogPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.ANALOG_INPUT && item.Index == pairs["Current_Tap3"])
                {
                    TRCurrent3 = item as AnalogPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.ANALOG_INPUT && item.Index == pairs["Voltage_Tap3"])
                {
                    TRVoltage3 = item as AnalogPoint;
                }
            }
        }
예제 #13
0
        public static AnalogPoint GetLineAnalogCsv(string csvLine)
        {
#if true  //เกิดปัญหากับ Points : "NCO", "51-212,51-222,51-232 CONVERTER FAIL"
            string   csnLineTemp = csvLine.Replace("\"", string.Empty);
            string[] value       = csnLineTemp.Split(new char[] { ',', '"' });
#else
            List <string> iColumn = new List <string>();
            using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(csvLine)))
            {
                using (TextFieldParser parser = new TextFieldParser(ms))
                {
                    parser.Delimiters = new string[] { "," };
                    int iLine = 0;
                    while (true)
                    {
                        string[] parts = parser.ReadFields();
                        if (parts == null)
                        {
                            break;
                        }
                        if (iLine++ == 0)
                        {
                            for (int iCol = 0; iCol < parts.Length; iCol++)
                            {
                                iColumn.Add(parts[iCol]);
                            }
                            continue;
                        }
                    }
                }
            }
#endif
            try
            {
                AnalogPoint localValue = new AnalogPoint(value);
                return(localValue);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(null);
            }
        }
예제 #14
0
        public bool CheckDPZero()
        {
            bool bResult = false;

            if (this.m_bMWLossEnable)
            {
                for (int iPoint = 0; iPoint < this.m_listMWPoint.Count; iPoint++)
                {
                    AnalogPoint point = this.m_listMWPoint[iPoint];
                    if (point.ActualValue != point.PreFaultValue)
                    {
                        if (point.ActualValue < this.m_fMWThreshold)
                        {
                            bResult = true;
                        }
                    }
                }
            }
            return(bResult);
        }
예제 #15
0
        public override Dictionary <Tuple <RegisterType, int>, BasePoint> PareseResponse(byte[] response)
        {
            if (!CrcCalculator.CheckCRC(response))
            {
                return(null);
            }

            byte[] dataObjects = MessagesHelper.GetResponseDataObjects(response);
            var    index       = (ushort)BitConverter.ToUInt16(dataObjects, 5);
            var    value       = BitConverter.ToUInt16(dataObjects, 7);

            Dictionary <Tuple <RegisterType, int>, BasePoint> retVal = new Dictionary <Tuple <RegisterType, int>, BasePoint>();
            AnalogPoint point = new AnalogPoint();

            point.Index        = index;
            point.Value        = value;
            point.RegisterType = RegisterType.ANALOG_OUTPUT;
            retVal.Add(new Tuple <RegisterType, int>(RegisterType.ANALOG_OUTPUT, point.Index), point);

            return(retVal);
        }
예제 #16
0
        public bool CheckVoltageSwing()
        {
            bool bResult = false;

            if (this.m_bVoltageSwingEnable)
            {
                for (int iPoint = 0; iPoint < this.m_listKVPoint.Count; iPoint++)
                {
                    AnalogPoint point = this.m_listKVPoint[iPoint];
                    if (point.Update)
                    {
                        if ((point.AlarmType == (UInt32)AlarmEvent.ANALOG_LOW2) ||
                            (point.AlarmType == (UInt32)AlarmEvent.ANALOG_HIGH2))
                        {
                            bResult = true;
                        }
                    }
                }
            }
            return(bResult);
        }
예제 #17
0
        private void GetPoints()
        {
            simulator.MarshalUnmananagedArray2Struct(db.psServerDatabasePoint, (int)db.u32TotalPoints, out List <dnp3_protocol.dnp3types.sServerDatabasePoint> points);

            var result = simulator.ConvertToPoints(points);

            foreach (var item in result)
            {
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.BINARY_OUTPUT && item.Index == pairs["Breaker_01Status"])
                {
                    breaker01 = item as BinaryPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.BINARY_OUTPUT && item.Index == pairs["Breaker_12Status"])
                {
                    breaker12 = item as BinaryPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.BINARY_OUTPUT && item.Index == pairs["Breaker_22Status"])
                {
                    breaker22 = item as BinaryPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.BINARY_OUTPUT && item.Index == pairs["Discrete_Disc01"])
                {
                    dis01 = item as BinaryPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.BINARY_OUTPUT && item.Index == pairs["Discrete_Disc02"])
                {
                    dis02 = item as BinaryPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.BINARY_OUTPUT && item.Index == pairs["Discrete_Disc12"])
                {
                    dis12 = item as BinaryPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.BINARY_OUTPUT && item.Index == pairs["Discrete_Disc22"])
                {
                    dis22 = item as BinaryPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.ANALOG_INPUT && item.Index == pairs["Flow_AM2"])
                {
                    pump2flow = item as AnalogPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.ANALOG_INPUT && item.Index == pairs["Temp_AM2"])
                {
                    pump2temp = item as AnalogPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.ANALOG_OUTPUTS && item.Index == pairs["Discrete_Tap2"])
                {
                    tapChanger2 = item as AnalogPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.ANALOG_INPUT && item.Index == pairs["Current_Tap2"])
                {
                    TRCurrent2 = item as AnalogPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.ANALOG_INPUT && item.Index == pairs["FluidLevel_Tank"])
                {
                    fluidLever = item as AnalogPoint;
                }
                if (item.GroupId == dnp3_protocol.dnp3types.eDNP3GroupID.ANALOG_INPUT && item.Index == pairs["Voltage_Tap2"])
                {
                    TRVoltage2 = item as AnalogPoint;
                }
            }
        }
예제 #18
0
 public void ProccessEGUValue(AnalogPoint point)
 {
     point.Value = (point.Value * float.Parse(ConfigurationManager.AppSettings["Analog_Scale"])) +
                   float.Parse(ConfigurationManager.AppSettings["Analog_Deviation"]);
 }
예제 #19
0
        public bool ReadAnalogCSV(bool bConfig)
        {
            string strFilePath = this.m_strCsvPath + @"\\10.20.86.210\ExportDB\\Analog.csv";

            if (!System.IO.File.Exists(strFilePath))
            {
                return(false);
            }

            int iLoop = 0;

            using (TextFieldParser parser = new TextFieldParser(strFilePath))
            {
                parser.Delimiters = new string[] { "," };
                while (true)
                {
                    string[] parts = parser.ReadFields();
                    if (parts == null)
                    {
                        break;
                    }
                    if (iLoop++ == 0)
                    {
                        continue;
                    }

                    if (bConfig)
                    {
                        string strShortName = parts[(int)AnalogTableField.SHORTNAME_FIELD].ToString();
                        if (strShortName.IndexOf(this.DPParse) >= 0)
                        {
                            if (strShortName.IndexOf(this.MWParse) >= 0)
                            {
                                AnalogPoint point   = new AnalogPoint(parts);
                                AnalogPoint pointMW = GetMWPoint(point.StationName, point.PointName);
                                if (pointMW != null)
                                {
                                    throw (new Exception());
                                }
                                this.ListMWPoint.Add(point);
                            }
                            else if (strShortName.IndexOf(this.KVParse) >= 0)
                            {
                                AnalogPoint point   = new AnalogPoint(parts);
                                AnalogPoint pointKV = GetKVPoint(point.StationName, point.PointName);
                                if (pointKV != null)
                                {
                                    throw (new Exception());
                                }
                                this.ListKVPoint.Add(point);
                            }
                        }
                    }
                    else
                    {
                        string      strStationName = parts[(int)AnalogTableField.STATIONNAME_FIELD].ToString();
                        string      strPointName   = parts[(int)AnalogTableField.POINTNAME_FIELD].ToString();
                        AnalogPoint pointMW        = GetMWPoint(strStationName, strPointName);
                        if (pointMW != null)
                        {
                            pointMW.UpdateValue(parts);
                        }
                        AnalogPoint pointKV = GetKVPoint(strStationName, strPointName);
                        if (pointKV != null)
                        {
                            pointKV.UpdateValue(parts);
                        }
                    }
                }
                return(false);
            }
        }
예제 #20
0
 public void ProccessAnalog(AnalogPoint point)
 {
     point.TimeStamp = DateTime.Now.ToString();
     ProccessAnalogAlarm(point);
     ProccessEGUValue(point);
 }
 public AnalogPoint ProccessEGUValue(AnalogPoint point)
 {
     point.Value = (point.Value * _scale) + _deviation;
     return(point);
 }
예제 #22
0
        public DNA <float> Start(float currentFluidLevel)
        {
            var scadaProxy = new SF.Common.Proxies.ScadaExportProxy(ConfigurationManager.AppSettings["Scada"]);

            model = scadaProxy.GetData().GetAwaiter().GetResult();

            if (currentFluidLevel == 0 || IsCurrentOptimal(currentFluidLevel))
            {
                var ret = new DNA <float>();
                ret.Genes = new float[] { 0, 0, 0 };
                return(ret);
            }

            foreach (var m in model)
            {
                if (m.Value.Mrid == "Flow_AM2")
                {
                    pump2flow = m.Value as AnalogPoint;
                }
                else if (m.Value.Mrid == "Discrete_Tap2")
                {
                    tapChanger2 = m.Value as AnalogPoint;
                }
                else if (m.Value.Mrid == "FluidLevel_Tank")
                {
                    fluidLevel = m.Value as AnalogPoint;
                }
            }

            if (pump2flow.Value > 0)
            {
                isWorking2 = 0;
            }
            else
            {
                isWorking2 = 1;
            }

            firstGenes = new float[] { isWorking2, pump2flow.Value, 0.1f };

            DNA <float> firstHromozome = new DNA <float>(3, random, GetRandomGene, FitnessFunction, false, true, GetGene);

            hromozomes.Add(firstHromozome);
            ga = new GeneticAlgorithm <float>(1, 3, random, GetRandomGene, FitnessFunction, elitism, mutationRate, hromozomes, GetGene);

            population = ga.Population;

            do
            {
                Update();

                population = ga.Population;

                results = new List <float>();
                for (int i = 0; i < population.Count(); i++)
                {
                    results.Add(currentFluidLevel - FitnessFunction(i));
                }

                List <Tuple <int, float> > potentialSolutions = utils.FindPotentialSolutions(results, workingTimes);

                if (potentialSolutions.Count() > 0)
                {
                    Tuple <int, float> bestSolution = utils.FindBestSolution(potentialSolutions);

                    if (bestSolution.Item2 < lastBestSolution)
                    {
                        lastBestSolution  = bestSolution.Item2;
                        bestSolutionIndex = bestSolution.Item1;
                        bestIndividual    = population[bestSolution.Item1];
                    }
                }

                countIteration++;
                if (countIteration == iterations || utils.IsSolutionCorrect(lastBestSolution, workingTimes[bestSolutionIndex]))
                {
                    break;
                }
            } while (true);

            // bestIndividual send to scada
            return(bestIndividual);
        }
        public DNA <float> Start(float currentFluidLevel)
        {
            model = CeProxyFactory.Instance().ScadaExportProxy().GetData();

            if (currentFluidLevel == 0 || IsCurrentOptimal(currentFluidLevel))
            {
                var ret = new DNA <float>();
                ret.Genes = new float[] { 0, 0, 0, 0, 0, 0 };
                return(ret);
            }

            foreach (var m in model)
            {
                if (m.Value.Mrid == "Flow_AM1")
                {
                    pump1flow = m.Value as AnalogPoint;
                }
                else if (m.Value.Mrid == "Flow_AM2")
                {
                    pump2flow = m.Value as AnalogPoint;
                }
                else if (m.Value.Mrid == "Discrete_Tap1")
                {
                    tapChanger1 = m.Value as AnalogPoint;
                }
                else if (m.Value.Mrid == "Discrete_Tap2")
                {
                    tapChanger2 = m.Value as AnalogPoint;
                }
                else if (m.Value.Mrid == "FluidLevel_Tank")
                {
                    fluidLevel = m.Value as AnalogPoint;
                }
            }

            if (pump1flow.Value > 0)
            {
                isWorking1 = 0;
            }
            else
            {
                isWorking1 = 1;
            }
            if (pump2flow.Value > 0)
            {
                isWorking2 = 0;
            }
            else
            {
                isWorking2 = 1;
            }

            //PRVA GENERACIJA IMA JEDNU JEDINKU

            firstGenes = new float[] { isWorking1, pump1flow.Value, 0.1f,
                                       isWorking2, pump2flow.Value, 0.1f };

            DNA <float> firstHromozome = new DNA <float>(6, random, GetRandomGene, FitnessFunction, false, true, GetGene);

            hromozomes.Add(firstHromozome);
            ga = new GeneticAlgorithm <float>(1, 6, random, GetRandomGene, FitnessFunction, elitism, mutationRate, hromozomes, GetGene);

            population = ga.Population;

            do
            {
                Update();

                population = ga.Population;

                results = new List <float>();
                for (int i = 0; i < population.Count(); i++)
                {
                    results.Add(currentFluidLevel - FitnessFunction(i));
                }

                List <Tuple <int, float> > potentialSolutions = utils.FindPotentialSolutions(results, workingTimes);

                if (potentialSolutions.Count() > 0)
                {
                    Tuple <int, float> bestSolution = utils.FindBestSolution(potentialSolutions);

                    if (bestSolution.Item2 < lastBestSolution)
                    {
                        lastBestSolution  = bestSolution.Item2;
                        bestSolutionIndex = bestSolution.Item1;
                        bestIndividual    = population[bestSolution.Item1];
                    }
                }

                countIteration++;
                if (countIteration == iterations || utils.IsSolutionCorrect(lastBestSolution, workingTimes[bestSolutionIndex]))
                {
                    break;
                }
            } while (true);

            lastBestSolution = float.MaxValue;
            return(bestIndividual);
        }
예제 #24
0
        private Dictionary <Tuple <RegisterType, int>, BasePoint> ConvertPoints(Container analogContainer, Container discreteContainer)
        {
            Dictionary <Tuple <RegisterType, int>, BasePoint> points = new Dictionary <Tuple <RegisterType, int>, BasePoint>();

            foreach (var item in analogContainer.Entities.Values)
            {
                if (item is Analog)
                {
                    Analog analog = item as Analog;

                    AnalogPoint analogPoint = new AnalogPoint()
                    {
                        ClassType       = ClassType.CLASS_1,
                        Direction       = analog.Direction,
                        RegisterType    = GetRegistryType(analog.Direction),
                        Index           = GetIndex(GetRegistryType(analog.Direction)),
                        MaxValue        = analog.MaxValue,
                        MinValue        = analog.MinValue,
                        MeasurementType = analog.MeasurementType,
                        Mrid            = analog.MRID,
                        NormalValue     = analog.NormalValue,
                        ObjectMrid      = analog.ObjectMRID ?? null,
                        TimeStamp       = String.Empty,
                        Value           = 0,
                        Alarm           = AlarmType.NO_ALARM
                    };
                    points.Add(new Tuple <RegisterType, int>(analogPoint.RegisterType, analogPoint.Index), analogPoint);
                }
            }

            foreach (var item in discreteContainer.Entities.Values)
            {
                if (item is Discrete)
                {
                    Discrete discrete = item as Discrete;
                    if (discrete.MRID.Contains("Discrete_Tap"))
                    {
                        AnalogPoint discretePoint = new AnalogPoint()
                        {
                            ClassType       = ClassType.CLASS_2,
                            Direction       = discrete.Direction,
                            RegisterType    = RegisterType.ANALOG_OUTPUT,
                            Index           = GetIndex(RegisterType.ANALOG_OUTPUT),
                            MaxValue        = discrete.MaxValue,
                            MinValue        = discrete.MinValue,
                            MeasurementType = discrete.MeasurementType,
                            Mrid            = discrete.MRID,
                            NormalValue     = discrete.NormalValue,
                            ObjectMrid      = discrete.ObjectMRID ?? null,
                            TimeStamp       = String.Empty,
                            Value           = discrete.NormalValue,
                            Alarm           = AlarmType.NO_ALARM
                        };
                        points.Add(new Tuple <RegisterType, int>(discretePoint.RegisterType, discretePoint.Index), discretePoint);
                    }
                    else
                    {
                        DiscretePoint discretePoint = new DiscretePoint()
                        {
                            ClassType       = ClassType.CLASS_2,
                            Direction       = discrete.Direction,
                            RegisterType    = GetRegistryType(discrete.Direction, true),
                            Index           = GetIndex(GetRegistryType(discrete.Direction, true)),
                            MaxValue        = discrete.MaxValue,
                            MinValue        = discrete.MinValue,
                            MeasurementType = discrete.MeasurementType,
                            Mrid            = discrete.MRID,
                            NormalValue     = discrete.NormalValue,
                            ObjectMrid      = discrete.ObjectMRID ?? null,
                            TimeStamp       = String.Empty,
                            Value           = discrete.NormalValue,
                            Alarm           = AlarmType.NO_ALARM
                        };
                        points.Add(new Tuple <RegisterType, int>(discretePoint.RegisterType, discretePoint.Index), discretePoint);
                    }
                }
            }

            return(points);
        }