Как определить набор путей или набор последовательностей ссылок в AMPL?

Если вы делаете большое приложение с другими разработчиками в своей команде и собираетесь иметь все хорошо организованное без разброса кода или разных экземпляров SharedPreferences, вы можете сделать что-то вроде этого:

//SharedPreferences manager class
public class SharedPrefs {

    //SharedPreferences file name
    private static String SHARED_PREFS_FILE_NAME = "my_app_shared_prefs";

    //here you can centralize all your shared prefs keys
    public static String KEY_MY_SHARED_BOOLEAN = "my_shared_boolean";
    public static String KEY_MY_SHARED_FOO = "my_shared_foo";

    //get the SharedPreferences object instance
    //create SharedPreferences file if not present


    private static SharedPreferences getPrefs(Context context) {
        return context.getSharedPreferences(SHARED_PREFS_FILE_NAME, Context.MODE_PRIVATE);
    }

    //Save Booleans
    public static void savePref(Context context, String key, boolean value) {
        getPrefs(context).edit().putBoolean(key, value).commit();       
    }

    //Get Booleans
    public static boolean getBoolean(Context context, String key) {
        return getPrefs(context).getBoolean(key, false);
    }

    //Get Booleans if not found return a predefined default value
    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        return getPrefs(context).getBoolean(key, defaultValue);
    }

    //Strings
    public static void save(Context context, String key, String value) {
        getPrefs(context).edit().putString(key, value).commit();
    }

    public static String getString(Context context, String key) {
        return getPrefs(context).getString(key, "");
    }

    public static String getString(Context context, String key, String defaultValue) {
        return getPrefs(context).getString(key, defaultValue);
    }

    //Integers
    public static void save(Context context, String key, int value) {
        getPrefs(context).edit().putInt(key, value).commit();
    }

    public static int getInt(Context context, String key) {
        return getPrefs(context).getInt(key, 0);
    }

    public static int getInt(Context context, String key, int defaultValue) {
        return getPrefs(context).getInt(key, defaultValue);
    }

    //Floats
    public static void save(Context context, String key, float value) {
        getPrefs(context).edit().putFloat(key, value).commit();
    }

    public static float getFloat(Context context, String key) {
        return getPrefs(context).getFloat(key, 0);
    }

    public static float getFloat(Context context, String key, float defaultValue) {
        return getPrefs(context).getFloat(key, defaultValue);
    }

    //Longs
    public static void save(Context context, String key, long value) {
        getPrefs(context).edit().putLong(key, value).commit();
    }

    public static long getLong(Context context, String key) {
        return getPrefs(context).getLong(key, 0);
    }

    public static long getLong(Context context, String key, long defaultValue) {
        return getPrefs(context).getLong(key, defaultValue);
    }

    //StringSets
    public static void save(Context context, String key, Set<String> value) {
        getPrefs(context).edit().putStringSet(key, value).commit();
    }

    public static Set<String> getStringSet(Context context, String key) {
        return getPrefs(context).getStringSet(key, null);
    }

    public static Set<String> getStringSet(Context context, String key, Set<String> defaultValue) {
        return getPrefs(context).getStringSet(key, defaultValue);
    }
}

В вашем вы можете сохранить SharedPreferences таким образом

//saving a boolean into prefs
SharedPrefs.savePref(this, SharedPrefs.KEY_MY_SHARED_BOOLEAN, booleanVar);

, и вы можете получить свои SharedPreferences таким образом

//getting a boolean from prefs
booleanVar = SharedPrefs.getBoolean(this, SharedPrefs.KEY_MY_SHARED_BOOLEAN);
0
задан Matt Hall 15 January 2019 в 20:01
поделиться

1 ответ

Учитывая, что у ваших путей нет повторяющихся узлов, самый естественный способ определить пути - это набор упорядоченных наборов узлов.

reset;
model;
set NODES;
set LINKS within {NODES,NODES};

param n_paths;
set PATHS{1..n_paths} within NODES ordered;

# Optional: identify all of the links implied by these paths, so we can
# check that they are in fact within LINKS. 

param longest_path_length := max{i in 1..n_paths} card(PATHS[i]);
set LINKS_IMPLIED_BY_PATHS within LINKS := setof{
      i in 1..n_paths, 
      j in 1..(longest_path_length-1): j < card(PATHS[i])
    } (member(j,PATHS[i]),member(j+1,PATHS[i]))
    ;

data;
set NODES := A B C;
set LINKS := (A,B) (B,C);

param n_paths := 3;
set PATHS[1] := A B;
set PATHS[2] := A B C;
set PATHS[3] := B C;

display LINKS_IMPLIED_BY_PATHS;
# if we include a path like C A, we will get an error here because ("C","A") 
# is not within LINKS.
# It should be possible to do this more tidily with a check statement but
# for the moment the syntax escapes me.
# Note that this error will ONLY appear at the point where we try to
# do something with LINKS_IMPLIED_BY_PATHS; it's not calculated or checked
# until then. 

Это не совсем то, что вы просили, так как он определяет пути как последовательность узлов, а не ссылок, но это самый близкий, который я мог получить.

0
ответ дан Geoffrey Brent 15 January 2019 в 20:01
поделиться
Другие вопросы по тегам:

Похожие вопросы: