<commit_before>-module (labs_ops).
-behaviour (genetic_ops).
-export ([solution/1, evaluation/1, mutation/1, recombination/2]).

-include ("emas.hrl").

%% @doc Generates a random solution.
-spec solution(integer()) -> solution().
solution(ProblemSize) ->
   [random:uniform(2)-1 || _ <- lists:seq(1, ProblemSize)].


%% @doc Evaluates a given solution. Higher is better.
-spec evaluation(solution()) -> float().
evaluation(Solution) ->
    local_search(Solution).

-spec energy(solution()) -> float().
energy(Solution) ->
    L = length(Solution),
    Cs = [foldzip(drop(Solution, K), Solution)
        || K <- lists:seq(1, L-1)],
    E = lists:foldl(fun (X, Acc) -> X*X + Acc end, 0, Cs),
    L*L*0.5/E.


-spec recombination(solution(),solution()) -> {solution(),solution()}.
recombination(S1, S2) ->
    lists:unzip([recombinationFeatures(F1, F2) || {F1, F2} <- lists:zip(S1,S2)]).

%% @doc Chooses a random order between the two initial features.
-spec recombinationFeatures(float(),float()) -> {float(),float()}.
recombinationFeatures(F, F) -> {F, F};
recombinationFeatures(F1, F2) ->
    case random:uniform() < 0.5 of
        true -> {F1, F2};
        false -> {F2, F1}
    end.

%% @doc Reproduction function for a single agent (mutation only).
-spec mutation(solution()) -> solution().
mutation(Solution) ->
    lists:map(fun(X) -> 
        case random:uniform() < emas_config:mutationRate() of
            true -> fnot(X);
            _ -> X
        end
    end, Solution).

%% internal functions

drop([], _) -> [];
drop(L, 0) -> L;
drop([_ | T], N) ->
    drop(T, N - 1).

foldzip(A, B) -> foldzip(A, B, 0).

foldzip([], _, Acc) -> Acc;
foldzip(_, [], Acc) -> Acc;
foldzip([HA|TA], [HB|TB], Acc) ->
    foldzip(TA, TB, Acc + dot(HA,HB)).

dot(X, X) -> 1;
dot(_, _) -> -1.<commit_msg>updated labs_ops for new config files<commit_after>-module (labs_ops).
-behaviour (genetic_ops).
-export ([solution/1, evaluation/2, mutation/2, recombination/3]).

-include ("emas.hrl").

%% @doc Generates a random solution.
-spec solution(sim_params()) -> solution().
solution(SP) ->
   [random:uniform(2)-1 || _ <- lists:seq(1, SP#sim_params.problem_size)].


%% @doc Evaluates a given solution. Higher is better.
-spec evaluation(solution(), sim_params()) -> float().
evaluation(Solution, _SP) ->
    local_search(Solution).

-spec energy(solution()) -> float().
energy(Solution) ->
    L = length(Solution),
    Cs = [foldzip(drop(Solution, K), Solution)
        || K <- lists:seq(1, L-1)],
    E = lists:foldl(fun (X, Acc) -> X*X + Acc end, 0, Cs),
    L*L*0.5/E.


-spec recombination(solution(), solution(), sim_params()) -> {solution(), solution()}.
recombination(S1, S2, _SP) ->
    lists:unzip([recombination_features(F1, F2) || {F1, F2} <- lists:zip(S1, S2)]).

%% @doc Chooses a random order between the two initial features.
-spec recombination_features(float(), float()) -> {float(), float()}.
recombination_features(F, F) -> {F, F};
recombination_features(F1, F2) ->
    case random:uniform() < 0.5 of
        true -> {F1, F2};
        false -> {F2, F1}
    end.

%% @doc Reproduction function for a single agent (mutation only).
-spec mutation(solution(), sim_params()) -> solution().
mutation(Solution, SP) ->
    lists:map(fun(X) ->
        case random:uniform() < SP#sim_params.mutation_rate of
            true -> fnot(X);
            _ -> X
        end
    end, Solution).

%% internal functions

drop([], _) -> [];
drop(L, 0) -> L;
drop([_ | T], N) ->
    drop(T, N - 1).

foldzip(A, B) -> foldzip(A, B, 0).

foldzip([], _, Acc) -> Acc;
foldzip(_, [], Acc) -> Acc;
foldzip([HA|TA], [HB|TB], Acc) ->
    foldzip(TA, TB, Acc + dot(HA,HB)).

dot(X, X) -> 1;
dot(_, _) -> -1.
{'fraction_non_alphanumeric': 0.2062330623306233, 'fraction_numerical': 0.01788617886178862, 'mean_word_length': 3.301864801864802, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5286459', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1305, 'n_words': 331}