[107] | 1 | package com.framsticks.standard; |
---|
| 2 | |
---|
| 3 | import java.util.LinkedList; |
---|
| 4 | import java.util.List; |
---|
| 5 | import java.util.Random; |
---|
| 6 | import java.util.TimerTask; |
---|
| 7 | |
---|
| 8 | import com.framsticks.experiment.Experiment; |
---|
| 9 | import com.framsticks.experiment.Simulator; |
---|
| 10 | import com.framsticks.model.Genotype; |
---|
| 11 | import com.framsticks.params.Access; |
---|
| 12 | import com.framsticks.params.EventListener; |
---|
| 13 | import com.framsticks.params.Registry; |
---|
| 14 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
| 15 | import com.framsticks.structure.messages.ListChange; |
---|
| 16 | import com.framsticks.util.dispatching.Dispatching; |
---|
| 17 | import com.framsticks.util.dispatching.Future; |
---|
| 18 | |
---|
| 19 | import org.apache.logging.log4j.Logger; |
---|
| 20 | import org.apache.logging.log4j.LogManager; |
---|
| 21 | |
---|
| 22 | @FramsClassAnnotation |
---|
| 23 | public class StandardExperiment extends Experiment { |
---|
| 24 | |
---|
| 25 | private static final Logger log = LogManager.getLogger(StandardExperiment.class); |
---|
| 26 | |
---|
| 27 | protected final Registry registry = new Registry(); |
---|
| 28 | |
---|
| 29 | protected List<Genotype> genotypes = new LinkedList<>(); |
---|
| 30 | protected Random random = new Random(); |
---|
| 31 | |
---|
| 32 | // @ParamAnnotation |
---|
| 33 | // protected final NetLoadSaveLogic<StandardState> netLoadSaveLogic; |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * |
---|
| 37 | */ |
---|
| 38 | public StandardExperiment() { |
---|
| 39 | |
---|
| 40 | setExpdef("standard"); |
---|
| 41 | registry.registerAndBuild(StandardState.class); |
---|
| 42 | |
---|
| 43 | addSimulatorsListener(new EventListener<ListChange>() { |
---|
| 44 | @Override |
---|
| 45 | public void action(final ListChange change) { |
---|
| 46 | assert isActive(); |
---|
| 47 | final Simulator simulator = getSimulators().get(change.getIdentifier()); |
---|
| 48 | if (change.getAction().equals(ListChange.Action.Add)) { |
---|
| 49 | simulator.init(); |
---|
| 50 | return; |
---|
| 51 | } |
---|
| 52 | if (change.getAction().equals(ListChange.Action.Modify)) { |
---|
| 53 | if (change.hasHint("ready")) { |
---|
| 54 | log.debug("issuing netsave"); |
---|
| 55 | |
---|
| 56 | simulator.netsave(StandardState.class, new Future<StandardState>(StandardExperiment.this) { |
---|
| 57 | @Override |
---|
| 58 | protected void result(StandardState result) { |
---|
| 59 | assert isActive(); |
---|
| 60 | assert result != null; |
---|
| 61 | processState(simulator, result); |
---|
| 62 | |
---|
| 63 | simulator.netload(result, new Future<Object>(StandardExperiment.this) { |
---|
| 64 | |
---|
| 65 | @Override |
---|
| 66 | protected void result(Object result) { |
---|
| 67 | runSimulatorFor(simulator); |
---|
| 68 | } |
---|
| 69 | }); |
---|
| 70 | } |
---|
| 71 | }); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | return; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | } |
---|
| 78 | }); |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | // netLoadSaveLogic = new NetLoadSaveLogic<StandardState>(this, StandardState.class) { |
---|
| 82 | // @Override |
---|
| 83 | // public void netload(Simulator simulator, FutureHandler<StandardState> net) { |
---|
| 84 | // assert isActive(); |
---|
| 85 | // // Dispatching.sleep(0.1); |
---|
| 86 | // net.pass(null); |
---|
| 87 | // // simulator.start(); |
---|
| 88 | // } |
---|
| 89 | |
---|
| 90 | // @Override |
---|
| 91 | // public void netsave(Simulator simulator, StandardState net) { |
---|
| 92 | // assert isActive(); |
---|
| 93 | // log.debug("saved state: {}", net); |
---|
| 94 | // runSimulatorFor(simulator); |
---|
| 95 | // } |
---|
| 96 | // }; |
---|
| 97 | |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | protected void processState(Simulator simulator, StandardState state) { |
---|
| 101 | Access genePoolAccess = registry.bindAccessFor(state.genepools.get(0)); |
---|
| 102 | Access genotypesAccess = registry.bindAccessFor(genePoolAccess, "genotypes"); |
---|
| 103 | int count = genotypesAccess.getParamCount(); |
---|
| 104 | int toRemove = count / 10; |
---|
| 105 | |
---|
| 106 | log.debug("processing state {} for simulator {} with {} genotypes", state, simulator, count); |
---|
| 107 | |
---|
| 108 | List<Genotype> removed = new LinkedList<>(); |
---|
| 109 | for (int c = 0; c < toRemove; ++c) { |
---|
| 110 | int number = random.nextInt(genotypesAccess.getParamCount()); |
---|
| 111 | Genotype genotype = genotypesAccess.get(number, Genotype.class); |
---|
| 112 | genotypesAccess.set(number, null); |
---|
| 113 | log.debug("removing {} from {}", genotype, simulator); |
---|
| 114 | removed.add(genotype); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | int toAdd = toRemove; |
---|
| 118 | if (toAdd > genotypes.size()) { |
---|
| 119 | toAdd = genotypes.size(); |
---|
| 120 | } |
---|
| 121 | if (genotypes.size() > 100) { |
---|
| 122 | toAdd = genotypes.size() - 100; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | for (int a = 0; (a < toAdd) && (!genotypes.isEmpty()); ++a) { |
---|
| 126 | Genotype genotype = genotypes.remove(0); |
---|
| 127 | log.debug("adding {} to {}", genotype, simulator); |
---|
| 128 | genotype.uid = null; |
---|
| 129 | genotypesAccess.set((String) null, genotype); |
---|
| 130 | } |
---|
| 131 | log.debug("state processed"); |
---|
| 132 | genotypes.addAll(removed); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | protected void runSimulatorFor(final Simulator simulator) { |
---|
| 136 | simulator.start(); |
---|
| 137 | Dispatching.getTimer().schedule(new TimerTask() { |
---|
| 138 | |
---|
| 139 | @Override |
---|
| 140 | public void run() { |
---|
| 141 | simulator.stop(); |
---|
| 142 | } |
---|
| 143 | }, 2000); |
---|
| 144 | } |
---|
| 145 | } |
---|