Esempio n. 1
0
/*******************************+++*******************************/
unsigned MinMultiStart(real (*ObjFunc)(real *x, size_t nDims),
          real AbsTol, real RelTol, unsigned MaxFuncs,
          const Matrix *XReg, size_t nDims, int MinAlg,
          const Matrix *StartPt, real *x, real *Obj)
/*****************************************************************/
/*   Purpose:  Multiple local searches (via MinAnyX) from each   */
/*             row in StartPt.                                   */
/*                                                               */
/*   Returns:  Total number of function evaluations.             */
/*                                                               */
/*   Version:  1996.04.14                                        */
/*****************************************************************/
{
     real      ObjTry;
     real      *xTry;
     size_t    i;
     unsigned  nEvals;

     CodeCheck(MatNumCols(StartPt) == nDims);

     xTry = AllocReal(nDims, NULL);

     nEvals = 0;
     for (*Obj = REAL_MAX, i = 0; i < MatNumRows(StartPt); i++)
     {
          /* Local search starting at row i of StartPt. */
          MatRow(StartPt, i, xTry);

          ObjTry = ObjFunc(xTry, nDims);
          nEvals++;

          nEvals += MinAnyX(ObjFunc, AbsTol, RelTol, MaxFuncs,
                    XReg, nDims, MinAlg, xTry, &ObjTry);

          if (ObjTry < *Obj)
          {
               *Obj = ObjTry;
               VecCopy(xTry, nDims, x);
          }
     }

     AllocFree(xTry);

     return nEvals;
}
Esempio n. 2
0
void Guess (int level, int code[])
{
     static int guessNum = 1;
     int i = 0, j;
     int guess[level];
     char selection;
     int x,y;
     
     outtextxy(410,150, "Now, Let\'s make your guess!");     
     
     if (guessNum == 13)
     {
         printf("\n\nYou have made 12 guesses already! You lost!\n\n");
         outtextxy(380,180, "GAME OVER! You have lost the game");
         printf("\nWould you like to play again? (Y/N): ");
         fflush(stdin);
         scanf("%c",&selection);
         fflush(stdin);
         if (selection == 'Y' || selection == 'y')
         {
              guessNum = 1;
              Menu();
         }
         else if (selection == 'N' || selection == 'n')
         {
              printf("\n");
              system("pause");
              exit(1);     
         }
         else
         {
              printf("\aWARNING: you should enter \'Y\' or \'N\'\n");
              Guess(level,code);
         }
     }
     
     printf("\nGuess %d:\n------------\n",guessNum);
     
     while (i != level)
     {
         switch (i)                               // according to i value (which goes to level), automatically running correct printf function..
         {
              case 0:
                   printf("Enter your guess for hat");
                   break;
                   
              case 1:
                   printf("Enter your guess for eyes");
                   break;
                   
              case 2:
                   printf("Enter your guess for nose");
                   break; 
                   
              case 3:
                   printf("Enter your guess for mouth");
                   break;
                   
              case 4:
                   printf("Enter your guess for clothes");
                   break; 
         }
         FillColor(i,11);                                            // changing color of current feature
         
         if (level == 5)                                             // according to level, running correct printf function..
         printf(" (1:red, 2:green, 3:blue, 4:purple, 5:yellow): ");
         else
         printf(" (1:red, 2:green, 3:blue): ");
         
         while (1)                                                  // taking mouse clicks or running scanf function
         {            
                guess[i] = -1;
    // ---------------------------------------------------------------------------------------------------------  
    // NOTICE:  Make "Comment" here, If you want to use Mouse while guessing          
    
    // <== REMOVE HERE              /*         
    
                if ( scanf("%d",&guess[i]) != -1 )
                {
                     if (guess[i] > level)
                     continue;
                     
                     else
                     {
                         clearmouseclick(WM_LBUTTONDOWN);     
                         break;
                     }
                }
                
    //                      */
    // ---------------------------------------------------------------------------------------------------------  
    
                if ( ismouseclick(WM_LBUTTONDOWN) )
                {
                      x = mousex();
                      y = mousey();
                      
                      if ( ( (x >= 380 && x <= 420) && (y >= 80 && y <= 120) )  ||
                           ( (x >= 430 && x <= 470) && (y >= 80 && y <= 120) )  ||
                           ( (x >= 480 && x <= 520) && (y >= 80 && y <= 120) )  ||
                           ( (x >= 530 && x <= 570) && (y >= 80 && y <= 120) && level == 5 )  ||
                           ( (x >= 580 && x <= 620) && (y >= 80 && y <= 120) && level == 5 )  )
                      {
                                                 
                           clearmouseclick(WM_LBUTTONDOWN);      // if user clicks on correct place (buttons), take it..
                           break;
                      }
                      else
                      {
                           clearmouseclick(WM_LBUTTONDOWN);      // if user clicks on anywhere except buttons, wait to be clicked correctly..
                           continue;
                      }
                }
                

         }
      
                                                                   // Assigning guess[] array values according to the coordinates of mouse clicks
         if ( (x >= 380 && x <= 420) && (y >= 80 && y <= 120) )
         guess[i] = 1;
         if ( (x >= 430 && x <= 470) && (y >= 80 && y <= 120) )
         guess[i] = 2;
         if ( (x >= 480 && x <= 520) && (y >= 80 && y <= 120) )
         guess[i] = 3;
         if ( (x >= 530 && x <= 570) && (y >= 80 && y <= 120) )
         guess[i] = 4;
         if ( (x >= 580 && x <= 620) && (y >= 80 && y <= 120) )
         guess[i] = 5;
         
         
         if (guess[i] != -1)                                    
         printf("%d\n",guess[i]);                                  // this runs when Mouse is used.. 
    
         if (guess[i] == 1)                                        // Calling FillColor() function according to the guesses..
         FillColor(i,4);
         if (guess[i] == 2)
         FillColor(i,2);
         if (guess[i] == 3)
         FillColor(i,1);
         if (guess[i] == 4)
         FillColor(i,5);
         if (guess[i] == 5)
         FillColor(i,14);
           
         i++;                                                    // increasing i, which goes to level
     }
     guessNum++;                                                 // increasing guessNum
     
     CodeCheck(level,code,guess,&guessNum);                      // continue through CodeCheck()
}
Esempio n. 3
0
/*******************************+++*******************************/
unsigned MinAnyX(real (*ObjFunc)(real *x, size_t nDims),
          real AbsTol, real RelTol, unsigned MaxFuncs,
          const Matrix *XReg, size_t nDims, int MinAlg, real *x,
          real *Obj)
/*****************************************************************/
/*   Purpose:  Optimize any objective function of x, where x may */
/*             include fixed, discrete, or continuous            */
/*             variables.                                        */
/*                                                               */
/*   Args:     ObjFunc   The objective function.                 */
/*             AbsTol    Absolute tolerance on function value    */
/*                       for convergence.                        */
/*             RelTol    Relative tolerance on function value    */
/*                       for convergence.                        */
/*             MaxFuncs  Maximum function evaluations.           */
/*             XReg      X-variable feasibility region.          */
/*             nDims     Number of dimensions.                   */
/*             MinAlg    The minimizer called for unconstrained  */
/*                       minimization.                           */
/*             x         Input:  Starting point (including any   */
/*                               fixed variables);               */
/*                       Output: "Optimal" point.                */
/*             Obj       Input:  Objective at x;                 */
/*                       Output: "Optimal" objective.            */
/*                                                               */
/*   Returns:  Total number of function evaluations.             */
/*                                                               */
/*   96.01.18: CodeBug parameters changed and CodeBug replaced   */
/*             by CodeCheck.                                     */
/*   96.01.18: CodeBug parameters changed.                       */
/*   96.03.07: Extrapolation at end of each iteration.           */
/*   96.03.08: MinConverged replaced by ApproxEq.                */
/*             Extrapolation removed.                            */
/*   96.03.09: Extrapolation at end of each iteration.           */
/*                                                               */
/*   Version:  1996.03.09                                        */
/*****************************************************************/
{
     real      ObjOld;
     real      *ContMax, *ContMin, *xCont, *xOld;
     real      *xExtCopy;
     real      (*ObjFuncExtCopy)(real *x, size_t nDims);
     size_t    i, ThisGroup;
     size_t    *ContDistrib, **GroupIndex, *Group;
     size_t    *GroupSize, *UngroupedIndex;
     size_t    *IndexContCopy, nDimsExtCopy;
     size_t    j, nContVars, nGroups, nUngroupedVars;
     unsigned  nEvals, NumOpts;

     /* Save statics to local variables, */
     /* to enable recursive calling.     */
     ObjFuncExtCopy = ObjFuncExt;
     xExtCopy = xExt;
     IndexContCopy = IndexCont;
     nDimsExtCopy = nDimsExt;

     CodeCheck(RegNumVars(XReg) == nDims);

     ContDistrib = AllocSize_t(nDims, NULL);
     IndexCont   = AllocSize_t(nDims, NULL);
     ContMax     = AllocReal(nDims, NULL);
     ContMin     = AllocReal(nDims, NULL);
     xCont       = AllocReal(nDims, NULL);
     xOld        = AllocReal(nDims, NULL);

     UngroupedIndex = AllocSize_t(nDims, NULL);

     GroupIndex  = AllocPtrSize_t(nDims, NULL);
     Group       = AllocSize_t(nDims, NULL);
     GroupSize   = AllocSize_t(nDims, NULL);

     /* Number of continuous variables. */
     nContVars = 0;

     /* Number of GRID or ungrouped DISCRETE variables. */
     nUngroupedVars = 0;

     /* Number of grouped DISCRETE variables. */
     nGroups = 0;

     /* Count the continuous variables, etc. */
     for (j = 0; j < nDims; j++)
          switch (RegSupport(XReg, j))
          {
               case FIXED:
                    break;

               case CONTINUOUS:
                    IndexCont[nContVars] = j;
                    ContMin[nContVars]   = RegMin(XReg, j);
                    ContMax[nContVars]   = RegMax(XReg, j);
                    ContDistrib[nContVars++]
                              = RegDistrib(XReg, j);
                    break;

               case GRID:
                    UngroupedIndex[nUngroupedVars++] = j;
                    break;

               case DISCRETE:
                    ThisGroup = RegCandGroup(XReg, j);

                    if (ThisGroup == 0)
                         /* Ungrouped. */
                         UngroupedIndex[nUngroupedVars++] = j;

                    else
                    {
                         for (i = 0; i < nGroups; i++)
                              if (ThisGroup == Group[i])
                                   break;

                         if (i == nGroups)
                         {
                              /* New candidate group. */
                              Group[i] = ThisGroup;
                              GroupSize[i] = 1;
                              GroupIndex[i] = AllocSize_t(nDims, NULL);
                              GroupIndex[i][0] = j;
                              nGroups++;
                         }
                         else
                              /* Existing candidate group. */
                              GroupIndex[i][GroupSize[i]++] = j;
                    }

                    break;

               default:
                    CodeBug("Illegal support");
          }

     /* External equivalents. */
     ObjFuncExt = ObjFunc;
     nDimsExt   = nDims;
     xExt       = x;

     /* Iterate until converged. */
     nEvals = 0;
     do
     {
          /* Used in test for convergence. */
          ObjOld = *Obj;
          NumOpts = 0;

          VecCopy(x, nDims, xOld);

          if (nContVars > 0)
          {
               /* Load continuous x's into xCont. */
               VecCopyIndex(nContVars, IndexCont, x, NULL, xCont);

               nEvals += MinCont(ObjCont, AbsTol, RelTol, MaxFuncs,
                         ContMin, ContMax, ContDistrib, nContVars,
                         MinAlg, xCont, Obj);
               NumOpts++;

               /* Put best continuous levels back in x. */
               VecCopyIndex(nContVars, NULL, xCont, IndexCont, x);
          }

          /* Ungrouped variables. */
          for (j = 0; j < nUngroupedVars; j++)
          {
               /* Optimize ungrouped-variable j. */
               nEvals += MinDisc(1, &UngroupedIndex[j], XReg, x,
                         Obj);
               NumOpts++;
          }

          /* Grouped variables. */
          for (j = 0; j < nGroups; j++)
          {
               /* Optimize group j. */
               nEvals += MinDisc(GroupSize[j], GroupIndex[j],
                         XReg, x, Obj);
               NumOpts++;
          }

          /* Try extrapolating. */
          nEvals += MinExtrap(ObjFunc, XReg, nDims, xOld, x, Obj);

     } while (NumOpts > 1 &&
               !ApproxEq(ObjOld, *Obj, AbsTol, RelTol));

     AllocFree(ContDistrib);
     AllocFree(IndexCont);
     AllocFree(ContMax);
     AllocFree(ContMin);
     AllocFree(xCont);
     AllocFree(xOld);

     AllocFree(UngroupedIndex);

     for (j = 0; j < nGroups; j++)
          AllocFree(GroupIndex[j]);
     AllocFree(GroupIndex);
     AllocFree(Group);
     AllocFree(GroupSize);

     /* Restore statics. */
     ObjFuncExt = ObjFuncExtCopy;
     xExt = xExtCopy;
     IndexCont = IndexContCopy;
     nDimsExt = nDimsExtCopy;

     return nEvals;
}