#-------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
#-------------------------------------------------------------

# Implements binary-class SVM with squared slack variables
#
#
# INPUT PARAMETERS:
# ---------------------------------------------------------------------------------------------
# NAME      TYPE    DEFAULT     MEANING
# ---------------------------------------------------------------------------------------------
# X         String  ---         Location to read the matrix X of feature vectors
# Y         String  ---         Location to read response matrix Y
# icpt      Int     0           Intercept presence
#                               0 = no intercept
#                               1 = add intercept;
# tol       Double  0.001       Tolerance (epsilon);
# reg       Double  1.0         Regularization parameter (lambda) for L2 regularization
# maxiter   Int     100         Maximum number of conjugate gradient iterations
# model     String  ---         Location to write model
# fmt       String  "text"      The output format of the output, such as "text" or "csv"
# ---------------------------------------------------------------------------------------------

# Example Execution:
# systemds -f $SYSTEMDS_ROOT/scripts/algorithms/l2-svm.dml \
#   -nvargs X=$INPUT_DIR/X Y=$INPUT_DIR/Y \
#   icpt=FALSE tol=0.001 reg=1 maxiter=100 \
#   model=$OUPUT_DIR/w fmt="text"

# Note about inputs: 
# Assumes that labels (entries in Y) are set to either -1 or +1 or non-negative integers

fmt = ifdef($fmt, "text")
intercept = ifdef($icpt, FALSE)
epsilon = ifdef($tol, 0.001)
lambda = ifdef($reg, 1.0)
maxIterations = ifdef($maxiter, 100)
verbose = ifdef($verbose, FALSE)

X = read($X)
Y = read($Y)

positive_label = max(Y)
negative_label = min(Y)
dimensions = ncol(X)

w = l2svm(X=X, Y=Y, intercept=intercept, 
  epsilon=epsilon, lambda=lambda, 
  maxIterations=maxIterations,
  verbose=verbose)

extra_model_params = matrix(0, 4, 1)
extra_model_params[1,1] = positive_label
extra_model_params[2,1] = negative_label
extra_model_params[3,1] = as.double(intercept)
extra_model_params[4,1] = dimensions

w = rbind(w, extra_model_params)
write(w, $model, format=fmt)
