Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] improve carry recycling #403

Closed
wants to merge 3 commits into from
Closed

[WIP] improve carry recycling #403

wants to merge 3 commits into from

Conversation

kahojyun
Copy link
Contributor

@kahojyun kahojyun commented Sep 10, 2017

The main point of this commit is improving energy harvest efficiency.

  1. Improved the way to check extra carries. Carries will be recycled if they are waiting for sourcer.
  2. Before recycling carries with a spawn, check spawn queue and assign a new target to carry.
  3. Sourcers calculate spawn rate depending on resourceAtPostion.
  4. Added an option to switch on/off energy transfer between carries. It's turned off by default.
  5. Modified spawn priority for reserver and carry.

With this improvement, carries will work more efficiently. Tested on the official server:
2017-09-12 10 12 47
Stored about 900k energy in storage before upgrading to RCL6. (five sources in extern room)

@kahojyun kahojyun mentioned this pull request Sep 11, 2017
Copy link
Contributor

@samogot samogot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea, but implementation seems to be a bit hackish. I'd like to avoid carry- or whatever role -specific logic in common code. At least avoid new such code if possible)
Also make sure you won't recycle last carry for target

@@ -63,15 +63,22 @@ Creep.prototype.spawnCarry = function() {
resourceAtPosition += _.sum(container.store);
}

const waitTime = carryCapacity / (HARVEST_POWER * workParts) *
Math.sqrt(carryCapacity / Math.max(resourceAtPosition, carryCapacity));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like it

@@ -267,6 +267,10 @@ Creep.prototype.moveByPathMy = function(route, routePos, start, target, skipPreM
if (this.memory.killPrevious) {
this.killPrevious();
}
if (this.memory.checkRecycle && this.isStuck() && !this.memory.routing.reverse) {
Copy link
Contributor

@samogot samogot Sep 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not like it. carry may stuck not only because of lack of resources at sourcer
either way, name recycleOnStuck would be better then checkRecycle

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we may move it to carries pre-move?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little bit tricky to move this to pre-move because I need to use path.length. Maybe we can store path.length in creeps' memory.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can pass it the same way as directions

@@ -63,7 +63,7 @@ Creep.prototype.handle = function() {
return;
}

if (this.memory.recycle) {
if (this.memory.recycle && this.memory.moveToSpawn) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not like it at all. creep.memory.recycle meant to be universal and easily to set manually from interface, if we need it

}
}
if (roles.carry.checkOnPathStart(creep)) {
creep.memory.moveToSpawn = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to handle carries pre-recycle logic with another memory flag, and set recycle flag here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, I'll use resetTarget for pre-recycle logic

@@ -63,15 +63,22 @@ Creep.prototype.spawnCarry = function() {
resourceAtPosition += _.sum(container.store);
}

const waitTime = carryCapacity / (HARVEST_POWER * workParts) *
Math.sqrt(carryCapacity / Math.max(resourceAtPosition, carryCapacity));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the approach, can you explain a bit more what the idea is here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduce waitTime according to resourceAtPosition. I use Math.sqrt in the hope that it will reach an equilibrium quickly (not call/spawn too much carries).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

K, cool, let's see

}

// low minSpawnRate helps carry recycling
let minSpawnRate = 10;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract it to the config

@kahojyun kahojyun changed the title improve carry recycling [WIP] improve carry recycling Sep 15, 2017
@ghost ghost added the WIP label Sep 15, 2017
// when ticksToLive > recycleThreshold, reuse carry
recycleThreshold: 200,
// keep at least one carry for a target
ensureOne: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't think up a better name...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fine, maybe ensureOnePerTarget ... :-)

if (baseRoom.controller.level < 4) {
this.memory.wait = Math.max(waitTime, config.carry.minSpawnRate);
} else {
this.memory.wait = waitTime;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally, waitTime won't get too small because of Math.sqrt.

@@ -139,7 +139,13 @@ global.config = {
1800: [8, 15], // RCL 5
2300: [11, 21], // RCL 6
},
transferToCarry: false,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the intention is.

The default behavior should be that carries transfer to carries. If I understand the implementation correct, that would be disabled here.
And there are two methods, something like transferToCreep which actually transferrs and something like checkForTransfer which lets the carry move in the other direction because it will get energy in this tick.

Both needs to be adapted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can swap carries' target when they transfer to each other.

@@ -164,6 +192,35 @@ roles.carry.preMove = function(creep, directions) {
}

reverse = creep.pickupWhileMoving(reverse);

// recycle carry
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not like the recycling idea sooo much.

If there is a problem with too many carries, the call carry logic should be improved.
But fine until the call carry logic is better.

Copy link
Contributor Author

@kahojyun kahojyun Sep 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ultimate solution to call carry logic would be #294. My solution is making use of 'negative feedback'. The latter one is not as efficient as the former one, but it's easier to be implemented, and, in my opinion, their performance difference is not quite significant.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with adding this one and I'm also not so sure about the perfect solution, especially due to the fact that paths should be shared over multiple sources, which makes it more tricky.

- move codes related to carry recycling into `role_carry.js`
- add an option of keeping at least one carry for a target
- normally carry spawn rate won't get too fast because `Math.sqrt` is used
- fixed pickupWhileMoving. carry might move back and forth next to sourcer
- ensureOne: exclude creeps that have `resetTarget` flag
// when ticksToLive > recycleThreshold, reuse carry
recycleThreshold: 200,
// keep at least one carry for a target
ensureOne: true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fine, maybe ensureOnePerTarget ... :-)

@@ -63,15 +63,22 @@ Creep.prototype.spawnCarry = function() {
resourceAtPosition += _.sum(container.store);
}

const waitTime = carryCapacity / (HARVEST_POWER * workParts) *
Math.sqrt(carryCapacity / Math.max(resourceAtPosition, carryCapacity));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

K, cool, let's see

@@ -106,7 +99,7 @@ Creep.prototype.pickupWhileMoving = function(reverse) {
if (resources.length > 0) {
const resource = resources[0];
const amount = this.pickupOrWithdrawFromSourcer(resource);
return _.sum(this.carry) + amount > 0.5 * this.carryCapacity;
return this.checkEnergyTransfer(amount);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -164,6 +192,35 @@ roles.carry.preMove = function(creep, directions) {
}

reverse = creep.pickupWhileMoving(reverse);

// recycle carry
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with adding this one and I'm also not so sure about the perfect solution, especially due to the fact that paths should be shared over multiple sources, which makes it more tricky.

@Nevrdid
Copy link
Contributor

Nevrdid commented Sep 21, 2017

might have a look at my https://screeps.com/a/#!/room/shard1/W31N49 room. got a pretty good result with my amount calculation and creeps modifs.

@kahojyun kahojyun closed this Dec 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants