示例#1
0
void		Camera::lockShader(gdl::AShader& shader, bool setColor, glm::vec4 const& color) const
{
  if (setColor)
    shader.setUniform(SHADER_CURRENT_COLOR, color);
  shader.setUniform(SHADER_TRANSFORMATION_MATRIX, getTransformationMatrix());
  shader.setUniform(SHADER_PROJECTION_MATRIX, getProjectionMatrix());
}
示例#2
0
Mat RANSAC::compute(std::vector<Mat>& X, std::vector<Mat>& Y){
	static int num_points = 5;
	Mat T = Mat::eye(4, 4, CV_32FC1);
	Mat best_T = Mat::eye(4, 4, CV_32FC1);

	int max_inliers = 0;
	std::srand(unsigned(std::time(0)));

	std::vector<int> indices;
	for (int i = 0; i < X.size(); ++i) indices.push_back(i);
	for (int i = 0; i < m_max_iterations; i++){
		//get random points
		std::random_shuffle(indices.begin(), indices.end(), myrandom);
		std::vector<Mat> new_X;
		std::vector<Mat> new_Y;
		for (int j = 0; j < num_points; ++j){
			new_X.push_back(X[indices[j]]);
			new_Y.push_back(Y[indices[j]]);
		}
		//compute T
		T = getTransformationMatrix(new_X, new_Y);
		int inliers = countInliers(T, X, Y, m_margin);

		if (inliers > max_inliers){
			best_T = T;
			max_inliers = inliers;
		}
	}
	static float percentage_outliers = 0;
	percentage_outliers = (percentage_outliers + (X.size() - max_inliers)* 100.f / X.size()) / 2;
	printf("Ouliers(Accumulated): %.2f%%		Inliers: %i \n", percentage_outliers, max_inliers);
	return best_T;
}
	void Image::render() {
		if(visible) {
			if(texture) {

				if(parent)
				{
					texture->render(this->parent->rec*getTransformationMatrix());
				} else {

					texture->render(getTransformationMatrix());

				}

			}
		}
	}
示例#4
0
void Map::draw(){
	GLuint obj_transform = gl::GetUniformLocation(_shader_program, "obj_transform");
	glm::mat4 obj_transform_matrix = getTransformationMatrix();
	gl::UseProgram(_shader_program);
	gl::UniformMatrix4fv(obj_transform, 1, gl::FALSE_, &obj_transform_matrix[0][0]);
	_cached_mesh->Render("all");
	gl::UseProgram(0);
}
示例#5
0
/*
 * Compute power basis coefficients from B-spline basis coefficients
 */
DenseMatrix getPowerBasisCoefficients(DenseVector c, std::vector<unsigned int> degrees, std::vector<double> lb, std::vector<double> ub)
{
    // Calculate tensor product transformation matrix T*lambda = c
    DenseMatrix T = getTransformationMatrix(degrees, lb, ub);

    // Compute power basis coefficients from B-spline basis coefficients
    DenseMatrix L = T.colPivHouseholderQr().solve(c);

    return L;
}
示例#6
0
std::vector<double> Transformation::transformToNormal(double kalAngleX, double kalAngleY, double kalAngleZ, double gyroXrate, double gyroYrate, double gyroZrate, double dt) {
	getTransformationMatrix(gyroXrate, gyroYrate, gyroZrate, dt);
	std::vector<double> absoluteAcceleration(3);
	for (int i = 0; i < 3; i++) {
		absoluteAcceleration[i] = kalAngleX * matrix[0][i]
			+ kalAngleY * matrix[1][i]
			+ kalAngleZ * matrix[2][i];
	}
	cancelGravity(absoluteAcceleration);
	return absoluteAcceleration;
}
示例#7
0
  Mat Transformation::getTransformationMatrix(vector<Point2f> corners, Size outputImageSize)
  {
    // Corners of the destination image
    vector<Point2f> quad_pts;
    quad_pts.push_back(Point2f(0, 0));
    quad_pts.push_back(Point2f(outputImageSize.width, 0));
    quad_pts.push_back(Point2f(outputImageSize.width, outputImageSize.height));
    quad_pts.push_back(Point2f(0, outputImageSize.height));

    return getTransformationMatrix(corners, quad_pts);
  }
示例#8
0
/*
 * Calculate coefficients of B-spline representing a multivariate polynomial
 *
 * The polynomial f(x), with x in R^n, has m terms on the form
 * f(x) = c(0)*x(0)^E(0,0)*x(1)^E(0,1)*...*x(n-1)^E(0,n-1)
 *       +c(1)*x(0)^E(1,0)*x(1)^E(1,1)*...*x(n-1)^E(1,n-1)
 *       +...
 *       +c(m-1)*x(0)^E(m-1,0)*x(1)^E(m-1,1)*...*x(n-1)^E(m-1,n-1)
 * where c in R^m is a vector with coefficients for each of the m terms,
 * and E in N^(mxn) is a matrix with the exponents of each variable in each of the m terms,
 * e.g. the first row of E defines the first term with variable exponents E(0,0) to E(0,n-1).
 *
 * Note: E must be a matrix of nonnegative integers
 */
DenseMatrix getBSplineBasisCoefficients(DenseVector c, DenseMatrix E, std::vector<double> lb, std::vector<double> ub)
{
    unsigned int dim = E.cols();
    unsigned int terms = E.rows();
    assert(dim >= 1); // At least one variable
    assert(terms >= 1); // At least one term (assumes that c is a column vector)
    assert(terms == c.rows());
    assert(dim == lb.size());
    assert(dim == ub.size());

    // Get highest power of each variable
    DenseVector powers = E.colwise().maxCoeff();

    // Store in std vector
    std::vector<unsigned int> powers2;
    for (unsigned int i = 0; i < powers.size(); ++i)
        powers2.push_back(powers(i));

    // Calculate tensor product transformation matrix T
    DenseMatrix T = getTransformationMatrix(powers2, lb, ub);

    // Compute power basis coefficients (lambda vector)
    SparseMatrix L(T.cols(),1);
    L.setZero();

    for (unsigned int i = 0; i < terms; i++)
    {
        SparseMatrix Li(1,1);
        Li.insert(0,0) = 1;

        for (unsigned int j = 0; j < dim; j++)
        {
            int e = E(i,j);
            SparseVector li(powers(j)+1);
            li.reserve(1);
            li.insert(e) = 1;

            SparseMatrix temp = Li;
            Li = kroneckerProduct(temp, li);
        }

        L += c(i)*Li;
    }

    // Compute B-spline coefficients
    DenseMatrix C = T*L;

    return C;
}
int ParticleSystem::render()
{
	Matrix4f modelMat;	// final model matrix
	
	//passing in the material information
	if (materialUsed){
		material.render(shader);
	}

	//passing the texture sample information
	if (textureUsed){
		texture.bindToTextureUnit();
		texture.setTextureSampler(shader, "texture");
	}

	glDepthMask(GL_FALSE);
	glEnable(GL_BLEND);
	glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_ONE);
	//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


	modelMat = getTransformationMatrix();

	// move matrix to shader
	shader.useProgram(1);
	shader.copyMatrixToShader(modelMat, "model");


	// bind the vao
	glBindVertexArray(vao);

	// draw using indices
	glDrawArrays(GL_POINTS, 0, numIndices);

	// unbind the vao
	glBindVertexArray(0);

	renderChildren(modelMat);
	glDisable(GL_BLEND);
	glDepthMask(GL_TRUE);
	return 0;
}
示例#10
0
文件: pop03.cpp 项目: simudream/censo
void POP03::runProblem()
{
    // Problem 3 (Nataraj)
    // cout << "\n\nSolving problem P03..." << endl;

    int dim = 4;

    // x1,x2,l1,l2
    std::vector<double> costs = {1, 0, 0, 0};
    std::vector<double> lb = {-10,-10,0,-1000};
    std::vector<double> ub = {10,10,100,1000};
    std::vector<double> z0(dim,0);

    std::vector<VariablePtr> vars;
    for (int i = 0; i < dim; i++)
    {
        auto var = std::make_shared<Variable>(costs.at(i), lb.at(i), ub.at(i));
        vars.push_back(var);
    }

    ConstraintSetPtr cs = std::make_shared<ConstraintSet>();

    { // x1^2 = l1
        VariablePtr var = vars.at(0);
        std::vector<double> thislb = {var->getLowerBound()};
        std::vector<double> thisub = {var->getUpperBound()};

        std::vector<unsigned int> deg = {2};

        DenseVector c(3);
        c.setZero();
        c(0) = 0;
        c(1) = 0;
        c(2) = 1;

        DenseMatrix T = getTransformationMatrix(deg, thislb, thisub);
        DenseMatrix coeffs = T*c;

        std::vector< std::vector<double> > knots = getRegularKnotVectors(deg, thislb, thisub);

        BSpline bs(coeffs.transpose(), knots, deg);

        std::vector<VariablePtr> cvars = {var, vars.at(2)};
        ConstraintPtr cbs = std::make_shared<ConstraintBSpline>(cvars, bs, true);
        cs->add(cbs);
    }

    { // x1^3 = l1
        VariablePtr var = vars.at(0);
        std::vector<double> thislb = {var->getLowerBound()};
        std::vector<double> thisub = {var->getUpperBound()};

        std::vector<unsigned int> deg = {3};

        DenseVector c(4); c.setZero();
        c(0) = 0;
        c(1) = 0;
        c(2) = 0;
        c(3) = 1;

        DenseMatrix T = getTransformationMatrix(deg, thislb, thisub);
        DenseMatrix coeffs = T*c;

        std::vector< std::vector<double> > knots = getRegularKnotVectors(deg, thislb, thisub);

        BSpline bs(coeffs.transpose(), knots, deg);

        std::vector<VariablePtr> cvars = {var, vars.at(3)};
        ConstraintPtr cbs = std::make_shared<ConstraintBSpline>(cvars, bs, true);
        cs->add(cbs);
    }

    { // x2 - l1 <= 0, x2 - l2 <= 0

        DenseMatrix A(2,3);
        A.setZero();
        A(0,0) = -1; A(0,1) = 1;
        A(1,0) = 1; A(1,1) = 2; A(1,2) = -1;

        DenseVector b; b.setZero(2); b(1) = -1e-5;

        std::vector<VariablePtr> cvars = {
            vars.at(1),
            vars.at(2),
            vars.at(3)
        };
        ConstraintPtr lincon = std::make_shared<ConstraintLinear>(cvars, A, b, false);
        cs->add(lincon);
    }

    BB::BranchAndBound bnb(cs);
    Timer timer;
    timer.start();
    SolverResult res = bnb.optimize();
    timer.stop();
    cout << "Time: " << timer.getMilliSeconds() << " (ms)" << endl;

    if (res.status == SolverStatus::OPTIMAL)
        fopt_found = res.objectiveValue;
}
示例#11
0
int main () {

    //start logger system
    assert(restart_gl_log());

    hardware = {}; //must initialize window before starting gl stuff

    //create our main window
    assert(start_gl());

    cursor = {};
    cursor.vertexData = new GLfloat[36 + (3 * 6)];
    setCursorCoordinates(cursor.vertexData, &cursor);
    GLfloat*cursorColourData = new GLfloat[36 + (3 * 6)];
    {
        for (int i = 0; i < 12; ++i) {
            cursorColourData[i * 3] = 0.5f;
            cursorColourData[i * 3 + 1] = 0.0f;
            cursorColourData[i * 3 + 2] = 0.5f;
        };
        for (int j = 12; j < 17; ++j) {
            cursorColourData[j * 3] = 0.5f;
            cursorColourData[j * 3 + 1] = 0.5f;
            cursorColourData[j * 3 + 2] = 0.5f;
        }
    }

    grid = {};
    grid.numberOfLines = 100;
    grid.heightValue = 0.0f;

    GLfloat *gridColourData = new GLfloat[100 * 2 * 3];
    {
        int totalVerteces = 100 * 2;
        for (int i = 0; i < totalVerteces; ++i) {
            gridColourData[i * 3  ]  = 0.5f;
            gridColourData[i * 3 + 1]  = 0.0f;
            gridColourData[i * 3 + 2]  = 0.5f;
        }
    }

    //Create our gridPoints coordinates
    GLfloat *gridVertexData = new GLfloat[grid.numberOfLines * 6];
    {
        for (int i = 0; i < grid.numberOfLines; ++i) {
            //draw the lines parallel to the x axis
            if (i < 50) {
                gridVertexData[i * 6] = i - 25;  //
                gridVertexData[i * 6 + 1] = grid.heightValue;
                gridVertexData[i * 6 + 2] = -100.f;
                gridVertexData[i * 6 + 3] = i - 25;  //
                gridVertexData[i * 6 + 4] = grid.heightValue;
                gridVertexData[i * 6 + 5] = 100.0f;
            }
            //draw the lines parallel to the z axis;
            if (i >= 50) {
                gridVertexData[i * 6] = -100.0f;  //
                gridVertexData[i * 6 + 1] = grid.heightValue;
                gridVertexData[i * 6 + 2] = i - 50 - 25.0f;
                gridVertexData[i * 6 + 3] = 100.0f;  //
                gridVertexData[i * 6 + 4] = grid.heightValue;
                gridVertexData[i * 6 + 5] = i - 50 - 25.0f;
            }
        }
    }

    //create our wall items //2 triangles , 3 points each, 3 coordinate



    Wall wall = {};
    wall.scale = vec3(1.0f, 0, 1.0f);
    wall.position = vec3(1.0f, 1.0f, 1.0f);
    GLfloat *wallVertexData = new GLfloat[2 * 3 * 3];
    {
        wallVertexData[0] = wall.scale.v[0] * 0.5f;
        wallVertexData[1] = 0.0f;
        wallVertexData[2] = - wall.scale.v[2] * 0.5f;
        wallVertexData[3] = -wall.scale.v[0] * 0.5f;
        wallVertexData[4] = 0.0f;
        wallVertexData[5] = + wall.scale.v[2] * 0.5f;
        wallVertexData[6] = - wall.scale.v[0] * 0.5f;
        wallVertexData[7] = 0.0f;
        wallVertexData[8] = - wall.scale.v[2] * 0.5f;
        wallVertexData[9] = + wall.scale.v[0] * 0.5f;
        wallVertexData[10] = 0.0f;
        wallVertexData[11] = - wall.scale.v[2] * 0.5f;
        wallVertexData[12] = + wall.scale.v[0] * 0.5f;
        wallVertexData[13] = 0.0f;
        wallVertexData[14] = + wall.scale.v[2] * 0.5f;
        wallVertexData[15] = - wall.scale.v[0] * 0.5f;
        wallVertexData[16] = 0.0f;
        wallVertexData[17] = + wall.scale.v[2] * 0.5f;
    }

    GLfloat *wallColourData = new GLfloat[2 * 3 * 3];
    {
        for (int i = 0; i < 6; ++i) {
            wallColourData[i * 3 + 0] = 0.8f;
            wallColourData[i * 3 + 1] = 0.8f;
            wallColourData[i * 3 + 2] = 0.8f;
        }
    }



    glfwSetCursorPosCallback(hardware.window,cursor_position_callback);
    glfwSetInputMode(hardware.window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
    glfwSetKeyCallback(hardware.window, key_callback);
    glfwSetInputMode(hardware.window,GLFW_STICKY_KEYS, 1);

    GLuint shader_program = create_programme_from_files(VERTEX_SHADER, FRAGMENT_SHADER);
    /* get version info */
    glEnable (GL_DEPTH_TEST); /* enable depth-testing */
    glDepthFunc (GL_LESS);

    createVertexBufferObject(&wallColourVbo, 3 * 3 * 2 * sizeof(GLfloat), wallColourData);
    createVertexBufferObject(&wallVertexVbo, 3 * 3 * 2 * sizeof(GLfloat), wallVertexData);

    createVertexBufferObject(&grid.vertexVbo, grid.numberOfLines * 6 * sizeof(GLfloat), gridVertexData);
    createVertexBufferObject(&grid.colourVbo, grid.numberOfLines * 6 * sizeof(GLfloat), gridColourData);

    createVertexBufferObject(&cursor.vertexVbo, (36 + (3 * 6)) * sizeof(GLfloat), cursor.vertexData);
    createVertexBufferObject(&cursor.colourVbo, (36 + (3 * 6)) * sizeof(GLfloat), cursorColourData);

    createVertexArrayObjet(&wallVao, &wallVertexVbo, 3);
    createVertexArrayObjet(&grid.vao, &grid.vertexVbo, 3);
    createVertexArrayObjet(&cursor.vao, &cursor.vertexVbo, 3);

    cursor.colourAttributeIndex = 1;
    grid.colourAttributeIndex = 1;
    wallColourAttributeIndex = 1;
    setColourMesh(&cursor.vao, &cursor.colourVbo, 3, &cursor.colourAttributeIndex);
    setColourMesh(&grid.vao, &grid.colourVbo, 3, &grid.colourAttributeIndex);
    setColourMesh(&wallVao, &wallColourVbo, 3, &wallColourAttributeIndex);

    free(cursor.vertexData);
    free(cursorColourData);
    free(wallVertexData);
    free(wallColourData);

    // camera stuff
#define PI 3.14159265359
#define DEG_TO_RAD (2.0 * PI) / 360.0

    float near = 0.1f;
    float far = 100.0f;
    double fov = 67.0f * DEG_TO_RAD;
    float aspect = (float)hardware.vmode->width /(float)hardware.vmode->height;

    // matrix components
    double range = tan (fov * 0.5f) * near;
    double Sx = (2.0f * near) / (range * aspect + range * aspect);
    double Sy = near / range;
    float Sz = -(far + near) / (far - near);
    float Pz = -(2.0f * far * near) / (far - near);
    GLfloat proj_mat[] = {
            Sx, 0.0f, 0.0f, 0.0f,
            0.0f, Sy, 0.0f, 0.0f,
            0.0f, 0.0f, Sz, -1.0f,
            0.0f, 0.0f, Pz, 0.0f
    };

    camera = {};

    //create view matrix
    camera.pos[0] = 0.0f; // don't start at zero, or we will be too close
    camera.pos[1] = 0.0f; // don't start at zero, or we will be too close
    camera.pos[2] = 0.5f; // don't start at zero, or we will be too close
    camera.T = translate (identity_mat4 (), vec3 (-camera.pos[0], -camera.pos[1], -camera.pos[2]));
    camera.Rpitch = rotate_y_deg (identity_mat4 (), -camera.yaw);
    camera.Ryaw = rotate_y_deg (identity_mat4 (), -camera.yaw);
    camera.viewMatrix = camera.Rpitch * camera.T;

    cursor.yaw = cursor.roll = cursor.pitch += 0.0f;
    calculateCursorRotations(&cursor);

    //create the viewmatrix of the wall
    wall.T= translate (identity_mat4 (), vec3 (2.0f, 2.0f, 2.0f));


    glUseProgram(shader_program);

    camera.view_mat_location = glGetUniformLocation(shader_program, "view");
    camera.proj_mat_location = glGetUniformLocation(shader_program, "proj");

    glUniformMatrix4fv(camera.view_mat_location, 1, GL_FALSE, camera.viewMatrix.m);
    glUniformMatrix4fv(camera.proj_mat_location, 1, GL_FALSE, proj_mat);

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_FRONT);

    walls.push_back(wall);

    while (!glfwWindowShouldClose (hardware.window)) {
        updateMovement(&camera);
        calculateViewMatrices(&camera, &cursor);
        //set the new view matrix @ the shader level

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glViewport(0, 0, hardware.vmode->width, hardware.vmode->height);
        glUseProgram(shader_program);

        //draw the cursor
        glUniformMatrix4fv(camera.view_mat_location, 1, GL_FALSE, cursor.viewMatrix.m);
        glBindVertexArray(cursor.vao);
        glDrawArrays(GL_TRIANGLES, 0, 18);

        //draw the walls in place
        //for each wall, draw it.
        //get view matrix

        for ( std::vector<Wall>::iterator it = walls.begin(); it != walls.end(); ++it) {
            Wall tempWall = *it;
            getTransformationMatrix(&tempWall);
            glUniformMatrix4fv(camera.view_mat_location, 1, GL_FALSE, tempWall.transformationMatrix.m);
            glBindVertexArray(wallVao);
            glDrawArrays(GL_TRIANGLES, 0, 6);
        }


        //draw the grid
        glUniformMatrix4fv(camera.view_mat_location, 1, GL_FALSE, camera.viewMatrix.m);
        glBindVertexArray(grid.vao);
        glDrawArrays(GL_LINES, 0, grid.numberOfLines* 2);

        glfwPollEvents();
        if (GLFW_PRESS == glfwGetKey(hardware.window, GLFW_KEY_ESCAPE)) {
            glfwSetWindowShouldClose(hardware.window, 1);
        }
        glfwSwapBuffers(hardware.window);
    }

    /* close GL context and any other GLFW resources */
    glfwTerminate();
    return 0;
}
示例#12
0
CVecR3 Local::convertToGlobal(const CVecR3& local) const {
    MatR33 transformation = getTransformationMatrix();
    CVecR3 global = transformation * local + origin_;
    return global;
}
示例#13
0
MatR33 Local::convertToGlobal(const MatR33& local) const {
    MatR33 transformation = getTransformationMatrix();
    MatR33 transformationTransposed = getTransformationMatrix().transpose();
    MatR33 global = transformation * local * transformationTransposed;
    return global;
}
示例#14
0
文件: pop10.cpp 项目: simudream/censo
void POP10::runProblem()
{
    // Problem 10 (Nataraj), QP
    // Five 1-D B-splines
    cout << "\n\nSolving problem P10..." << endl;

    int dim = 6+5; // x1,..,x5,y,l1,...,l5

    // x1,x2,x3,x4,l1,l2,l3
    std::vector<double> costs = {0,0,0,0,0,-10,1,1,1,1,1};
    std::vector<double> lb = {0,0,0,0,0,0,-INF,-INF,-INF,-INF,-INF};
    std::vector<double> ub = {1,1,1,1,1,INF,INF,INF,INF,INF,INF};
    std::vector<double> z0(dim,0);

    std::vector<VariablePtr> vars;
    for (int i = 0; i < dim; i++)
    {
        auto var = std::make_shared<Variable>(costs.at(i), lb.at(i), ub.at(i));
        vars.push_back(var);
    }

    ConstraintSetPtr cs = std::make_shared<ConstraintSet>();

    std::vector<double> a = {-10.5,-7.5,-3.5,-2.5,-1.5};

    // Add one B-spline for each variable
    for (int i = 0; i < 5; i++)
    {
        std::vector<VariablePtr> cvars = {
            vars.at(i),
            vars.at(i+6)
        };

        std::vector<double> thislb = {cvars.at(0)->getLowerBound()};
        std::vector<double> thisub = {cvars.at(0)->getUpperBound()};

        std::vector<unsigned int> deg = {2};

        // Poly coeffs
        DenseVector c(3);
        c.setZero();
        c(0) = 0;
        c(1) = a.at(i);
        c(2) = -0.5; // -50 or -0.5 (Floudas' problem has -50)

        DenseMatrix T = getTransformationMatrix(deg, thislb, thisub);
        DenseMatrix coeffs = T*c;

        std::vector< std::vector<double> > knots = getRegularKnotVectors(deg, thislb, thisub);

        BSpline bs(coeffs.transpose(), knots, deg);

        ConstraintPtr cbs = std::make_shared<ConstraintBSpline>(cvars, bs, true);

        cs->add(cbs);
    }

    { // Linear constraints

        std::vector<VariablePtr> cvars = {
            vars.at(0),
            vars.at(1),
            vars.at(2),
            vars.at(3),
            vars.at(4),
            vars.at(5)
        };

        DenseMatrix A = DenseMatrix::Zero(2,6);
        A(0,0) = 6; A(0,1) = 3; A(0,2) = 3; A(0,3) = 2; A(0,4) = 1;
        A(1,0) = 10; A(1,2) = 10; A(1,5) = 1;

        DenseVector b;
        b.setZero(2);
        b(0) = 6.5;
        b(1) = 20;

        ConstraintPtr lincon = std::make_shared<ConstraintLinear>(cvars, A, b, false);

        cs->add(lincon);
    }

    BB::BranchAndBound bnb(cs);
    Timer timer;
    timer.start();
    SolverResult res = bnb.optimize();
    timer.stop();
    cout << "Time: " << timer.getMilliSeconds() << " (ms)" << endl;
    cout << "Time: " << timer.getMicroSeconds() << " (us)" << endl;

    if (res.status == SolverStatus::OPTIMAL)
        fopt_found = res.objectiveValue;
}
示例#15
0
文件: pop01.cpp 项目: bgrimstad/censo
void POP01::runProblem()
{
    // Problem 1 (Nataraj)
    // cout << "\n\nSolving problem P01..." << endl;

    int dim = 4;

    // x1,x2,l1,l2
    std::vector<double> costs = {-1, -1, 0, 0};
    std::vector<double> lb = {0,0,-INF,-INF};
    std::vector<double> ub = {3,4,INF,INF};
    //std::vector<double> z0(dim,0);

    std::vector<VariablePtr> vars;
    for (int i = 0; i < dim; i++)
    {
        auto var = std::make_shared<Variable>(costs.at(i), lb.at(i), ub.at(i));
        vars.push_back(var);
    }

    ConstraintSetPtr cs = std::make_shared<ConstraintSet>();

    { // 2*x1^4 - 8*x1^3 + 8*x1^2 + 2

        std::vector<VariablePtr> cvars = {vars.at(0), vars.at(2)};
        std::vector<double> thislb = {cvars.at(0)->getLowerBound()};
        std::vector<double> thisub = {cvars.at(0)->getUpperBound()};

        std::vector<unsigned int> deg = {4};

        DenseVector c(5);
        c.setZero();
        c(0) = 2;
        c(1) = 0;
        c(2) = 8;
        c(3) = -8;
        c(4) = 2;

        DenseMatrix T = getTransformationMatrix(deg, thislb, thisub);
        DenseMatrix coeffs = T*c;

        std::vector< std::vector<double> > knots = getRegularKnotVectors(deg, thislb, thisub);

        BSpline bs(coeffs, knots, deg);

        ConstraintPtr cbs = std::make_shared<ConstraintBSpline>(cvars, bs, true);

        cs->add(cbs);
    }

    { // 4*x1^4 - 32*x1^3 + 88*x1^2 - 96*x1 + 36

        std::vector<VariablePtr> cvars = {vars.at(0), vars.at(3)};
        std::vector<double> thislb = {cvars.at(0)->getLowerBound()};
        std::vector<double> thisub = {cvars.at(0)->getUpperBound()};

        std::vector<unsigned int> deg = {4};

        DenseVector c(5); c.setZero();
        c(0) = 36;
        c(1) = -96;
        c(2) = 88;
        c(3) = -32;
        c(4) = 4;

        DenseMatrix T = getTransformationMatrix(deg, thislb, thisub);
        DenseMatrix coeffs = T*c;

        std::vector< std::vector<double> > knots = getRegularKnotVectors(deg, thislb, thisub);

        BSpline bs(coeffs, knots, deg);

        ConstraintPtr cbs = std::make_shared<ConstraintBSpline>(cvars, bs, true);

        cs->add(cbs);
    }

    { // x2 - l1 <= 0, x2 - l2 <= 0

        std::vector<VariablePtr> cvars = {
            vars.at(1),
            vars.at(2),
            vars.at(3)
        };

        DenseMatrix A(2,3);
        A.setZero();
        A(0,0) = 1; A(0,1) = -1;
        A(1,0) = 1; A(1,2) = -1;

        DenseVector b; b.setZero(2);

        ConstraintPtr lincon = std::make_shared<ConstraintLinear>(cvars, A, b, false);

        cs->add(lincon);
    }    

    BB::BranchAndBound solver(cs);
    Timer timer;
    timer.start();
    SolverResult res = solver.optimize();
    timer.stop();
    cout << res << endl;
    cout << res.primalVariables << endl;
    cout << "Time: " << timer.getMilliSeconds() << " (ms)" << endl;

    if (res.status == SolverStatus::OPTIMAL)
        fopt_found = res.objectiveValue;
}