实例介绍
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
using CreateContour;
namespace ContourGui
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//double DeJongX_max = 6, DeJongX_min = -6;
//double RosenbrockX_max = 2, RosenbrockX_min = -2;
//double RastriginX_max = 4, RastriginX_min = -4;
//double SchwefelX_max = 100, SchwefelX_min = -100;
//double GriewangkX_max = 10, GriewangkX_min = -10;
//double MichalewiczX_max = 4, MichalewiczX_min = 0;
//double EasomX_max = 20, EasomX_min = -20;
//double DropWaveX_max = 1, DropWaveX_min = -1;
//double SchubertX_max = 4, SchubertX_min = -4;
if (MaxBoxX.Text.Equals(""))
MaxBoxX.Text = "100";
if (MinBoxX.Text.Equals(""))
MinBoxX.Text = "-100";
if (MaxBoxY.Text.Equals(""))
MaxBoxY.Text = "100";
if (MinBoxY.Text.Equals(""))
MinBoxY.Text = "-100";
double maxX_v = double.Parse(MaxBoxX.Text);
double minX_v = double.Parse(MinBoxX.Text);
double maxY_v = double.Parse(MaxBoxY.Text);
double minY_v = double.Parse(MinBoxY.Text);
if (maxX_v == minX_v)
maxX_v = minX_v 100;
if (maxX_v < minX_v)
{
double temp = maxX_v;
maxX_v = minX_v;
minX_v = temp;
}
if (maxY_v == minY_v)
maxY_v = minY_v 100;
if (maxY_v < minY_v)
{
double temp = maxY_v;
maxY_v = minY_v;
minY_v = temp;
}
Contour this_contour = new Contour(maxX_v, minX_v, maxY_v, minY_v, 500, 500);
//De Jong 1
//Rosenbrock's Valley
//Rastrigin's Function
//Schwefel's Function
//Griewangk's Function
//Michalewicz's Function
//Easom's Function
//Drop Wave Function
//Schubert's Function
//User Entered
switch(defaultFunctions.SelectedIndex)
{
case 0:
this_contour.create_contour(Program.DeJong);
break;
case 1:
this_contour.create_contour(Program.Rosenbrock);
break;
case 2:
this_contour.create_contour(Program.Rastrigin);
break;
case 3:
this_contour.create_contour(Program.Schwefel);
break;
case 4:
this_contour.create_contour(Program.Griewangk);
break;
case 5:
this_contour.create_contour(Program.Michalewicz);
break;
case 6:
this_contour.create_contour(Program.Easom);
break;
case 7:
this_contour.create_contour(Program.DropWave);
break;
case 8:
this_contour.create_contour(Program.Schubert);
break;
case 9:
CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
string SourceString = SourceCode.Text;
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, SourceString);
string error_string = "";
if (results.Errors.Count > 0)
{
foreach (CompilerError CompErr in results.Errors)
{
error_string = String.Format("{0} \n Line number " CompErr.Line ", Error Number: " CompErr.ErrorNumber ", '" CompErr.ErrorText ";", error_string);
}
ErrorText.Text = error_string;
return;
}
Assembly mAssembly = results.CompiledAssembly;
#region error checking
MethodInfo[] user_meth = mAssembly.GetType("userclass").GetMethods(BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static);
if (user_meth.Length != 1)
{
ErrorText.Text = "Either no class: \"userclass\" or no functions were defined.";
return;
}
string func_name = user_meth[0].Name;
ParameterInfo[] param = user_meth[0].GetParameters();
if (param.Length != 2)
{
ErrorText.Text = "Function has an incorrect number of parameters: Should be 2.";
return;
}
if (param[0].ParameterType != Type.GetType("System.Double") || param[1].ParameterType != Type.GetType("System.Double"))
{
ErrorText.Text = "Function requires parameters of type double.";
return;
}
if (user_meth[0].ReturnType != Type.GetType("System.Double"))
{
ErrorText.Text = "Function returns an incorrect data type: Should be double.";
return;
}
if (user_meth[0].ReturnType != Type.GetType("System.Double"))
{
ErrorText.Text = "Function returns an incorrect data type: Should be double.";
return;
}
#endregion
Type script = mAssembly.GetType("userclass");
this_contour.create_contour(ref script, ref func_name);
break;
default:
ErrorText.Text = "Nothing Selected.";
break;
}
ContourBox.Image = this_contour.contour_bmp;
}
}
}
标签: 等值线
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论