コード例 #1
0
 private void Awake()
 {
     rb       = GetComponent <Rigidbody>();
     lr       = GetComponent <LineRenderer>();
     body     = GetComponent <Cbody>();
     bodycoll = GetComponent <SphereCollider>();
 }
コード例 #2
0
    // private void Awake()
    // {
    //     axes.SetActive(true);
    //     space.GetComponent<Space>().enabled = false;
    //
    //    spoofbodylist = savedSimulations.simulationList[savedSimulations.selectedB];
    //
    //     foreach (var spoofBody in spoofbodylist)
    //     {
    //         bodyInstance = Instantiate(linedSp) as GameObject;
    //         bodyInstanceCbody = bodyInstance.GetComponent<Cbody>();
    //
    //         bodyInstanceCbody.m = spoofBody.mass;
    //         bodyInstanceCbody.setMass();
    //
    //         bodyInstanceCbody.radius = spoofBody.radius;
    //         bodyInstanceCbody.setRadius();
    //
    //         bodyInstanceCbody.initialPos = spoofBody.initialPos;
    //         bodyInstanceCbody.setInitialPos();
    //
    //         bodyInstanceCbody.initialVel = spoofBody.initialVel;
    //         bodyInstanceCbody.setIntialVel();
    //     }
    //     lines = FindObjectsOfType<nullScriptforVtoggle>();
    //
    //     CB = FindObjectsOfType<Cbody>();
    //
    // }


    private void Awake()
    {
        if (File.Exists(Application.persistentDataPath + "/" + savedSimulations.selectedB + ".CBSarrayHolder"))
        {
            BinaryFormatter bf   = new BinaryFormatter();
            FileStream      file = File.Open(Application.persistentDataPath + "/" + savedSimulations.selectedB + ".CBSarrayHolder", FileMode.Open);
            CBSarrayHolder  save = (CBSarrayHolder)bf.Deserialize(file);
            file.Close();

            CbodySpoof[] CBS = save.CBS;

            foreach (var spoofBody in CBS)
            {
                bodyInstance      = Instantiate(linedSp) as GameObject;
                bodyInstanceCbody = bodyInstance.GetComponent <Cbody>();


                bodyInstanceCbody.m = spoofBody.mass;
                bodyInstanceCbody.setMass();

                bodyInstanceCbody.radius = spoofBody.radius;
                bodyInstanceCbody.setRadius();

                bodyInstanceCbody.initialPos = new Vector3(spoofBody.initialPos.x, spoofBody.initialPos.y, spoofBody.initialPos.z);
                bodyInstanceCbody.setInitialPos();

                bodyInstanceCbody.initialVel = new Vector3(spoofBody.initialVel.x, spoofBody.initialVel.y, spoofBody.initialVel.z);
                bodyInstanceCbody.setIntialVel();
            }
        }
        lines = FindObjectsOfType <nullScriptforVtoggle>();

        CB = FindObjectsOfType <Cbody>();
    }
    public void showProperties()
    {
        cbody = gamemanagerTes.bodyselected.GetComponent <Cbody>();


        massI.text   = (cbody.m).ToString();
        radiusI.text = (cbody.radius).ToString();
        posxI.text   = (cbody.initialPos.x).ToString();
        posyI.text   = (cbody.initialPos.y).ToString();
        poszI.text   = (cbody.initialPos.z).ToString();
        velxI.text   = (cbody.initialVel.x).ToString();
        velyI.text   = (cbody.initialVel.y).ToString();
        velzI.text   = (cbody.initialVel.z).ToString();
    }
    public void setProperties()
    {
        cbody = gamemanagerTes.bodyselected.GetComponent <Cbody>();

        cbody.radius = GetFloat(radiusI.text, 0.0f);
        cbody.setRadius();

        cbody.m = GetFloat(massI.text, 0.0f);
        cbody.setMass();

        cbody.initialPos = new Vector3(GetFloat(posxI.text, 0.0f), GetFloat(posyI.text, 0.0f), GetFloat(poszI.text, 0.0f));
        cbody.setInitialPos();

        cbody.initialVel = new Vector3(GetFloat(velxI.text, 0.0f), GetFloat(velyI.text, 0.0f), GetFloat(velzI.text, 0.0f));
        cbody.setIntialVel();
    }
コード例 #5
0
 public Chromosome(Cbody bodyID, Lens lensID, int fitness)
 {
     this.cbody   = bodyID;
     this.lens    = lensID;
     this.fitness = 0;
 }
コード例 #6
0
    protected void recommend_onClick(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(5000);
        MySqlConnection conn = new MySqlConnection(connectionString);

        //open database table lens and store in lens object list
        conn.Open();
        MySqlCommand    cmd    = new MySqlCommand("select * from lens", conn);
        MySqlDataReader reader = cmd.ExecuteReader();

        List <Lens> lens = new List <Lens>();

        while (reader.Read())
        {
            string   lensID    = reader.GetValue(0) + "";
            string   lensName  = reader.GetValue(1) + "";
            string   lensBrand = reader.GetValue(2) + "";
            double   lensPrice = Convert.ToDouble(reader.GetValue(3) + "");
            string[] flengthID = (reader.GetValue(4) + "").Split('-');
            string[] fstopID   = (reader.GetValue(5) + "").Split('-');
            string   llocation = reader.GetValue(6) + "";
            string   limg      = reader.GetValue(7) + "";

            Lens temp = new Lens(lensID, lensName, lensBrand, lensPrice, flengthID, fstopID, llocation, limg);
            lens.Add(temp);
        }
        conn.Close();

        //open database table body and store in cbody object list
        conn.Open();
        cmd    = new MySqlCommand("select * from body", conn);
        reader = cmd.ExecuteReader();

        List <Cbody> cbody = new List <Cbody>();

        while (reader.Read())
        {
            string bodyID    = reader.GetValue(0) + "";
            string bodyName  = reader.GetValue(1) + "";
            string bodyBrand = reader.GetValue(2) + "";
            double bodyPrice = Convert.ToDouble(reader.GetValue(3) + "");
            double bodyMP    = Convert.ToDouble(reader.GetValue(4) + "");
            string bodySize  = reader.GetValue(5) + "";
            string location  = reader.GetValue(6) + "";
            string bimg      = reader.GetValue(7) + "";

            Cbody temp = new Cbody(bodyID, bodyName, bodyBrand, bodyPrice, bodyMP, bodySize, location, bimg);
            cbody.Add(temp);
        }
        conn.Close();

        //GENETIC ALGORITHM START
        //POPULATION GENERATION

        int population = 100; //bilangan gabungan lens + body
        int generation = 100; //bilangan loop yg generate population

        Random rnd = new Random();

        Chromosome[] chrome = new Chromosome[population];

        for (int g = 0; g < generation; g++)
        {
            if (g == 0) //first generation
            {
                for (int i = 0; i < population; i++)
                {
                    chrome[i] = new Chromosome(cbody[rnd.Next(cbody.Count)], lens[rnd.Next(lens.Count)], 0);

                    //FITNESS EVALUATION
                    int fitness = fitnessFunction(chrome[i]);
                }
            }
            else //for second and next generation
            {
                for (int i = population / 100; i < population; i++)
                {
                    chrome[i] = new Chromosome(cbody[rnd.Next(cbody.Count)], lens[rnd.Next(lens.Count)], 0);

                    //FITNESS EVALUATION
                    int fitness = fitnessFunction(chrome[i]);
                }
            }

            Array.Sort(chrome, delegate(Chromosome x, Chromosome y) { return(y.fitness.CompareTo(x.fitness)); });

            //CROSSOVER
            int first = chrome.Length / 2; //take top 50 population only

            for (int k = 0; k < chrome.Length / 2; k++)
            {
                Chromosome nextP = CrossOver(chrome[k], chrome[k + 1]);

                //MUTATION
                mutation(chrome[first], cbody, lens);

                nextP.fitness = fitnessFunction(nextP);
                chrome[first] = nextP;
                first++;
            }

            //SELECTION
            Array.Sort(chrome, delegate(Chromosome x, Chromosome y) { return(y.fitness.CompareTo(x.fitness)); });
        }

        //to send data to next page
        //display body result
        List <string> bodyNameL = new List <string>();
        List <string> bodyImgL  = new List <string>();
        List <string> bodyDetL  = new List <string>();

        List <string> lensNameL = new List <string>();
        List <string> lensImgL  = new List <string>();
        List <string> lensDetL  = new List <string>();

        double[] price = new double[3];
        for (int z = 0; z < 3; z++)
        {
            String bName  = chrome[z].cbody.bodyName;
            String bBrand = chrome[z].cbody.bodyBrand;
            String bPrice = Convert.ToString("RM" + chrome[z].cbody.bodyPrice);
            String bMP    = Convert.ToString(chrome[z].cbody.bodyMP);
            String bSize  = chrome[z].cbody.bodySize;
            String bLoc   = chrome[z].cbody.location;
            String bImg   = chrome[z].cbody.bimg;
            String bDet   = "Brand: " + bBrand + "\nPrice: " + bPrice + "\nMegapixel: " + bMP + "MP\nSensor Size: " + bSize + "\nLocation: " + bLoc;

            bodyNameL.Add(bName);
            bodyImgL.Add(bImg);
            bodyDetL.Add(bDet);

            //display lens result
            String lName  = chrome[z].lens.lensName;
            String lBrand = chrome[z].lens.lensBrand;
            String lPrice = Convert.ToString("RM" + chrome[z].lens.lensPrice);
            String lf1    = chrome[z].lens.flengthID.First();
            String lf2    = chrome[z].lens.flengthID.Last();
            String lfs1   = chrome[z].lens.fstopID.First();
            String lfs2   = chrome[z].lens.fstopID.Last();


            //retrieve flength and fstop details
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();
            cmd = new MySqlCommand("select flengthDesc from flength where flengthID=@lf1 or flengthID=@lf2", con);
            cmd.Parameters.AddWithValue("@lf1", lf1);
            cmd.Parameters.AddWithValue("@lf2", lf2);
            reader = cmd.ExecuteReader();
            List <string> al = new List <string>();
            while (reader.Read())
            {
                al.Add(reader[0].ToString() + "");
            }
            conn.Close();

            conn.Open();
            cmd = new MySqlCommand("select fstopDesc from fstop where fstopID=@lfs1 or fstopID=@lfs2", conn);
            cmd.Parameters.AddWithValue("@lfs1", lfs1);
            cmd.Parameters.AddWithValue("@lfs2", lfs2);
            reader = cmd.ExecuteReader();
            List <string> al1 = new List <string>();
            while (reader.Read())
            {
                al1.Add(reader[0].ToString() + "");
            }
            conn.Close();

            string lf;
            if (al.Count == 1)
            {
                lf = Convert.ToString(al[0]);
            }
            else
            {
                lf = Convert.ToString(al[0] + "-" + al[1]);
            }

            string lfs;
            if (al1.Count == 1)
            {
                lfs = Convert.ToString(al1[0]);
            }
            else
            {
                lfs = Convert.ToString(al1[0] + "-" + al1[1]);
            }

            String lLoc = chrome[z].lens.llocation;
            String lImg = chrome[z].lens.limg;
            String lDet = "Brand: " + lBrand + "\nPrice: " + lPrice +
                          "\nFocal Length: " + lf +
                          "\nAperture: " + lfs +
                          "\nLocation: " + lLoc;

            lensNameL.Add(lName);
            lensImgL.Add(lImg);
            lensDetL.Add(lDet);

            price[z] = chrome[z].cbody.bodyPrice + chrome[z].lens.lensPrice;
        }

        //store details in array session
        Session["bodyName"]    = bodyNameL.ToArray();
        Session["bodyImage"]   = bodyImgL.ToArray();
        Session["bodyDetails"] = bodyDetL.ToArray();

        Session["lensName"]    = lensNameL.ToArray();
        Session["lensImage"]   = lensImgL.ToArray();
        Session["lensDetails"] = lensDetL.ToArray();

        Session["price"] = price.ToArray <double>();

        Session["budget"] = txtBudget.Text;

        var pstyle = new List <string>();

        foreach (ListItem item in cboxListPhoto.Items)
        {
            if (item.Selected)
            {
                pstyle.Add(item.Value);
            }
        }

        Session["photo"]    = string.Join(", ", pstyle);
        Session["brand"]    = dropBrand.SelectedValue;
        Session["location"] = dropLocation.SelectedValue;

        //redirect to recommendation result page
        Response.Redirect("next.aspx");
    }