cvxgrp / CVXR

An R modeling language for convex optimization problems.

Home Page:https://cvxr.rbind.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem does not follow GDP rules

makgeha opened this issue · comments

I am starting to learn the use of CVXR.
I am building up to a portfolio optimization problem with a small twist that each "stock" can only be purchased once.
The intent is to maximize the average return and not necessarily the total return.
Running the simple maximizing of the sum works without issues.
I am running into "Error in construct_intermediate_chain(object, candidate_solvers, gp = gp) : Problem does not follow DCP rules."
I am including a sample for demonstration.
Any help pointing me in the right direction would be greatly appreciated.

Test_Set<-data.frame(rnorm(10,5,1))
colnames(Test_Set)<-"Value"
#Test the portfolio optimization code
#Set the variable for selection
buy<-Variable(nrow(Test_Set),integer=TRUE)
#Set the number of picks
n<-sum(buy)
#Set the objective function
objective<-(t(Test_Set$Value)%*%buy)/n
#Set the basic constraint of 5 total
#setting buy to be >=0 and buy <=1 because buy is integer should make it binary 0,1
constraints<-list(buy >=0, buy <=1, n <=5)
#Set the problem
problem<-Problem(Maximize(objective),constraints)
#Solve the problem
solution<-solve(problem)