switch_model/solve.py
def reload_prior_solution_from_csvs(instance):
    """
    Assign values to all model variables from <variable>.csv files saved after
    previous solution. (Not currently used.)
    """
    import csv
    var_objects = instance.component_objects(Var)
    for var in var_objects:
        var_file = os.path.join(instance.options.outputs_dir, '{}.csv'.format(var.name))
        if not os.path.isfile(var_file):
            raise RuntimeError(
                "Tab output file for variable {} cannot be found in outputs "
                "directory. Exiting.".format(var.name)
            )
        try:
            # check types of the first tuple of keys for this variable
            key_types = [type(i) for i in make_iterable(next(var.iterkeys()))]
        except StopIteration:
            key_types = []  # no keys
        with open(var_file,'r') as f:
            reader = csv.reader(f, delimiter=',')
            next(reader) # skip headers
            for row in reader:
                index = tuple(t(k) for t, k in zip(key_types, row[:-1]))
                try:
                    v = var[index]
                except KeyError:
                    raise KeyError(
                        "Unable to set value for {}[{}]; index is invalid."
                        .format(var.name, index)
                    )
                if row[-1] == '':
                    # Variables that are not used in the model end up with no
                    # value after the solve and get saved as blanks; we skip those.
                    continue
                val = float(row[-1])
                if v.is_integer() or v.is_binary():
                    val = int(val)
                v.value = val
        if instance.options.verbose:
            print('Loaded variable {} values into instance.'.format(var.name))

    Assign values to all model variables from <variable>.csv files saved after
    previous solution. (Not currently used.)
    
Assign values to all model variables from .csv files saved after
previous solution. (Not currently used.)
Assign values to all model variables from . csv files saved after previous solution . ( Not currently used . )
def reload_prior_solution_from_csvs(instance):
    import csv
    var_objects = instance.component_objects(Var)
    for var in var_objects:
        var_file = os.path.join(instance.options.outputs_dir, '{}.csv'.format(var.name))
        if not os.path.isfile(var_file):
            raise RuntimeError(
                "Tab output file for variable {} cannot be found in outputs "
                "directory. Exiting.".format(var.name)
            )
        try:
            key_types = [type(i) for i in make_iterable(next(var.iterkeys()))]
        except StopIteration:
            key_types = []  
        with open(var_file,'r') as f:
            reader = csv.reader(f, delimiter=',')
            next(reader) 
            for row in reader:
                index = tuple(t(k) for t, k in zip(key_types, row[:-1]))
                try:
                    v = var[index]
                except KeyError:
                    raise KeyError(
                        "Unable to set value for {}[{}]; index is invalid."
                        .format(var.name, index)
                    )
                if row[-1] == '':
                    continue
                val = float(row[-1])
                if v.is_integer() or v.is_binary():
                    val = int(val)
                v.value = val
        if instance.options.verbose:
            print('Loaded variable {} values into instance.'.format(var.name))
def reload_prior_solution_from_csvs ( instance ) : import csv var_objects = instance . component_objects ( Var ) for var in var_objects : var_file = os . path . join ( instance . options . outputs_dir , '{}.csv' . format ( var . name ) ) if not os . path . isfile ( var_file ) : raise RuntimeError ( "Tab output file for variable {} cannot be found in outputs " "directory. Exiting." . format ( var . name ) ) try : key_types = [ type ( i ) for i in make_iterable ( next ( var . iterkeys ( ) ) ) ] except StopIteration : key_types = [ ] with open ( var_file , 'r' ) as f : reader = csv . reader ( f , delimiter = ',' ) next ( reader ) for row in reader : index = tuple ( t ( k ) for t , k in zip ( key_types , row [ : - 1 ] ) ) try : v = var [ index ] except KeyError : raise KeyError ( "Unable to set value for {}[{}]; index is invalid." . format ( var . name , index ) ) if row [ - 1 ] == '' : continue val = float ( row [ - 1 ] ) if v . is_integer ( ) or v . is_binary ( ) : val = int ( val ) v . value = val if instance . options . verbose : print ( 'Loaded variable {} values into instance.' . format ( var . name ) )
Assign values to all model variables from <variable>.csv files saved after
previous solution.
Assign values to all model variables from <variable > . csv files saved after previous solution .
"""
    Assign values to all model variables from <variable>.csv files saved after
    previous solution. (Not currently used.)
    """ # check types of the first tuple of keys for this variable # no keys # skip headers # Variables that are not used in the model end up with no # value after the solve and get saved as blanks; we skip those.
instance