Ejemplo n.º 1
0
void restrictedOptimizationHandler::simplex<Type>::simplexOptimization(const LinAlg::Matrix<Type> &c,
                                                                       const LinAlg::Matrix<Type> &A,
                                                                       const LinAlg::Matrix<Type> &b,
                                                                       LinAlg::Matrix<Type> &B,
                                                                       LinAlg::Matrix<Type> &N)
{
//    %% selecione uma base B e calcule B^-1
    LinAlg::Matrix<Type> An = A(from(1)-->A.getNumberOfRows(),N);
    LinAlg::Matrix<Type> Ab = A(from(1)-->A.getNumberOfRows(),B);
//    std::cout << N << "\n";
    LinAlg::Matrix<Type> Abinv = LinAlg::inv_numeric(Ab);
//    LinAlg::Matrix<Type> Abinv = (Ab^-1);
    LinAlg::Matrix<Type> xb;
//    %% Verifique o vetor dos custos reduzidos
    for(unsigned terminate = 1; terminate <= 100; ++terminate)
    {
        LinAlg::Matrix<Type> cn = c(N,1);
        LinAlg::Matrix<Type> cb = c(B,1);
        LinAlg::Matrix<Type>An = A(from(1)-->A.getNumberOfRows(),N);
        Ab = A(from(1)-->A.getNumberOfRows(),B);
        LinAlg::Matrix<Type> r = (~cn) - (~cb)*Abinv*An;
        this->cost = ((~cb)*Abinv*b)(1,1);
        xb = Abinv*b;
        unsigned contFlag = 0;
        for (unsigned i=1; i <= r.getNumberOfColumns(); ++i)
           if(r(1,i) >= 0)
           {
               contFlag = contFlag + 1;
//%               break;
           }
        if(contFlag == r.getNumberOfColumns())
            break;
        //%determine o vetor de chegada
        unsigned indMinD = LinAlg::lineOfMinValue(~r);

        this->x = Abinv*An(from(1)-->An.getNumberOfRows(),indMinD);
        LinAlg::Matrix<Type> d = LinAlg::divPoint(xb,this->x);
        Type dMin = INFINITY;
        unsigned indMinB = 1;
        for (unsigned i = 1; i <= d.getNumberOfRows(); ++i)
           if(this->x(i,1) > 0 && d(i,1) < dMin)
           {
               dMin = d(i,1);
               indMinB = i;
           }

        Abinv = EscalSimplex((Abinv | xb | this->x),indMinB);
        Type Bout = B(1,indMinB);
        Type Nout = N(1,indMinD);
        N = N(1,from(1)-->(indMinD-1)) | Bout | N(1, from(indMinD+1)-->(N.getNumberOfColumns()));
        B = B(1,from(1)-->(indMinB-1)) | Nout | B(1,from(indMinB+1)-->(B.getNumberOfColumns()));
    }

    this->x = xb || LinAlg::Zeros<Type>(An.getNumberOfColumns(),1);
    LinAlg::Matrix<Type> ind = LinAlg::sortColumnVectorIndices<Type>(B|N);
    this->x = this->x(ind,1);
//    std::cout << "\n\n" << ind << "\n\n";
    N = LinAlg::sortColumnVector<Type>(N);
    B = LinAlg::sortColumnVector<Type>(B);
}
LinAlg::Matrix<float> SistemasLineares::GaussJacobi(LinAlg::Matrix<float> MatrizUni, unsigned MaxIterations)
{
    //Matriz Resposta
    LinAlg::Matrix<float> MatrizRes(MaxIterations, MatrizUni.getNumberOfColumns());
    LinAlg::Matrix<float> C (MatrizUni.getNumberOfRows(), MatrizUni.getNumberOfColumns() - 1);
    LinAlg::Matrix<float> g (MatrizUni.getNumberOfRows(), 1);
    LinAlg::Matrix<float> x0(C.getNumberOfColumns(), 1);

    //    //Deixa o vetor de chute inicial padronizado como vetor linha
    if(this->X0.getNumberOfColumns() < this->X0.getNumberOfRows())
        ~this->X0;

    //    //Insere o chute inicial na Matriz resposta
    for(unsigned i = 1; i < MatrizRes.getNumberOfColumns() - 1; ++i)
        x0(1,i) = this->X0(1,i);

        //Laço para contar as linhas da MatrizUni e Matriz C.
        for(unsigned i = 1; i <= MatrizUni.getNumberOfRows(); ++i)
        {   //Laço para contar as colunas da MAtrizUni e Matriz C.
            for(unsigned j = 1; j < MatrizUni.getNumberOfColumns(); ++j)
            {
                if(i != j)
                    C(i,j) = - MatrizUni(i,j)/MatrizUni(i,i);//Matriz com a diagonal zerada.
            }
            g(i,1) = MatrizUni(i,MatrizUni.getNumberOfColumns()) / MatrizUni(i,i);//Matriz dos termos independentes.
        }

        MatrizRes = ~x0;
        for(unsigned k = 1; k < MaxIterations; ++k)
        {
            x0 =  (C * x0) + g;
            MatrizRes = MatrizRes || ~x0;
        }
    return MatrizRes;
}
PolynomHandler::Polynom<Type> PolynomHandler::simplify(const Type *num,const Type *den,const unsigned &numSize,const unsigned &denSize)
{
    LinAlg::Matrix<Type> numRoots = Roots<Type>(num, numSize);
    LinAlg::Matrix<Type> denRoots = Roots<Type>(den, denSize);
    unsigned equalRootsCount = 0;

    for(unsigned i = 1; i <= numRoots.getNumberOfRows(); ++i)
          if(PolynomHandler::rootsContainRoot(numRoots(i,1),denRoots))
              ++equalRootsCount;

    LinAlg::Matrix<Type>  newNumRoots(numSize - equalRootsCount,2);
    LinAlg::Matrix<Type>  newDenRoots(denSize - equalRootsCount,2);

    unsigned cont = 1;
    for(unsigned i = 1; i <= numRoots.getNumberOfRows(); ++i)
        if(!PolynomHandler::rootsContainRoot(numRoots(i,1),denRoots))
        {
            newNumRoots(cont,1) = numRoots(i,1);
            newNumRoots(cont,2) = numRoots(i,2);
            cont++;
        }

    cont = 1;
    for(unsigned i = 1; i <= denRoots.getNumberOfRows(); ++i)
        if(!PolynomHandler::rootsContainRoot(denRoots(1,i),numRoots))
        {
            newDenRoots(cont,1) = denRoots(i,1);
            newDenRoots(cont,2) = denRoots(i,2);
            cont++;
        }

    return Polynom<Type>(Root2Poly(newNumRoots),Root2Poly(newDenRoots));

//   return ret;
}
LinAlg::Matrix<Type> LinAlg::Matrix<Type>::operator|| (LinAlg::Matrix<RightType> rhs)
{
    LinAlg::Matrix<Type>ret;

    if(this->mat == NULL)
        ret = rhs;
    else
    {
        unsigned aux = this->rows;

        if(this->columns < rhs.getNumberOfColumns())
            ret.Init(this->rows + rhs.getNumberOfRows(), rhs.getNumberOfColumns());
        else
            ret.Init(this->rows + rhs.getNumberOfRows(), this->columns);

        for(unsigned i = 0; i < this->rows; i++)
            for(unsigned j = 0; j < this->columns; j++)
                ret.mat[i][j] = this->mat[i][j];

        for(unsigned i = 1; i <= rhs.getNumberOfRows(); i++)
            for(unsigned j = 1; j <= rhs.getNumberOfColumns(); j++)
                ret(i + aux, j) = rhs(i, j);
    }

    return ret;
}
LinAlg::Matrix<float> SistemasLineares::Gauss(LinAlg::Matrix<float> MatrizUni)
{
    LinAlg::Matrix<float> MatrizGauss;
    //Laço para contagem das colunas de MatrizUni.
    for(unsigned i = 1; i < MatrizUni.getNumberOfColumns(); i++)
    {   //Laço para contagem das linhas de MatrizUni.
        for(unsigned j = i + 1; j <= MatrizUni.getNumberOfRows();  j++)
        {
            float m = MatrizUni(j,i)/MatrizUni(i,i);
            //Laço para contagem das colunas da equação.
            for(unsigned z = i ; z <= MatrizUni.getNumberOfColumns(); z++)
                MatrizUni(j,z) = MatrizUni(j,z) - m*MatrizUni(i,z);
        }
    }

    MatrizGauss = LinAlg::Zeros<float>(1, MatrizUni.getNumberOfRows());
    float R;
    for(unsigned i = 1; i <= MatrizUni.getNumberOfRows(); ++i)
    {
        unsigned k = MatrizUni.getNumberOfRows() - i + 1;
        R = 0;
        for(unsigned j = k + 1; j <= MatrizUni.getNumberOfColumns() - 1; ++j)
                R = R + MatrizUni(k, j) * MatrizGauss(1, j);

        MatrizGauss(1, k) = (MatrizUni(k, MatrizUni.getNumberOfColumns()) - R) / MatrizUni(k, k);
    }
    return MatrizGauss;
}
LinAlg::Matrix<Type> LinAlg::operator~ (LinAlg::Matrix<Type> mat)
{
    LinAlg::Matrix<Type> temp(mat.getNumberOfColumns(), mat.getNumberOfRows());

    for(unsigned i = 1; i <= mat.getNumberOfRows(); i++)
        for(unsigned j = 1; j <= mat.getNumberOfColumns(); j++)
            temp(j, i) = mat(i, j);

    return temp;
}
Ejemplo n.º 7
0
void OptimizationHandler::RecursiveLeastSquare<Type>::Optimize(LinAlg::Matrix<Type> Input, LinAlg::Matrix<Type> Output)
{
    this->model->setLinearVector(Input, Output(from(1)-->Output.getNumberOfRows(),1));

    LinAlg::Matrix<Type> phi = this->model->getLinearVectorA();
    E = Output(from(1)-->Output.getNumberOfRows(),2) - phi*this->model->getModelCoef();
    K = (P*~phi)/(((phi*P)*~phi) + lambda);
    this->model->setModelCoef(this->model->getModelCoef() + K*E);
    P = (P - (K*(phi*P)))/lambda;
}
Ejemplo n.º 8
0
void PlotHandler::plot<Type>::generalPlot(LinAlg::Matrix<Type> X, LinAlg::Matrix<Type> Y)
{
//    centralWidget = new QWidget(this->properties.MainWindow);
//    centralWidget->setGeometry(QRect(this->properties.windowPosX, this->properties.windowPosY, this->properties.windowSizeX, this->properties.windowSizeY));
    customPlot = new QCustomPlot(this->properties.PlotFrame);
    customPlot->setGeometry(QRect(0, 0,this->properties.PlotFrame->geometry().width(), this->properties.PlotFrame->geometry().height()));

    // add title layout element:
    if(this->properties.titleFlag)
    {
        customPlot->plotLayout()->insertRow(0);
        customPlot->plotLayout()->addElement(0, 0, new QCPPlotTitle(this->customPlot, this->properties.title.c_str()));
    }

    if(X.getNumberOfColumns() == Y.getNumberOfColumns() && X.getNumberOfRows() == Y.getNumberOfRows())
    {
        this->properties.rows = X.getNumberOfRows();
        this->properties.columns = X.getNumberOfColumns();
//        this->setLegend();

        QPen pen;
        // add graphs with different scatter styles:
        for (unsigned i = 0; i < this->properties.rows; ++i)
        {
          customPlot->addGraph();
          pen.setColor(QColor(qSin(i*0.3)*100+100, qSin(i*0.6+0.7)*100+100, qSin(i*0.4+0.6)*100+100));
          // generate data:
          QVector<double> x(this->properties.columns), y(this->properties.columns);
          for (unsigned k = 0; k < this->properties.columns; ++k)
          {
            x[k] = X(i+1,k+1);
            y[k] = Y(i+1,k+1);
          }
          customPlot->graph()->setData(x, y);
          customPlot->graph()->rescaleAxes(true);
          customPlot->graph()->setPen(pen);

          if(this->properties.variablesNameFlag)
              customPlot->graph()->setName("Grafico" + QString::number(i+1));

          customPlot->graph()->setLineStyle(QCPGraph::lsLine);

          if(this->properties.xLabelFlag)
              customPlot->xAxis->setLabel(this->properties.xLabel.c_str());
          if(this->properties.yLabelFlag)
              customPlot->yAxis->setLabel(this->properties.yLabel.c_str());
          // set scatter style:
        }
     }
    customPlot->rescaleAxes();
}
Ejemplo n.º 9
0
void restrictedOptimizationHandler::activeSet<Type>::KKT(LinAlg::Matrix<Type> A,
                                                         LinAlg::Matrix<Type> b,
                                                         LinAlg::Matrix<Type> &x,
                                                         LinAlg::Matrix<Type> &v)
{
    LinAlg::Matrix<Type> K = (this->QuadMat | (~A)) ||
                             (A | LinAlg::Zeros<Type>(A.getNumberOfRows(),A.getNumberOfRows()));
    LinAlg::Matrix<Type> L = -this->LinMat || b;
    LinAlg::Matrix<Type> S = (((~K)*K)^-1)*(~K)*L;
    x = S(from(1)-->this->LinMat.getNumberOfRows(),1);
    if(A.getNumberOfRows() > 0)
        v = S(from(this->LinMat.getNumberOfRows()+1)-->(this->LinMat.getNumberOfRows() + b.getNumberOfRows()),
              from(1)-->S.getNumberOfColumns());
}
void SistemasLineares::LU_Factorization(LinAlg::Matrix<float> Matriz, LinAlg::Matrix<float> &L, LinAlg::Matrix<float> &U)//Para Matriz Quadrada.
{
    L = LinAlg::Eye<float>(Matriz.getNumberOfRows());

    for(unsigned i = 1; i < Matriz.getNumberOfColumns(); ++i)
    {   //Laço para contagem das linhas de MatrizUni.
        for(unsigned j = i + 1; j <= Matriz.getNumberOfRows();  ++j)
        {
            L(j, i) = Matriz(j, i)/Matriz(i, i);
            //Laço para contagem das colunas da equação.
            for(unsigned z = i ; z <= Matriz.getNumberOfColumns(); ++z)
                Matriz(j, z) = Matriz(j, z) - L(j, i)*Matriz(i, z);
        }
    }
    U = Matriz;
}
Ejemplo n.º 11
0
Type LinAlg::Trace (const LinAlg::Matrix<Type>& mat)
{
    Type ret = 0;

    if(mat.getNumberOfRows() != mat.getNumberOfColumns())
        std::cout << "O traco so e calculado para matrizes quadradas.";
    else
    {
        for(unsigned i = 1; i <= mat.getNumberOfRows(); i++)
            for(unsigned j = 1; j <= mat.getNumberOfColumns(); j++)
                if(i == j)
                    ret += mat(i, j);
    }

    return ret;
}
bool PolynomHandler::rootsContainRoot(const Type &root, const LinAlg::Matrix<Type> &roots)
{
    for(unsigned i = 1; i <= roots.getNumberOfRows(); ++i)
    {
        if(root == roots(1,i))
            return 1;
    }
    return 0;
}
Type NeuralNetworkHandler::Neuron<Type>::sim(LinAlg::Matrix<Type> Input, LinAlg::Matrix<Type> Weight, Type Bias){

    Type tempOutput = 0;

    if(Input.getNumberOfRows() < Input.getNumberOfColumns() || Weight.getNumberOfRows() < Weight.getNumberOfColumns())
        std::cout<<"As dimensoes nao batem. Impossivel realizar operacao."<<std::endl;
    else{
        this->Input = Input;
        this->Weight = Weight;
        this->Bias = Bias;

        tempOutput = ((~this->Input * this->Weight) - this->Bias)(1,1);
        //std::cout<<tempOutput<<std::endl;

        if(this->ActiveFunctionType == "Signal")
            this->Output = this->Signal(tempOutput);
        else if(this->ActiveFunctionType == "HardLimit")
            this->Output = this->HardLimit(tempOutput);
        else if(this->ActiveFunctionType == "Linear")
            this->Output = this->Linear(tempOutput);
        else if(this->ActiveFunctionType == "PositiveLinear")
            this->Output = this->PositiveLinear(tempOutput);
        else if(this->ActiveFunctionType == "SymmetricLinear")
            this->Output = this->SymmetricLinear(tempOutput);
        else if(this->ActiveFunctionType == "SaturatingLinear")
            this->Output = this->SaturatingLinear(tempOutput);
        else if(this->ActiveFunctionType == "Gaussian")
            this->Output = this->Gaussian(tempOutput);
        else if(this->ActiveFunctionType == "Multiquadrics")
            this->Output = this->Multiquadrics(tempOutput);
        else if(this->ActiveFunctionType == "InverseMultiquadrics")
            this->Output = this->InverseMultiquadrics(tempOutput);
        else if(this->ActiveFunctionType == "Sigmoid")
            this->Output = this->Sigmoid(tempOutput);
        else if(this->ActiveFunctionType == "HyperbolicTangentSigmoid")
            this->Output = this->HyperbolicTangentSigmoid(tempOutput);
        else{
            std::cout<<"Metodo nao encontrado"<<std::endl;
            this->Output = tempOutput;
        }
        return Output;
    }
    return 0;
}
Ejemplo n.º 14
0
LinAlg::Matrix<Type> restrictedOptimizationHandler::simplex<Type>::EscalSimplex(LinAlg::Matrix<Type> A,
                                                                                unsigned index)
{
    for(unsigned i = 1; i <= A.getNumberOfColumns(); ++i)
        A(index,i) = A(index,i)/A(index,A.getNumberOfColumns());

    for (unsigned i = 1; i <= A.getNumberOfRows(); ++i)
        if(i != index)
        {
            Type m = A(i,A.getNumberOfColumns())/A(index,A.getNumberOfColumns());
            for(unsigned j = 1; j <= A.getNumberOfColumns(); ++j)
                A(i,j)=A(i,j)-m*A(index,j);
        }

//    Xb   = A(from(1)-->A.getNumberOfRows(),A.getNumberOfColumns()-1);
    LinAlg::Matrix<Type> Binv = A(from(1)-->A.getNumberOfRows(),from(1)-->A.getNumberOfColumns()-2);
//    X    = A(from(1)--> A.getNumberOfRows(),A.getNumberOfColumns());
    return Binv;
}
void SistemasLineares::CritLinhas(LinAlg::Matrix<float> MatrizUni)
{
    LinAlg::Matrix<float> MatrizRes(1,MatrizUni.getNumberOfRows());
    for(unsigned i = 1; i <= MatrizUni.getNumberOfRows(); i++)
    {
        for(unsigned j =1; j <= MatrizUni.getNumberOfColumns(); j++)
        {
            if(i != j)
            {
                MatrizRes(1, i) += MatrizUni(i,j)/MatrizUni(i,i);
            }
        }
        if(MatrizRes(1, i) > 1)
        {
            cout<<"O sistema não possui solução para qualquer valor inicial de X0";
            break;
        }
    }
}
Ejemplo n.º 16
0
LinAlg::Matrix<Type> restrictedOptimizationHandler::activeSet<Type>::activeRestrictions(const LinAlg::Matrix<Type> &A,
                                                                                        const LinAlg::Matrix<Type> &b,
                                                                                        Type tol)
{
    LinAlg::Matrix<Type> restrictionsTest = (A*this->x - b), ind;
    for(unsigned i = 1; i <= restrictionsTest.getNumberOfRows(); ++i)
        if(restrictionsTest(i,1) >= tol)
            ind = ind | Type(i);
    return ind;
}
Ejemplo n.º 17
0
bool LinAlg::Matrix<Type>::CheckDimensions (const LinAlg::Matrix<OtherMatrixType>& rhs, unsigned operation)
{
    bool checked;

    switch(operation)
    {
    case 0:
        try
        {
            if((this->rows == rhs.getNumberOfRows()) && (this->columns == rhs.getNumberOfColumns()))
                checked = true;
            else
            {
                throw "As dimensoes nao batem. Impossivel realizar operacao";
                checked = false;
            }
        }
        catch(const char* msg)
        {
            std::cerr << msg;
        }
        break;
    case 1:
        try
        {
            if(this->columns == rhs.getNumberOfRows())
                checked = true;
            else
            {
                throw "As dimensoes nao batem. Impossivel realizar operacao";
                checked = false;
            }
        }
        catch(const char* msg)
        {
            std::cerr << msg;
        }
        break;
    }

    return checked;
}
bool LinAlg::Matrix<Type>::CheckDimensions (const LinAlg::Matrix<OtherMatrixType>& rhs, unsigned operation)
{
    bool checked = false;

    switch(operation)
    {
    case 0:
        if((this->rows == rhs.getNumberOfRows()) && (this->columns == rhs.getNumberOfColumns()))
            checked = true;
        else
            std::cout << "As dimensoes nao batem. Impossivel realizar operacao." << std::endl;
        break;
    case 1:
        if(this->columns == rhs.getNumberOfRows())
            checked = true;
        else
            std::cout << "As dimensoes nao batem. Impossivel realizar operacao." << std::endl;
        break;
    }

    return checked;
}
void SistemasLineares::PivotParcial(LinAlg::Matrix<float> &MatrizUni, int cols)
{
    SistemasLineares A;
    LinAlg::Matrix<float> AUX;

        for(unsigned i = cols + 1; i <= MatrizUni.getNumberOfRows(); i++){
            if(abs(MatrizUni(cols,cols)) < abs(MatrizUni(i,cols)))
            {
//                AUX = LinAlg::Matrix<float>::GetRow(cols);
//                LinAlg::Matrix<float>::SwapRows(cols,i);
//                LinAlg::Matrix<float>::SwapRows(i,AUX);
            }
      }
}
Ejemplo n.º 20
0
void LinAlg::Balance (LinAlg::Matrix<Type> &matrix_to_balance)
{
    unsigned aux = 0;
    Type radix = FLT_RADIX, sqrdx = radix*radix, s, r, g, f, c;

    while(aux == 0)
    {
        aux = 1;
        for(unsigned i = 1; i <= matrix_to_balance.getNumberOfRows(); i++)
        {
            r = c = 0.0;
            for(unsigned j = 1; j <= matrix_to_balance.getNumberOfColumns(); j++)
                if( j != i)
                {
                    c += std::fabs(matrix_to_balance(j, i));
                    r += std::fabs(matrix_to_balance(i, j));
                }
            if(c && r)
            {
                g = r/radix;
                f = 1.0;
                s = c + r;
                while(c < g)
                {
                    f *= radix;
                    c *= sqrdx;
                }

                g = r*radix;
                while(c > g)
                {
                    f /= radix;
                    c /= sqrdx;
                }
                if((c + r)/f < 0.95*s)
                {
                    aux = 0;
                    g = 1.0/f;
                    for(unsigned j = 1; j <= matrix_to_balance.getNumberOfColumns(); j++)
                    {
                        matrix_to_balance(i, j) *= g;
                        matrix_to_balance(j, i) *= f;
                    }
                }
            }
        }
    }

}
Ejemplo n.º 21
0
void LinAlg::Matrix<Type>::swap (const LinAlg::Matrix<OtherMatrixType>& otherMatrix)
{
    using std::swap;

    LinAlg::Matrix<Type> temp(otherMatrix.getNumberOfRows(), otherMatrix.getNumberOfColumns());

    for(unsigned i = 0; i < temp.rows; i++)
        for(unsigned j = 0; j < temp.columns; j++)
            temp.mat[i][j] = otherMatrix(i, j);

    swap (rows, temp.rows);
    swap (columns, temp.columns);

    swap (mat, temp.mat);
}
Ejemplo n.º 22
0
void LinAlg::QR_Factorization (const LinAlg::Matrix<Type>& input_matrix,
                               LinAlg::Matrix<Type>& output_Q_matrix,
                               LinAlg::Matrix<Type>& output_R_matrix)
{

    //Constants calculated and needed for the QR algorithm.
    unsigned R_rows = input_matrix.getNumberOfRows(), R_columns = input_matrix.getNumberOfColumns();
    Type tal, gama, sigma;

    output_Q_matrix = LinAlg::Eye<Type>(R_rows);
    output_R_matrix = input_matrix;

    for(unsigned j = 1; j <= R_columns; j++)
        for(unsigned i = R_rows; i >= j + 1; i--)
        {
            if(output_R_matrix(i, j) != 0)
            {
                LinAlg::Matrix<Type> temp;

                temp = LinAlg::Eye<Type>(R_rows);

                if(std::abs(output_R_matrix(i - 1, j)) > std::abs(output_R_matrix(i, j)))
                {
                    tal = output_R_matrix(i, j)/output_R_matrix(i - 1, j);
                    gama = 1/(std::sqrt(1 + std::pow(tal, 2)));
                    sigma = tal*gama;
                }
                else
                {
                    tal = output_R_matrix(i - 1, j)/output_R_matrix(i, j);
                    sigma = 1/(std::sqrt(1 + std::pow(tal, 2)));
                    gama = sigma*tal;
                }

                temp(i, i) = gama;
                temp(i, i - 1) = sigma;
                temp(i - 1, i) = -sigma;
                temp(i - 1, i - 1) = gama;

                output_R_matrix = (~temp)*output_R_matrix;
                output_Q_matrix *= temp;
            }

        }

}
LinAlg::Matrix<Type> PolynomHandler::Root2Poly(const LinAlg::Matrix<Type> &root)
{
    unsigned n = root.getNumberOfRows();
    LinAlg::Matrix<Type> ret(1,n+1);
    std::complex<Type> *tempPoly = new std::complex<Type> [2];
    tempPoly[0] = 1;
    tempPoly[1] = std::complex<Type>(-root(1,1),-root(1,2));
    std::complex<Type> * tempRoot = new std::complex<Type>[2];

    unsigned sizeTempPoly = 2;
    tempRoot[0] = 1;
    for(unsigned i = 2; i <= n ; ++i)
    {
        tempRoot[1] = std::complex<Type>(-root(i,1),-root(i,2));//apos o templade entre (real,imaginario) atribuição
        tempPoly = PolynomHandler::MultPoly(tempPoly,tempRoot,sizeTempPoly,2);
        sizeTempPoly++;
    }
    for(unsigned i = 0; i < sizeTempPoly; ++i)
    {
        ret(1,i+1) = tempPoly[i].real();
    }
    return ret;
}
Ejemplo n.º 24
0
void restrictedOptimizationHandler::activeSet<Type>::activeSetMethod(const LinAlg::Matrix<Type> &P,
                                                                     const LinAlg::Matrix<Type> &q,
                                                                     const LinAlg::Matrix<Type> &A0,
                                                                     const LinAlg::Matrix<Type> &b0,
                                                                     Type tol)
{
    LinAlg::Matrix<Type> S = this->activeRestrictions(A0,b0,tol);

    if(S.getNumberOfColumns() > P.getNumberOfRows())
        S = S(1,from(1)-->P.getNumberOfRows());

    if(S.getNumberOfColumns() == 0 || S.getNumberOfRows() == 0)
        S = LinAlg::Matrix<Type>(1);

    unsigned contS = 0;
    for(unsigned terminate = 1; terminate <= 100; ++terminate)
    {
        LinAlg::Matrix<Type> A = A0(S,from(1)-->A0.getNumberOfColumns());
        LinAlg::Matrix<Type> b = b0(S,1);
        LinAlg::Matrix<Type> Xeqp,v,t(b0.getNumberOfRows(),1);

        this->KKT(A,b,Xeqp,v);

        if(LinAlg::sumOfColumnsElements(A0*Xeqp - b0 <= tol)(1,1) == b0.getNumberOfRows())
        {
            if(v.getNumberOfRows() == 0)
            {
                this->x = Xeqp;
                break;
            }
            else if((LinAlg::sumOfColumnsElements<Type>(v >= 0.0)(1,1) == v.getNumberOfRows()))
            {
               this->x = Xeqp;
               break;
            }
            else
            {
               for (unsigned i = 1; i <= v.getNumberOfRows(); ++i)
               {
                    if(v(i,1)< 0)
                    {
                        S.removeColumn(i);
                        contS = S.getNumberOfColumns();
                        break;
                    }
                }
            }

            this->x = Xeqp;
        }
        else
        {
            LinAlg::Matrix<Type> pos;
            A = LinAlg::Matrix<Type>();
            b = LinAlg::Matrix<Type>();
            t = LinAlg::Zeros<Type>(b0.getNumberOfRows(),1);
            for (unsigned i = 1; i<= A0.getNumberOfRows(); ++i)
            {
               if((A0(i,from(1)-->A0.getNumberOfColumns())*Xeqp)(1,1) > b0(i,1))
               {
                   A = A || A0(i,from(1)-->A0.getNumberOfColumns());
                   b = b || LinAlg::Matrix<Type>(b0(i,1));
                   LinAlg::Matrix<Type> tTemp = (b0(i,1) - (A0(i,from(1)-->A0.getNumberOfColumns())*this->x)(1,1)/((A0(i,from(1)-->A0.getNumberOfColumns())*(Xeqp-this->x))(1,1)));
                   LinAlg::Matrix<Type> xTemp =  this->x + tTemp*(Xeqp - this->x);
                   if(LinAlg::sumOfColumnsElements<Type>((A0*xTemp - b0) <= tol)(1,1) == b0.getNumberOfRows())
                   {
                       t = t|tTemp;
                       pos = pos | i;
                   }
               }
             }
            Type MaxT = LinAlg::MaxValue(t);
            unsigned posMax = LinAlg::lineOfMaxValue(t);
            this->x = this->x + MaxT*(Xeqp - this->x);
            if(S.getNumberOfColumns() < this->QuadMat.getNumberOfRows() && pos.getNumberOfColumns() != 0 && pos.getNumberOfRows() != 0)
            {
//               contS = contS+1;
//               S(1,contS) = pos(1,posMax);
                S = S | pos(1,posMax);
            }
//            else
//            {
//               contS = 0;
//            }5
        }
    }
}