Listing 2: Partial listing of TimeShareSolver.h
// 4 condo units in building // 16 one-week periods. // 22 maximum occupants in building const int MAX_UNITS = 4; const int MAX_PERIODS = 16; const int MAX_OCCUPANTS = 22; // // Max iterations in main loop. const int MAX_ITERATIONS = 1000; const int UNASSIGNED = -1; // // CTimeShareSolver class ATL_NO_VTABLE CTimeShareSolver : public CComObjectRootEx<CComSingleThreadModel>, public CComCoClass<CTimeShareSolver,&CLSID_TimeShareSolver>, public IDispatchImpl<ITimeShareSolver,&IID_ITimeShareSolver, &LIBID_TSSOLVELib> { public: CTimeShareSolver(); ~CTimeShareSolver(); // // Condo owner. class Owner { public: int m_ownerID; int m_occupCnt; // total family members int m_choice[3]; // 3 preferred choices. int CalcCost(int assignment) { int cost; if (assignment == m_choice[0]) cost = 0; else if (assignment == m_choice[1]) cost = 2*m_occupCnt; else if (assignment == m_choice[2]) cost = 4*m_occupCnt; else if (assignment != UNASSIGNED) cost = 50 + 7*m_occupCnt; // cash + 7 incentive else cost = 1000; // unassigned return cost; } }; // // List of owners. int m_ownerCnt; Owner *m_owner ; // // The 3 solutions. int *m_trial; int *m_trialCost; // for each owner int m_trialCostTotal; int *m_curr; int m_currCostTotal; int *m_best; int m_bestCostTotal; // // For each 1 week period keep track of // of number units occupied and number // of people in building. int m_unitsOccup [MAX_PERIODS]; int m_totalOccup [MAX_PERIODS]; // // Iteration count, annealing temp. int m_iterCnt; double m_saTemp; // // Main loop. Initialize(); AssignOwners(); SwapOwners(); EvaluateCost(); UnassignOwners(); // // Supporting routines Assign(int ownerIdx, int period); Unassign( int ownerIdx); Swap(int i1); // // Input, Output HRESULT ReadOwnerInfo(); WriteBestSolution(); public: STDMETHOD(Solve)(); } End of Listing