/*
   Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.

   File :      midual1.java

   Purpose:  Demonstrates how to compute dual values with
             respect to a fixed integer solution for a MIO problem.

             minimize    500 s1 + 300 s2 + 10 x1 + 14 x2
             subject to  x1 + x2 >= 100
                         0 <= x1 <= 70 s1
                         0 <= x2 <= 80 s2
                         s1, s2 - binary
*/

package com.mosek.example;
import com.mosek.mosek.*;

public class midual1 {

  public static String midual1_ptf = String.join("\n",
"Task midual1",
"Objective",
"    Minimize + 10 x1 + 14 x2 + 500 s1 + 300 s2",
"Constraints",
"    'demand' [100] + x1 + x2",
"    'production1' [-inf;0] + x1 - 70 s1",
"    'production2' [-inf;0] + x2 - 80 s2",
"Variables",
"    x1 [0;+inf]",
"    x2 [0;+inf]",
"    s1 [0;1]",
"    s2 [0;1]",
"Integers",
"    s1 s2");

  public static void main (String[] args) throws java.lang.Exception {
    try (Task task = new Task()) {
      // Task contains the original mixed-integer problem
      task.readptfstring(midual1_ptf);

      task.optimize();

      if (task.getprosta(com.mosek.mosek.soltype.itg) != com.mosek.mosek.prosta.prim_feas)
        throw new java.lang.Exception("Unsuitable problem status, exiting");

      double[] xx = task.getxxslice(com.mosek.mosek.soltype.itg, 0, 2);
      System.out.println("x = " + xx[0] + ", " + xx[1]);

      // fixTask is the continuous task with fixed variables 
      try (Task fixTask = task.getfixedproblem())
      {
        fixTask.optimize();

        if (fixTask.getprosta(com.mosek.mosek.soltype.bas) != com.mosek.mosek.prosta.prim_and_dual_feas)
          throw new java.lang.Exception("Unsuitable problem status, exiting");

        double[] xfix = fixTask.getxxslice(com.mosek.mosek.soltype.bas, 0, 2);
        double[] y = fixTask.getyslice(com.mosek.mosek.soltype.bas, 0, 3);

        System.out.println("xfix = " + xfix[0] + ", " + xfix[1]);
        System.out.println("demand dual = " + y[0]);
        System.out.println("production dual = " + y[1] + ", " + y[2]);
      }
    }
  }
}
