Exemplo n.º 1
0
        internal MethodResults CompileFunction(string code, int dims)
        {
            code = FillCode(code, dims);

            code = "return (float) ( " + code + ");";

            StringBuilder source = new StringBuilder();

            source.Append("public float OptFunction(float[] x, float t)");
            source.Append(Environment.NewLine);
            source.Append("{");
            source.Append(Environment.NewLine);
            source.Append(code);
            source.Append(Environment.NewLine);
            source.Append("}");

            try
            {
                return(Eval.CreateVirtualMethod(
                           new CSharpCodeProvider().CreateCompiler(),
                           source.ToString(),
                           "OptFunction",
                           new CSharpLanguage(),
                           false,
                           "System.dll",
                           "System.Drawing.dll"));
            }
            catch (CompilationException ce)
            {
                MessageBox.Show(this, "Compilation Errors: " + Environment.NewLine + ce.ToString());
            }

            return(null);
        }
Exemplo n.º 2
0
        internal MethodResults CompileConstraints(List <string> constraints, int dims)
        {
            string code = "return ";

            foreach (string eq in constraints)
            {
                code += "(" + FillCode(eq, dims) + ") &&";
            }

            // if no constraints then constraints always met (true)
            if (constraints.Count == 0)
            {
                code += "true";
            }
            else
            {
                code = code.TrimEnd('&', ' ');
            }

            code += ";";

            StringBuilder source = new StringBuilder();

            source.Append("public bool MeetsConstraint(float[] x, float t)");
            source.Append(Environment.NewLine);
            source.Append("{");
            source.Append(Environment.NewLine);
            source.Append(code);
            source.Append(Environment.NewLine);
            source.Append("}");

            try
            {
                return(Eval.CreateVirtualMethod(
                           new CSharpCodeProvider().CreateCompiler(),
                           source.ToString(),
                           "MeetsConstraint",
                           new CSharpLanguage(),
                           false,
                           "System.dll",
                           "System.Drawing.dll"));
            }
            catch (CompilationException ce)
            {
                MessageBox.Show(this, "Compilation Errors: " + Environment.NewLine + ce.ToString());
            }

            return(null);
        }