Пример #1
0
        private string GetFuncionZDisplay(FunctionZ functionZ, string tipoFuncion)
        {
            string funcionZDisplay = tipoFuncion.ToUpper() + "(Z)= ";

            for (int i = 0; i < functionZ.variables.Length; i++)
            {
                funcionZDisplay += functionZ.variables[i] + "X" + (i + 1) + "+";
            }
            if (functionZ.c > 0)
            {
                funcionZDisplay += functionZ.c;
            }
            else
            {
                funcionZDisplay = funcionZDisplay.Substring(0, funcionZDisplay.Length - 1);
            }

            return(funcionZDisplay);
        }
Пример #2
0
        private FunctionZ GetFunctionZ()
        {
            try
            {
                float[] variablesFuncion = new float[nroVariables];
                for (int i = 0; i < nroVariables; i++)
                {
                    variablesFuncion[i] = float.Parse(dgvFuncionZ.Rows[0].Cells[i].Value.ToString());
                }
                float c = float.Parse(dgvFuncionZ.Rows[0].Cells[nroVariables].Value.ToString());

                FunctionZ functionZ = new FunctionZ(variablesFuncion, c);

                return(functionZ);
            }
            catch (Exception e)
            {
                throw new Exception("Se han Ingresado datos erróneos al ingresar los valores de la Funcion Objetivo");
            }
        }
Пример #3
0
        private void Simular(string tipoFuncion, int cantIteraciones, int mostrarDesde, int cantAMostrar, Restricciones[] restricciones, FunctionZ funcionZ, GestorEstadistico numerosAleatorios)
        {
            var progressBar = (ProgressBar)Controls.Find("progressbar", true).FirstOrDefault();

            progressBar.Value   = 0;
            progressBar.Maximum = cantIteraciones;

            float[] variablesOptimas = new float[funcionZ.variables.Length];
            float   zOptima          = 0;

            txtFuncionZDisplay.Text      = GetFuncionZDisplay(funcionZ, tipoFuncion);
            txtRestriccionesDisplay.Text = GetRestrincionesDisplay(restricciones);

            dgvResultados.DataSource = new ManejadorSimulacion().Simular(tipoFuncion, cantIteraciones, mostrarDesde, cantAMostrar, restricciones, funcionZ, gestor, progressBar, ref variablesOptimas, ref zOptima);

            dgvResultados.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            dgvResultados.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvResultados.ColumnHeadersDefaultCellStyle.Font      = new Font(dgvResultados.Font, FontStyle.Bold);
            dgvResultados.ColumnHeadersDefaultCellStyle.BackColor = Color.LightGray;
            dgvResultados.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            tabParametros.SelectTab(tabResult);
            Cursor.Current           = Cursors.Default;
            txtVariablesOptimas.Text = GetVariablesOptimasDisplay(variablesOptimas);
            txtZOptima.Text          = "Z(X)= " + zOptima.ToString();
        }
        public DataTable Simular(string tipoFuncion, int cantIteraciones, int mostrarDesde, int cantAMostrar, Restricciones[] restricciones, FunctionZ functionZ, GestorEstadistico numerosAleatorios, ProgressBar pb, ref float[] variablesOptimas, ref float zOptima)
        {
            DataTable tabla        = new DataTable();
            int       mostrarHasta = mostrarDesde + cantAMostrar;

            //NRO ITERACION
            tabla.Columns.Add("#");
            //VARIABLES
            for (int i = 1; i <= functionZ.variables.Length; i++)
            {
                tabla.Columns.Add("RND X" + i);
                tabla.Columns.Add("X" + i);
            }

            //RESTRICCIONES
            for (int i = 1; i <= restricciones.Length; i++)
            {
                tabla.Columns.Add("R" + i);
                tabla.Columns.Add("VÁLIDO R" + i);
            }
            //FUNCION Z
            tabla.Columns.Add("Z");
            //FUNCION Z OPTIMA
            tabla.Columns.Add("Z OPT");
            //VARIABLES OPTIMAS
            for (int i = 1; i <= functionZ.variables.Length; i++)
            {
                tabla.Columns.Add("X" + i + " OPT");
            }

            var random = numerosAleatorios.Distribucion;

            string[] vector     = new string[tabla.Columns.Count];
            var      porcAvance = 0;

            for (int j = 1; j <= cantIteraciones; j++)

            {
                //NUMERO ITERACION
                vector[0] = j.ToString();

                //CALCULO VARIABLES
                var  randomVariables = new float[functionZ.variables.Length];
                var  posicionVector  = 1;
                bool variable0       = false;
                for (int i = 0; i < randomVariables.Length; i++)
                {
                    float rndvariable = random.Generar();
                    if (rndvariable < 0)
                    {
                        variable0 = true;
                    }
                    randomVariables[i]     = (float)Math.Round(rndvariable, 4);
                    vector[posicionVector] = rndvariable.ToString();
                    posicionVector++;
                    vector[posicionVector] = randomVariables[i].ToString();
                    posicionVector++;
                }
                if (!variable0)
                {
                    ////CALCULO RESTRICIONES
                    var restriccionesresult = new List <string>();
                    for (int i = 0; i < restricciones.Length; i++)
                    {
                        float restriccionTotal = 0;
                        for (int h = 0; h < restricciones[i].variables.Length; h++)
                        {
                            restriccionTotal += randomVariables[h] * restricciones[i].variables[h];
                        }
                        vector[posicionVector] = Math.Round(restriccionTotal, 4).ToString();
                        posicionVector++;
                        var restriccValid = GetValidacionRestriccion(restriccionTotal, restricciones[i]);
                        restriccionesresult.Add(restriccValid);
                        vector[posicionVector] = restriccValid;
                        posicionVector++;
                    }

                    //FUNCTION Z
                    var isValid = restriccionesresult.FirstOrDefault(x => x == "NO");
                    if (isValid == null)
                    {
                        float funcionZResult = 0;
                        for (int i = 0; i < randomVariables.Length; i++)
                        {
                            funcionZResult += (randomVariables[i] * functionZ.variables[i]);
                        }

                        funcionZResult        += functionZ.c;
                        vector[posicionVector] = Math.Round(funcionZResult, 4).ToString();
                        posicionVector++;
                        if (tipoFuncion == "MAX")
                        {
                            if (string.IsNullOrEmpty(vector[posicionVector]) || float.Parse(vector[posicionVector]) <= funcionZResult)
                            {
                                vector[posicionVector] = Math.Round(funcionZResult, 4).ToString();
                                zOptima = (float)Math.Round(funcionZResult, 4);
                                posicionVector++;
                                for (int i = 0; i < randomVariables.Length; i++)
                                {
                                    vector[posicionVector] = randomVariables[i].ToString();
                                    variablesOptimas[i]    = randomVariables[i];
                                    posicionVector++;
                                }
                            }
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(vector[posicionVector]) || float.Parse(vector[posicionVector]) >= funcionZResult)
                            {
                                vector[posicionVector] = Math.Round(funcionZResult, 4).ToString();
                                zOptima = (float)Math.Round(funcionZResult, 4);
                                posicionVector++;
                                for (int i = 0; i < randomVariables.Length; i++)
                                {
                                    vector[posicionVector] = randomVariables[i].ToString();
                                    variablesOptimas[i]    = randomVariables[i];
                                    posicionVector++;
                                }
                            }
                        }
                    }
                    else
                    {
                        vector[posicionVector] = "";
                    }
                }
                else
                {
                    vector = LimpiarVector(posicionVector, vector.Length, functionZ.variables.Length, vector);
                }
                if (j >= mostrarDesde && j <= mostrarHasta)
                {
                    tabla.LoadDataRow(vector, true);
                }

                pb.Increment(pb.Step);
                porcAvance = cantIteraciones / j;
            }

            tabla.LoadDataRow(vector, true);

            return(tabla);
        }