文本操作的底层

下面是一个简单的操作文本的底层,通过自定义存储格式,读取遵循定义好的协议,从而对数据进行操作。通过将文本数据转化为类返回给用户。

直接上代码:

 /// <summary>
    /// 文本阅读器
    /// </summary>
    public class TxtReader
    {
        private static string path = System.AppDomain.CurrentDomain.BaseDirectory + "Data\\datafile.txt";

        public static string Path
        {
            get { return path; }
            set { path = value; }
        }
        /// <summary>
        /// 从文本取数据,单条
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str_LineData"></param>
        /// <returns></returns>
        public static T AddData<T>(string str_LineData)
            where T:new()
        {
            string[] str_Data = str_LineData.Split(new string[]{"-|-"},StringSplitOptions.RemoveEmptyEntries);
            T model = new T();
            for (int i = 0; i < str_Data.Length; i++)
            {
                string[] property = str_Data[i].Split(new string[]{"||"},StringSplitOptions.RemoveEmptyEntries);
                PropertyInfo propertyInfo = model.GetType().GetProperty(property[0]);
                if (propertyInfo != null)
                    propertyInfo.SetValue(model, GetDecodstr(property[1]), null);
            }
            return model;
        }
        /// <summary>
        /// 从文本取数据,批量
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str_MultiData"></param>
        /// <returns></returns>
        public static IList<T> AddList<T>(string str_MultiData)
             where T:new()
        {
            IList<T> _List=new List<T>();
            string[] str_Data = str_MultiData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach(var item in str_Data)
            {
                T info = AddData<T>(item);
                _List.Add(info);
            }
            return _List;
        }
        /// <summary>
        /// 取数据,批量
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static IList<T> GetData<T>()
            where T:new()
        {
            string str_Data = ReadStr();
            return AddList<T>(str_Data);
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        public static void Add<T>(T obj)
            where T:new()
        {
            string str = ConvetToEncodeStr<T>(obj);
           SaveStr(str);
        }
        /// <summary>
        /// 对数据进行编码
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str_LineData"></param>
        /// <returns></returns>
        public static T GetEncodeData<T>(string str_LineData)
            where T : new()
        {
            string[] str_Data = str_LineData.Split(new string[] { "-|-" }, StringSplitOptions.RemoveEmptyEntries);
            T model = new T();
            for (int i = 0; i < str_Data.Length; i++)
            {
                string[] property = str_Data[i].Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                PropertyInfo propertyInfo = model.GetType().GetProperty(property[0]);
                if (propertyInfo != null)
                    propertyInfo.SetValue(model, GetEncodStr(property[1]), null);
            }
            return model;
        }
        /// <summary>
        /// 将实体进行编码,返回字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static string ConvetToEncodeStr<T>(T obj)
            where T:new()
        {
            PropertyInfo[] propertyInfos = obj.GetType().GetProperties();
            string result = string.Empty;
            foreach (var item in propertyInfos)
            {
                result += item.Name + "||" + GetEncodStr(item.GetValue(obj, null).ToStr()) + "-|-";
            }
            return result;
        }
        /// <summary>
        /// 将实体进行编码,返回实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static T ConvetToEncode<T>(T obj)
            where T : new()
        {
            PropertyInfo[] propertyInfos = obj.GetType().GetProperties();
            foreach (var item in propertyInfos)
            {
                item.SetValue(obj, item.GetValue(obj,null), null);
            }
            return obj;
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Id"></param>
        /// <param name="FiledName"></param>
        public static void Delete<T>(string Id,string FiledName)
            where T:new()
        {
            string result = string.Empty;
            using (StreamReader sr = new StreamReader(Path, System.Text.ASCIIEncoding.UTF8))
            {
                string str = null;
                while ((str = sr.ReadLine()) != null)
                {
                    if (str.Length > 0)
                    {
                        T _t = AddData<T>(str);
                        PropertyInfo pinfo = _t.GetType().GetProperty(FiledName);
                        if (pinfo != null)
                        {
                            object ibj = pinfo.GetValue(_t, null);
                            if (!Id.Equals(ibj.ToStr()))
                            {
                                result += ConvetToEncodeStr<T>(_t)+"\r\n";
                            }
                        }
                    }
                }
            }
            _SaveStr(result);
        }
        private static string GetEncodStr(string _str)
        {
            return Convert.ToBase64String(System.Text.ASCIIEncoding.UTF8.GetBytes(_str)).ToStr();
        }
        private static string GetDecodstr(string str)
        {
            return System.Text.ASCIIEncoding.UTF8.GetString(Convert.FromBase64CharArray(str.ToArray(), 0, str.Length)).ToStr();
        }
        private static string ReadStr()
        {
            using (StreamReader sr = new StreamReader(Path, System.Text.ASCIIEncoding.UTF8))
            {
                return sr.ReadToEnd();
            }
        }
        private static void SaveStr(string str)
        {
            using (StreamWriter sw = new StreamWriter(Path, true, System.Text.ASCIIEncoding.UTF8))
            {
                sw.WriteLine(str);
            }
        }
        private static void _SaveStr(string str)
        {
            using (StreamWriter sw = new StreamWriter(Path, false, System.Text.ASCIIEncoding.UTF8))
            {
                sw.WriteLine(str);
            }
        }
    }


内容概要:本文提出了一种基于非合作博弈理论的居民负荷分层调度模型,并结合双层鲸鱼优化算法(Two-level Whale Optimization Algorithm)进行高效求解,模型与算法均通过Matlab代码实现。研究针对电力系统中居民侧用电负荷的复杂调度问题,引入非合作博弈机制刻画各用户之间的利益竞争关系,实现负荷的分层优化分配;同时设计双层优化架构,上层优化资源配置,下层模拟用户自主决策行为,提升了模型的实用性与合理性。通过智能优化算法求解多层级、非凸非线性的博弈模型,有效提高了调度方案的收敛性与全局寻优能力,适用于现代智能电网中的需求侧管理与能源优化场景。; 适合人群:具备电力系统基础理论知识和Matlab编程能力,从事智能电网、能源优化调度、需求侧管理、博弈论应用等方向的科研人员、高校研究生及工程技术人员。; 使用场景及目标:①应用于居民区电力负荷的分层优化调度系统设计与仿真分析;②为非合作博弈在多主体能源系统建模中的应用提供方法论支持;③利用双层鲸鱼算法解决具有嵌套结构的复杂双层优化问题,提升求解效率与调度方案的可行性。; 阅读建议:建议读者结合提供的Matlab代码深入理解模型构建逻辑与算法实现流程,重点关注博弈模型的效用函数设计、纳什均衡求解思路以及双层优化结构的迭代机制,宜配合实际用电数据开展复现实验以验证模型有效性与鲁棒性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值