GIS开发进阶之路(十三) Activator和new的区别、GP工具输入参数问题、写入JObject到json文件、ArcGIS Server REST API、动态规划、贪心算法

  1. Activator和new的区别

    需要动态的创建一个实例模型的时候,就用Activator.CreateInstance(Type type);如果是明确的知道要创建哪个实例的模型,就可以用 new

    C#在类工厂中动态创建类的实例,所使用的方法为:

    1. Activator.CreateInstance (Type)

    2. Activator.CreateInstance (Type, Object[])
      两种方法区别仅为:创建无参数的构造方法和创建有参数的构造函数。

    动态创建时,可能会动态使用到外部应用的DLL中类的实例,则此时需要进行反编译操作,使用Reflection命名控件下的Assembly类。

    New 关键字实例化一个类
    New 关键字用于创建对象和调用构造函数。是实例化一个类最常见的方式。

Activator 实例化一个类
Activator 用以在本地或从远程创建对象类型,或获取对现有远程对象的引用。其 CreateInstance 方法创建在程序集中定义的类型的实例。

  1. GP工具输入参数问题

    目前遇到的多要素输入主要有两种方式

    方式一:使用IGpValueTableObject

    IGpValueTableObject gpValueTableObject = new GpValueTableObjectClass();
                gpValueTableObject.SetColumns(1);
                object o1 = featureClass1;
                object o2 = featureClass2;
    intersect.in_features = intersect_GPValueTableObject;
    

    方式二:使用’;'作为分隔符输入

    string inputParam_1 = gdbpath + @"\" + name;
    string inputParam_2 = gdbpath + @"\" + name;
    gp.in_features = inputParam_1+';'+inputParam_2;
    
  2. 写入JObject到json文件

    //设置将写入文件的全名(带路径)
    fp = ConfigUtil.GetConfigValue(configPath, "ResultPath") + @"\" + "台账_" + xzqType + resultName + ".json";
    //初始化一个IO流以创建文件
    System.IO.FileStream fs1 = new System.IO.FileStream(fp, FileMode.Create, FileAccess.ReadWrite);
    //设置JSON写入流的参数
    Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                    serializer.Converters.Add(new Newtonsoft.Json.Converters.JavaScriptDateTimeConverter());
                    serializer.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
                    serializer.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto;
                    serializer.Formatting = Newtonsoft.Json.Formatting.Indented;
    //开始写入
    using (StreamWriter sw = new StreamWriter(fp))
    using (Newtonsoft.Json.JsonWriter writer = new Newtonsoft.Json.JsonTextWriter(sw))
          {
             serializer.Serialize(writer, res, typeof(List<LandStatisticsXZQ>));
          }
    
  3. ArcGIS Server REST API

    URLhttp://<layer-url>/query
    Parent ResourceLayer / Table
    Required CapabilityData

    重要的几个主要参数:

    参数说明
    Format:html、json、kmz、amf格式可选,决定了返回数据的格式
    Where:筛选条件,语法类似于SQL,默认是1=1
    Out Fields:输出的字段,默认是*
    Return IDs Only:默认false,如果改为true则返回所有的objectid方便分块查询
    Return Count Only:默认false,如果改为true则返回总数
  4. 动态规划(Dynamic Programming)

    动态规划算法通常基于一个递推公式及一个或多个初始状态。当前子问题的解将由上一次子问题的解推出。

    动态规划和分治法相似,都是通过组合子问题的解来求解原问题。分治法将问题划分成互不相交的子问题,递归求解子问题,再将他们的解组合起来,求出原问题的解。与之相反,动态规划应用于子问题重叠的情况,即不同的子问题具有公共的子子问题。在这种情况下,分治算法会做出许多不必要的工作,它会反复的求解那些公共子问题。而动态规划算法对每个子子问题只求解一次,将结果保存到表格中,从而无需每次求解一个子子问题都要重新计算。

    第一种方法是 带备忘的自顶向下法

    此方法依然是按照自然的递归形式编写过程,但过程中会保存每个子问题的解(通常保存在一个数组中)。当需要计算一个子问题的解时,过程首先检查是否已经保存过此解。如果是,则直接返回保存的值,从而节省了计算时间;如果没有保存过此解,按照正常方式计算这个子问题。我们称这个递归过程是带备忘的。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace 钢条切割问题_带备忘的自顶向下法_动态规划_
    {
        class Program
        {
            static void Main(string[] args)
            {
                //int n = 5;//我们要切割售卖的钢条的长度
                int[] result = new int[11];
                int[] p = { 0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30 };//索引代表 钢条的长度,值代表价格
                Console.WriteLine(UpDown(0, p, result));
                Console.WriteLine(UpDown(1, p, result));
                Console.WriteLine(UpDown(2, p, result));
                Console.WriteLine(UpDown(3, p, result));
                Console.WriteLine(UpDown(4, p, result));
                Console.WriteLine(UpDown(5, p, result));
                Console.WriteLine(UpDown(6, p, result));
                Console.WriteLine(UpDown(7, p, result));
                Console.WriteLine(UpDown(8, p, result));
                Console.WriteLine(UpDown(9, p, result));
                Console.WriteLine(UpDown(10, p, result));
                Console.ReadKey();
     
            }
     
            //带备忘的自顶向下
            public static int UpDown(int n, int[] p, int[] result)//求得长度为n的最大收益
            {
                if (n == 0) return 0;
                if (result[n] != 0)
                {
                    return result[n];
                }
                int tempMaxPrice = 0;
                for (int i = 1; i < n + 1; i++)
                {
                    int maxPrice = p[i] + UpDown(n - i, p, result);
                    if (maxPrice > tempMaxPrice)
                        tempMaxPrice = maxPrice;
                }
                result[n] = tempMaxPrice;
                return tempMaxPrice;
            }
        }
    }
    

    第二种方法是 自底向上法

    首先恰当的定义子问题的规模,使得任何问题的求解都只依赖于更小的子问题的解。因而我们将子问题按照规模排序,按从小到大的顺序求解。当求解某个问题的时候,它所依赖的更小的子问题都已经求解完毕,结果已经保存。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace 钢条切割问题_自底向上法_动态规划算法_
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] result = new int[11];
                int[] p = { 0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30 };//索引代表 钢条的长度,值代表价格
     
                Console.WriteLine(BottomUp(1, p, result));
                Console.WriteLine(BottomUp(2, p, result));
                Console.WriteLine(BottomUp(3, p, result));
                Console.WriteLine(BottomUp(4, p, result));
                Console.WriteLine(BottomUp(5, p, result));
                Console.WriteLine(BottomUp(6, p, result));
                Console.WriteLine(BottomUp(7, p, result));
                Console.WriteLine(BottomUp(8, p, result));
                Console.WriteLine(BottomUp(9, p, result));
                Console.WriteLine(BottomUp(10, p, result));
                Console.ReadKey();
            }
     
            public static int BottomUp(int n, int[] p, int[] result)
            {
                for (int i = 1; i < n + 1; i++)
                {
                    //下面取得 钢条长度为i时最大的收益
                    int tempMaxPrice = -1;
                    for(int j = 1; j <= i; j++)
                    {
                        int maxPrice = p[j] + result[i - j];
                        if (maxPrice > tempMaxPrice)
                            tempMaxPrice = maxPrice;
                    }
                    result[i] = tempMaxPrice;
                }
                return result[n];
            }
        }
    }
    
  5. 贪心算法

    对于许多最优化问题,使用动态规划算法来求最优解有些杀鸡用牛刀了,可以使用更加简单、更加高效的算法。贪心算法就是这样的算法,它在每一步做出当时看起来最佳的选择。也就是说它总是做出局部最优的选择,从而得到全局最优解。

    对于某些问题并不保证得到最0优解,但对很多问题确实可以求得最优解。

    对于任何非空的活动集合S,假如am是S中结束时间最早的活动,则am一定在S的某个最大兼容活动子集中

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace 活动选择问题_贪心算法_递归解决_
    {
        class Program
        {
     
            static int[] s = { 0, 1, 3, 0, 5, 3, 5, 6, 8, 8, 2, 12 };
            static int[] f = { 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
     
            public static List<int> ActivitySelection(int startActivityNumber, int endActivityNumber, int startTime, int endTime) 
            {
                if (startActivityNumber > endActivityNumber || startTime >= endTime)
                    return new List<int>();
                //找到结束时间最早的活动
                int tempNumber = 0;
                for (int number = startActivityNumber; number <= endActivityNumber; number++)
                {
                    if(s[number]>=startTime && f[number] <= endTime)
                    {
                        tempNumber = number;
                        break;
                    }
                }
                List<int> list = ActivitySelection(tempNumber + 1, endActivityNumber, f[tempNumber], endTime);
                list.Add(tempNumber);
                return list;
            }
     
            static void Main(string[] args)
            {
                List<int> list = ActivitySelection(1, 11, 0, 24);
                foreach (int temp in list)
                {
                    Console.WriteLine(temp);
                }
                Console.ReadKey();
            }
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值