/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*                                                                           */
/*               This file is part of the program and library                */
/*    PaPILO --- Parallel Presolve for Integer and Linear Optimization       */
/*                                                                           */
/* Copyright (C) 2020-2025 Zuse Institute Berlin (ZIB)                       */
/*                                                                           */
/* Licensed 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.                                            */
/*                                                                           */
/* You should have received a copy of the Apache-2.0 license                 */
/* along with PaPILO; see the file LICENSE. If not visit scipopt.org.        */
/*                                                                           */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "papilo/presolvers/DominatedCols.hpp"
#include "papilo/external/catch/catch_amalgamated.hpp"
#include "papilo/core/PresolveMethod.hpp"
#include "papilo/core/Problem.hpp"
#include "papilo/core/ProblemBuilder.hpp"

using namespace papilo;

Problem<double>
setupMatrixForDominatedCols();

Problem<double>
setupMatrixForDominatedColsParallel();

Problem<double>
setupMatrixForDominatedColsMultipleParallel();

Problem<double>
setupMatrixForMultipleDominatedCols();

TEST_CASE( "domcol-happy-path", "[presolve]" )
{
   double time = 0.0;
   int cause = -1;
   Timer t{time};
   Num<double> num{};
   Message msg{};
   Problem<double> problem = setupMatrixForDominatedCols();
   Statistics statistics{};
   PresolveOptions presolveOptions{};
   PostsolveStorage<double> postsolve =
       PostsolveStorage<double>( problem, num, presolveOptions );
   ProblemUpdate<double> problemUpdate( problem, postsolve, statistics,
                                        presolveOptions, num, msg );

   DominatedCols<double> presolvingMethod{};
   Reductions<double> reductions{};
   problem.recomputeAllActivities();

   PresolveStatus presolveStatus =
       presolvingMethod.execute( problem, problemUpdate, num, reductions, t, cause);

   REQUIRE( presolveStatus == PresolveStatus::kReduced );
   REQUIRE( reductions.size() == 8 );

   REQUIRE( reductions.getReduction( 0 ).row == ColReduction::LOCKED );
   REQUIRE( reductions.getReduction( 0 ).col == 0 );

   REQUIRE( reductions.getReduction( 1 ).row == ColReduction::BOUNDS_LOCKED );
   REQUIRE( reductions.getReduction( 1 ).col == 0 );

   REQUIRE( reductions.getReduction( 2 ).row == ColReduction::LOCKED );
   REQUIRE( reductions.getReduction( 2 ).col == 1 );

   REQUIRE( reductions.getReduction( 3 ).row == ColReduction::BOUNDS_LOCKED );
   REQUIRE( reductions.getReduction( 3 ).col == 1 );

   REQUIRE( reductions.getReduction( 4 ).row == 0 );
   REQUIRE( reductions.getReduction( 4 ).col == RowReduction::LOCKED );

   REQUIRE( reductions.getReduction( 5 ).row == ColReduction::CERTIFICATE_DOMINANCE );
   REQUIRE( reductions.getReduction( 5 ).col == 1 );
   REQUIRE( reductions.getReduction( 5 ).newval == 0 );

   REQUIRE( reductions.getReduction( 6 ).row == 0 );
   REQUIRE( reductions.getReduction( 6 ).col == RowReduction::SAVE_ROW );

   REQUIRE( reductions.getReduction( 7 ).row == ColReduction::FIXED );
   REQUIRE( reductions.getReduction( 7 ).col == 1 );
   REQUIRE( reductions.getReduction( 7 ).newval == 0 );
}

TEST_CASE( "domcol-parallel-columns", "[presolve]" )
{
   double time = 0.0;
   int cause = -1;
   Timer t{time};
   Num<double> num{};
   Message msg{};
   Problem<double> problem = setupMatrixForDominatedColsParallel();
   Statistics statistics{};
   PresolveOptions presolveOptions{};
   PostsolveStorage<double> postsolve =
       PostsolveStorage<double>( problem, num, presolveOptions );
   ProblemUpdate<double> problemUpdate( problem, postsolve, statistics,
                                        presolveOptions, num, msg );

   DominatedCols<double> presolvingMethod{};
   Reductions<double> reductions{};
   problem.recomputeAllActivities();

   PresolveStatus presolveStatus =
       presolvingMethod.execute( problem, problemUpdate, num, reductions, t, cause );

   REQUIRE( presolveStatus == PresolveStatus::kReduced );
   REQUIRE( reductions.size() == 8 );

   REQUIRE( reductions.getReduction( 0 ).row == ColReduction::LOCKED );
   REQUIRE( reductions.getReduction( 0 ).col == 0 );

   REQUIRE( reductions.getReduction( 1 ).row ==ColReduction::BOUNDS_LOCKED );
   REQUIRE( reductions.getReduction( 1 ).col == 0 );

   REQUIRE( reductions.getReduction( 2 ).row == ColReduction::LOCKED );
   REQUIRE( reductions.getReduction( 2 ).col == 3 );

   REQUIRE( reductions.getReduction( 3 ).row ==ColReduction::BOUNDS_LOCKED );
   REQUIRE( reductions.getReduction( 3 ).col == 3 );

   REQUIRE( reductions.getReduction( 4 ).row == 0 );
   REQUIRE( reductions.getReduction( 4 ).col == RowReduction::LOCKED );

   REQUIRE( reductions.getReduction( 5 ).row == ColReduction::CERTIFICATE_DOMINANCE );
   REQUIRE( reductions.getReduction( 5 ).col == 3 );
   REQUIRE( reductions.getReduction( 5 ).newval == 0 );

   REQUIRE( reductions.getReduction( 6 ).row == 0 );
   REQUIRE( reductions.getReduction( 6 ).col == RowReduction::SAVE_ROW );

   REQUIRE( reductions.getReduction( 7 ).row == ColReduction::FIXED );
   REQUIRE( reductions.getReduction( 7 ).col == 3 );
   REQUIRE( reductions.getReduction( 7 ).newval == 0 );
}

TEST_CASE( "domcol-multiple-parallel-cols-generate_redundant-reductions", "[presolve]" )
{
   double time = 0.0;
   int cause = -1;
   Timer t{time};
   Num<double> num{};
   Message msg{};
   Problem<double> problem = setupMatrixForDominatedColsMultipleParallel();
   Statistics statistics{};
   PresolveOptions presolveOptions{};
   PostsolveStorage<double> postsolve =
       PostsolveStorage<double>( problem, num, presolveOptions );
   ProblemUpdate<double> problemUpdate( problem, postsolve, statistics,
                                        presolveOptions, num, msg );

   DominatedCols<double> presolvingMethod{};
   Reductions<double> reductions{};
   problem.recomputeAllActivities();

   PresolveStatus presolveStatus =
       presolvingMethod.execute( problem, problemUpdate, num, reductions, t, cause);

   REQUIRE( presolveStatus == PresolveStatus::kReduced );
   REQUIRE( reductions.getTransactions().size() == 2 );
}

TEST_CASE( "domcol-multiple-columns", "[presolve]" )
{
   double time = 0.0;
   int cause = -1;
   Timer t{time};
   Num<double> num{};
   Message msg{};
   Problem<double> problem = setupMatrixForMultipleDominatedCols();
   Statistics statistics{};
   PresolveOptions presolveOptions{};
   PostsolveStorage<double> postsolve =
       PostsolveStorage<double>( problem, num, presolveOptions );
   ProblemUpdate<double> problemUpdate( problem, postsolve, statistics,
                                        presolveOptions, num, msg );

   DominatedCols<double> presolvingMethod{};
   Reductions<double> reductions{};
   problem.recomputeAllActivities();

   PresolveStatus presolveStatus =
       presolvingMethod.execute( problem, problemUpdate, num, reductions, t, cause );

   REQUIRE( presolveStatus == PresolveStatus::kReduced );
   REQUIRE( reductions.size() == 16 );

   Vec<int> dominated_cols = { 0, 0 };
   Vec<int> dominating_cols = { 2, 1 };
   for( int i = 0; i < 16; i += 8 )
   {
      REQUIRE( reductions.getReduction( i ).row == ColReduction::LOCKED );
      REQUIRE( reductions.getReduction( i ).col == dominated_cols[i / 8] );

      REQUIRE( reductions.getReduction( i + 1 ).row ==
               ColReduction::BOUNDS_LOCKED );
      REQUIRE( reductions.getReduction( i + 1 ).col == dominated_cols[i / 8] );

      REQUIRE( reductions.getReduction( i + 2 ).row == ColReduction::LOCKED );
      REQUIRE( reductions.getReduction( i + 2 ).col == dominating_cols[i / 8] );

      REQUIRE( reductions.getReduction( i + 3 ).row ==
               ColReduction::BOUNDS_LOCKED );
      REQUIRE( reductions.getReduction( i + 3 ).col == dominating_cols[i / 8] );

      REQUIRE( reductions.getReduction( i + 4 ).row == 0 );
      REQUIRE( reductions.getReduction( i + 4 ).col == RowReduction::LOCKED );

      REQUIRE( reductions.getReduction( i + 5 ).row == ColReduction::CERTIFICATE_DOMINANCE );
      REQUIRE( reductions.getReduction( i + 5 ).col == dominating_cols[i / 8] );
      REQUIRE( reductions.getReduction( i + 5 ).newval == dominated_cols[i / 8] );

      REQUIRE( reductions.getReduction( i + 6 ).row == 0 );
      REQUIRE( reductions.getReduction( i + 6 ).col == RowReduction::SAVE_ROW );

      REQUIRE( reductions.getReduction( i + 7 ).row == ColReduction::FIXED );
      REQUIRE( reductions.getReduction( i + 7 ).col == dominating_cols[i / 8] );
      REQUIRE( reductions.getReduction( i + 7 ).newval == 0 );
   }
}

Problem<double>
setupMatrixForDominatedCols()
{
   // x is dominated by y
   // min -2x -y -2z
   // a: 2x + 3y +  z <= 6
   // b:       y + 3z <= 1
   // Optimal solution x=3, y=0, z=0

   Vec<std::string> columnNames{ "x", "y", "z" };

   Vec<double> coefficients{ -2.0, -1.0, -2.0 };
   Vec<double> upperBounds{ 4.0, 2.0, 2.0 };
   Vec<double> lowerBounds{ 0.0, 0.0, 0.0 };
   Vec<uint8_t> isIntegral{ 1, 1, 1 };

   Vec<double> rhs{ 6.0, 1.0 };
   Vec<std::string> rowNames{ "a", "b" };
   Vec<uint8_t> lhsInfinity{ 1, 1 };
   Vec<uint8_t> rhsInfinity{ 0, 0 };
   Vec<std::tuple<int, int, double>> entries{
       std::tuple<int, int, double>{ 0, 0, 2.0 },
       std::tuple<int, int, double>{ 0, 1, 3.0 },
       std::tuple<int, int, double>{ 0, 2, 1.0 },

       std::tuple<int, int, double>{ 1, 1, 1.0 },
       std::tuple<int, int, double>{ 1, 2, 3.0 },
   };

   ProblemBuilder<double> pb;
   pb.reserve( (int) entries.size(), (int) rowNames.size(), (int) columnNames.size() );
   pb.setNumRows( (int) rowNames.size() );
   pb.setNumCols( (int) columnNames.size() );
   pb.setColUbAll( upperBounds );
   pb.setColLbAll( lowerBounds );
   pb.setObjAll( coefficients );
   pb.setObjOffset( 0.0 );
   pb.setColIntegralAll( isIntegral );
   pb.setRowRhsAll( rhs );
   pb.setRowLhsInfAll( lhsInfinity );
   pb.setRowRhsInfAll( rhsInfinity );
   pb.addEntryAll( entries );
   pb.setColNameAll( columnNames );
   pb.setProblemName( "matrix x dom y" );
   Problem<double> problem = pb.build();
   return problem;
}

Problem<double>
setupMatrixForDominatedColsParallel()
{
   Vec<std::string> columnNames{ "x", "y", "z", "v", "w" };

   Vec<double> coefficients{ -1.0, 1.0, -1.0, - 1, 1 };
   Vec<double> upperBounds{ 4.0, 5.0, 2.0, 2.0, 5.0 };
   Vec<double> lowerBounds{ 0.0, 0.0, 0.0, 0,0 };
   Vec<uint8_t> isIntegral{ 1, 0, 1, 1, 0 };

   Vec<double> rhs{ 6.0, 1.0 };
   Vec<std::string> rowNames{ "a", "b" };
   Vec<uint8_t> lhsInfinity{ 1, 1 };
   Vec<uint8_t> rhsInfinity{ 0, 0 };
   Vec<std::tuple<int, int, double>> entries{
       std::tuple<int, int, double>{ 0, 0, 2.0 },
       std::tuple<int, int, double>{ 0, 1, 6.0 },
       std::tuple<int, int, double>{ 0, 2, 4.0 },
       std::tuple<int, int, double>{ 0, 3, 3.0 },
       std::tuple<int, int, double>{ 0, 4, 1.0 },

       std::tuple<int, int, double>{ 1, 0, 1.0 },
       std::tuple<int, int, double>{ 1, 1, -1.0 },
       std::tuple<int, int, double>{ 1, 2, -1.0 },
       std::tuple<int, int, double>{ 1, 3, 1.0 },
       std::tuple<int, int, double>{ 1, 4, 3.0 },
   };

   ProblemBuilder<double> pb;
   pb.reserve( (int) entries.size(), (int) rowNames.size(), (int) columnNames.size() );
   pb.setNumRows( (int) rowNames.size() );
   pb.setNumCols( (int) columnNames.size() );
   pb.setColUbAll( upperBounds );
   pb.setColLbAll( lowerBounds );
   pb.setObjAll( coefficients );
   pb.setObjOffset( 0.0 );
   pb.setColIntegralAll( isIntegral );
   pb.setRowRhsAll( rhs );
   pb.setRowLhsInfAll( lhsInfinity );
   pb.setRowRhsInfAll( rhsInfinity );
   pb.addEntryAll( entries );
   pb.setColNameAll( columnNames );
   pb.setProblemName( "matrix parllel x and v" );
   Problem<double> problem = pb.build();
   return problem;
}

Problem<double>
setupMatrixForDominatedColsMultipleParallel()
{
   Vec<std::string> columnNames{ "x", "y", "z" };

   Vec<double> coefficients{ -1.0, -1.0, -1.0 };
   Vec<double> upperBounds{ 4.0, 2.0, 2.0 };
   Vec<double> lowerBounds{ 0.0, 0.0, 0.0 };
   Vec<uint8_t> isIntegral{ 1, 1, 1 };

   Vec<double> rhs{ 6.0, 1.0 };
   Vec<std::string> rowNames{ "a", "b" };
   Vec<uint8_t> lhsInfinity{ 1, 1 };
   Vec<uint8_t> rhsInfinity{ 0, 0 };
   Vec<std::tuple<int, int, double>> entries{
       std::tuple<int, int, double>{ 0, 0, 2.0 },
       std::tuple<int, int, double>{ 0, 1, 2.0 },
       std::tuple<int, int, double>{ 0, 2, 2.0 },

       std::tuple<int, int, double>{ 1, 0, 1.0 },
       std::tuple<int, int, double>{ 1, 1, 1.0 },
       std::tuple<int, int, double>{ 1, 2, 1.0 },
   };

   ProblemBuilder<double> pb;
   pb.reserve( (int) entries.size(), (int) rowNames.size(), (int) columnNames.size() );
   pb.setNumRows( (int) rowNames.size() );
   pb.setNumCols( (int) columnNames.size() );
   pb.setColUbAll( upperBounds );
   pb.setColLbAll( lowerBounds );
   pb.setObjAll( coefficients );
   pb.setObjOffset( 0.0 );
   pb.setColIntegralAll( isIntegral );
   pb.setRowRhsAll( rhs );
   pb.setRowLhsInfAll( lhsInfinity );
   pb.setRowRhsInfAll( rhsInfinity );
   pb.addEntryAll( entries );
   pb.setColNameAll( columnNames );
   pb.setProblemName( "matrix multiple parallel cols" );
   Problem<double> problem = pb.build();
   return problem;
}


Problem<double>
setupMatrixForMultipleDominatedCols()
{
   // x is dominated by y and y is dominated by z
   // min -3x -2y -1z
   // a: 2x + 3y + 4z <= 6
   // b:       y + 3z <= 1
   // Optimal solution x=3, y=0, z=0

   Vec<std::string> columnNames{ "x", "y", "z" };

   Vec<double> coefficients{ -3.0, -2.0, -1.0 };
   Vec<double> upperBounds{ 4.0, 2.0, 2.0 };
   Vec<double> lowerBounds{ 0.0, 0.0, 0.0 };
   Vec<uint8_t> isIntegral{ 1, 1, 1 };

   Vec<double> rhs{ 6.0, 1.0 };
   Vec<std::string> rowNames{ "a", "b" };
   Vec<uint8_t> lhsInfinity{ 1, 1 };
   Vec<uint8_t> rhsInfinity{ 0, 0 };
   Vec<std::tuple<int, int, double>> entries{
       std::tuple<int, int, double>{ 0, 0, 2.0 },
       std::tuple<int, int, double>{ 0, 1, 3.0 },
       std::tuple<int, int, double>{ 0, 2, 4.0 },

       std::tuple<int, int, double>{ 1, 1, 1.0 },
       std::tuple<int, int, double>{ 1, 2, 3.0 },
   };

   ProblemBuilder<double> pb;
   pb.reserve( (int) entries.size(), (int) rowNames.size(), (int) columnNames.size() );
   pb.setNumRows( (int) rowNames.size() );
   pb.setNumCols( (int) columnNames.size() );
   pb.setColUbAll( upperBounds );
   pb.setColLbAll( lowerBounds );
   pb.setObjAll( coefficients );
   pb.setObjOffset( 0.0 );
   pb.setColIntegralAll( isIntegral );
   pb.setRowRhsAll( rhs );
   pb.setRowLhsInfAll( lhsInfinity );
   pb.setRowRhsInfAll( rhsInfinity );
   pb.addEntryAll( entries );
   pb.setColNameAll( columnNames );
   pb.setProblemName( "matrix x dom y dom z" );
   Problem<double> problem = pb.build();
   return problem;
}
