예제 #1
0
Vector<double> ObjectiveFunction::getGradient(Vector<double> argument)
{
   Vector<double> gradient(numberOfVariables, 0.0);

   double evaluation1 = 0.0, evaluation2 = 0.0;

   for (int i = 0; i < numberOfVariables; i++)
   {
      // Add epsilon to argument

      argument[i] += epsilon;

      // Get evaluation

      evaluation2 = getEvaluation(argument);

      // Restart original argument

      argument[i] -= epsilon;

      // Substract epsilon from argument

      argument[i] -= epsilon;

      // Get evaluation

      evaluation1 = getEvaluation(argument);

      // Restart original argument

      argument[i] += epsilon;

      // Calculate derivative

      gradient[i] = (evaluation2 - evaluation1)/(2.0*epsilon);
   }

   return(gradient);
}
예제 #2
0
static char *eval_test() {
	assert(gs.initialized);
	printf("Running eval_test\n");
	char fen[] = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
	parseFen(fen);
	assert(gs.material[0]==gs.material[1]);
	assert(gs.material[0]==4165);
	printf(" material white %d material black %d\n", gs.material[0],gs.material[1]);
	printf(" positional white %d positional black %d\n", gs.positional[0],gs.positional[1]);

	int eval = getEvaluation(gs);
	printf("evaluation= %d\n", eval);
	assert( eval==0);
	return 0;
}
예제 #3
0
//Minimax, performs the minimax algorithm from the node down to the depth specified
int minimax(struct BFTreeNode* node, int depth, int player) {
    int value;
    struct QueueNode* child;

    //THE FOLLOWING PART WONT WORK FOR CONSECUTIVE TURNS... (but maybe that's moot?)
    if (node->nodeWrapped->evaluation != NULL ) {
        return node->nodeWrapped->evaluation;
    }

    // Return if at the bottom.
    if (depth == 0 || node->nodeWrapped->children->length == 0) {
        node->nodeWrapped->evaluation = getEvaluation(node);
        return node->nodeWrapped->evaluation;
    }

    // Recursively call and pass up the max or the min dependent on the player
    if (player == MAX_PLAYER) {
        int bestValue = INT_MIN;
        child = node->nodeWrapped->children->head;
        while (child) {
            value = minimax(child->thisNode, depth - 1, MIN_PLAYER);
            bestValue = max(bestValue, value);
            child = child->nextNode;
        }
        node->nodeWrapped->evaluation = bestValue;
        return bestValue;
    } else {
        int bestValue = INT_MAX;
        child = node->nodeWrapped->children->head;
        while (child) {
            value = minimax(child->thisNode, depth - 1, MAX_PLAYER);
            bestValue = min(bestValue, value);
            child = child->nextNode;
        }
        node->nodeWrapped->evaluation = bestValue;
        return bestValue;
    }
}
예제 #4
0
파일: putDiigo.c 프로젝트: tsupo/bookey
BOOL
_putDiigo(
    const MyClip *mp,
    char         *errorString,
    PARAM_PBM    *args
)
{
// char        *cookie   = args->cookie;
    char        *request  = args->request;
    char        *response = args->response;
    size_t      sz        = args->sz;
    PARAM_DIIGO *param = (PARAM_DIIGO *)(args->args);
    char        url[MAX_URLLENGTH * 4];
    char        tmpTitle[2048];
    char        *p, *q;
    char        *URLforJSON  = NUL;
    char        *tagsForJSON = NUL;
    char        *bookmarks   = NUL;
    int         rate;
    BOOL        ret = FALSE;

    errorString[0] = NUL;
    rate = 0;

    /* ブックマークを POST */
    strcpy( url, "http://api2.diigo.com/bookmarks" );

    p = any2utf( mp->title );
    strcpy( tmpTitle, p ? p : mp->title );

    // MM/Memo 形式の title を扱うための処理
    rate = getEvaluation( tmpTitle, UTF8 );
    q = tmpTitle;

    if ( rate == 0 )
        if ( (mp->evaluation >= EVL_ONE)  &&
                (mp->evaluation <= EVL_FIVE)    )
            rate = mp->evaluation;

    // タイトル
    sprintf( request,
             "[{\"title\":\"%s\",",
             encodeContents( q ) );

    // URL
    URLforJSON = encodeURLforJSON( mp->url );
    sprintf( request + strlen(request),
             "\"url\":\"%s\",",
             URLforJSON );

    // 公開・非公開
    sprintf( request + strlen(request),
             "\"shared\":\"%s\",",
             mp->publication == PUB_PRIVATE ? "no" : "yes" );

    // タグ (コンマ区切り)
    p = any2utf( mp->tags );
    tagsForJSON = encodeTagsForJSON( p ? p : mp->tags );
    sprintf( request + strlen(request),
             "\"tags\":\"%s\",",
             encodeContents( tagsForJSON ) );

    // 説明
    p = any2utf( mp->comment );
    sprintf( request + strlen(request),
             "\"desc\":\"%s\"}]",
             encodeContents( p ? p : mp->comment ) );

    bookmarks = (char *)malloc( strlen( request ) * 8 + 1 );
    if ( bookmarks ) {
        sprintf( bookmarks, "bookmarks=%s", encodeURL(request) );

        setUpReceiveBuffer( response, sz );
        http_postBASIC( url, param->userName, param->password,
                        "application/x-www-form-urlencoded",
                        bookmarks, response );
        if ( *response ) {
            p = strstr( response, "added" );
            if ( p )
                ret = TRUE; /* 登録成功 */
            else {
                p = response;
                if ( (*p >= 'A') && (*p <= 'Z') && (strlen(p) <= 64) )
                    strcpy( errorString, p );
#ifdef  _DEBUG
                else {
                    FILE    *fp = fopen( "./diigo.txt", "w" );
                    if ( fp ) {
                        fputs( response, fp );
                        fclose( fp );
                    }
                }
#endif
            }
        }
    }

    if ( bookmarks )
        free( bookmarks );
    if ( tagsForJSON )
        free( tagsForJSON );
    if ( URLforJSON )
        free( URLforJSON );

    return ( ret );
}
예제 #5
0
void do_eval() {
	char fen[]="2rr3k/pp3pp1/1nNqbN1p/3p4/2pP3P/2P3Q1/PPB5/R4RK1 b - - 0 1";
	parseFen(fen);
	getEvaluation();
}
예제 #6
0
Matrix<double> ObjectiveFunction::getHessian(Vector<double> argument)
{
   Matrix<double> hessian(numberOfVariables, numberOfVariables, 0.0);

   double evaluation11 = 0.0, evaluation12 = 0.0;
   double evaluation21 = 0.0, evaluation22 = 0.0;

   // Obtain the upper part of the Hessian matrix

   for(int i = 0; i < numberOfVariables; i++)
   {
      for(int j = i; j < numberOfVariables; j++)
      {
         // Perturb argument components i and j

         argument[i] += epsilon;
         argument[j] += epsilon;

         // Calculate evaluation

         evaluation22 = getEvaluation(argument);

         // Restart argument components i and j

         argument[i] -= epsilon;
         argument[j] -= epsilon;


         // Perturb argument components i and j

         argument[i] += epsilon;
         argument[j] -= epsilon;

         // Calculate evaluation

         evaluation21 = getEvaluation(argument);

         // Restart argument components i and j

         argument[i] -= epsilon;
         argument[j] += epsilon;


         // Perturb argument components i and j

         argument[i] -= epsilon;
         argument[j] += epsilon;

         // Calculate evaluation

         evaluation12 = getEvaluation(argument);

         // Restart potential free parameters i and j

         argument[i] += epsilon;
         argument[j] -= epsilon;


         // Perturb potential free parameters i and j

         argument[i] -= epsilon;
         argument[j] -= epsilon;

         // Calculate evaluation

         evaluation11 = getEvaluation(argument);

         // Restart potential free parameters i and j

         argument[i] += epsilon;
         argument[j] += epsilon;

         // Calculate second derivative

         hessian[i][j]
         = (evaluation22 - evaluation21 - evaluation12 + evaluation11)/(4.0*pow(epsilon,2));
      }
   }

   // Obtain the rest of elements by symmetry

   for(int i = 0; i < numberOfVariables; i++)
   {
      for(int j = 0; j < i; j++)
      {
         hessian[i][j] = hessian[j][i];
      }
   }

   return(hessian);
}
예제 #7
0
파일: outputOpml.c 프로젝트: tsupo/bookey
void
outputOPML(
        const MyClip *mp,
        int          numOfClips,
        const char   *title,
        int          codeChange,
        FILE         *fp
    )
{
    int         i;
    int         rate;
    char        buf[BUFSIZ];
    char        tag[4096];
    char        comment[4096];

    struct tm   tm, *tt;
    time_t      t;
    int         yyyy, mm, dd, HH, MM, SS;
    char        *p = NULL;

    /* OPML ファイル書き出し */
    switch ( codeChange ) {
    case SJIS2UTF:
        p = sjis2utf( title );
        break;

    case EUC2UTF:
        p = euc2utf( title );
        break;

    case UTF8:
    default:
        p = NULL;
        break;
    }
    fprintf( fp,
             "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
             "<opml version=\"1.1\">\n<head>\n"
             "<title>%s</title>\n",
             p ? p : title );
    fprintf( fp, "</head>\n<body>\n" );

    for ( i = 0; i < numOfClips; i++ ) {
        switch ( codeChange ) {
        case SJIS2UTF:
         // p = sjis2utf( mp[i].title );
         // strcpy( buf, p ? p : mp[i].title );
            p = sjis2utf( mp[i].tags );
            strcpy( tag, p ? p : mp[i].tags );
            p = sjis2utf( mp[i].comment );
            strcpy( comment, p ? p : mp[i].comment );
            break;

        case EUC2UTF:
         // p = euc2utf( mp[i].title );
         // strcpy( buf, p ? p : mp[i].title );
            p = euc2utf( mp[i].tags );
            strcpy( tag, p ? p : mp[i].tags );
            p = euc2utf( mp[i].comment );
            strcpy( comment, p ? p : mp[i].comment );
            break;

        case UTF8:
        default:
         // strcpy( buf, mp[i].title );
            strcpy( tag, mp[i].tags );
            strcpy( comment, mp[i].comment );
            break;
        }

        // MM/Memo 形式の title を扱うための処理
        strcpy( buf, mp[i].title );
        rate = getEvaluation( buf, UTF8 );

        regularize( buf );
        regularize( tag );
        regularize( comment );

        tm.tm_year = mp[i].yyyy - 1900;
        tm.tm_mon  = mp[i].mm - 1;
        tm.tm_mday = mp[i].dd;
        tm.tm_hour = mp[i].HH;
        tm.tm_min  = mp[i].MM;
        tm.tm_sec  = mp[i].SS;
        t = timelocal( &tm );
#ifdef  WIN32
        if ( _timezone != 0 )
            t += _timezone;
#else
        t -= 9 * 60 * 60;   /* JST → GMT */
#endif

        tt = gmtime( &t );
        if ( !tt )
            tt = localtime( &t );
        if ( tt ) {
            yyyy = tt->tm_year + 1900;
            mm   = tt->tm_mon + 1;
            dd   = tt->tm_mday;
            HH   = tt->tm_hour;
            MM   = tt->tm_min;
            SS   = tt->tm_sec;
        }
        else {
            yyyy = mp[i].yyyy;
            mm   = mp[i].mm;
            dd   = mp[i].dd;
            HH   = mp[i].HH;
            MM   = mp[i].MM;
            SS   = mp[i].SS;
        }

        strcpy( buf, escapeQuote( buf ) );
        fprintf( fp,
                 "<outline text=\"%s\" type=\"Link\" "
                 "url=\"%s\" title=\"%s\" notes=\"%s\" "
                 "date=\"%04d-%02d-%02dT%02d:%02d:%02dZ\"",
                 buf,
                 escapeURL( mp[i].url ),
                 tag,
                 escapeQuote( comment ),
                 yyyy, mm, dd, HH, MM, SS );

        // 以下は bookey 独自拡張 [OPML 1.0 の仕様書を読む限りは、独自に属性を
        //                         追加するのは問題なさそう]
        //   -- 2006年11月17日現在、OPML validator は Internal database error
        //      が発生して利用できない(新しいデータベースでサービス再開するま
        //      で待っていてください、云々と表示される)ため、本当に valid なの
        //      かどうかは未確認
        if ( ((rate >= EVL_ONE)  &&
              (rate <= EVL_FIVE)    )             ||
             ((mp[i].evaluation >= EVL_ONE)  &&
              (mp[i].evaluation <= EVL_FIVE)    )    )
            fprintf( fp,
                     " evaluation=\"%d\"",
                     mp[i].evaluation );
        if ( (mp[i].rating >= RAT_ONE) && (mp[i].rating <= RAT_THREE) )
            fprintf( fp,
                     " rating=\"%d\"",
                     mp[i].rating );
        if ((mp[i].affirmation == AFF_GOOD) || (mp[i].affirmation == AFF_BAD))
            fprintf( fp,
                     " affirmation=\"%s\"",
                     mp[i].affirmation == AFF_GOOD ? "good" : "bad" );

        if ( mp[i].publication != PUB_EVERYONE ) {
            switch ( mp[i].publication ) {
            case PUB_FRIENDS:
                fprintf( fp, " publication=\"friends\"" );
                break;
            case PUB_PRIVATE:
                fprintf( fp, " publication=\"private\"" );
                break;
            }
        }

        fputs( " />\n", fp );
#ifdef  _DEBUG
        fprintf( stderr, "%d) %s (URL: %s)\n",
                 i + 1, utf2sjis(buf),
                 mp[i].url );
#endif
    }

 // fprintf( fp, "</body>\n</opml>\n" );
    fputs( "</body>\n</opml>\n", fp );
}
예제 #8
0
파일: inputOpml.c 프로젝트: tsupo/bookey
MyClip  *
inputOPML( int *numOfClips, FILE *fp )
{
    char    buf[65536], *p, *q;
    char    tmp[65536];
    char    *note;
    int     num = 1000; /* 暫定 */
    int     cnt = 0, block = 1;
    MyClip  *mp = NULL;
    MyClip  *newClip_p;
    int     rate;

    if ( !numOfClips )
        return ( mp );
    *numOfClips = 0;

    while ( ( p = fgets( buf, 65536 - 1, fp ) ) != NULL ) {
        while ( (*p == ' ') || (*p == '\t') )
            p++;
        if ( !strncmp( p, "</body>", 7 ) || !strncmp( p, "</opml>", 7 ) )
            break;
        if ( strncmp( p, "<outline ", 9 ) != 0 )
            continue;

        newClip_p = allocateMyClipIfNecessary( &mp, num, &block, cnt );
        if ( !newClip_p )
            break;
        mp = newClip_p;

        rate = 0;
        note = p;

        /* text [必須] */
        q = strstr( note, "text=\"" );
        if ( !q )
            continue;
        p = q + 6;
        q = strchr( p, '"' );
        while ( !q ) {
            /* '<outline text="' の直後に改行がある場合の救済措置 */
            p = fgets( buf, 65536 - 1, fp );
            if ( !p )
                break;
            while ( (*p == ' ') || (*p == '\t') )
                p++;
            q = strchr( p, '"' );
            if ( q )
                note = q;
        }
        if ( !p )
            break;
        if ( q ) {
            strncpy( mp[cnt].title, p, q - p );
            mp[cnt].title[q - p] = '\0';
            p = q + 1;

            // MM/Memo 形式の title を扱うための処理
            rate = getEvaluation( mp[cnt].title, UTF8 );
        }

        /* url [必須] */
        q = strstr( note, "url=\"" );
        if ( q ) {
            q += 5;
            p = strchr( q, '"' );
            if ( p ) {
                strncpy( tmp, q, p - q );
                tmp[p - q] = '\0';
                strcpy( mp[cnt].url, unescapeURL( tmp ) );
                p++;
            }
            else
                p = q;
        }

        /* title [必須] */
        q = strstr( note, "title=\"" );
        if ( q ) {
            q += 7;
            p = strchr( q, '"' );
            if ( p ) {
                strncpy( mp[cnt].tags, q, p - q );
                mp[cnt].tags[p - q] = NUL;
                p++;
            }
            else
                p = q;
        }

        /* notes [必須] */
        q = strstr( note, "notes=\"" );
        if ( q ) {
            q += 7;
            p = strchr( q, '"' );
            if ( p ) {
                strncpy( mp[cnt].comment, q, p - q );
                mp[cnt].comment[p - q] = '\0';
                p++;
            }
            else
                p = q;
        }

        /* date [必須] */
        q = strstr( note, "date=\"" );
        if ( q ) {
            q += 6;
            p = strchr( q, '"' );
            if ( p ) {
                strncpy( tmp, q, p - q );
                tmp[p - q] = '\0';
                getDateTimeFromDateString( tmp,
                                           &(mp[cnt].yyyy),
                                           &(mp[cnt].mm),
                                           &(mp[cnt].dd),
                                           &(mp[cnt].HH),
                                           &(mp[cnt].MM),
                                           &(mp[cnt].SS) );
                p++;
            }
            else
                p = q;
        }

        /* 以下は bookey 独自拡張 */
        /* evaluation [optional] */
        q = strstr( note, "evaluation=\"" );
        if ( q ) {
            q += 12;
            mp[cnt].evaluation = atol( q );
            p = strchr( q, '"' );
            if ( !p )
                p = q;
        }
        if ( mp[cnt].evaluation == 0 )
            if ( rate > 0 )
                mp[cnt].evaluation = rate;

        /* rating [optional] */
        q = strstr( note, "rating=\"" );
        if ( q ) {
            q += 8;
            mp[cnt].rating = atol( q );
            p = strchr( q, '"' );
            if ( !p )
                p = q;
        }

        /* affirmation [optional] */
        q = strstr( note, "affirmation=\"" );
        if ( q ) {
            q += 13;
            if ( !strncmpi( q, "good", 4 ) )
                mp[cnt].affirmation = AFF_GOOD;
            else if ( !strncmpi( q, "bad", 3 ) )
                mp[cnt].affirmation = AFF_BAD;
            p = strchr( q, '"' );
            if ( !p )
                p = q;
        }

        /* publication [optional] */
        q = strstr( note, "publication=\"" );
        if ( q ) {
            q += 13;
            if ( !strncmpi( q, "friends", 7 ) )
                mp[cnt].publication = PUB_FRIENDS;
            else if ( !strncmpi( q, "private", 7 ) )
                mp[cnt].publication = PUB_PRIVATE;
            p = strchr( q, '"' );
            if ( !p )
                p = q;
        }

        cnt++;
    }
    if ( fp == stdin ) {
        clearerr( fp );
    }

    *numOfClips = cnt;

    return ( mp );
}
예제 #9
0
bool FastConvexFitting::getFitting(Geometry & geometry)
{
    if(initflag)
    {
        assert(getConfigurations());
        initflag=0;
    }
    if(configurations.size()==0)
    {
        return 0;
    }

    centerposition=-(orientation.inverse()*position);

    int i,n=configurations.size();
    for(i=0;i<n;i++)
    {
        int j,m=configurations[i].edges.size();
        for(j=0;j<m;j++)
        {
            configurations[i].globaledges[j].startcorner=position+orientation*configurations[i].edges[j].startcorner;
            configurations[i].globaledges[j].endcorner=position+orientation*configurations[i].edges[j].endcorner;
        }
    }

    G.score=0;
    for(i=0;i<n;i++)
    {
        if(getEvaluation(configurations[i])&&G.score<configurations[i].score)
        {
            G=configurations[i];
        }
    }
    if(G.score>0)
    {
        int j,m=G.geometry.size();
        G.sigma.fill(0,m);
        QVector<double> sum;
        sum.fill(0,m);

        for(i=0;i<n;i++)
        {
            int diffcount=0;
            int diffid;
            for(j=0;j<m;j++)
            {
                if(configurations[i].geometry[j]!=G.geometry[j])
                {
                    diffcount++;
                    diffid=j;
                }
                if(diffcount>=2)
                {
                    break;
                }
            }
            if(diffcount==1)
            {
                G.sigma[diffid]+=configurations[i].score*pow(configurations[i].geometry[diffid]-G.geometry[diffid],2);
                sum[diffid]+=configurations[i].score;
            }
            else if(diffcount==0)
            {
                for(j=0;j<m;j++)
                {
                    sum[j]+=configurations[i].score;
                }
            }
        }
        for(j=0;j<m;j++)
        {
            if(sum[j]>0)
            {
                G.sigma[j]/=sum[j];
            }
            if(G.sigma[j]<=0)
            {
                G.sigma[j]=100;
            }
        }
        geometry=G;
        return 1;
    }
    else
    {
        return 0;
    }
}