【C#】 24. HybridLocalSearchSolver + 自定义函数调用 + AddConstraint (constraint===1)
2015-04-18 21:56
513 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SolverFoundation.Services; using Microsoft.SolverFoundation.Solvers; namespace ConsoleApplication1 { public static class SF { public static void Main() { IndividualSolvers(); //TheAbstractWay(); Console.ReadLine(); } private static double MinimizeThat(double[] args) { return Math.Cos(args[0]) + args[1] * args[1]; } private static double Cons(double[] args) { return(Math.Abs (args[0] + args[1])); } private static void IndividualSolvers() { var hls = new HybridLocalSearchSolver(); int x, y,z; hls.AddVariable(out x, 0, 5, false); hls.AddVariable(out y,-1, 1,true); z = hls.CreateNaryFunction(Cons, new[] { x, y }); hls.AddConstraint(z); //constraint z =1 hls.AddGoal(hls.CreateNaryFunction(MinimizeThat, new[] { x, y })); var hlsr = hls.Solve(new HybridLocalSearchParameters()); Console.WriteLine("HLS: f({0}, {1}) = {2}", hlsr.GetValue(x),hlsr.GetValue(y), hlsr.GetSolutionValue(0)); Console.WriteLine("Constraint x+y = {0}",hlsr.GetValue(z)); //var nms = NelderMeadSolver.Solve(MinimizeThat, new[] { 0.0, 0.0 }, new[] { 0.0, -1.0 }, new[] { 5.0, 1.0 }); //Console.WriteLine("NMS: f({0}, {1}) = {2}", nms.GetValue(1), nms.GetValue(2), nms.GetSolutionValue(0)); } private static void TheAbstractWay() { var ctx = new SolverContext(); var m = ctx.CreateModel(); var x = new Decision(Domain.Real, "x"); var y = new Decision(Domain.Real, "y"); m.AddDecisions(x, y); m.AddConstraints("Bounds", 0 <= x, x <= 5, -1 <= y, y <= 1); m.AddGoal("Minimize", GoalKind.Minimize, "Cos[x]+y^2"); //m.AddGoal("Minimize", GoalKind.Minimize, Model.Cos(x) + y * y); var r = ctx.Solve(); ctx.PropagateDecisions(); Console.WriteLine("{3}: f({0}, {1}) = {2}", x.GetDouble(), y.GetDouble(), r.Goals.Single().ToDouble(), r.GetReport().SolverType.Name); } } }
相关文章推荐
- csharp(C#) 调用SQL自定义函数返回值
- sql自定义函数及C#中调用
- C# winform中自定义用户控件 然后在页面中调用用户控件的事件
- c#继承中的函数调用实例
- Makefile中自定义函数的调用
- net中前台javascript与后台c#函数相互调用问题
- c#外部调用类里边的函数
- 使用call_user_func调用自定义函数
- C#与DLL和COM的混合编程(1)-C#调用C++写的非托管的DLL中导出的函数
- 在.net C#里怎样调用非托管动态库函数dll?
- 使用C#调用存储过程,用函数合理组织代码,使程序更加的清晰(示例)
- c#调用ORACLE 存储过程和函数的简单样例
- c# 多线程 调用带参数函数
- C#调用windows api 函数GetShortPathName
- ECShop 自定义函数以及调用
- 在分支循环结构中调用自定义函数
- SQL调用正则表达式自定义函数(转载:CSDN)
- 关于在C#中构造函数中调用虚函数的问题
- C# 调用Dll中非托管C++代码时,函数参数的类型对照
- 自定义序列化4 (MFC调用C#的.dll)