# Copyright (c) 2012 by Zuse-Institute Berlin and the Technical University of Denmark.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     1. Redistributions of source code must retain the above copyright
#        notice, this list of conditions and the following disclaimer.
#     2. Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in the
#        documentation and/or other materials provided with the distribution.
#     3. Neither the name of the copyright holders nor contributors may not
#        be used to endorse or promote products derived from this software
#        without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS NOR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


# Direct execution requires top level directory on python path
if __name__ == "__main__":
  import os, sys, inspect
  scriptdir = os.path.split(inspect.getfile( inspect.currentframe() ))[0]
  packagedir = os.path.realpath(os.path.abspath(os.path.join(scriptdir,'..')))
  if packagedir not in sys.path:
    sys.path.insert(0, packagedir)


import os, sys, inspect
from timeit import Timer
from summary import summary
from data.CBFdata import CBFdata

def run(solver, probfile, solfile, printer):

  ss = None
  try:
    for (i,pp) in enumerate(CBFdata(probfile).iterator()):
      solver.read(probfile, pp)
      pptime = Timer(solver.optimize).timeit(number=1)
      ppsol = solver.getsolution()

      # Write to file
      if i == 0:
        ss = open(solfile, 'w')
      else:
        ss.write('CHANGE\n\n')
      ppsol.printsol( lambda x: ss.write(str(x) + '\n') )

      # Write to printer
      printer('\n' + pp.name + '[' + str(i) + ']: ' + str(pptime) + 's')
      summary(pp, ppsol, printer)

  finally:
    if ss is not None:
      ss.close()



if __name__ == "__main__":
  try:
    # Verify command line arguments
    if len(sys.argv) <= 3:
      scriptdir = os.path.split(inspect.getfile( inspect.currentframe() ))[0]
      rootdir = os.path.join(scriptdir,'..')

      raise Exception(''.join([
          'Incorrect usage, try solving \"qssp30.cbf\" using MOSEK:', '\n',
          '  python ', sys.argv[0], ' runmosek -f ', os.path.realpath(os.path.abspath(os.path.join(rootdir,'instances','cbf','qssp30.cbf'))) ]))

    # Load solver
    solver = __import__('solvers.' + sys.argv[1], fromlist=sys.argv[1]).solver(sys.stdout.write)

    # Load problem and solution files
    if sys.argv[2] == '-f':
      probfiles = sys.argv[3:]
      solfiles = [x + '.sol' for x in probfiles]

    elif sys.argv[2] == '-l':
      ff = open(sys.argv[3], 'rt')
      try:
        probfiles = filter(bool, ff.read().split('\n'))
        solfiles = [os.path.realpath(os.path.abspath(os.path.join(os.path.dirname(sys.argv[3]), os.path.basename(x) + '.sol'))) for x in probfiles]
      finally:
        ff.close()

    # Start runs
    for (probfile, solfile) in zip(probfiles, solfiles):
      run(solver, probfile, solfile,  lambda x: sys.stdout.write(str(x)+'\n'))

  except Exception as e:
    print(str(e))
