Ejemplo n.º 1
0
void *xsResource::loadData(const char *name, size_t *size)
{
	if (_loadMode == xsRuntimeContext::MODE_STAGING)
	{
		xsTChar path[XS_MAX_PATH];
		xsTcsCpy(path, _context->getBasePath());
		xsTcsCatStrS(path, XS_MAX_PATH - 1, name);
		// read data from file
		return loadDataFromFile(path, size);
	}
	
	xsValue value;
	int ret;
	if (_loadMode == xsRuntimeContext::MODE_RESOURCE)
		ret = xsBonReadValueFromRes(_res, name, &value);
	else if (_loadMode == xsRuntimeContext::MODE_FILE)
		ret = xsBonReadValueFromFd(_fd, name, &value);
	else
		return NULL;

	if (ret != XS_EC_OK || value.type != XS_VALUE_BINARY)
	{
		xsValueDestroy(&value, XS_FALSE);
		return NULL;
	}

	*size = *(size_t *)value.data.ptr;
	return (char *)value.data.ptr + sizeof(void *); // FIXME: Who will free these memory?
}
Ejemplo n.º 2
0
void ofxLayout::loadFromTemplate(string templateFolder, ofxJSONElement data){
    loadDataFromFile(templateFolder+"/layout-data.json");
    loadData(data);
    loadOfmlFromFile(templateFolder+"/index.ofml");
    loadOssFromFile(templateFolder+"/styles.oss");
    loadAnimationsFromFile(templateFolder+"/animations.json");
}
Ejemplo n.º 3
0
Dynamic::LastFmBias::LastFmBias()
    : SimpleMatchBias()
    , m_match( SimilarArtist )
    , m_mutex( QMutex::Recursive )
{
    loadDataFromFile();
}
Ejemplo n.º 4
0
void __fastcall TForm1::runAlgorithm1Click(TObject *Sender)
{
  loadDataFromFile("gaData.dat");
  dataLoaded=true;
  setParameters();
  printCriteriaGraph();
}
Ejemplo n.º 5
0
	public: BorderConditionSunWind(TaskData* td){
		BorderConditionSunWind::td = td;
		dynamical = false;
		if(td->doLoadSWDataFromFile()){
			loadDataFromFile();
			dynamical = true;
		}
	}
Ejemplo n.º 6
0
void SVMPathPlanning::testSingleProblem()
{
    PointCloudType::Ptr X, Y;
    
    loadDataFromFile ( "/home/nestor/Dropbox/projects/MultiSVMPathPlanning/out1.txt", X, Y );
    
    CornerLimitsType minCorner = make_pair<double, double> ( 0.0, 0.0 );
    CornerLimitsType maxCorner = make_pair<double, double> ( 1.0, 1.0 );
    
//     visualizeClasses(X, Y);
    
    getBorderFromPointClouds ( X, Y, minCorner, maxCorner, 400.0);
}
Ejemplo n.º 7
0
DataLayer::DataLayer(QObject *parent) :
    QObject(parent)
{
    _currentDay = NULL;
    _isPending = false;
    _authRequired = false;

    connect(&_webClient,SIGNAL(dataUpdated(Day*)),this,SLOT(setDataModel(Day*)));
    connect(&_webClient,SIGNAL(authRequiered()),this,SLOT(authenticationRequired()));
    connect(&_webClient,SIGNAL(loginFailed()),this,SLOT(authenticationRequired()));

    loadDataFromFile();
}
Ejemplo n.º 8
0
void ofxLayout::loadFromFile(string filename){
    ofxJSONElement layoutConfig;
    bool layoutParsingSuccessful = layoutConfig.open(filename);
    if(layoutParsingSuccessful){
        loadDataFromFile(filename);
        loadOfmlFromFile(layoutConfig["template"].asString()+"/index.ofml");
        loadOssFromFile(layoutConfig["template"].asString()+"/styles.oss");
        loadAnimationsFromFile(layoutConfig["template"].asString()+"/animations.json");
    }
    else{
        ofLogError("ofxLayout::loadFromFile","Unable to parse config file "+filename+".");
    }
}
Ejemplo n.º 9
0
QList<AnalysisDao::AnalysisInfo> AnalysisDao::loadAnalysesFromQuery(TrackId trackId, QSqlQuery* query) {
    QList<AnalysisDao::AnalysisInfo> analyses;
    PerformanceTimer time;
    time.start();

    if (!query->exec()) {
        LOG_FAILED_QUERY(*query) << "couldn't get analyses for track" << trackId;
        return analyses;
    }

    int bytes = 0;
    QSqlRecord queryRecord = query->record();
    const int idColumn = queryRecord.indexOf("id");
    const int typeColumn = queryRecord.indexOf("type");
    const int descriptionColumn = queryRecord.indexOf("description");
    const int versionColumn = queryRecord.indexOf("version");
    const int dataChecksumColumn = queryRecord.indexOf("data_checksum");

    while (query->next()) {
        AnalysisDao::AnalysisInfo info;
        info.analysisId = query->value(idColumn).toInt();
        info.trackId = trackId;
        info.type = static_cast<AnalysisType>(query->value(typeColumn).toInt());
        info.description = query->value(descriptionColumn).toString();
        info.version = query->value(versionColumn).toString();
        int checksum = query->value(dataChecksumColumn).toInt();
        QString dataPath = getAnalysisStoragePath().absoluteFilePath(
            QString::number(info.analysisId));
        QByteArray compressedData = loadDataFromFile(dataPath);
        int file_checksum = qChecksum(compressedData.constData(),
                                      compressedData.length());
        if (checksum != file_checksum) {
            qDebug() << "WARNING: Corrupt analysis loaded from" << dataPath
                     << "length" << compressedData.length();
            continue;
        }
        info.data = qUncompress(compressedData);
        bytes += info.data.length();
        analyses.append(info);
    }
    qDebug() << "AnalysisDAO fetched" << analyses.size() << "analyses,"
             << bytes << "bytes for track"
             << trackId << "in" << time.elapsed().debugMillisWithUnit();
    return analyses;
}
Ejemplo n.º 10
0
void GA::Initialize(std::string filePath){
	std::string fpath(filePath);
	baseCityVector = loadDataFromFile(fpath);
	std::cout << "City vector size: " << baseCityVector.size() << "\n";
	for(const auto& c : baseCityVector)
		std::cout << c << "\n";

	distances = createDistanceMatrix(baseCityVector);
	for(int i = 0; i < populationSize ; i++){
		population.push_back( Candidate(generateRandomCandidate(baseCityVector),distances) );
	}
	maxXPosition = 0;
	maxYPosition = 0;
	for(const auto& city : population[0].getCandidate()){
		if(city.getX() > maxXPosition)
			maxXPosition = city.getX();
		if(city.getY() > maxYPosition)
			maxYPosition = city.getY();
	}
}
int main(int argc, char* argv[])
{

    char hostName[200] = DEFAULT_HOSTNAME;
    int port = DEFAULT_RESMGR_TPM_PORT;

    TPMI_RH_HIERARCHY hierarchyValue;
    TPM2B_PUBLIC inPublic;
    TPM2B_SENSITIVE inPrivate;
    UINT16 size;

    memset(&inPublic,0,sizeof(TPM2B_PUBLIC));
    memset(&inPrivate,0,sizeof(TPM2B_SENSITIVE));

    setbuf(stdout, NULL);
    setvbuf (stdout, NULL, _IONBF, BUFSIZ);

    int opt = -1;
    const char *optstring = "hvH:u:r:p:d:C:";
    static struct option long_options[] = {
      {"help",0,NULL,'h'},
      {"version",0,NULL,'v'},
      {"Hierachy",1,NULL,'H'},
      {"pubfile",1,NULL,'u'},
      {"privfile",1,NULL,'r'},
      {"port",1,NULL,'p'},
      {"debugLevel",1,NULL,'d'},
      {"context",1,NULL,'C'},
      {0,0,0,0}
    };

    int returnVal = 0;
    int flagCnt = 0;
    int h_flag = 0,
        v_flag = 0,
        H_flag = 0,
        u_flag = 0,
        C_flag = 0,
        r_flag = 0;
    char *contextFile = NULL;

    if(argc == 1)
    {
        showHelp(argv[0]);
        return 0;
    }

    while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1)
    {
        switch(opt)
        {
        case 'h':
            h_flag = 1;
            break;
        case 'v':
            v_flag = 1;
            break;
        case 'H':
            if(getHierarchyValue(optarg,&hierarchyValue) != 0)
            {
                returnVal = -1;
                break;
            }
            printf("\nhierarchyValue: 0x%x\n\n",hierarchyValue);
            H_flag = 1;
            break;
        case 'u':
            size = sizeof(inPublic);
            if(loadDataFromFile(optarg, (UINT8 *)&inPublic, &size) != 0)
            {
                returnVal = -2;
                break;
            }
            u_flag = 1;
            break;
        case 'r':
            size = sizeof(inPrivate);
            if(loadDataFromFile(optarg, (UINT8 *)&inPrivate, &size) != 0)
            {
                returnVal = -3;
                break;
            }
            r_flag = 1;
            break;
        case 'p':
            if( getPort(optarg, &port) )
            {
                printf("Incorrect port number.\n");
                returnVal = -4;
            }
            break;
        case 'd':
            if( getDebugLevel(optarg, &debugLevel) )
            {
                printf("Incorrect debug level.\n");
                returnVal = -5;
            }
            break;
        case 'C':
            contextFile = optarg;
            if(contextFile == NULL || contextFile[0] == '\0')
            {
                returnVal = -6;
                break;
            }
            printf("contextFile = %s\n", contextFile);
            C_flag = 1;
            break;
        case ':':
//              printf("Argument %c needs a value!\n",optopt);
            returnVal = -7;
            break;
        case '?':
//              printf("Unknown Argument: %c\n",optopt);
            returnVal = -8;
            break;
        //default:
        //  break;
        }
        if(returnVal)
            break;
    };

    if(returnVal != 0)
        return returnVal;
    flagCnt = h_flag + v_flag + H_flag + u_flag ;
    if(flagCnt == 1)
    {
        if(h_flag == 1)
            showHelp(argv[0]);
        else if(v_flag == 1)
            showVersion(argv[0]);
        else
        {
            showArgMismatch(argv[0]);
            return -9;
        }
    }
    else if(flagCnt == 2 && H_flag == 1 && u_flag == 1)
    {

        prepareTest(hostName, port, debugLevel);

        returnVal = loadExternal(hierarchyValue, &inPublic, &inPrivate, r_flag);
        if(returnVal == 0 && C_flag)
            returnVal = saveTpmContextToFile(sysContext, handle2048rsa, contextFile);

        finishTest();

        if(returnVal)
            return -10;
    }
    else
    {
        showArgMismatch(argv[0]);
        return -11;
    }

    return 0;
}
Ejemplo n.º 12
0
int main(int argc, char *argv[])
{
    char hostName[200] = DEFAULT_HOSTNAME;
    int port = DEFAULT_RESMGR_TPM_PORT;

    setbuf(stdout, NULL);
    setvbuf (stdout, NULL, _IONBF, BUFSIZ);

    int opt = -1;
    const char *optstring = "hvk:c:P:l:q:g:o:Xp:d:";
    static struct option long_options[] = {
        {"help",0,NULL,'h'},
        {"version",0,NULL,'v'},
        {"akHandle",1,NULL,'k'},
        {"akContext",1,NULL,'c'},
        {"akPassword",1,NULL,'P'},  //add ak auth
        {"idList",1,NULL,'l'},
        {"algorithm",1,NULL,'g'},
        {"qualifiedData",1,NULL,'q'},
        {"outFile",1,NULL,'o'},
        {"passwdInHex",0,NULL,'X'},
        {"port",1,NULL,'p'},
        {"debugLevel",1,NULL,'d'},
        {0,0,0,0}
    };

    char *contextFilePath = NULL;
    TPM_HANDLE akHandle;
    TPMI_ALG_HASH algorithmId;
    PCR_LIST pcrList;

    int returnVal = 0;
    int flagCnt = 0;
    int h_flag = 0,
        v_flag = 0,
        k_flag = 0,
        c_flag = 0,
        P_flag = 0,
        l_flag = 0,
        g_flag = 0,
        q_flag = 0,
        o_flag = 0;

    if(argc == 1)
    {
        showHelp(argv[0]);
        return 0;
    }
    while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1)
    {
        switch(opt)
        {
        case 'h':
            h_flag = 1;
            break;
        case 'v':
            v_flag = 1;
            break;
        case 'k':
            if(getSizeUint32Hex(optarg,&akHandle) != 0)
            {
                showArgError(optarg, argv[0]);
                returnVal = -1;
                break;
            }
            k_flag = 1;
            break;
        case 'c':
            contextFilePath = optarg;
            if(contextFilePath == NULL || contextFilePath[0] == '\0')
            {
                returnVal = -2;
                break;
            }
            printf("contextFile = %s\n", contextFilePath);
            c_flag = 1;
            break;

        case 'P':
            sessionData.hmac.t.size = sizeof(sessionData.hmac.t) - 2;
            if(str2ByteStructure(optarg,&sessionData.hmac.t.size,sessionData.hmac.t.buffer) != 0)
            {
                returnVal = -3;
                break;
            }
            P_flag = 1;
            break;
        case 'l':
            if(parseList(optarg, &pcrList) != 0)
            {
                returnVal = -4;
                break;
            }
            l_flag = 1;
            break;
        case 'g':
            if(getSizeUint16Hex(optarg,&algorithmId) != 0)
            {
                showArgError(optarg, argv[0]);
                returnVal = -5;
                break;
            }
            g_flag = 1;
            break;
        case 'q':
            qualifedData.t.size = sizeof(qualifedData.t.buffer);
            if(loadDataFromFile(optarg, qualifedData.t.buffer, &qualifedData.t.size) != 0)
            {
              returnVal = -6;
              break;
            }
            q_flag = 1;
            break;
        case 'o':
            safeStrNCpy(outFilePath, optarg, sizeof(outFilePath));
            if(checkOutFile(outFilePath) != 0)
            {
                returnVal = -7;
                break;
            }
            o_flag = 1;
            break;
        case 'X':
            hexPasswd = true;
            break;
        case 'p':
            if( getPort(optarg, &port) )
            {
                printf("Incorrect port number.\n");
                returnVal = -8;
            }
            break;
        case 'd':
            if( getDebugLevel(optarg, &debugLevel) )
            {
                printf("Incorrect debug level.\n");
                returnVal = -9;
            }
            break;
       case ':':
            //              printf("Argument %c needs a value!\n",optopt);
            returnVal = -10;
            break;
        case '?':
            //              printf("Unknown Argument: %c\n",optopt);
            returnVal = -11;
            break;
            //default:
            //  break;
        }
        if(returnVal)
            break;
    };

    if(returnVal != 0)
        return returnVal;

    flagCnt = h_flag + v_flag + k_flag + c_flag + l_flag + g_flag + o_flag;
    if(flagCnt == 1)
    {
        if(h_flag == 1)
            showHelp(argv[0]);
        else if(v_flag == 1)
            showVersion(argv[0]);
        else
        {
            showArgMismatch(argv[0]);
            return -12;
        }
    }
    else if(flagCnt == 4 && ((k_flag || c_flag) && l_flag && g_flag && o_flag))
    {
        if(P_flag == 0)
            sessionData.hmac.t.size = 0;

        prepareTest(hostName, port, debugLevel);

        if(c_flag)
            returnVal = loadTpmContextFromFile(sysContext, &akHandle, contextFilePath);
        if(returnVal == TPM_RC_SUCCESS)
            returnVal = quote(akHandle, pcrList, algorithmId);

        finishTest();

        if(returnVal)
            return -13;
    }
    else
    {
        showArgMismatch(argv[0]);
        return -13;
    }

    return 0;
}
Ejemplo n.º 13
0
int main(int argc, char* argv[])
{
    char hostName[200] = DEFAULT_HOSTNAME;
    int port = DEFAULT_RESMGR_TPM_PORT;
    UINT16 size;

    setbuf(stdout, NULL);
    setvbuf (stdout, NULL, _IONBF, BUFSIZ);

    int opt = -1;
    const char *optstring = "hve:s:n:o:p:d:";
    static struct option long_options[] = {
      {"help",0,NULL,'h'},
      {"version",0,NULL,'v'},
      {"encKey",1,NULL,'e'},
      {"sec",1,NULL,'s'},
      {"name",1,NULL,'n'},
      {"outFile",1,NULL,'o'},
      {"port",1,NULL,'p'},
      {"debugLevel",1,NULL,'d'},
      {0,0,0,0}
    };

    int returnVal = 0;
    int flagCnt = 0;
    int h_flag = 0,
        v_flag = 0,
        e_flag = 0,
        s_flag = 0,
        n_flag = 0,
        o_flag = 0;

    if(argc == 1)
    {
        showHelp(argv[0]);
        return 0;
    }

    while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1)
    {
        switch(opt)
        {
        case 'h':
            h_flag = 1;
            break;
        case 'v':
            v_flag = 1;
            break;
        case 'e':
            size = sizeof(inPublic);
            if(loadDataFromFile(optarg, (UINT8 *)&inPublic, &size) != 0)
            {
                returnVal = -1;
                break;
            }
            e_flag = 1;
            break;
        case 's':
            inCredential.t.size = sizeof(inCredential) - 2;
            if(loadDataFromFile(optarg, inCredential.t.buffer, &inCredential.t.size) != 0)
            {
                returnVal = -2;
                break;
            }
            s_flag = 1;
            break;
        case 'n':
            objectName.t.size = sizeof(objectName) - 2;
            if(hex2ByteStructure(optarg,&objectName.t.size,objectName.t.name) != 0)
            {
                returnVal = -3;
                break;
            }
            n_flag = 1;
            break;
        case 'o':
            safeStrNCpy(outFilePath, optarg, sizeof(outFilePath));
            if(checkOutFile(outFilePath) != 0)
            {
                returnVal = -4;
                break;
            }
            o_flag = 1;
            break;
        case 'p':
            if( getPort(optarg, &port) )
            {
                printf("Incorrect port number.\n");
                returnVal = -5;
            }
            break;
        case 'd':
            if( getDebugLevel(optarg, &debugLevel) )
            {
                printf("Incorrect debug level.\n");
                returnVal = -6;
            }
            break;
        case ':':
//              printf("Argument %c needs a value!\n",optopt);
            returnVal = -7;
            break;
        case '?':
//              printf("Unknown Argument: %c\n",optopt);
            returnVal = -8;
            break;
        //default:
        //  break;
        }
        if(returnVal)
            break;
    };

    if(returnVal != 0)
        return returnVal;

    flagCnt = h_flag + v_flag + e_flag + s_flag + n_flag + o_flag;
    if(flagCnt == 1)
    {
        if(h_flag == 1)
            showHelp(argv[0]);
        else if(v_flag == 1)
            showVersion(argv[0]);
        else
        {
            showArgMismatch(argv[0]);
            return -9;
        }
    }
    else if(flagCnt == 4 && h_flag != 1 && v_flag != 1)
    {
        prepareTest(hostName, port, debugLevel);

        returnVal = makeCredential();

        finishTest();

        if(returnVal)
            return -10;
    }
    else
    {
        showArgMismatch(argv[0]);
        return -11;
    }

    return 0;
}
Ejemplo n.º 14
0
void L1regularizedL2() {
	int n;
	int m;
	double lambda = 1;
	double C = 1;
	int NMax = 10000;
	double* A_h;
	int * C_Idx_h;
	int * R_Idx_h;
	int * C_Count_h;
	int nnz, i, j, k, l;
	double* y;
	double *L;
	double *Li;

	double* A_test;
	int * C_Idx_test;
	int * R_Idx_test;
	int * C_Count_test;
	double* y_test;
	int n_test;
	int m_test;
	int nnz_test;

	loadDataFromFile("/document/rcv1_train.binary", &A_test, &R_Idx_test,
			&C_Idx_test, &C_Count_test, &y_test, &n_test, &m_test, &nnz_test,
			MAXINSTANCES, 1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test, m_test, nnz_test);

	int trainingset = m_test * 9 / 10;
	loadDataFromFile("/document/rcv1_train.binary", &A_h, &R_Idx_h, &C_Idx_h,
			&C_Count_h, &y, &n, &m, &nnz, trainingset, 1);
	printf("all features:%d , all data:%d, nnz:%d\n", n, m, nnz);

	double* A_test_final;
	int * C_Idx_test_final;
	int * R_Idx_test_final;
	int * C_Count_test_final;
	double* y_test_final;
	int n_test_final;
	int m_test_final;
	int nnz_test_final;

	loadDataFromFile("/document/rcv1_test.binary", &A_test_final,
			&R_Idx_test_final, &C_Idx_test_final, &C_Count_test_final,
			&y_test_final, &n_test_final, &m_test_final, &nnz_test_final,
			MAXINSTANCES, 1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test_final,
			m_test_final, nnz_test_final);

	double w[n];
	for (i = 0; i < n; i++)
		w[i] = 0;

	double value = 0;
	double crossvalidationValue;
	double finalTestValue;
	double finalvalidation[cscount];
	double validation[cscount];
	for (i = 0; i < n; i++)
		w[i] = 0;
	for (i = 0; i < cscount; i++) {
		if (i > 0) {
			free(L);
			free(Li);
		}
		C = CS[i];
		computeLipsitzConstantsForL1RegL2(&L, &Li, A_h, R_Idx_h, C_Idx_h,
				C_Count_h, n, nnz, C);
		value = NRCDM_L1L2SVM(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, y,
				w, lambda, C, Li, NMax, value, 0);
		crossvalidationValue = crossvalidationForL1L2SVM(A_test, R_Idx_test,
				C_Idx_test, C_Count_test, n_test, m_test, y_test, w,
				trainingset, m_test);
		finalTestValue = crossvalidationForL1L2SVM(A_test_final,
				R_Idx_test_final, C_Idx_test_final, C_Count_test_final,
				n_test_final, m_test_final, y_test_final, w, 0, m_test_final);

		printf("C:%f, crossvalidation: %f, final: %f \n", C,
				crossvalidationValue, finalTestValue);
		validation[i] = crossvalidationValue;
		finalvalidation[i] = finalTestValue;
	}

	for (i = 0; i < cscount; i++) {
		printf("C:%f, crossvalidation: %f; final: %f \n", CS[i], validation[i],
				finalvalidation[i]);
	}

}
Ejemplo n.º 15
0
int main(int argc, char* argv[])
{
    char hostName[200] = DEFAULT_HOSTNAME;
    int port = DEFAULT_RESMGR_TPM_PORT;

    TPMI_RH_HIERARCHY hierarchyValue;
    TPM2B_MAX_BUFFER data;
    TPMI_ALG_HASH  halg;
    char outHashFilePath[PATH_MAX] = {0};
    char outTicketFilePath[PATH_MAX] = {0};
    long fileSize = 0;

    setbuf(stdout, NULL);
    setvbuf (stdout, NULL, _IONBF, BUFSIZ);

    int opt = -1;
    const char *optstring = "hvH:g:I:o:t:p:d:";
    static struct option long_options[] = {
      {"help",0,NULL,'h'},
      {"version",0,NULL,'v'},
      {"Hierachy",1,NULL,'H'},
      {"halg",1,NULL,'g'},
      {"infile",1,NULL,'I'},
      {"outfile",1,NULL,'o'},
      {"ticket",1,NULL,'t'},
      {"port",1,NULL,'p'},
      {"debugLevel",1,NULL,'d'},
      {0,0,0,0}
    };

    int returnVal = 0;
    int flagCnt = 0;
    int h_flag = 0,
        v_flag = 0,
        H_flag = 0,
        g_flag = 0,
        I_flag = 0,
        o_flag = 0,
        t_flag = 0;

    if(argc == 1)
    {
        showHelp(argv[0]);
        return 0;
    }

    while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1)
    {
        switch(opt)
        {
        case 'h':
            h_flag = 1;
            break;
        case 'v':
            v_flag = 1;
            break;
        case 'H':
            if(getHierarchyValue(optarg,&hierarchyValue) != 0)
            {
                returnVal = -1;
                break;
            }
            printf("\nhierarchyValue: 0x%x\n\n",hierarchyValue);
            H_flag = 1;
            break;
        case 'g':
            if(getSizeUint16Hex(optarg,&halg) != 0)
            {
                showArgError(optarg, argv[0]);
                returnVal = -2;
                break;
            }
            printf("halg = 0x%4.4x\n", halg);
            g_flag = 1;
            break;
        case 'I':
            if( getFileSize(optarg, &fileSize) != 0)
            {
                returnVal = -3;
                break;
            }
            if(fileSize > MAX_DIGEST_BUFFER)
            {
                printf("Input data too long: %ld, should be less than %d bytes\n", fileSize, MAX_DIGEST_BUFFER);
                returnVal = -4;
                break;
            }
            data.t.size = fileSize;
            if(loadDataFromFile(optarg, data.t.buffer, &data.t.size) != 0)
            {
                returnVal = -5;
                break;
            }
            I_flag = 1;
            break;
        case 'o':
            safeStrNCpy(outHashFilePath, optarg, sizeof(outHashFilePath));
            if(checkOutFile(outHashFilePath) != 0)
            {
                returnVal = -6;
                break;
            }
            o_flag = 1;
            break;
        case 't':
            safeStrNCpy(outTicketFilePath, optarg, sizeof(outTicketFilePath));
            if(checkOutFile(outTicketFilePath) != 0)
            {
                returnVal = -7;
                 break;
            }
            t_flag = 1;
            break;
        case 'p':
            if( getPort(optarg, &port) )
            {
                printf("Incorrect port number.\n");
                returnVal = -8;
            }
            break;
        case 'd':
            if( getDebugLevel(optarg, &debugLevel) )
            {
                printf("Incorrect debug level.\n");
                returnVal = -9;
            }
            break;
        case ':':
//              printf("Argument %c needs a value!\n",optopt);
            returnVal = -10;
            break;
        case '?':
//              printf("Unknown Argument: %c\n",optopt);
            returnVal = -11;
            break;
        //default:
        //  break;
        }
        if(returnVal)
            break;
    };

    if(returnVal != 0)
        return returnVal;
    flagCnt = h_flag + v_flag + H_flag + g_flag + I_flag + o_flag + t_flag;
    if(flagCnt == 1)
    {
        if(h_flag == 1)
            showHelp(argv[0]);
        else if(v_flag == 1)
            showVersion(argv[0]);
        else
        {
            showArgMismatch(argv[0]);
            return -12;
        }
    }
    else if(flagCnt == 5 && h_flag != 1 && v_flag != 1)
    {
        prepareTest(hostName, port, debugLevel);

        returnVal = hash(hierarchyValue, &data, halg, outHashFilePath, outTicketFilePath);

        finishTest();

        if(returnVal)
            return -13;
    }
    else
    {
        showArgMismatch(argv[0]);
        return -14;
    }

    return 0;
}
Ejemplo n.º 16
0
int main(int argc, char* argv[])
{

    char hostName[200] = DEFAULT_HOSTNAME;
    int port = DEFAULT_RESMGR_TPM_PORT;

    TPM2B_SENSITIVE_CREATE  inSensitive;
    inSensitive.t.sensitive.data.t.size = 0;
    TPM2B_PUBLIC            inPublic;
    TPMI_ALG_PUBLIC type;
    TPMI_ALG_HASH nameAlg;
    TPMI_DH_OBJECT parentHandle;
    UINT32 objectAttributes = 0;
    char opuFilePath[PATH_MAX] = {0};
    char oprFilePath[PATH_MAX] = {0};
    char *contextParentFilePath = NULL;

    setbuf(stdout, NULL);
    setvbuf (stdout, NULL, _IONBF, BUFSIZ);

    int opt = -1;
    const char *optstring = "hvH:P:K:g:G:A:I:L:o:O:p:d:c:";
    static struct option long_options[] = {
      {"help",0,NULL,'h'},
      {"version",0,NULL,'v'},
      {"parent",1,NULL,'H'},
      {"pwdp",1,NULL,'P'},
      {"pwdk",1,NULL,'K'},
      {"halg",1,NULL,'g'},
      {"kalg",1,NULL,'G'},
      {"objectAttributes",1,NULL,'A'},
      {"inFile",1,NULL,'I'},
      {"policyFile",1,NULL,'L'},
      {"opu",1,NULL,'o'},
      {"opr",1,NULL,'O'},
      {"contextParent",1,NULL,'c'},
      {"port",1,NULL,'p'},
      {"debugLevel",1,NULL,'d'},
      {0,0,0,0}
    };


    int returnVal = 0;
    int flagCnt = 0;
    int h_flag = 0,
        v_flag = 0,
        H_flag = 0,
        P_flag = 0,
        K_flag = 0,
        g_flag = 0,
        G_flag = 0,
        A_flag = 0,
        I_flag = 0,
        L_flag = 0,
        o_flag = 0,
        c_flag = 0,
        O_flag = 0/*,
        f_flag = 0*/;
    if(argc == 1)
    {
        showHelp(argv[0]);
        return 0;
    }

    while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1)
    {
        switch(opt)
        {
        case 'h':
            h_flag = 1;
            break;
        case 'v':
            v_flag = 1;
            break;
        case 'H':
            if(getSizeUint32Hex(optarg,&parentHandle) != 0)
            {
                showArgError(optarg, argv[0]);
                returnVal = -1;
                break;
            }
            H_flag = 1;
            break;

        case 'P':
            sessionData.hmac.t.size = sizeof(sessionData.hmac.t) - 2;
            if(str2ByteStructure(optarg,&sessionData.hmac.t.size,sessionData.hmac.t.buffer) != 0)
            {
                returnVal = -2;
                break;
            }
            P_flag = 1;
            break;
        case 'K':
            inSensitive.t.sensitive.userAuth.t.size = sizeof(inSensitive.t.sensitive.userAuth.t) - 2;
            if(str2ByteStructure(optarg,&inSensitive.t.sensitive.userAuth.t.size, inSensitive.t.sensitive.userAuth.t.buffer) != 0)
            {
                returnVal = -3;
                break;
            }
            K_flag = 1;
            break;
        case 'g':
            if(getSizeUint16Hex(optarg,&nameAlg) != 0)
            {
                showArgError(optarg, argv[0]);
                returnVal = -4;
                break;
            }
            printf("nameAlg = 0x%4.4x\n", nameAlg);
            g_flag = 1;
            break;
        case 'G':
            if(getSizeUint16Hex(optarg,&type) != 0)
            {
                showArgError(optarg, argv[0]);
                returnVal = -5;
                break;
            }
            printf("type = 0x%4.4x\n", type);
            G_flag = 1;
            break;
        case 'A':
            if(getSizeUint32Hex(optarg,&objectAttributes) != 0)
            {
                showArgError(optarg, argv[0]);
                returnVal = -6;
                break;
            }
            A_flag = 1;//H_flag = 1;
            break;
        case 'I':
            inSensitive.t.sensitive.data.t.size = sizeof(inSensitive.t.sensitive.data) - 2;
            if(loadDataFromFile(optarg, inSensitive.t.sensitive.data.t.buffer, &inSensitive.t.sensitive.data.t.size) != 0)
            {
                returnVal = -7;
                break;
            }
            I_flag = 1;
            printf("inSensitive.t.sensitive.data.t.size = %d\n",inSensitive.t.sensitive.data.t.size);
            break;
        case 'L':
            inPublic.t.publicArea.authPolicy.t.size = sizeof(inPublic.t.publicArea.authPolicy) - 2;
            if(loadDataFromFile(optarg, inPublic.t.publicArea.authPolicy.t.buffer, &inPublic.t.publicArea.authPolicy.t.size) != 0)
            {
                returnVal = -8;
                break;
            }
            L_flag = 1;
            break;
        case 'o':
            safeStrNCpy(opuFilePath, optarg, sizeof(opuFilePath));
            if(checkOutFile(opuFilePath) != 0)
            {
                returnVal = -9;
                break;
            }
            o_flag = 1;
            break;
        case 'O':
            safeStrNCpy(oprFilePath, optarg, sizeof(oprFilePath));
            if(checkOutFile(oprFilePath) != 0)
            {
                returnVal = -10;
                break;
            }
            O_flag = 1;
            break;
        case 'c':
            contextParentFilePath = optarg;
            if(contextParentFilePath == NULL || contextParentFilePath[0] == '\0')
            {
                returnVal = -11;
                break;
            }
            printf("contextParentFile = %s\n", contextParentFilePath);
            c_flag = 1;
            break;
        case 'p':
            if( getPort(optarg, &port) )
            {
                printf("Incorrect port number.\n");
                returnVal = -12;
            }
            break;
        case 'd':
            if( getDebugLevel(optarg, &debugLevel) )
            {
                printf("Incorrect debug level.\n");
                returnVal = -13;
            }
            break;
        case ':':
//              printf("Argument %c needs a value!\n",optopt);
            returnVal = -14;
            break;
        case '?':
//              printf("Unknown Argument: %c\n",optopt);
            returnVal = -15;
            break;
        //default:
        //  break;
        }
        if(returnVal)
            break;
    };

    if(returnVal != 0)
        return returnVal;

    if(P_flag == 0)
        sessionData.hmac.t.size = 0;
    if(I_flag == 0)
        inSensitive.t.sensitive.data.t.size = 0;
    if(K_flag == 0)
        inSensitive.t.sensitive.userAuth.t.size = 0;
    if(L_flag == 0)
        inPublic.t.publicArea.authPolicy.t.size = 0;

    *((UINT8 *)((void *)&sessionData.sessionAttributes)) = 0;

    flagCnt = h_flag + v_flag + H_flag + g_flag + G_flag + c_flag ;
    if(flagCnt == 1)
    {
        if(h_flag == 1)
            showHelp(argv[0]);
        else if(v_flag == 1)
            showVersion(argv[0]);
        else
        {
            showArgMismatch(argv[0]);
            return -16;
        }
    }
    else if(flagCnt == 3 && (H_flag == 1 || c_flag == 1) && g_flag == 1 && G_flag == 1)
    {
        prepareTest(hostName, port, debugLevel);

        if(c_flag)
            returnVal = loadTpmContextFromFile(sysContext, &parentHandle, contextParentFilePath);
        if(returnVal == 0)
            returnVal = create(parentHandle, &inPublic, &inSensitive, type, nameAlg, opuFilePath, oprFilePath, o_flag, O_flag, I_flag, A_flag, objectAttributes);

        finishTest();

        if(returnVal)
            return -17;
    }
    else
    {
        showArgMismatch(argv[0]);
        return -18;
    }
    return 0;
}
Ejemplo n.º 17
0
void porovnaniePodlaIteraciiKDDB() {
	int n;
	int m;
	FILE *fp;
	fp = fopen("/tmp/Vyvoj.csv", "w");

	double lambda = 1;
	double C = 1;
	int NMax = 100;
	double* A_h;
	int * C_Idx_h;
	int * R_Idx_h;
	int * C_Count_h;
	int nnz, i, j, k, l;
	double* y;
	double *L;
	double *Li;

	double* A_test;
	int * C_Idx_test;
	int * R_Idx_test;
	int * C_Count_test;
	double* y_test;
	int n_test;
	int m_test;
	int nnz_test;

	loadDataFromFile("/home/s1052689/kddb", &A_test, &R_Idx_test, &C_Idx_test,
			&C_Count_test, &y_test, &n_test, &m_test, &nnz_test, MAXINSTANCES,
			1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test, m_test, nnz_test);

	int trainingset = m_test;
	loadDataFromFile("/home/s1052689/kddb", &A_h, &R_Idx_h, &C_Idx_h,
			&C_Count_h, &y, &n, &m, &nnz, MAXINSTANCES, 1);
	printf("all features:%d , all data:%d, nnz:%d\n", n, m, nnz);

	double* A_test_final;
	int * C_Idx_test_final;
	int * R_Idx_test_final;
	int * C_Count_test_final;
	double* y_test_final;
	int n_test_final;
	int m_test_final;
	int nnz_test_final;

	loadDataFromFile("/home/s1052689/kddb.t", &A_test_final, &R_Idx_test_final,
			&C_Idx_test_final, &C_Count_test_final, &y_test_final,
			&n_test_final, &m_test_final, &nnz_test_final, MAXINSTANCES, 1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test_final,
			m_test_final, nnz_test_final);

	double w[n];
	for (i = 0; i < n; i++)
		w[i] = 0;

	double value = 0;
	double crossvalidationValue;
	double finalTestValue;
	double finalvalidation[NMax];
	double validation[NMax];
	double finalvalidationLog[NMax];
	double validationLog[NMax];
	int nnzofWLog[NMax];
	int nnzofW[NMax];
	computeLipsitzConstantsForL1RegL2(&L, &Li, A_h, R_Idx_h, C_Idx_h,
			C_Count_h, n, nnz, C);
	for (i = 0; i < n; i++)
		w[i] = 0;
	for (i = 0; i < NMax; i++) {
		crossvalidationValue = crossvalidationForL1L2SVM(A_test, R_Idx_test,
				C_Idx_test, C_Count_test, n_test, m_test, y_test, w, 0, m_test);
		finalTestValue = crossvalidationForL1L2SVM(A_test_final,
				R_Idx_test_final, C_Idx_test_final, C_Count_test_final,
				n_test_final, m_test_final, y_test_final, w, 0, m_test_final);
		nnzofW[i] = 0;
		for (j = 0; j < n; j++) {
			if (w[j] != 0)
				nnzofW[i]++;
		}

		clock_t t1, t2;
		t1 = clock();
		value = NRCDM_L1L2SVM(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, y,
				w, lambda, C, Li, 1, value, 0);
		t2 = clock();
		float diff = ((float) t2 - (float) t1) / 1000000.0F;
		printf("calculation:%f\n", diff);

		fprintf(
				fp,
				"N,%d,crossvalidation,%f,final,%f,nnz,%d, calculaltionTime:%f\n",
				i, crossvalidationValue, finalTestValue, nnzofW[i], diff);
		printf("N,%d,crossvalidation,%f,final,%f,nnz,%d\n", i,
				crossvalidationValue, finalTestValue, nnzofW[i]);
		print_double_array(&w[0], 100);
		validation[i] = crossvalidationValue;
		finalvalidation[i] = finalTestValue;
	}

	free(L);
	free(Li);
	computeLipsitzConstantsForL1RegLog(&L, &Li, A_h, R_Idx_h, C_Idx_h,
			C_Count_h, n, nnz, C);
	for (i = 0; i < n; i++)
		w[i] = 0;
	for (i = 0; i < NMax; i++) {

		crossvalidationValue = crossvalidationForL1L2SVM(A_test, R_Idx_test,
				C_Idx_test, C_Count_test, n_test, m_test, y_test, w, 0, m_test);
		finalTestValue = crossvalidationForL1L2SVM(A_test_final,
				R_Idx_test_final, C_Idx_test_final, C_Count_test_final,
				n_test_final, m_test_final, y_test_final, w, 0, m_test_final);
		clock_t t1, t2;
		t1 = clock();
		value = NRCDM_L1Log(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, y, w,
				lambda, C, Li, 1, value, 0);

		t2 = clock();
		float diff = ((float) t2 - (float) t1) / 1000000.0F;
		printf("calculation:%f\n", diff);

		validationLog[i] = crossvalidationValue;
		finalvalidationLog[i] = finalTestValue;
		nnzofWLog[i] = 0;
		for (j = 0; j < n; j++) {
			if (w[j] != 0)
				nnzofWLog[i]++;
		}
		fprintf(fp, "N,%d,crossvalidation,%f,final,%f,nnz,%d,calculationTime:%f\n", i,
				crossvalidationValue, finalTestValue, nnzofWLog[i],diff);
		printf("N,%d,crossvalidation,%f,final,%f,nnz,%d\n", i,
				crossvalidationValue, finalTestValue, nnzofWLog[i]);
	}

	fclose(fp);
	fp = fopen("/tmp/PorovnaniePodlaIteracii.csv", "w");
	printf("N,CVLog,CVL2,FVLog,FVL2,nnzLog,nnzL2\n");
	for (i = 0; i < NMax; i++) {
		printf("%d,%f,%f,%f,%f,%d,%d\n", i, validationLog[i], validation[i],
				finalvalidationLog[i], finalvalidation[i], nnzofWLog[i],
				nnzofW[i]);
		fprintf(fp, "%d,%f,%f,%f,%f,%d,%d\n", i, validationLog[i],
				validation[i], finalvalidationLog[i], finalvalidation[i],
				nnzofWLog[i], nnzofW[i]);
	}
	fclose(fp);
}
Ejemplo n.º 18
0
void cas() {
	int n;
	int m;
	double lambda = 1;
	double C = 1;
	int NMax = 100;
	double* A_h;
	int * C_Idx_h;
	int * R_Idx_h;
	int * C_Count_h;
	int nnz, i, j, k, l;
	double* y;
	double *L;
	double *Li;

	double* A_test;
	int * C_Idx_test;
	int * R_Idx_test;
	int * C_Count_test;
	double* y_test;
	int n_test;
	int m_test;
	int nnz_test;

	loadDataFromFile("/document/rcv1_train.binary", &A_test, &R_Idx_test,
			&C_Idx_test, &C_Count_test, &y_test, &n_test, &m_test, &nnz_test,
			MAXINSTANCES, 1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test, m_test, nnz_test);

	int trainingset = m_test * 9 / 10;
	loadDataFromFile("/document/rcv1_train.binary", &A_h, &R_Idx_h, &C_Idx_h,
			&C_Count_h, &y, &n, &m, &nnz, trainingset, 1);
	printf("all features:%d , all data:%d, nnz:%d\n", n, m, nnz);

	double* A_test_final;
	int * C_Idx_test_final;
	int * R_Idx_test_final;
	int * C_Count_test_final;
	double* y_test_final;
	int n_test_final;
	int m_test_final;
	int nnz_test_final;

	loadDataFromFile("/document/rcv1_test.binary", &A_test_final,
			&R_Idx_test_final, &C_Idx_test_final, &C_Count_test_final,
			&y_test_final, &n_test_final, &m_test_final, &nnz_test_final,
			MAXINSTANCES, 1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test_final,
			m_test_final, nnz_test_final);

	double w[n];
	for (i = 0; i < n; i++)
		w[i] = 0;

	double value = 0;
	double crossvalidationValue;
	double finalTestValue;
	double finalvalidation[NMax];
	double validation[NMax];
	double finalvalidationLog[NMax];
	double validationLog[NMax];
	int nnzofWLog[NMax];
	int nnzofW[NMax];
	computeLipsitzConstantsForL1RegL2(&L, &Li, A_h, R_Idx_h, C_Idx_h,
			C_Count_h, n, nnz, C);
	for (i = 0; i < n; i++)
		w[i] = 0;

	clock_t t1, t2;
	t1 = clock();

	value = NRCDM_L1L2SVM(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, y, w,
			lambda, C, Li, 10, value, 0);
	t2 = clock();
	float diff = ((float) t2 - (float) t1) / 1000000.0F;
	printf("calculation:%f\n", diff);
}
Ejemplo n.º 19
0
void L1regularizedL2_KDDB() {
	int n;
	int m;
	double lambda = 1;
	double C = 1;
	int NMax = 50;
	double* A_h;
	int * C_Idx_h;
	int * R_Idx_h;
	int * C_Count_h;
	int nnz, i, j, k, l;
	double* y;
	double *L;
	double *Li;

	double* A_test;
	int * C_Idx_test;
	int * R_Idx_test;
	int * C_Count_test;
	double* y_test;
	int n_test;
	int m_test;
	int nnz_test;

	printf("Idem nacitavat data!\n");

	loadDataFromFile("/home/s1052689/kddb", &A_test, &R_Idx_test, &C_Idx_test,
			&C_Count_test, &y_test, &n_test, &m_test, &nnz_test, MAXINSTANCES,
			1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test, m_test, nnz_test);

	int trainingset = m_test;
	loadDataFromFile("/home/s1052689/kddb", &A_h, &R_Idx_h, &C_Idx_h,
			&C_Count_h, &y, &n, &m, &nnz, MAXINSTANCES, 1);
	printf("all features:%d , all data:%d, nnz:%d\n", n, m, nnz);

	printf("Training data loades!\n");

	double* A_test_final;
	int * C_Idx_test_final;
	int * R_Idx_test_final;
	int * C_Count_test_final;
	double* y_test_final;
	int n_test_final;
	int m_test_final;
	int nnz_test_final;

	loadDataFromFile("/home/s1052689/kddb.t", &A_test_final, &R_Idx_test_final,
			&C_Idx_test_final, &C_Count_test_final, &y_test_final,
			&n_test_final, &m_test_final, &nnz_test_final, MAXINSTANCES, 1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test_final,
			m_test_final, nnz_test_final);

	double w[n];
	for (i = 0; i < n; i++)
		w[i] = 0;

	double value = 0;
	double crossvalidationValue;
	double finalTestValue;
	double finalvalidation[cscount];
	double validation[cscount];
	for (i = 0; i < n; i++)
		w[i] = 0;
	for (i = 0; i < cscount; i++) {
		if (i > 0) {
			free(L);
			free(Li);
		}
		C = CS[i];
		printf("idem pocitat Li\n");
		computeLipsitzConstantsForL1RegL2(&L, &Li, A_h, R_Idx_h, C_Idx_h,
				C_Count_h, n, nnz, C);
		printf("idem ucit model \n");
		value = NRCDM_L1L2SVM(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, y,
				w, lambda, C, Li, NMax, value, 0);
		printf("idem ctosvalidation \n");
		crossvalidationValue = crossvalidationForL1L2SVM(A_test, R_Idx_test,
				C_Idx_test, C_Count_test, n_test, m_test, y_test, w, 1, m_test);
		printf("idem fiinal test robit \n");
		finalTestValue = crossvalidationForL1L2SVM(A_test_final,
				R_Idx_test_final, C_Idx_test_final, C_Count_test_final,
				n_test_final, m_test_final, y_test_final, w, 0, m_test_final);
		//		crossvalidationValue = finalTestValue;

		printf("C:%f, crossvalidation: %f, final: %f \n", C,
				crossvalidationValue, finalTestValue);
		validation[i] = crossvalidationValue;
		finalvalidation[i] = finalTestValue;
		print_double_array(&w[0], 100);
	}

	for (i = 0; i < cscount; i++) {
		printf("C:%f, crossvalidation: %f; final: %f \n", CS[i], validation[i],
				finalvalidation[i]);
	}

}
Ejemplo n.º 20
0
void L1regularizedLogisticRegression_KDDB() {
	int n;
	int m;
	double lambda = 1;
	double C = 1;
	int NMax = 10;
	double* A_h;
	int * C_Idx_h;
	int * R_Idx_h;
	int * C_Count_h;
	int nnz, i, j, k, l;
	double* y;
	double *L;
	double *Li;
	double* A_test;
	int * C_Idx_test;
	int * R_Idx_test;
	int * C_Count_test;
	double* y_test;
	int n_test;
	int m_test;
	int nnz_test;

	loadDataFromFile("/home/s1052689/kddb", &A_test, &R_Idx_test, &C_Idx_test,
			&C_Count_test, &y_test, &n_test, &m_test, &nnz_test, MAXINSTANCES,
			1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test, m_test, nnz_test);

	int trainingset = 0;
	loadDataFromFile("/home/s1052689/kddb", &A_h, &R_Idx_h, &C_Idx_h,
			&C_Count_h, &y, &n, &m, &nnz, MAXINSTANCES, 1);
	printf("all features:%d , all data:%d, nnz:%d\n", n, m, nnz);

	double* A_test_final;
	int * C_Idx_test_final;
	int * R_Idx_test_final;
	int * C_Count_test_final;
	double* y_test_final;
	int n_test_final;
	int m_test_final;
	int nnz_test_final;

	loadDataFromFile("/home/s1052689/kddb.t", &A_test_final, &R_Idx_test_final,
			&C_Idx_test_final, &C_Count_test_final, &y_test_final,
			&n_test_final, &m_test_final, &nnz_test_final, MAXINSTANCES, 1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test_final,
			m_test_final, nnz_test_final);

	double w[n];
	double value = 0;
	for (i = 0; i < n; i++)
		w[i] = 0;

	double crossvalidationValue;
	double finalTestValue;
	double finalvalidation[cscount];
	double validation[cscount];

	for (i = 0; i < cscount; i++) {
		if (i > 0) {
			free(L);
			free(Li);
		}
		C = CS[i];
		computeLipsitzConstantsForL1RegLog(&L, &Li, A_h, R_Idx_h, C_Idx_h,
				C_Count_h, n, nnz, C);
		printf("Idem ucit \n");
		value = NRCDM_L1Log(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, y, w,
				lambda, C, Li, NMax, value, 0);
		printf("Naucena siet \n");
		crossvalidationValue = crossvalidationForL1L2SVM(A_test, R_Idx_test,
				C_Idx_test, C_Count_test, n_test, m_test, y_test, w,
				trainingset, m_test);
		finalTestValue = crossvalidationForL1L2SVM(A_test_final,
				R_Idx_test_final, C_Idx_test_final, C_Count_test_final,
				n_test_final, m_test_final, y_test_final, w, 0, m_test_final);
		printf("C:%f, crossvalidation: %f, final: %f \n", C,
				crossvalidationValue, finalTestValue);
		validation[i] = crossvalidationValue;
		finalvalidation[i] = finalTestValue;
		printf("C:%f, crossvalidation: %f \n", C, crossvalidationValue);
		validation[i] = crossvalidationValue;
	}

	for (i = 0; i < cscount; i++) {
		printf("C:%f, crossvalidation: %f; final: %f \n", CS[i], validation[i],
				finalvalidation[i]);
	}

	//	exportDataForSVM("/document/rcv1_matrixA.csv", "/document/rcv1_vectorY.csv", A_h,
	//			 R_Idx_h, C_Idx_h, C_Count_h, y, n,  m);
}
Ejemplo n.º 21
0
int main(int argc, char *argv[])
{
	/* Save start time of program for later use */
	const time_t startTime = time(NULL);

	srand(startTime);

	/* Create pointers to manipulate with */
	Firm * firm = (Firm *) calloc(1, sizeof(Firm));

	Employee* employees = NULL;
	Employee ** sortedEmployes;

	/* Loads data from the file in the correct file in the correct directory */
	char runPath[strlen(argv[0]) + 20];
	getRunPath(argv[0], runPath);

	loadDataFromFile(firm, &employees, runPath);

	sortedEmployes = sortEmployeesBy(employees, firm->skills);

	int validCounter = 0;
	int randomCounter;

	float highestScore = 0;

	firm->bestSchedule = generateBestRandomSchedule(firm, employees, sortedEmployes, &randomCounter, &validCounter);

	if(!firm->bestSchedule)
	{
		printf("Error, no result from random generation\n");
		exit(-1);
	}

	highestScore = genScore(firm, firm->bestSchedule, employees);

	//int swapsTries;

	/**
	 * findBestRoughSwap
	 */
	firm->bestSchedule = findBestRoughSwap(firm, firm->bestSchedule, employees, &highestScore);

	/**
	 * fineSwap
	 */

	findBestFineSwap(firm, &(firm->bestSchedule), employees, &highestScore);


	Shift * tmpShift = listCopy((void*) firm->bestSchedule, sizeof(Shift));

	Employee * unassignedEmployeeList = generateUnassignedEmployeeList(tmpShift, employees);

	assignEmployeeListRandomly(tmpShift, employees, &unassignedEmployeeList);

	tmpShift = findBestRoughSwap(firm, tmpShift, employees, &highestScore);

	findBestFineSwap(firm, &tmpShift, employees, &highestScore);

	if(isValid(tmpShift, employees) && genScore(firm, firm->bestSchedule, employees) < highestScore)
	{
		listClean((void **) &(firm->bestSchedule), sizeof(Shift));
		firm->bestSchedule = tmpShift;
	}
	else
	{
		listClean((void **) &tmpShift, sizeof(Shift));
	}

	printHLine(129);
	printf("PRINTING BEST SCHEDULE (THAT I FOUND) - BY THE WAY, THE BEST SCORE WAS: %f\n", highestScore);
	printHLine(129);
	if(printScheduleHeadline(""))
	{
		printSchedule(firm->bestSchedule, employees);
	}
	printHLine(129);

	/**
	 * TODO: Clean bedstscore osv.
	 */

	cleanUp(&firm, &employees, &sortedEmployes);

	return EXIT_SUCCESS;
}
Ejemplo n.º 22
0
void porovnaniePodlaIteracii() {
	int n;
	int m;
	double lambda = 1;
	double C = 1;
	int NMax = 100;
	double* A_h;
	int * C_Idx_h;
	int * R_Idx_h;
	int * C_Count_h;
	int nnz, i, j, k, l;
	double* y;
	double *L;
	double *Li;

	double* A_test;
	int * C_Idx_test;
	int * R_Idx_test;
	int * C_Count_test;
	double* y_test;
	int n_test;
	int m_test;
	int nnz_test;

	loadDataFromFile("/document/rcv1_train.binary", &A_test, &R_Idx_test,
			&C_Idx_test, &C_Count_test, &y_test, &n_test, &m_test, &nnz_test,
			MAXINSTANCES, 1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test, m_test, nnz_test);

	int trainingset = m_test * 9 / 10;
	loadDataFromFile("/document/rcv1_train.binary", &A_h, &R_Idx_h, &C_Idx_h,
			&C_Count_h, &y, &n, &m, &nnz, trainingset, 1);
	printf("all features:%d , all data:%d, nnz:%d\n", n, m, nnz);

	double* A_test_final;
	int * C_Idx_test_final;
	int * R_Idx_test_final;
	int * C_Count_test_final;
	double* y_test_final;
	int n_test_final;
	int m_test_final;
	int nnz_test_final;

	loadDataFromFile("/document/rcv1_test.binary", &A_test_final,
			&R_Idx_test_final, &C_Idx_test_final, &C_Count_test_final,
			&y_test_final, &n_test_final, &m_test_final, &nnz_test_final,
			MAXINSTANCES, 1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test_final,
			m_test_final, nnz_test_final);

	double w[n];
	for (i = 0; i < n; i++)
		w[i] = 0;

	double value = 0;
	double crossvalidationValue;
	double finalTestValue;
	double finalvalidation[NMax];
	double validation[NMax];
	double finalvalidationLog[NMax];
	double validationLog[NMax];
	int nnzofWLog[NMax];
	int nnzofW[NMax];
	computeLipsitzConstantsForL1RegL2(&L, &Li, A_h, R_Idx_h, C_Idx_h,
			C_Count_h, n, nnz, C);
	for (i = 0; i < n; i++)
		w[i] = 0;
	for (i = 0; i < NMax; i++) {
		value = NRCDM_L1L2SVM(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, y,
				w, lambda, C, Li, 1, value, 0);
		crossvalidationValue = crossvalidationForL1L2SVM(A_test, R_Idx_test,
				C_Idx_test, C_Count_test, n_test, m_test, y_test, w,
				trainingset, m_test);
		finalTestValue = crossvalidationForL1L2SVM(A_test_final,
				R_Idx_test_final, C_Idx_test_final, C_Count_test_final,
				n_test_final, m_test_final, y_test_final, w, 0, m_test_final);
		printf("N,%d,crossvalidation,%f,final,%f,nnz,%d\n", i + 1,
				crossvalidationValue, finalTestValue, nnzofW[i]);

		validation[i] = crossvalidationValue;
		finalvalidation[i] = finalTestValue;
		nnzofW[i] = 0;
		for (j = 0; j < n; j++) {
			if (w[j] != 0)
				nnzofW[i]++;
		}
	}
	free(L);
	free(Li);
	computeLipsitzConstantsForL1RegLog(&L, &Li, A_h, R_Idx_h, C_Idx_h,
			C_Count_h, n, nnz, C);
	for (i = 0; i < n; i++)
		w[i] = 0;
	for (i = 0; i < NMax; i++) {
		value = NRCDM_L1Log(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, y, w,
				lambda, C, Li, 1, value, 0);
		crossvalidationValue = crossvalidationForL1L2SVM(A_test, R_Idx_test,
				C_Idx_test, C_Count_test, n_test, m_test, y_test, w,
				trainingset, m_test);
		finalTestValue = crossvalidationForL1L2SVM(A_test_final,
				R_Idx_test_final, C_Idx_test_final, C_Count_test_final,
				n_test_final, m_test_final, y_test_final, w, 0, m_test_final);
		validationLog[i] = crossvalidationValue;
		finalvalidationLog[i] = finalTestValue;
		nnzofWLog[i] = 0;
		for (j = 0; j < n; j++) {
			if (w[j] != 0)
				nnzofWLog[i]++;
		}
		printf("N,%d,crossvalidation,%f,final,%f,nnz,%d\n", i + 1,
				crossvalidationValue, finalTestValue, nnzofWLog[i]);
	}

	FILE *fp;
	fp = fopen("/tmp/PorovnaniePodlaIteracii.csv", "w");
	printf("N,CVLog,CVL2,FVLog,FVL2,nnzLog,nnzL2\n");
	for (i = 0; i < NMax; i++) {
		printf("%d,%f,%f,%f,%f,%d,%d\n", i + 1, validationLog[i],
				validation[i], finalvalidationLog[i], finalvalidation[i],
				nnzofWLog[i], nnzofW[i]);
		fprintf(fp, "%d,%f,%f,%f,%f,%d,%d\n", i + 1, validationLog[i],
				validation[i], finalvalidationLog[i], finalvalidation[i],
				nnzofWLog[i], nnzofW[i]);
	}
	fclose(fp);
}
Ejemplo n.º 23
0
int main(int argc, char* argv[])
{
    char hostName[200] = DEFAULT_HOSTNAME;
    int port = DEFAULT_RESMGR_TPM_PORT;

    TPMI_DH_OBJECT keyHandle;
    BYTE *msg = NULL;
    UINT16 length = 0;
    UINT16 size = 0;
    long fileSize = 0;

    TPMT_TK_HASHCHECK validation;
    TPMI_ALG_HASH halg;
    char outFilePath[PATH_MAX] = {0};
    char inMsgFileName[PATH_MAX] = {0};
    char *contextKeyFile = NULL;

    setbuf(stdout, NULL);
    setvbuf (stdout, NULL, _IONBF, BUFSIZ);

    int opt = -1;
    const char *optstring = "hvk:P:g:m:t:s:p:d:c:";
    static struct option long_options[] = {
      {"help",0,NULL,'h'},
      {"version",0,NULL,'v'},
      {"keyHandle",1,NULL,'k'},
      {"pwdk",1,NULL,'P'},
      {"halg",1,NULL,'g'},
      {"msg",1,NULL,'m'},
      {"sig",1,NULL,'s'},
      {"ticket",1,NULL,'t'},
      {"port",1,NULL,'p'},
      {"debugLevel",1,NULL,'d'},
      {"keyContext",1,NULL,'c'},
      {0,0,0,0}
    };

    int returnVal = 0;
    int flagCnt = 0;
    int h_flag = 0,
        v_flag = 0,
        k_flag = 0,
        P_flag = 0,
        g_flag = 0,
        m_flag = 0,
        t_flag = 0,
        c_flag = 0,
        s_flag = 0;

    if(argc == 1)
    {
        showHelp(argv[0]);
        return 0;
    }

    while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1)
    {
        switch(opt)
        {
        case 'h':
            h_flag = 1;
            break;
        case 'v':
            v_flag = 1;
            break;
        case 'k':
            if(getSizeUint32Hex(optarg,&keyHandle) != 0)
            {
                returnVal = -1;
                break;
            }
            k_flag = 1;
            break;
        case 'P':
            sessionData.hmac.t.size = sizeof(sessionData.hmac.t) - 2;
            if(str2ByteStructure(optarg,&sessionData.hmac.t.size,sessionData.hmac.t.buffer) != 0)
            {
                returnVal = -2;
                break;
            }
            P_flag = 1;
            break;
        case 'g':
            if(getSizeUint16Hex(optarg,&halg) != 0)
            {
                showArgError(optarg, argv[0]);
                returnVal = -3;
                break;
            }
            printf("halg = 0x%4.4x\n", halg);
            g_flag = 1;
            break;
        case 'm':
            safeStrNCpy(inMsgFileName, optarg, sizeof(inMsgFileName));
            m_flag = 1;
            break;
        case 't':
            size = sizeof(validation);
            if(loadDataFromFile(optarg, (UINT8 *)&validation, &size) != 0)
            {
                returnVal = -4;
                break;
            }
            t_flag = 1;
            break;
        case 's':
            safeStrNCpy(outFilePath, optarg, sizeof(outFilePath));
            if(checkOutFile(outFilePath) != 0)
            {
                returnVal = -5;
                break;
            }
            s_flag = 1;
            break;
        case 'p':
            if( getPort(optarg, &port) )
            {
                printf("Incorrect port number.\n");
                returnVal = -6;
            }
            break;
        case 'd':
            if( getDebugLevel(optarg, &debugLevel) )
            {
                printf("Incorrect debug level.\n");
                returnVal = -7;
            }
            break;
        case 'c':
            contextKeyFile = optarg;
            if(contextKeyFile == NULL || contextKeyFile[0] == '\0')
            {
                returnVal = -8;
                break;
            }
            printf("contextKeyFile = %s\n", contextKeyFile);
            c_flag = 1;
            break;
        case ':':
//              printf("Argument %c needs a value!\n",optopt);
            returnVal = -9;
            break;
        case '?':
//              printf("Unknown Argument: %c\n",optopt);
            returnVal = -10;
            break;
        //default:
        //  break;
        }
        if(returnVal)
            break;
    };

    if(returnVal != 0)
        goto end;

    if(m_flag)
    {
        if(getFileSize(inMsgFileName, &fileSize))
        {
            returnVal = -11;
            goto end;
        }
        if(fileSize == 0)
        {
            printf("the message file is empty !\n");
            returnVal = -12;
            goto end;
        }
        if(fileSize > 0xffff)
        {
            printf("the message file was too long !\n");
            returnVal = -13;
            goto end;
        }
        msg = (BYTE*)malloc(fileSize);
        if(msg == NULL)
        {
            returnVal = -14;
            goto end;
        }
        memset(msg, 0, fileSize);

        length = fileSize;
        if(loadDataFromFile(inMsgFileName, msg, &length) != 0)
        {
            returnVal = -15;
            goto end;
        }
#if 0
        printf("\nmsg length: %d\n",length);
        printf("msg content: ");
        for(int i = 0; i < length; i++)
        {
            printf("%02x ", msg[i]);
        }
        printf("\n");
        return -1;
#endif
    }

    if(P_flag == 0)
        sessionData.hmac.t.size = 0;
    if(t_flag == 0)
    {
        validation.tag = TPM_ST_HASHCHECK;
        validation.hierarchy = TPM_RH_NULL;
        validation.digest.t.size = 0;
    }

    flagCnt = h_flag + v_flag + k_flag + g_flag + m_flag + s_flag + c_flag;

    if(flagCnt == 1)
    {
        if(h_flag == 1)
            showHelp(argv[0]);
        else if(v_flag == 1)
            showVersion(argv[0]);
        else
        {
            showArgMismatch(argv[0]);
            returnVal = -16;
        }
    }
    else if((flagCnt == 4) && (k_flag == 1 || c_flag == 1) && (g_flag == 1) && (m_flag == 1) && (s_flag == 1))
    {
        prepareTest(hostName, port, debugLevel);

        if(c_flag)
            returnVal = loadTpmContextFromFile(sysContext, &keyHandle, contextKeyFile);
        if(returnVal == 0)
            returnVal = sign(keyHandle, halg, msg, length, &validation, outFilePath);

        finishTest();

        if(returnVal)
            returnVal = -17;
    }
    else
    {
        showArgMismatch(argv[0]);
        returnVal = -18;
    }

end:
    if(msg)
        free(msg);
    return returnVal;
}
Ejemplo n.º 24
0
int main(int argc, char* argv[])
{
    char hostName[200] = DEFAULT_HOSTNAME;
    int port = DEFAULT_RESMGR_TPM_PORT;

    TPMI_DH_OBJECT keyHandle;
    TPM2B_PUBLIC_KEY_RSA cipherText;
    char outFilePath[PATH_MAX] = {0};
    char *contextKeyFile = NULL;

    setbuf(stdout, NULL);
    setvbuf (stdout, NULL, _IONBF, BUFSIZ);

    int opt = -1;
    const char *optstring = "hvk:P:I:o:p:d:c:";
    static struct option long_options[] = {
      {"help",0,NULL,'h'},
      {"version",0,NULL,'v'},
      {"keyHandle",1,NULL,'k'},
      {"pwdk",1,NULL,'P'},
      {"inFile",1,NULL,'I'},
      {"outFile",1,NULL,'o'},
      {"port",1,NULL,'p'},
      {"debugLevel",1,NULL,'d'},
      {"keyContext",1,NULL,'c'},
      {0,0,0,0}
    };

    int returnVal = 0;
    int flagCnt = 0;
    int h_flag = 0,
        v_flag = 0,
        k_flag = 0,
        P_flag = 0,
        I_flag = 0,
        c_flag = 0,
        o_flag = 0;

    if(argc == 1)
    {
        showHelp(argv[0]);
        return 0;
    }

    while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1)
    {
        switch(opt)
        {
        case 'h':
            h_flag = 1;
            break;
        case 'v':
            v_flag = 1;
            break;
        case 'k':
            if(getSizeUint32Hex(optarg,&keyHandle) != 0)
            {
                returnVal = -1;
                break;
            }
            k_flag = 1;
            break;
        case 'P':
            sessionData.hmac.t.size = sizeof(sessionData.hmac.t) - 2;
            if(str2ByteStructure(optarg,&sessionData.hmac.t.size,sessionData.hmac.t.buffer) != 0)
            {
                returnVal = -2;
                break;
            }
            P_flag = 1;
            break;
        case 'I':
            cipherText.t.size = sizeof(cipherText) - 2;
            if(loadDataFromFile(optarg, cipherText.t.buffer, &cipherText.t.size) != 0)
            {
                returnVal = -3;
                break;
            }
            I_flag = 1;
            break;
        case 'o':
            safeStrNCpy(outFilePath, optarg, sizeof(outFilePath));
            if(checkOutFile(outFilePath) != 0)
            {
                returnVal = -4;
                break;
            }
            o_flag = 1;
            break;
        case 'p':
            if( getPort(optarg, &port) )
            {
                printf("Incorrect port number.\n");
                returnVal = -5;
            }
            break;
        case 'd':
            if( getDebugLevel(optarg, &debugLevel) )
            {
                printf("Incorrect debug level.\n");
                returnVal = -6;
            }
            break;
        case 'c':
            contextKeyFile = optarg;
            if(contextKeyFile == NULL || contextKeyFile[0] == '\0')
            {
                returnVal = -7;
                break;
            }
            printf("contextKeyFile = %s\n", contextKeyFile);
            c_flag = 1;
            break;
        case ':':
//              printf("Argument %c needs a value!\n",optopt);
            returnVal = -8;
            break;
        case '?':
//              printf("Unknown Argument: %c\n",optopt);
            returnVal = -9;
            break;
        //default:
        //  break;
        }
        if(returnVal)
            break;
    };

    if(returnVal != 0)
        return returnVal;

    if(P_flag == 0)
        sessionData.hmac.t.size = 0;

    flagCnt = h_flag + v_flag + k_flag + I_flag + o_flag + c_flag;

    if(flagCnt == 1)
    {
        if(h_flag == 1)
            showHelp(argv[0]);
        else if(v_flag == 1)
            showVersion(argv[0]);
        else
        {
            showArgMismatch(argv[0]);
            return -10;
        }
    }
    else if((flagCnt == 3) && (k_flag == 1 || c_flag == 1) && (I_flag == 1) && (o_flag == 1))
    {
        prepareTest(hostName, port, debugLevel);

        if(c_flag)
            returnVal = loadTpmContextFromFile(sysContext, &keyHandle, contextKeyFile);
        if(returnVal == 0)
            returnVal = rsaDecrypt(keyHandle, &cipherText, outFilePath);

        finishTest();

        if(returnVal)
            return -11;
    }
    else
    {
        showArgMismatch(argv[0]);
        return -12;
    }

    return 0;
}
Ejemplo n.º 25
0
int main(int argc, char* argv[])
{
    int opt;
    char type[100] = "local";
    char hostName[200] = DEFAULT_HOSTNAME;
    int port = DEFAULT_RESMGR_TPM_PORT;
    int returnVal = 0;

    struct option sOpts[] = {
        { "index"       , required_argument, NULL, 'x' },
        { "authHandle"  , required_argument, NULL, 'a' },
        { "file"        , required_argument, NULL, 'f' },
        { "handlePasswd", required_argument, NULL, 'P' },
        { "passwdInHex" , no_argument,       NULL, 'X' },
        { "port"        , required_argument, NULL, 'p' },
        { "dbg"         , required_argument, NULL, 'd' },
        { "help"        , no_argument,       NULL, 'h' },
        { "version"     , no_argument,       NULL, 'v' },
        { NULL          , no_argument,       NULL,  0  },
    };

    if(argc == 1)
    {
        showHelp(argv[0]);
        return 0;
    }

    if( argc > (int)(2*sizeof(sOpts)/sizeof(struct option)) )
    {
        showArgMismatch(argv[0]);
        return -1;
    }

    while ( ( opt = getopt_long( argc, argv, "x:a:f:P:Xp:d:hv", sOpts, NULL ) ) != -1 )
    {
        switch ( opt ) {
        case 'h':
        case '?':
            showHelp(argv[0]);
            return 0;
        case 'v':
            showVersion(argv[0]);
            return 0;

        case 'x':
            if( getSizeUint32Hex(optarg, &nvIndex) != 0 )
            {
                return -2;
            }
            break;

        case 'a':
            if( getSizeUint32Hex(optarg, &authHandle) != 0 )
            {
                return -3;
            }
            break;

        case 'P':
            if( optarg == NULL || (strlen(optarg) >= sizeof(TPMU_HA)) )
            {
                printf("\nPlease input the handle password(optional,no more than %d characters).\n", (int)sizeof(TPMU_HA)-1);
                return -4;
            }
            safeStrNCpy(&handlePasswd[0], optarg, sizeof(handlePasswd));
            break;

        case 'f':
            if( optarg == NULL )
            {
                printf("\nPlease input the nv data file.\n");
                return -5;
            }
            safeStrNCpy(&fileName[0], optarg, sizeof(fileName));
            break;

        case 'X':
            hexPasswd = true;
            break;

        case 'p':
            if( getPort(optarg, &port) )
            {
                printf("Incorrect port number.\n");
                return -6;
            }
            break;
        case 'd':
            if( getDebugLevel(optarg, &debugLevel) )
            {
                printf("Incorrect debug level.\n");
                return -7;
            }
            break;

        default:
            showArgMismatch(argv[0]);
            return -8;
        }
    }

    if( nvIndex == 0 )
    {
        printf("You must provide an index (!= 0) for the NVRAM area.\n");
        return -9;
    }

    if( authHandle == 0 )
    {
        printf("You must provide an right auth handle for this operation.\n");
        return -10;
    }

    dataSize = MAX_NV_INDEX_SIZE;
    if(loadDataFromFile(fileName, nvBuffer, &dataSize))
    {
        printf("Failed to read data from %s\n", fileName);
        return -11;
    }

    prepareTest(hostName, port, debugLevel);

    returnVal = nvWrite();

    finishTest();

    if(returnVal)
        return -12;

    return 0;
}