Example #1
0
        void TryNextMonth2(Company company, int kind)
        {
            if (decisionsList.Count >= 1) return;

            //var cs = CloneCompanyState(company);
            int i = company.MonthID;
            i++;
            Debug.WriteLine(String.Format("-------------------- MonthID = {0:D}, {1:S}", i, "Real Mode"));
            company.iMonth = i;
            if (kind > -1) company.CreateNewProduct((ProductKind)kind);

            currentDecisions[i] = kind;
            try
            {
                company.NextMonth();
            }
            catch (MyException)
            {
                //result.Balance = 0;
                //iResult = -1;
                return;
            }

            if (i >= nMonthsToRun - 1)
            {
                RecordCurrentDecisions();
                //var result = new Result(nMonthsToRun, cs.Accounting.Balance);
                //result.Balance = cs.Accounting.Balance;
                //result.Balance = company.Accounting.Balance;
                //resultList.Add(result);
                return;
            }
            else
            {
                var snapshot = CompanySaveState(company);

                for (int k = (int)ProductKind.Large; k >= -1; k--)
                {
                    if (k == (int)ProductKind.Classic || k == (int)ProductKind.Free) continue;
                    // Using suggestions
                    int suggestion = GetSuggestion(i);
                    if (suggestion >= -1 && suggestion != k) continue;
                    // Do not create new product at the end of the test period
                    if (nMonthsToRun - i < 6) k = -1;

                    Company cs;
                    if (company == null)
                    {
                        cs = CompanyRestoreState(snapshot);
                    }
                    else
                    {
                        cs = company;
                        company = null;
                    }
                    TryNextMonth2(cs, k);
                }
            }
        }
Example #2
0
 //Company CloneCompanyState(Company company)
 //{
 //    using (var memStream = new MemoryStream())
 //    {
 //        var binaryFormatter = new BinaryFormatter();
 //        binaryFormatter.Serialize(memStream, company);
 //        memStream.Seek(0, SeekOrigin.Begin);
 //        return (Company)binaryFormatter.Deserialize(memStream);
 //    }
 //}
 MemoryStream CompanySaveState(Company company)
 {
     var memStream = new MemoryStream();
     var binaryFormatter = new BinaryFormatter();
     binaryFormatter.Serialize(memStream, company);
     memStream.Seek(0, SeekOrigin.Begin);
     return memStream;
 }
Example #3
0
 //public void Run()
 //{
 //    //GrowHistory.Clear();
 //    Company company = new Company();
 //    company.SafeMode = false;
 //    //GrowHistory.Push(company);
 //    //TryGrowNextMonth();
 //    for (int i = 0; i <= nMonthsToRun; i++)
 //    {
 //        Debug.WriteLine(String.Format("-------------------- MonthID = {0:D}, {1:S}", i, "Grow Mode"));
 //        company.MonthID = i; // This state would not raise error since it's been test-driven or it's pre-defined
 //        Company lastGoodState = CloneCompanyState(company);
 //        int n = GetPredefindedResults(i);
 //        if (n < 0)
 //        {
 //            n = 0;
 //            bool successfullyDrived;
 //            var snapshot = CloneCompanyState(company);
 //            do
 //            {
 //                //lastState = CloneCompanyState(company);
 //                if (n != 0)
 //                {
 //                    company.CreateNewProducts(ProductKind.Classic);
 //                }
 //                successfullyDrived = TestDrive(company);
 //                if (successfullyDrived)
 //                {
 //                    lastGoodState = CloneCompanyState(company);
 //                    company = CloneCompanyState(snapshot); // Reset the state
 //                    n++;
 //                }
 //            } while (successfullyDrived);
 //            n--;
 //            company = CloneCompanyState(lastGoodState);
 //            if (n < 0) throw new Exception("Bad model");
 //        }
 //        else
 //        {
 //            for (int j = 0; j < n; j++)
 //            {
 //                if (j == 1)
 //                {
 //                    company.CreateNewProducts(ProductKind.Classic);
 //                }
 //                lastGoodState = CloneCompanyState(company);
 //            }
 //        }
 //        NotifyMonthDone(i, n, lastGoodState);
 //    }
 //}
 //bool TestDrive(Company company)
 //{
 //    Company cs = CloneCompanyState(company);
 //    //if (!company.SafeMode)
 //    //{
 //    //    SafeHistory.Clear();
 //    //    SafeHistory.Push(CloneCompanyState(company));
 //    //}
 //    //cs = SafeHistory.Peek();
 //    cs.SafeMode = true;
 //    return TestNextMonth(cs) == TestResult.Okay;
 //}
 //enum TestResult { Okay, SafeMode, InsufficientFunds, NoStaff }
 //TestResult TestNextMonth(Company cs)
 //{
 //    //Company cs = SafeHistory.Peek();
 //    int i = cs.MonthID;
 //    i++;
 //    Debug.WriteLine(String.Format("-------------------- MonthID = {0:D}, {1:S}", i, "Safe Mode"));
 //    try
 //    {
 //        cs.MonthID = i;
 //        if (i >= nMonthsToRun)
 //        {
 //            Debug.WriteLine("Test ended successfully.");
 //            return TestResult.Okay;
 //        }
 //    }
 //    catch (MyException ex)
 //    {
 //        //SafeHistory.Pop();
 //        if (ex is SafeModeException) return TestResult.SafeMode;
 //        else
 //            if (ex is InsufficientFundsException) return TestResult.InsufficientFunds;
 //            else
 //                if (ex is NoStaffException) return TestResult.NoStaff;
 //                else
 //                    throw;
 //    }
 //    if (i <= nMonthsToRun - 1)
 //    {
 //        TestResult testResult;
 //        int n = 0;
 //        var snapshot = CloneCompanyState(cs);
 //        //Company lastState;
 //        TestResult lastResult = TestResult.Okay;
 //        do
 //        {
 //            //lastState = CloneCompanyState(cs);
 //            //SafeHistory.Push(CloneCompanyState(cs));
 //            if (n == 1)
 //                cs.CreateNewProducts(ProductKind.Classic);
 //            testResult = TestNextMonth(cs);
 //            switch (testResult)
 //            {
 //                case TestResult.Okay:
 //                    {
 //                        return TestResult.Okay;
 //                    }
 //                case TestResult.InsufficientFunds:
 //                    {
 //                        if (lastResult == TestResult.SafeMode)
 //                        {
 //                            return TestResult.InsufficientFunds;
 //                        }
 //                        else
 //                        {
 //                            cs = CloneCompanyState(snapshot);
 //                            n++;
 //                            lastResult = TestResult.InsufficientFunds;
 //                            break;
 //                        }
 //                    }
 //                case TestResult.SafeMode:
 //                    {
 //                        if (lastResult == TestResult.InsufficientFunds)
 //                        {
 //                            return TestResult.SafeMode;
 //                        }
 //                        else
 //                        {
 //                            cs = CloneCompanyState(snapshot);
 //                            n--;
 //                            lastResult = TestResult.SafeMode;
 //                            if (n < 0) return TestResult.SafeMode;
 //                            break;
 //                        }
 //                    }
 //                case TestResult.NoStaff:
 //                    {
 //                        return TestResult.NoStaff;
 //                    }
 //                default:
 //                    {
 //                        Debug.Assert(false);
 //                        break;
 //                    }
 //            }
 //        } while (true);
 //        //n--;
 //    }
 //    else return TestNextMonth(cs);
 //}
 //public override void NextMonth()
 //{
 //    Debug.WriteLine("--------------------");
 //    Debug.WriteLine("MonthID = " + MonthID.ToString());
 //    Debug.WriteLine("Plan to create " + actions[MonthID].ToString() + " project(s).");
 //    if (actions[MonthID] < 0) throw new ApplicationException("action failed.");
 //    projectCreatedThisMonth = 0;
 //    Employees.MonthID = MonthID;
 //    BusinessCycles.MonthID = MonthID;
 //    Accounting.MonthID = MonthID;
 //    Debug.WriteLine("Totally " + projectCreatedThisMonth.ToString() + " project(s) created this month.");
 //    Debug.WriteLine(String.Format("nEmployees = {0:D}, nProjects = {1:D}, nProducts = {2:D}", Employees.GetNumOfEmployees(), BusinessCycles.NumberOfProjects, BusinessCycles.NumberOfProducts));
 //}
 void NotifyMonthDone(int iMonth, int ProductKind, Company companyState)
 {
     Report r = new Report();
     r.MonthID = iMonth;
     r.nEmployees = companyState.NumberOfEmployees;
     r.nIdle = companyState.NumberOfIdleStaffs;
     r.ProductKind = ProductKind;
     r.nProjects = companyState.NumberOfProjects;
     r.nProducts = companyState.NumberOfProducts;
     r.MonthlyCost = companyState.Accounting.MonthlyCost / 10000;
     r.MonthlyRevenue = companyState.Accounting.MonthlyRevenue / 10000;
     r.Balance = companyState.Accounting.Balance / 10000;
     OnMonthlyReport(r);
 }
Example #4
0
        public void Run2()
        {
            Company company = new Company();
            company.SafeMode = false;

            decisionsList = new List<int[]>();
            currentDecisions = new int[nMonthsToRun];
            TryNextMonth2(company, 1);

            var stateSequenceList = new List<Company[]>();
            int iMaxBalance = 0;
            float maxBalance = 0;
            for (int j = 0; j < decisionsList.Count; j++)
            {
                var d = decisionsList[j];
                Company[] stateSequence = new Company[nMonthsToRun];
                Company c = new Company();
                for (int i = 0; i < nMonthsToRun; i++)
                {
                    c.iMonth = i;
                    c.CreateNewProduct((ProductKind)d[i]);
                    c.NextMonth();
                    stateSequence[i] = CompanyRestoreState(CompanySaveState(c));
                }
                if (c.Accounting.Balance > maxBalance)
                {
                    maxBalance = c.Accounting.Balance;
                    iMaxBalance = j;
                }
                stateSequenceList.Add(stateSequence);
            }
            for (int i = 0; i < nMonthsToRun; i++)
            {
                NotifyMonthDone(i, (int)decisionsList[iMaxBalance][i], stateSequenceList[iMaxBalance][i]);
            }
        }