# 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, csv, re
from summary import summary

def update_stattable(probfiles, solfiles):

  # Find the directory of this script
  scriptdir = os.path.split(inspect.getfile( inspect.currentframe() ))[0]
  rootdir = os.path.join(scriptdir,'..','..')

  # Read instance status data
  csvfile = open(os.path.join(rootdir,'instances','stat.csv'), 'rt')
  csvreader = csv.reader(csvfile, delimiter=';', quotechar='"')
  header = next(csvreader)
  statdictnames = ['pcer','perr','pobj','dcer','derr','dobj','claim','comment']
  statdict = dict((rows[0], dict((x,y) for x,y in zip(statdictnames, rows[1:]))) for rows in csvreader)
  csvfile.close()

  # Update instance status data
  for (probfile, solfile) in zip(probfiles, solfiles):
    sys.stdout.write('\n' + solfile + '\n')
    solstat = summary(probfile, solfile, lambda x: None)
    csvstat = statdict[solstat['prob'].name]

    if 'claim' in solstat:
      if csvstat['claim'] == '':
        csvstat['claim'] = solstat['claim']
      elif csvstat['claim'] != solstat['claim']:
        raise Exception(solstat['prob'].name + ': The claim ' + solstat['claim'] + ' is incompatible with the existing claim ' + csvstat['claim'])

    if 'psol' in solstat:
      psol = solstat['psol']
      perr = max(psol[1].values())
      if worty_replacement(csvstat['perr'], csvstat['pobj'], perr, psol[0], solstat['prob'].obj=='MIN'):
        csvstat['pcer'] = 'FEASIBILITY'
        csvstat['perr'] = '{:.16g}'.format(perr)
        csvstat['pobj'] = '{:.16g}'.format(psol[0])

    if 'pinfeascer' in solstat:
      pinfeascer = solstat['pinfeascer']
      perr = max(pinfeascer[1].values())
      if worty_replacement(csvstat['perr'], csvstat['pobj'], perr, pinfeascer[0], solstat['prob'].obj=='MIN'):
        csvstat['pcer'] = 'INFEASIBILITY'
        csvstat['perr'] = '{:.16g}'.format(perr)
        csvstat['pobj'] = '{:.16g}'.format(pinfeascer[0])

    if solstat['prob'].intvarnum == 0:
      if 'dsol' in solstat:
        dsol = solstat['dsol']
        derr = max(dsol[1].values())
        if worty_replacement(csvstat['derr'], csvstat['dobj'], derr, dsol[0], solstat['prob'].obj=='MAX'):
          csvstat['dcer'] = 'FEASIBILITY'
          csvstat['derr'] = '{:.16g}'.format(derr)
          csvstat['dobj'] = '{:.16g}'.format(dsol[0])

      if 'dinfeascer' in solstat:
        dinfeascer = solstat['dinfeascer']
        derr = max(dinfeascer[1].values())
        if worty_replacement(csvstat['derr'], csvstat['dobj'], derr, dinfeascer[0], solstat['prob'].obj=='MAX'):
          csvstat['dcer'] = 'INFEASIBILITY'
          csvstat['derr'] = '{:.16g}'.format(derr)
          csvstat['dobj'] = '{:.16g}'.format(dinfeascer[0])

  # Define instance sorting
  convert = lambda text: int(text) if text.isdigit() else text
  alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
  sortedstatdict = sorted(statdict, key=alphanum_key)

  # Write instance status data
  csvfile = open(os.path.join(rootdir,'instances','stat.csv'), 'wt')
  csvwriter = csv.writer(csvfile, delimiter=';', quotechar='"')
  csvwriter.writerow(header)
  for k in sortedstatdict:
    csvwriter.writerow([k] + [statdict[k][x] for x in statdictnames])
  csvfile.close()

def worty_replacement(csverr, csvobj, solerr, solobj, objminimize):
  if csverr == '':
    return(True)

  csverr = float(csverr)
  csvobj = float(csvobj)

  if csverr > 1e-4:
    # Error measure first, then objective
    return (
      (solerr < csverr) or
      (solerr == csverr and solobj < csvobj and objminimize) or
      (solerr == csverr and solobj > csvobj and not objminimize)
    )
  elif solerr <= 1e-4:
    # Objective first, then error measure
    return (
      (solobj < csvobj and objminimize) or
      (solobj > csvobj and not objminimize) or
      (solobj == csvobj and solerr < csverr)
    )
  else:
    return(False)


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

      raise Exception(''.join([
          'Incorrect usage, to update solution statistics of \"qssp30.cbf.sol\":', '\n',
          '  python ', sys.argv[0], ' -f ', os.path.realpath(os.path.abspath(os.path.join(rootdir,'instances','cbf','qssp30.cbf'))) ]))

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

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

    # Update solution statistics
    update_stattable(probfiles, solfiles)

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