#' constructor for class constraint_ROI1#'2#' @param assets number of assets, or optionally a named vector of assets specifying seed weights3#' @param op.problem an object of type "OP" (optimization problem, of \code{ROI}) specifying the complete optimization problem, see ROI help pages for proper construction of OP object.4#' @param solver string argument for what solver package to use, must have ROI plugin installed for that solver. Currently support is for \code{glpk} and \code{quadprog}.5#' @param weight_seq seed sequence of weights, see \code{\link{generatesequence}}6#' @author Hezky Varon7#' @export8constraint_ROI <- function(assets=NULL, op.problem, solver=c("glpk", "quadprog"), weight_seq=NULL)9{10#11# Structure for this constructor function borrowed from "constraints.R"12#13if(is.null(op.problem) | !inherits(op.problem, "OP"))14stop("Need to pass in optimization problem of ROI:::OP object type.")1516if (is.null(assets)) {17stop("You must specify the assets")18}1920if(is.character(assets)){21nassets=length(assets)22assetnames=assets23message("assuming equal weighted seed portfolio")24assets<-rep(1/nassets,nassets)25names(assets)<-assetnames # set names, so that other code can access it,26# and doesn't have to know about the character vector27# print(assets)28}29if(!is.null(assets)){30# TODO FIXME this doesn't work quite right on matrix of assets31if(is.numeric(assets)){32if (length(assets) == 1) {33nassets=assets34#we passed in a number of assets, so we need to create the vector35message("assuming equal weighted seed portfolio")36assets<-rep(1/nassets,nassets)37} else {38nassets = length(assets)39}40# and now we may need to name them41if (is.null(names(assets))) {42for(i in 1:length(assets)){43names(assets)[i]<-paste("Asset",i,sep=".")44}45}46}47}48print(paste("You chose to use the ",solver[1]," solver", sep=""))49return(structure(50list(51assets = assets,52constrainted_objective = op.problem,53solver = solver[1],54weight_seq = weight_seq,55objectives = list(),56call = match.call()57),58class=c("constraint_ROI","constraint")59))60}616263###############################################################################64# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios65#66# Copyright (c) 2004-2021 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt67#68# This library is distributed under the terms of the GNU Public License (GPL)69# for full details see the file COPYING70#71# $Id$72#73###############################################################################747576