Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Optimization with Simulated Annealing


November 2001/Optimization with Simulated Annealing/Listing 2

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 —

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.