# Decision variable transformation in Gurobi

I'm trying to find a way of setting the values of a binary MVar object (which is my decision variable) of size n to {-1,1}. Right now I have a vector that can either take values 0 or 1 (due to binary condition), but instead of 0 and 1, I need the variable to take the values -1 and 1. This is what I tried doing, but I don't think it's working:

x = m.addMVar(shape = n, vtype = GRB.BINARY, name = 'X') #My decision variable

y = m.addMVar(shape = n, vtype = GRB.INTEGER, name = 'Y') #Auxiliar decision variable

m.addConstr(y == 2*x - 1) #Transformation from {0,1} to {-1,1}

m.setObjective(#function with y as decision variable)


If the shape n is for example a list of indices, then you need to index the variables when you add the constraints and loop through and each index:

m.addConstrs(y[i] == 2*x[i] - 1 for i in n)


y = m.addMVar(shape = n, lb = -1, vtype = GRB.INTEGER, name = 'Y').
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.3521692752838135, "perplexity": 1604.815892399665}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-04/segments/1610703537796.45/warc/CC-MAIN-20210123094754-20210123124754-00340.warc.gz"}