public InvokeMethodExpression()
        {
            //OkToRun = true;
            TestName = "Invoking a method using Expression";

            m_memberInfo = GetType().GetMethod("SomeOperation", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public);
            if (m_memberInfo == null)
            {
                throw new InvalidProgramException("Cannot find SomeOperation method");
            }

            try
            {
                var exObjParam   = Expression.Parameter(typeof(InvokeMethodBase), "ThisObject");
                var exValueParam = Expression.Parameter(typeof(long), "ValueToAssign");

                var exCall = Expression.Call(exObjParam, m_memberInfo, exValueParam);

                var ex = Expression.Lambda <TFunc>(exCall, exObjParam, exValueParam);

                m_caller = ex.Compile();
                m_caller(this, 0);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex}");
                throw;
            }
        }
Esempio n. 2
0
 public void AddFuncEvent1(object sender, TFunc func)
 {
     ListViewItem itm1 = new ListViewItem();
     itm1.Text = func.FName;
     itm1.Tag = func;
     listView3.Items.Add(itm1);
     if (func.type == 2)//Export
     {
         ListViewItem itm = new ListViewItem();
         itm.Text = func.FName;
         itm.Tag = func;
         itm.SubItems.Add(func.Addr.ToString("X8"));
         itm.SubItems.Add(func.Ordinal.ToString("X"));
         listView1.Items.Add(itm);
     }
     if (func.type == 3)//Import
     {
         ListViewItem itm = new ListViewItem();
         itm.Text = func.Addr.ToString("X8");
         itm.Tag = func;
         itm.SubItems.Add(func.Ordinal.ToString("X"));
         itm.SubItems.Add(func.FName);
         itm.SubItems.Add(func.LibraryName);
         listView2.Items.Add(itm);
     }
 }
Esempio n. 3
0
 public void AddFuncEvent1(object sender, TFunc func)
 {
     ListViewItem itm1 = new ListViewItem();
     itm1.Text = func.FName;
     itm1.Tag = func;
     listView3.Items.Add(itm1);
 }
Esempio n. 4
0
 public TProc()
 {
     Operation = TOprtn.None;
     Function  = TFunc.None;
     Lop_Res   = new TPNumber();
     Rop       = new TPNumber();
 }
        static public bool Exec <TTrans>(object it, TFunc <TTrans> func, bool needAllTrue)
            where TTrans : class, IDataTransactionBasic
        {
            if (it == null)
            {
                return(needAllTrue);
            }

            TTrans dataTransaction = it as TTrans;

            if (dataTransaction != null)
            {
                return(func(dataTransaction));
            }
            IList list = it as IList;

            if (list == null)
            {
                return(needAllTrue);
            }

            foreach (object item in list)
            {
                //needAllTrue=true, 只要有一個是false就return false,default是return true
                //needAllTrue=false,只要有一個是true就return true,default是return false
                if (DataTransactionHelper.Exec(item, func, needAllTrue) != needAllTrue)
                {
                    return(!needAllTrue);
                }
            }
            return(needAllTrue);
        }
Esempio n. 6
0
        public override AST.Node ToAST(Env env)
        {
            /// A return statement with an expression shall not appear in a function whose return type is void.
            /// A return statement without an expression shall only appear in a function whose return type is void.
            TFunc  f           = env.GetFuncType();
            string returnLabel = env.GetReturnLabel();

            if (expr != null)
            {
                if (f.ret.IsVoid)
                {
                    throw new Error(Pos, "a return statement with an expression shall not appear a function whose return type is void");
                }
                AST.Expr e = expr.ToASTExpr(env).ValueTransform();
                if (!Assign.SimpleAssignable(f.ret, e))
                {
                    throw new ETypeError(Pos, string.Format("cannot assign {0} to {1}", e.Type, f.ret));
                }
                return(new AST.Return(returnLabel, e.ImplicitConvert(f.ret)));
            }
            else
            {
                if (!f.ret.IsVoid)
                {
                    throw new Error(Pos, "a return statement without an expression shall only appear in a function whose return type is void");
                }
                return(new AST.Return(returnLabel, null));
            }
        }
Esempio n. 7
0
        private void Funclist_DoubleClick(object sender, EventArgs e)
        {
            string sss  = "";
            TFunc  Func = (TFunc)listView3.SelectedItems[0].Tag;

            Console.WriteLine(Func.Addr.ToString("X8"));
        }
Esempio n. 8
0
 public FuncScope(string name, string returnLabel, TFunc type) : base(ScopeKind.FUNC)
 {
     labels           = new Dictionary <string, Tuple <string, Scope> >();
     this.name        = name;
     this.returnLabel = returnLabel;
     this.type        = type;
 }
Esempio n. 9
0
 public void call(SprotoProtocolBase sp, TFunc func)
 {
     sessionMap [session] = func;
     byte[] tmp = client.Request(sp, session);
     sm.send(tmp);
     session++;
 }
Esempio n. 10
0
        public void AddFuncEvent1(object sender, TFunc func)
        {
            ListViewItem itm1 = new ListViewItem();

            itm1.Text = func.FName;
            itm1.Tag  = func;
            listView3.Items.Add(itm1);
        }
Esempio n. 11
0
        private void fastColoredTextBox1_DoubleClick(object sender, EventArgs e)
        {
            TFunc f = GetSelectedFunction();

            if (f != null)
            {
                //foreach(Line L in fastColoredTextBox1.Lines)
                //Console.WriteLine(hoveredWord);
            }
        }
Esempio n. 12
0
        private void fastColoredTextBox1_Click(object sender, EventArgs e)
        {
            TFunc f = GetSelectedFunction();

            if (f != null)
            {
                renameToolStripMenuItem1.Text = f.FName;
            }
            renameToolStripMenuItem1.Enabled = f != null;
        }
Esempio n. 13
0
 public void OnFuncChanged1(object sender, TFunc Func)
 {
     foreach(ListViewItem itm in listView3.Items)
         if ((itm.Tag as TFunc).Equals(Func))
         {
             fastColoredTextBox1.BeginUpdate();
             fastColoredTextBox1.Text = fastColoredTextBox1.Text.Replace(itm.Text, Func.FName);
             fastColoredTextBox1.EndUpdate();
             itm.Text = Func.FName;
         }
 }
Esempio n. 14
0
 public void OnFuncChanged1(object sender, TFunc Func)
 {
     foreach (ListViewItem itm in listView3.Items)
     {
         if ((itm.Tag as TFunc).Equals(Func))
         {
             fastColoredTextBox1.BeginUpdate();
             fastColoredTextBox1.Text = fastColoredTextBox1.Text.Replace(itm.Text, Func.FName);
             fastColoredTextBox1.EndUpdate();
             itm.Text = Func.FName;
         }
     }
 }
Esempio n. 15
0
 /// <summary>
 /// Push a function scope
 /// </summary>
 /// <param name="type"></param>
 public void PushFuncScope(string name, TFunc type, IEnumerable <Tuple <string, T> > parameters, Position pos)
 {
     // Clear the dynamic id.
     dynamicId = 0;
     scopes.Push(new FuncScope(name, string.Format("__{0}_return", name), type));
     ASTEnv = new AST.Env();
     /// Add all the parameter to the environment.
     foreach (var p in parameters)
     {
         scopes.Peek().AddSymbol(new EObj(dynamicId.ToString(), p.Item1, p.Item2, pos, SymbolEntry.Link.NONE, EObj.Storage.AUTO));
         ASTEnv.AddParam(dynamicId.ToString(), p.Item1, p.Item2);
         dynamicId++;
     }
 }
Esempio n. 16
0
        private void renameToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            TFunc f = GetSelectedFunction();

            if (f != null)
            {
                Form2 Fm = new Form2();
                Fm.NName = f.FName;
                if (Fm.ShowDialog() == DialogResult.OK)
                {
                    MyGNIDA.RenameFunction(f, Fm.NName);
                }
            }
        }
Esempio n. 17
0
        private static string AddProc(ulong x, MyDictionary ProcList, Dictionary <ulong, TFunc> NewSubs)
        {
            if (ProcList.ContainsKey(x))
            {
                return(ProcList[x].FName + "();");
            }
            TFunc tmpfunc = new TFunc(x, 1);

            if (!NewSubs.ContainsKey(x))
            {
                NewSubs.Add(x, tmpfunc);
            }
            return("proc_" + x.ToString("X8") + "();");
        }
Esempio n. 18
0
        public Lazy(TFunc <T> valueFactory, bool isThreadSafe)
        {
            if (valueFactory == null)
            {
                throw new ArgumentNullException(nameof(valueFactory));
            }
            //Requires.NotNull(valueFactory, "valueFactory");//This is changed by me!
            if (isThreadSafe)
            {
                this._lock = new object();
            }

            this._valueFactory = valueFactory;
        }
Esempio n. 19
0
 public FuncDef(
     string name,
     string returnLabel,
     TFunc type,
     IEnumerable <Tuple <string, T> > parameters,
     CompoundStmt body,
     Env env,
     bool isGlobal
     )
 {
     this.name        = name;
     this.returnLabel = returnLabel;
     this.type        = type;
     this.parameters  = parameters;
     this.body        = body;
     this.env         = env;
     this.isGlobal    = isGlobal;
 }
Esempio n. 20
0
 public Lazy(TFunc <T> valueFactory) :
     this(valueFactory, true)
 {
 }
Esempio n. 21
0
        public void LoadFile(string FName)
        {
            byte[] sf_prefixes = new byte[Dasmer.MAX_INSTRUCTION_LEN];
            IInstruction instr1;// = new mediana.INSTRUCTION();
            DISASM_INOUT_PARAMS param = new DISASM_INOUT_PARAMS();

            RaiseLogEvent(this, "Loading " + FName);
            IntPtr tmp = assembly.LoadFile(FName);
            //MeDisasm = new medi.mediana(assembly);
            int i = 0;
            foreach (Section1 sect in assembly.Sections())
            {
                RaiseLogEvent(this, i.ToString() + ". Creating a new segment " + sect.RVA.ToString("X8") + " - " + (sect.RVA + sect.VirtualSize).ToString("X8") + "... ... OK");
                i++;
            }

            TFunc fnc = new TFunc(assembly.ImageBase() + assembly.Entrypoint(), 0, 0, "main");

            param.arch = Dasmer.ARCH_ALL;
            param.sf_prefixes = sf_prefixes;
            param.mode = DISMODE.DISASSEMBLE_MODE_32;
            param.options = (byte)(Dasmer.DISASM_OPTION_APPLY_REL | Dasmer.DISASM_OPTION_OPTIMIZE_DISP);
            param.bas = assembly.ImageBase();
            MeDisasm.disassemble(RVA2FO(fnc.Addr), out instr1, ref param);
            Console.WriteLine(instr1.mnemonic);
            //MeDisasm.medi_dump(instr, buff, OUT_BUFF_SIZE, DUMP_OPTION_IMM_UHEX | DUMP_OPTION_DISP_HEX);
            FullProcList.AddFunc(fnc);
            foreach (ExportMethod1 func in assembly.LibraryExports())
            {

                TFunc tmpfunc = new TFunc((uint)assembly.ImageBase() + (uint)func.RVA, 2, (uint)func.Ordinal, func.Name);
                FullProcList.AddFunc(tmpfunc);
            }
            foreach (LibraryReference1 lib in assembly.LibraryImports())
            {
                foreach (ImportMethods1 func in lib.ImportMethods)
                {
                    TFunc tmpfunc = new TFunc((uint)assembly.ImageBase() + func.RVA, 3, func.Ordinal, func.Name, lib.LibraryName);
                    FullProcList.AddFunc(tmpfunc);
                }
            }
            bw.WorkerSupportsCancellation = true;
            bw.WorkerReportsProgress = false;
            bw.DoWork += bw_DoWork;
            bw.RunWorkerCompleted += bw_RunWorkerCompleted;
            bw.RunWorkerAsync();
        }
Esempio n. 22
0
 public void RaiseAddFuncEvent(object sender, TFunc Func)
 {
     if (OnAddFunc != null) OnAddFunc(sender, Func);
 }
Esempio n. 23
0
        static void Get_Functions_details(string F, out int dim, out TFunc fobj)
        {
            fobj = F1;
            dim  = 75;
            switch (F)
            {
            case "F1":
                break;

            case "F2":
                fobj = F2;
                dim  = 75;
                break;

            case "F3":
                fobj = F3;
                dim  = 75;
                break;

            case "F4":
                fobj = F4;
                dim  = 75;
                break;

            case "F5":
                fobj = F5;
                dim  = 75;
                break;

            case "F6":
                fobj = F6;
                dim  = 75;
                break;

            case "F7":
                fobj = F7;
                dim  = 75;
                break;

            case "F8":
                fobj = F8;
                dim  = 75;
                break;

            case "F9":
                fobj = F9;
                dim  = 75;
                break;

            case "F10":
                fobj = F10;
                dim  = 75;
                break;

            case "F11":
                fobj = F11;
                dim  = 75;
                break;

            case "F12":
                fobj = F12;
                dim  = 75;
                break;

            case "F13":
                fobj = F13;
                dim  = 75;
                break;
            }
        }
Esempio n. 24
0
 public int RenameFunction(TFunc f, string NName)
 {
     if (f != null) { f.FName = NName; RaiseFuncChanged(this, f); return 1; }
     return 0;
 }
Esempio n. 25
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <param name="propertyFilter">ex:  delegate(PropertyInfo it){ return it.Name!="IsReadOnly"; }</param>
        /// <param name="isClone"></param>
        public static void CopyProperties(object source, object target, TFunc <PropertyInfo, bool> propertyFilter, bool isClone)
        {
            Type sourceType = source.GetType();
            Type targetType = target.GetType();

            if (sourceType == targetType)
            {
                foreach (PropertyInfo field in sourceType.GetProperties())
                {
                    if (propertyFilter != null && !propertyFilter(field))
                    {
                        continue;
                    }
#if !SILVERLIGHT
                    if (!field.IsDefined(typeof(NonSerializedAttribute), false))
#endif
                    {
                        if (field.CanRead && field.CanWrite)
                        {
                            object value = field.GetValue(source, null);
                            if (value == null)
                            {
                                field.SetValue(target, null, null);
                            }
                            else
                            {
                                field.SetValue(target, (isClone) ? Clone(value) : value, null);
                            }
                        }
                    }
                }
                return;
            }
            PropertyInfo[] sourceFields = sourceType.GetProperties();
            foreach (PropertyInfo sourceField in sourceFields)
            {
                //991125 by sherlock 不同type也應該要支援filter
                if (propertyFilter != null && !propertyFilter(sourceField))
                {
                    continue;
                }
#if !SILVERLIGHT
                if (!sourceField.IsDefined(typeof(NonSerializedAttribute), false))
#endif
                {
                    PropertyInfo targetField = targetType.GetProperty(sourceField.Name);

                    if (targetField == null)
                    {
                        continue;
                    }
                    if (!targetField.PropertyType.IsAssignableFrom(sourceField.PropertyType))
                    {
                        continue;
                    }

                    if (targetField != null)
                    {
                        if (sourceField.CanRead && targetField.CanWrite)
                        {
                            object value = sourceField.GetValue(source, null);
                            if (value == null)
                            {
                                targetField.SetValue(target, (isClone) ? Clone(value) : value, null);
                            }
                            else
                            {
                                if (targetField.PropertyType != value.GetType() &&
                                    (IsNullableType(targetField.PropertyType) && GetNonNullableType(targetField.PropertyType) != value.GetType()))
                                {
                                    continue;
                                }
                                targetField.SetValue(target, (isClone) ? Clone(value) : value, null);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 26
0
 private static string AddProc(ulong x, MyDictionary ProcList, Dictionary<ulong, TFunc> NewSubs)
 {
     if (ProcList.ContainsKey(x)) return ProcList[x].FName + "();";
     TFunc tmpfunc = new TFunc(x, 1);
     if (!NewSubs.ContainsKey(x)) NewSubs.Add(x, tmpfunc);
     return "proc_" + x.ToString("X8") + "();";
 }
Esempio n. 27
0
        static void Get_Functions_details(string F, out double lb1, out double ub1, out int dim, out TFunc fobj)
        {
            fobj = F1;
            lb1  = -100;
            ub1  = 100;
            dim  = 10;
            switch (F)
            {
            case "F1":       //Sphere
                break;

            case "F2":       //Schwefel 2.22
                fobj = F2;
                lb1  = -10;
                ub1  = 10;
                dim  = 10;
                break;

            case "F3":       //Schwefel 1.2
                fobj = F3;
                lb1  = -100;
                ub1  = 100;
                dim  = 10;
                break;

            case "F4":       //Schwefel 2.21
                fobj = F4;
                lb1  = -100;
                ub1  = 100;
                dim  = 10;
                break;

            case "F5":       //Rosenbrock
                fobj = F5;
                lb1  = -30;
                ub1  = 30;
                dim  = 10;
                break;

            case "F6":       //Step
                fobj = F6;
                lb1  = -100;
                ub1  = 100;
                dim  = 10;
                break;

            case "F7":       //Quartic
                fobj = F7;
                lb1  = -1.28;
                ub1  = 1.28;
                dim  = 10;
                break;

            case "F8":       //Schwefel
                fobj = F8;
                lb1  = -500;
                ub1  = 500;
                dim  = 10;
                break;

            case "F9":       //Rastrigin
                fobj = F9;
                lb1  = -5.12;
                ub1  = 5.12;
                dim  = 10;
                break;

            case "F10":       //Ackley
                fobj = F10;
                lb1  = -32;
                ub1  = 32;
                dim  = 10;
                break;

            case "F11":       //Griewank
                fobj = F11;
                lb1  = -600;
                ub1  = 600;
                dim  = 10;
                break;

            case "F12":       //Penalized 1
                fobj = F12;
                lb1  = -50;
                ub1  = 50;
                dim  = 10;
                break;

            case "F13":       //Penalized 2
                fobj = F13;
                lb1  = -50;
                ub1  = 50;
                dim  = 10;
                break;
            }
        }
Esempio n. 28
0
        static void DA(int SearchAgents_no, int Max_iteration, out double[] lb, out double[] ub, int dim, TFunc fobj, double lb1, double ub1, out double[] Best_p, ref double Best_s)
        {
            int i, j; lb = new double[dim]; ub = new double[dim]; Best_p = new double[dim];

            for (i = 0; i < dim; i++)
            {
                lb[i] = lb1; ub[i] = ub1;
            }
            double[] r = new double[dim], Delta_max = new double[dim];
            for (i = 0; i < dim; i++)
            {
                r[i]         = (ub[i] - lb[i]) / 10.0;
                Delta_max[i] = (ub[i] - lb[i]) / 10.0; // The initial radius of gragonflies' neighbourhoods
            }
            double Food_fitness, Enemy_fitness;

            double[] Food_pos = new double[dim], Enemy_pos = new double[dim];
            Food_fitness = Double.PositiveInfinity; Enemy_fitness = Double.NegativeInfinity;
            double[][] X = new double[SearchAgents_no][]; double[][] DeltaX = new double[SearchAgents_no][];
            for (i = 0; i < SearchAgents_no; i++)
            {
                X[i]      = new double[dim];
                DeltaX[i] = new double[dim];
            }
            initialization(SearchAgents_no, dim, lb, ub, ref X);
            double[] Fitness = new double[SearchAgents_no];
            initialization(SearchAgents_no, dim, lb, ub, ref DeltaX);
            int iter; double my_c, w;

            for (iter = 1; iter <= Max_iteration; iter++)
            {
                for (i = 0; i < dim; i++)
                {
                    r[i] = (ub[i] - lb[i]) / 2.0 + ((ub[i] - lb[i]) * ((double)iter / (double)Max_iteration) * 2);
                }
                w    = 0.9 - iter * (0.15 / (double)Max_iteration);
                my_c = 0.1 - iter * (0.1 / ((double)Max_iteration / 2.0));
                if (my_c < 0)
                {
                    my_c = 0;
                }
                double s, a, c, f, e;
                s = 2 * rnd11.NextDouble() * my_c;    // Seperation weight
                a = 2 * rnd11.NextDouble() * my_c;    // Alignment weight
                c = 2 * rnd11.NextDouble() * my_c;    // Cohesion weight
                f = 2 * rnd11.NextDouble();           // Food attraction weight
                e = my_c;                             // Enemy distraction weight
                for (i = 0; i < SearchAgents_no; i++) //Calculate all the objective values
                {
                    Fitness[i] = fobj(X[i]);
                    if (Fitness[i] < Food_fitness)
                    {
                        Food_fitness = Fitness[i];
                        for (j = 0; j < dim; j++)
                        {
                            Food_pos[j] = X[i][j];
                        }
                    }
                    bool q = true;
                    if (Fitness[i] > Enemy_fitness)
                    {
                        for (j = 0; j < dim; j++)
                        {
                            if ((X[i][j] > ub[j]) || (X[i][j] < lb[j]))
                            {
                                q = false; break;
                            }
                        }
                        if (q == true)
                        {
                            Enemy_fitness = Fitness[i];
                            for (j = 0; j < dim; j++)
                            {
                                Enemy_pos[j] = X[i][j];
                            }
                        }
                    }
                }
                for (i = 0; i < SearchAgents_no; i++)
                {
                    double     Dist2Enemy = 0;
                    double     Dist2Food = 0;
                    int        index = 0; int neighbours_no = 0;
                    double[][] Neighbours_DeltaX = new double[SearchAgents_no][]; //clear?
                    double[][] Neighbours_X      = new double[SearchAgents_no][];
                    //find the neighbouring solutions
                    for (j = 0; j < SearchAgents_no; j++)
                    {
                        distance(X[i], X[j], ref Dist2Enemy);
                        bool p = true; for (int k = 0; k < dim; k++)
                        {
                            if ((Dist2Enemy > r[k]) || (Dist2Enemy == 0))
                            {
                                p = false; break;
                            }
                        }
                        if (p == true)
                        {
                            index = index + 1; neighbours_no = neighbours_no + 1;
                            Neighbours_DeltaX[index - 1] = new double[dim];
                            Neighbours_X[index - 1]      = new double[dim];
                            for (int k = 0; k < dim; k++)
                            {
                                Neighbours_DeltaX[index - 1][k] = DeltaX[j][k];
                                Neighbours_X[index - 1][k]      = X[j][k];
                            }
                        }
                    }
                    // Seperation Eq. (2.1)
                    double[] S = new double[dim];
                    if (neighbours_no >= 1)
                    {
                        for (int k = 0; k < neighbours_no; k++)
                        {
                            for (int ka = 0; ka < dim; ka++)
                            {
                                S[ka] = S[ka] + (Neighbours_X[k][ka] - X[i][ka]);  // mistakable?
                            }
                        }
                        for (int k = 0; k < dim; k++)
                        {
                            S[k] = -1 * S[k];
                        }
                    }
                    else
                    {
                        Array.Clear(S, 0, dim);
                    }
                    // Alignment Eq. (2.2)
                    double[] A = new double[dim];
                    if (neighbours_no > 1)
                    {
                        for (int k1 = 0; k1 < dim; k1++)
                        {
                            for (int k2 = 0; k2 < neighbours_no; k2++)
                            {
                                A[k1] = A[k1] + Neighbours_DeltaX[k2][k1];
                            }
                            A[k1] = A[k1] / (double)neighbours_no;
                        }
                    }
                    else
                    {
                        for (int k1 = 0; k1 < dim; k1++)
                        {
                            A[k1] = DeltaX[i][k1];
                        }
                    }
                    // Cohesion Eq. (2.3)
                    double[] C_temp = new double[dim];
                    if (neighbours_no >= 1)
                    {
                        for (int k1 = 0; k1 < dim; k1++)
                        {
                            for (int k2 = 0; k2 < neighbours_no; k2++)
                            {
                                C_temp[k1] = C_temp[k1] + Neighbours_X[k2][k1];
                            }
                            C_temp[k1] = C_temp[k1] / (double)neighbours_no;
                        }
                    }
                    else
                    {
                        for (int k1 = 0; k1 < dim; k1++)
                        {
                            C_temp[k1] = X[i][k1];
                        }
                    }
                    double[] C = new double[dim];
                    for (int k = 0; k < dim; k++)
                    {
                        C[k] = C_temp[k] - X[i][k];
                    }
                    // Attraction to food Eq. (2.4)
                    distance(X[i], Food_pos, ref Dist2Food);
                    double[] F  = new double[dim];
                    bool     q1 = true; for (int k = 0; k < dim; k++)
                    {
                        if (Dist2Food > r[k])
                        {
                            q1 = false; break;
                        }
                    }
                    if (q1 == true)
                    {
                        for (int k = 0; k < dim; k++)
                        {
                            F[k] = Food_pos[k] - X[i][k];
                        }
                    }
                    else
                    {
                        Array.Clear(F, 0, dim);
                    }
                    // Distraction from enemy Eq. (3.5)
                    distance(X[i], Enemy_pos, ref Dist2Enemy);
                    double[] Enemy = new double[dim];
                    bool     q2    = true; for (int k = 0; k < dim; k++)
                    {
                        if (Dist2Enemy > r[k])
                        {
                            q2 = false; break;
                        }
                    }
                    if (q2 == true)
                    {
                        for (int k = 0; k < dim; k++)
                        {
                            Enemy[k] = Enemy_pos[k] + X[i][k];
                        }
                    }
                    else
                    {
                        Array.Clear(Enemy, 0, dim);
                    }
                    for (int tt = 0; tt < dim; ++tt)
                    {
                        if (X[i][tt] > ub[tt])
                        {
                            X[i][tt]      = lb[tt];
                            DeltaX[i][tt] = rnd11.NextDouble();
                        }
                        if (X[i][tt] < lb[tt])
                        {
                            X[i][tt]      = ub[tt];
                            DeltaX[i][tt] = rnd11.NextDouble();
                        }
                    }
                    q1 = true; for (int k = 0; k < dim; k++)
                    {
                        if (Dist2Food > r[k])
                        {
                            q1 = false; break;
                        }
                    }
                    if (q1 == false)
                    {
                        if (neighbours_no >= 1)
                        {
                            double r1, r2, r3;
                            r1 = rnd11.NextDouble();
                            r2 = rnd11.NextDouble();
                            r3 = rnd11.NextDouble();
                            for (j = 0; j < dim; j++)
                            {
                                DeltaX[i][j] = w * DeltaX[i][j] + r1 * S[j] + r2 * A[j] + r3 * C[j];
                                if (DeltaX[i][j] > Delta_max[j])
                                {
                                    DeltaX[i][j] = Delta_max[j];
                                }
                                if (DeltaX[i][j] < (-1 * Delta_max[j]))
                                {
                                    DeltaX[i][j] = (-1 * Delta_max[j]);
                                }
                                X[i][j] = X[i][j] + DeltaX[i][j];
                            }
                        }
                        else
                        {
                            // Eq. (2.8)
                            double[] Ly = new double[dim]; Ly = Levy(dim);
                            for (j = 0; j < dim; j++)
                            {
                                X[i][j]      = X[i][j] + Ly[j] * X[i][j];
                                DeltaX[i][j] = 0;
                            }
                        }
                    }
                    else
                    {
                        for (j = 0; j < dim; j++)
                        {
                            // Eq. (2.6)
                            DeltaX[i][j] = (s * S[j] + a * A[j] + c * C[j] + f * F[j] + e * Enemy[j]) + w * DeltaX[i][j];
                            if (DeltaX[i][j] > Delta_max[j])
                            {
                                DeltaX[i][j] = Delta_max[j];
                            }
                            if (DeltaX[i][j] < (-1 * Delta_max[j]))
                            {
                                DeltaX[i][j] = -1 * Delta_max[j];
                            }
                            X[i][j] = X[i][j] + DeltaX[i][j];
                        }
                    }
                    for (j = 0; j < dim; j++)
                    {
                        if (X[i][j] > ub[j])
                        {
                            X[i][j] = ub[j];
                        }
                        if (X[i][j] < lb[j])
                        {
                            X[i][j] = lb[j];
                        }
                    }
                }
                Best_s         = Food_fitness;
                Aver[iter - 1] = Aver[iter - 1] + Food_fitness;
                Best_p         = Food_pos;
            }
        }
 public C1(TFunc arg)
 {
     this.arg = arg;
 }
Esempio n. 30
0
 public void RaiseFuncChanged(object sender, TFunc Func)
 {
     if (OnFuncChanged != null) OnFuncChanged(sender, Func);
 }
Esempio n. 31
0
        public override AST.Node ToAST(Env env)
        {
            T   baseType = specifiers.GetT(env);
            var result   = declarator.Declare(env, baseType);

            if (declarations != null)
            {
                throw new Error(Pos, "sorry we donot support old-style function definition");
            }

            /// The identifier declared in a function definition (which is the name of the function) shall
            /// have a function type
            if (!result.Item2.IsFunc)
            {
                throw new ETypeError(Pos, "function definition should have function type");
            }

            /// The storage-class specifier, if any, shall be either extern or static.
            if (specifiers.storage != StoreSpec.Kind.EXTERN &&
                specifiers.storage != StoreSpec.Kind.NONE &&
                specifiers.storage != StoreSpec.Kind.STATIC)
            {
                throw new Error(Pos, string.Format("illegal storage specifier {0} in function definition", specifiers.storage));
            }

            /// Determine the linkage.
            var link = specifiers.storage == StoreSpec.Kind.STATIC ? SymbolEntry.Link.INTERNAL : SymbolEntry.Link.EXTERNAL;

            var entry = env.GetSymbol(result.Item1);

            if (entry == null)
            {
                /// This identifier has not been used.
                env.AddFunc(result.Item1, result.Item2, link, Pos);
            }
            else
            {
                /// This identifier has been declared.
                /// Check if it's a function type.
                if (!entry.type.Equals(result.Item2))
                {
                    throw new ERedefineSymbolTypeConflict(Pos, result.Item1, entry.type, result.Item2);
                }

                /// Check if the function has already been defined.
                if (entry.type.IsDefined)
                {
                    throw new ERedefineFunction(Pos, result.Item1);
                }

                /// Define the function.
                entry.type.DefFunc();
            }

            TFunc type = result.Item2.nake as TFunc;

            /// Push a new scope.
            env.PushFuncScope(result.Item1, type, result.Item3, Pos);

            /// Semantic check the function body.
            AST.CompoundStmt b = body.ToASTCompoundStmt(env);

            string returnLabel = env.GetReturnLabel();

            env.PopScope();

            return(new AST.FuncDef(
                       result.Item1,
                       returnLabel,
                       type,
                       result.Item3,
                       b,
                       env.ASTEnv,
                       specifiers.storage != StoreSpec.Kind.STATIC
                       ));
        }
Esempio n. 32
0
        static void BDA(int SearchAgents_no, int Max_iteration, int dim, TFunc fobj, out double[] Best_p, ref double Best_s)
        {
            int    i, j; Best_p = new double[dim];
            double Food_fitness, Enemy_fitness;

            double[] Food_pos = new double[dim], Enemy_pos = new double[dim];
            Food_fitness = Double.PositiveInfinity; Enemy_fitness = Double.NegativeInfinity;
            double[][] X = new double[SearchAgents_no][]; double[][] DeltaX = new double[SearchAgents_no][];
            for (i = 0; i < SearchAgents_no; i++)
            {
                X[i]      = new double[dim];
                DeltaX[i] = new double[dim];
            }
            initialization(SearchAgents_no, dim, ref X);
            double[] Fitness = new double[SearchAgents_no];
            initialization(SearchAgents_no, dim, ref DeltaX);
            int iter; double my_c, w;

            for (iter = 1; iter <= Max_iteration; iter++)
            {
                w    = 0.9 - iter * (0.5 / (double)Max_iteration);
                my_c = 0.1 - iter * (0.1 / ((double)Max_iteration / 2.0));
                if (my_c < 0)
                {
                    my_c = 0;
                }
                double s, a, c, f, e;
                s = 2 * rnd11.NextDouble() * my_c; // Seperation weight
                a = 2 * rnd11.NextDouble() * my_c; // Alignment weight
                c = 2 * rnd11.NextDouble() * my_c; // Cohesion weight
                f = 2 * rnd11.NextDouble();        // Food attraction weight
                e = my_c;                          // Enemy distraction weight
                if (iter > (3.0 * Max_iteration / 4.0))
                {
                    e = 0;
                }
                for (i = 0; i < SearchAgents_no; i++) //Calculate all the objective values
                {
                    Fitness[i] = fobj(X[i]);
                    if (Fitness[i] < Food_fitness)
                    {
                        Food_fitness = Fitness[i];
                        for (j = 0; j < dim; j++)
                        {
                            Food_pos[j] = X[i][j];
                        }
                    }
                    if (Fitness[i] > Enemy_fitness)
                    {
                        Enemy_fitness = Fitness[i];
                        for (j = 0; j < dim; j++)
                        {
                            Enemy_pos[j] = X[i][j];
                        }
                    }
                }
                for (i = 0; i < SearchAgents_no; i++)
                {
                    int        index = 0; int neighbours_no = 0;
                    double[][] Neighbours_DeltaX = new double[SearchAgents_no][]; //clear?
                    double[][] Neighbours_X      = new double[SearchAgents_no][];
                    //Find the neighbouring solutions (all the dragonflies are assumed as a group in binary seach spaces)
                    for (j = 0; j < SearchAgents_no; j++)
                    {
                        if (i != j)
                        {
                            index         = index + 1;
                            neighbours_no = neighbours_no + 1;
                            Neighbours_DeltaX[index - 1] = new double[dim];
                            Neighbours_X[index - 1]      = new double[dim];
                            for (int k = 0; k < dim; k++)
                            {
                                Neighbours_DeltaX[index - 1][k] = DeltaX[j][k];
                                Neighbours_X[index - 1][k]      = X[j][k];
                            }
                        }
                    }
                    // Seperation Eq. (2.1)
                    double[] S = new double[dim];
                    for (int k = 0; k < neighbours_no; k++)
                    {
                        for (int ka = 0; ka < dim; ka++)
                        {
                            S[ka] = S[ka] + (Neighbours_X[k][ka] - X[i][ka]);      // mistakable?
                        }
                    }
                    for (int k = 0; k < dim; k++)
                    {
                        S[k] = -1 * S[k];
                    }

                    // Alignment Eq. (2.2)
                    double[] A = new double[dim];
                    for (int k1 = 0; k1 < dim; k1++)
                    {
                        for (int k2 = 0; k2 < neighbours_no; k2++)
                        {
                            A[k1] = A[k1] + Neighbours_DeltaX[k2][k1];
                        }
                        A[k1] = A[k1] / (double)neighbours_no;
                    }

                    // Cohesion Eq. (2.3)
                    double[] C_temp = new double[dim];
                    for (int k1 = 0; k1 < dim; k1++)
                    {
                        for (int k2 = 0; k2 < neighbours_no; k2++)
                        {
                            C_temp[k1] = C_temp[k1] + Neighbours_X[k2][k1];
                        }
                        C_temp[k1] = C_temp[k1] / (double)neighbours_no;
                    }
                    double[] C = new double[dim];
                    for (int k = 0; k < dim; k++)
                    {
                        C[k] = C_temp[k] - X[i][k];
                    }

                    // Attraction to food Eq. (2.4)
                    double[] F = new double[dim];
                    for (int k = 0; k < dim; k++)
                    {
                        F[k] = Food_pos[k] - X[i][k];
                    }

                    // Distraction from enemy Eq. (3.5)
                    double[] Enemy = new double[dim];
                    for (int k = 0; k < dim; k++)
                    {
                        Enemy[k] = Enemy_pos[k] + X[i][k];
                    }

                    for (j = 0; j < dim; j++)
                    {
                        // Eq. (2.6)
                        DeltaX[i][j] = (s * S[j] + a * A[j] + c * C[j] + f * F[j] + e * Enemy[j]) + w * DeltaX[i][j];
                        if (DeltaX[i][j] > 6)
                        {
                            DeltaX[i][j] = 6;
                        }
                        if (DeltaX[i][j] < -6)
                        {
                            DeltaX[i][j] = -6;
                        }
                        //Transfer function  Eq. (2.11)
                        double T, r2;
                        T = Math.Abs(Erf((Math.Sqrt(Math.PI) / 2.0) * DeltaX[i][j]));
                        //T = Math.Abs(Math.Tanh(DeltaX[i][j]));
                        //T = Math.Abs(DeltaX[i][j] / Math.Sqrt(1 + Math.Pow(DeltaX[i][j], 2)));
                        //T = Math.Abs((2.0/Math.PI)*Math.Atan((Math.PI/2.0)*DeltaX[i][j]));
                        //T = Sigmoid(DeltaX[i][j], 1.0);
                        //Eq. (3.12)
                        r2 = rnd11.NextDouble();
                        if (r2 < T)
                        {
                            if (X[i][j] == 0)
                            {
                                X[i][j] = 1;
                            }
                            else
                            {
                                X[i][j] = 0;
                            }
                        }
                        //if (r2 < T) X[i][j] = 0; else X[i][j] = 1;
                    }
                }
                Best_s         = Food_fitness;
                Aver[iter - 1] = Aver[iter - 1] + Food_fitness;
                Best_p         = Food_pos;
            }
        }
Esempio n. 33
0
 public void WriteFunction(TFunc Func)
 {
     Function = Func;
 }
Esempio n. 34
0
 public void FunctionReset()
 {
     Function = TFunc.None;
 }