Skip to content

Commit

Permalink
* Generation plan "Mono" now shows modification attempts
Browse files Browse the repository at this point in the history
* Split "Stability" overhaul priority into "Valid" and "Stable"
* Fixed wrongly removing valid magnesium heatsinks
  • Loading branch information
ThizThizzyDizzy committed Apr 22, 2020
1 parent d32928f commit 47961ce
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 37 deletions.
18 changes: 18 additions & 0 deletions src/common/SettingBoolean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package common;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
public class SettingBoolean extends Setting<Boolean>{
public SettingBoolean(String name, boolean defaultValue){
super(name, defaultValue);
}
@Override
public JComponent[] createComponents(int width){
JCheckBox box = new JCheckBox();
box.setSelected(value);
box.addActionListener((e) -> {
value = box.isSelected();
});
box.setSize(width,20);
return new JComponent[]{createLabel(name, width), box};
}
}
13 changes: 13 additions & 0 deletions src/common/ThingWithSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,17 @@ public Double getDouble(String name){
}
return null;
}
public Boolean getBoolean(String name){
for(Setting s : settings){
if(s.name.equalsIgnoreCase(name)){
if(s.value instanceof Boolean)return (Boolean)s.value;
}
}
for(Setting s : settings){
if(s.name.toLowerCase().contains(name.toLowerCase())){
if(s.value instanceof Boolean)return (Boolean)s.value;
}
}
return null;
}
}
6 changes: 4 additions & 2 deletions src/overhaul/GenerationModel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package overhaul;
import common.Setting;
import common.SettingBoolean;
import common.SettingDouble;
import common.SettingInt;
import common.ThingWithSettings;
import java.util.ArrayList;
import java.util.Random;
Expand All @@ -14,7 +16,7 @@ public Reactor generate(Reactor last, Fuel fuel, Fuel.Type type, int x, int y, i
return Reactor.random(fuel,type,x,y,z,rand);
}
});
models.add(new GenerationModel("Standard", "Generates random reactors until a valid reactor is found, then changes some random parts of the reactor to random other parts- if the result is better, keep the changes. if not, discard.", new SettingDouble("Change Chance", 1, 0.1f, 100, .1f)) {
models.add(new GenerationModel("Standard", "Generates random reactors until a valid reactor is found, then changes some random parts of the reactor to random other parts- if the result is better, keep the changes. if not, discard.", new SettingDouble("Change Chance", 1, 0.1, 100, .1)){
@Override
public Reactor generate(Reactor last, Fuel fuel, Fuel.Type type, int x, int y, int z, Random rand){
if(last!=null&&last.isValid()){
Expand All @@ -34,7 +36,7 @@ protected ReactorPart build(int X, int Y, int Z){
});
DEFAULT = get("Standard");
}
private static GenerationModel get(String name){
public static GenerationModel get(String name){
for(GenerationModel model : models){
if(model.name.equalsIgnoreCase(name)){
return model;
Expand Down
33 changes: 18 additions & 15 deletions src/overhaul/GenerationPlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@ public abstract class GenerationPlan extends ThingWithSettings{
public static final GenerationPlan DEFAULT;
static{
plans.add(new GenerationPlan("Mono", "Generates reactors following the selected model."){
private final Object yetAnotherSynchronizer = new Object();//that's better
private final Object synchronizer = new Object();
private Reactor imported;
private Reactor reactor;
private Reactor lastGuess;
@Override
public void run(Fuel fuel, Fuel.Type type, int x, int y, int z, Random rand){
Reactor r;
synchronized(yetAnotherSynchronizer){
synchronized(synchronizer){
r = reactor;
}
Reactor newReactor = Main.genModel.generate(r, fuel, type, x, y, z, rand);
if(newReactor==null)return;
if(Reactor.isbetter(newReactor,r)){
synchronized(yetAnotherSynchronizer){
synchronized(synchronizer){
reactor = newReactor;
}
}
lastGuess = newReactor;
}
@Override
public ArrayList<Reactor> getReactors(){
ArrayList<Reactor> reactors = new ArrayList<>();
synchronized(yetAnotherSynchronizer){
synchronized(synchronizer){
reactors.add(reactor);
}
reactors.add(lastGuess);
return reactors;
}
@Override
Expand All @@ -43,21 +46,21 @@ public void reset(Fuel fuel, Fuel.Type type, int x, int y, int z){
@Override
public void importReactor(Reactor reactor, boolean running){
if(running){
synchronized(yetAnotherSynchronizer){
synchronized(synchronizer){
if(Reactor.isbetter(reactor, this.reactor))this.reactor = reactor;
}
}else imported = reactor;
}
});
plans.add(new GenerationPlan("Multi", "Generates many reactors in parallel. If one stops improving, it is scrapped and reset.\nCreates a new thread for each reactor", new SettingInt("Reactors", 2, 2, 64), new SettingInt("Merge Timeout (Seconds)",2,1)) {
private final Object anotherSynchronizer = new Object();//Do I really need two of them?
private final Object synchronizer = new Object();
private int index = 0;
private Reactor[] reactors;
private Long[] lastUpdateTimes;
private Reactor imported;
@Override
public void run(Fuel fuel, Fuel.Type type, int x, int y, int z, Random rand){
synchronized(anotherSynchronizer){
synchronized(synchronizer){
if(reactors==null){
int num = (int)getSetting("Reactors");
reactors = new Reactor[num];
Expand All @@ -69,7 +72,7 @@ public void run(Fuel fuel, Fuel.Type type, int x, int y, int z, Random rand){
}
}
int idx;
synchronized(anotherSynchronizer){
synchronized(synchronizer){
idx = index;//grab ours
index++;//give the next thread the next one
if(index>=reactors.length){
Expand All @@ -78,22 +81,22 @@ public void run(Fuel fuel, Fuel.Type type, int x, int y, int z, Random rand){
}
long diff;
Reactor reactor;
synchronized(anotherSynchronizer){
synchronized(synchronizer){
reactor = reactors[idx];
long time = System.nanoTime();
diff = time-(lastUpdateTimes[idx]==null?time:lastUpdateTimes[idx]);
}
if(idx>0&&diff/1_000_000_000>(int)getSetting("Merge Timeout")){
synchronized(anotherSynchronizer){
synchronized(synchronizer){
if(Reactor.isbetter(reactor, reactors[0]))reactors[0] = reactor;
}
reactor = null;
synchronized(anotherSynchronizer){
synchronized(synchronizer){
reactors[idx] = null;
}
}
Reactor r = Main.genModel.generate(reactor, fuel, type, x, y, z, rand);
synchronized(anotherSynchronizer){
synchronized(synchronizer){
if(Reactor.isbetter(r, reactors[idx])){
reactors[idx] = r;
lastUpdateTimes[idx] = System.nanoTime();
Expand All @@ -104,7 +107,7 @@ public void run(Fuel fuel, Fuel.Type type, int x, int y, int z, Random rand){
public ArrayList<Reactor> getReactors(){
ArrayList<Reactor> reactors = new ArrayList<>();
if(this.reactors==null)return reactors;
synchronized(anotherSynchronizer){
synchronized(synchronizer){
for(Reactor r : this.reactors){
reactors.add(r);
}
Expand All @@ -116,7 +119,7 @@ public String getDetails(ArrayList<Reactor> reactors){
String details = "";
if(reactors==null)return details;
if(lastUpdateTimes==null)return details;
synchronized(anotherSynchronizer){
synchronized(synchronizer){
for(int i = 0; i<reactors.size(); i++){
if(reactors.get(i)==null)continue;
if(lastUpdateTimes[i]==null)continue;
Expand All @@ -134,7 +137,7 @@ public void reset(Fuel fuel, Fuel.Type type, int x, int y, int z){
@Override
public void importReactor(Reactor reactor, boolean running){
if(running){
synchronized(anotherSynchronizer){
synchronized(synchronizer){
if(reactors==null)return;
for(int i = 0; i<this.reactors.length; i++){
if(Reactor.isbetter(reactor, this.reactors[i])){
Expand Down
8 changes: 7 additions & 1 deletion src/overhaul/Priority.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
public abstract class Priority{
public static final ArrayList<Priority> priorities = new ArrayList<>();
static{
priorities.add(new Priority("Stable (Non-positive heat)"){
priorities.add(new Priority("Valid (>0 output)"){
@Override
protected double doCompare(Reactor main, Reactor other){
if(main.isValid()&&!other.isValid())return 1;
if(!main.isValid()&&other.isValid())return -1;
if(!main.isValid()&&!other.isValid())return 0;
return 0;
}
});
priorities.add(new Priority("Stability"){
@Override
protected double doCompare(Reactor main, Reactor other){
if(main.netHeat>0&&other.netHeat<0)return -1;
if(main.netHeat<0&&other.netHeat>0)return -1;
if(main.netHeat<0&&other.netHeat<0)return 0;
Expand Down
39 changes: 29 additions & 10 deletions src/overhaul/Reactor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,24 @@ public abstract class Reactor{
private static final double thresholdRatio = .75;
private static final double minimumMult = .5;
public static Reactor random(Fuel fuel, Fuel.Type type, int x, int y, int z, Random rand){
return random(fuel, type, x, y, z, rand, null);
}
public static Reactor random(Fuel fuel, Fuel.Type type, int x, int y, int z, Random rand, ArrayList<ReactorPart> allowedParts){
return new Reactor(fuel, type, x, y, z){
@Override
protected ReactorPart build(int X, int Y, int Z){
return ReactorPart.random(rand);
return ReactorPart.random(rand, allowedParts);
}
};
}
public static Reactor random(Fuel fuel, Fuel.Type type, int x, int y, int z, Random rand, boolean xSymm, boolean ySymm, boolean zSymm){
return random(fuel, type, x, y, z, rand, xSymm, ySymm, zSymm, null);
}
public static Reactor random(Fuel fuel, Fuel.Type type, int x, int y, int z, Random rand, boolean xSymm, boolean ySymm, boolean zSymm, ArrayList<ReactorPart> allowedParts){
return new Reactor(fuel, type, x, y, z, xSymm, ySymm, zSymm){
@Override
protected ReactorPart build(int X, int Y, int Z){
return ReactorPart.random(rand, allowedParts);
}
};
}
Expand Down Expand Up @@ -230,11 +244,16 @@ public Reactor(Fuel fuel, Fuel.Type type, int x, int y, int z, boolean symmetryX
}
}
}
applyExtraTransformations();
build();
}
public boolean isValid(){
return totalOutput>0;
}
/**
* Use to apply extra symmetries on the reactor before it's built
*/
protected void applyExtraTransformations(){}
protected abstract ReactorPart build(int X, int Y, int Z);
private ReactorPart get(int x, int y, int z){
if(x==-1||y==-1||z==-1||x==this.x||y==this.y||z==this.z)return ReactorPart.CASING;
Expand Down Expand Up @@ -285,15 +304,15 @@ private void build(){
continue;
}
}
if(p.type==ReactorPart.Type.HEATSINK){//if a heatsink is fundamentally invalid, replace it with air
for(PlacementRule rule : ((Heatsink)p).rules){
if(!rule.isValid(this,x,y,z)){
parts[x][y][z] = ReactorPart.AIR;
somethingChanged = true;
break;
}
}
}
// if(p.type==ReactorPart.Type.HEATSINK){//if a heatsink is fundamentally invalid, replace it with air
// for(PlacementRule rule : ((Heatsink)p).rules){
// if(!rule.isValid(this,x,y,z)){
// parts[x][y][z] = ReactorPart.AIR;
// somethingChanged = true;
// break;
// }
// }
// }//this causes problems with magnesium!
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/overhaul/ReactorPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ protected void draw(Graphics g, int w, int h) {
for(int z = 0; z<reactor.z; z++){
for(int x = 0; x<reactor.x; x++){
g.drawImage(reactor.parts[x][y][z].getImage(), x*blockSize, yOff, blockSize, blockSize, null);

// if(reactor.active[x][y][z]){
// g.setColor(new Color(0, 255, 0, 127));
// g.fillRect(x*blockSize, yOff, blockSize, blockSize);
// }

// int id = reactor.getClusterID(x,y,z);
// if(id>-1){
// g.setColor(new Color(id*80, 0, 0, 127));
Expand Down
61 changes: 52 additions & 9 deletions src/overhaul/ReactorPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Random;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javax.imageio.ImageIO;
public class ReactorPart implements ReactorBit{
public static final ArrayList<ReactorPart> parts = new ArrayList<>();//TODO update cooling values https://docs.google.com/spreadsheets/d/1zo8frawlKxA--vsu_dTYUKl1jtTdc-whzMTAVY9-4bs/edit#gid=606129178
public static final ArrayList<ReactorPart> GROUP_CORE = new ArrayList<>();
public static final ArrayList<ReactorPart> GROUP_CELL = new ArrayList<>();
public static final ArrayList<ReactorPart> GROUP_HEATSINK = new ArrayList<>();
public static final ArrayList<ReactorPart> GROUP_MODERATOR = new ArrayList<>();
public static final ArrayList<ReactorPart> GROUP_REFLECTOR = new ArrayList<>();
public static final ReactorPart AIR = air();
public static final ReactorPart FUEL_CELL = fuelCell(null, "{FUEL};False;None", 0);
public static final ReactorPart FUEL_CELL_CF_252 = fuelCell("Cf-252", "{FUEL};True;Cf-252", 1);
Expand Down Expand Up @@ -59,27 +65,64 @@ private static ReactorPart add(ReactorPart p){
return p;
}
private static ReactorPart air(){
return add(new ReactorPart(Type.AIR, "Air", null, "air"));
ReactorPart part = new ReactorPart(Type.AIR, "Air", null, "air");
GROUP_CORE.add(part);
return add(part);
}
private static ReactorPart fuelCell(String name, String jsonName, double efficiency){
return add(new FuelCell(name, jsonName, efficiency));
ReactorPart part = new FuelCell(name, jsonName, efficiency);
boolean hasBetter = false;
for(Iterator<ReactorPart> it = GROUP_CORE.iterator(); it.hasNext();){
ReactorPart p = it.next();
if(p instanceof FuelCell){
if(((FuelCell) p).efficiency<efficiency)it.remove();
else hasBetter = true;
}
}
if(!hasBetter)GROUP_CORE.add(part);
hasBetter = false;
for(Iterator<ReactorPart> it = GROUP_CELL.iterator(); it.hasNext();){
ReactorPart p = it.next();
if(p instanceof FuelCell){
if(((FuelCell) p).efficiency<efficiency)it.remove();
else hasBetter = true;
}
}
if(!hasBetter)GROUP_CELL.add(part);
return add(part);
}
private static ReactorPart heatsink(String name, String jsonName, int cooling, PlacementRule... rules){
return add(new Heatsink(name, jsonName, cooling, rules));
ReactorPart part = new Heatsink(name, jsonName, cooling, rules);
GROUP_HEATSINK.add(part);
return add(part);
}
private static ReactorPart moderator(String name, String jsonName, int fluxFactor, double efficiencyFactor){
return add(new Moderator(name, fluxFactor, jsonName, efficiencyFactor));
ReactorPart part = new Moderator(name, fluxFactor, jsonName, efficiencyFactor);
GROUP_CORE.add(part);
GROUP_MODERATOR.add(part);
return add(part);
}
private static ReactorPart conductor(){
return add(new ReactorPart(Type.CONDUCTOR, "Conductor", "Conductors", "conductor"));
ReactorPart part = new ReactorPart(Type.CONDUCTOR, "Conductor", "Conductors", "conductor");
return add(part);
}
private static ReactorPart reflector(String name, String jsonName, double reflectivity, double efficiency){
return add(new Reflector(name, reflectivity, jsonName, efficiency));
ReactorPart part = new Reflector(name, reflectivity, jsonName, efficiency);
GROUP_CORE.add(part);
GROUP_REFLECTOR.add(part);
return add(part);
}
public static ReactorPart random(Random rand){
int[] is = Main.instance.listParts.getSelectedIndices();
if(is.length==0)return ReactorPart.AIR;
return parts.get(is[rand.nextInt(is.length)]);
return random(rand, null);
}
public static ReactorPart random(Random rand, ArrayList<ReactorPart> allowedParts){
if(allowedParts==null){
int[] is = Main.instance.listParts.getSelectedIndices();
if(is.length==0)return ReactorPart.AIR;
return parts.get(is[rand.nextInt(is.length)]);
}
if(allowedParts.isEmpty())return AIR;
return allowedParts.get(rand.nextInt(allowedParts.size()));
}
public static ReactorPart parse(String string){
if(string.contains(";")){
Expand Down
Loading

0 comments on commit 47961ce

Please sign in to comment.